16 #include "Base/Util/Assert.h"
28 #include <QApplication>
30 #include <QFileDialog>
31 #include <QMessageBox>
36 const QString S_PROJECTMANAGER =
"ProjectManager";
37 const QString S_AUTOSAVE =
"EnableAutosave";
38 const QString S_DEFAULTPROJECTPATH =
"DefaultProjectPath";
39 const QString S_RECENTPROJECTS =
"RecentProjects";
40 const QString S_LASTUSEDIMPORTDIR =
"LastUsedImportDir";
41 const QString S_LASTUSEDIMPORFILTER1D =
"LastUsedImportFilter1D";
42 const QString S_LASTUSEDIMPORFILTER2D =
"LastUsedImportFilter2D";
55 throw Error(
"ProjectManager::ProjectManager -> Error. Attempt to create "
56 "ProjectManager twice.");
70 throw Error(
"ProjectManager::instance() -> Error. Attempt to access "
71 "non existing ProjectManager.");
82 if (settings.childGroups().contains(S_PROJECTMANAGER)) {
83 settings.beginGroup(S_PROJECTMANAGER);
85 if (!settings.contains(S_AUTOSAVE))
86 settings.setValue(S_AUTOSAVE,
true);
91 if (settings.contains(S_LASTUSEDIMPORTDIR))
108 settings.beginGroup(S_PROJECTMANAGER);
124 QStringList updatedList;
126 if (QFile fin(fileName); fin.exists())
127 updatedList.append(fileName);
196 settings.setValue(S_PROJECTMANAGER +
"/" + S_AUTOSAVE, value);
227 msgBox.setText(
"The project has been modified.");
228 msgBox.setInformativeText(
"Do you want to save your changes?");
229 msgBox.setStandardButtons(QMessageBox::Save | QMessageBox::Discard | QMessageBox::Cancel);
230 msgBox.setDefaultButton(QMessageBox::Save);
232 switch (msgBox.exec()) {
233 case QMessageBox::Save:
237 case QMessageBox::Discard:
239 case QMessageBox::Cancel:
255 if (projectFileName.isEmpty()) {
262 if (projectFileName.isEmpty())
267 }
catch (
const std::exception& ex) {
268 QString message = QString(
"Failed to save project under '%1'. \n\n").arg(projectFileName);
269 message.append(
"Exception was thrown.\n\n");
270 message.append(ex.what());
287 if (projectFileName.isEmpty())
300 if (fileName.isEmpty()) {
301 fileName = QFileDialog::getOpenFileName(
303 "BornAgain project Files (*.pro)",
nullptr,
305 : QFileDialog::DontUseNativeDialog);
306 if (fileName.isEmpty())
312 const auto readResult =
loadProject(fileName, messageService);
332 throw Error(
"ProjectManager::createNewProject() -> Project already exists");
336 const QVariant defFunctionalities =
369 const bool useAutosave =
373 QApplication::setOverrideCursor(Qt::WaitCursor);
379 QApplication::setOverrideCursor(Qt::WaitCursor);
383 QApplication::restoreOverrideCursor();
393 if (dialog.exec() != QDialog::Accepted)
426 QString result =
"Untitled";
429 for (
size_t i = 1; i < 99; ++i) {
430 result = QString(
"Untitled") + QString::number(i);
431 #if QT_VERSION >= QT_VERSION_CHECK(5, 13, 0)
445 QString message = QString(
"Failed to load the project '%1' \n\n")
448 for (
const auto& details : messageService.
errors())
449 message.append(details +
"\n");
457 auto* problemDialog =
461 problemDialog->show();
462 problemDialog->raise();
470 const QString title(
"Recover project");
471 const QString lmProject =
472 QFileInfo(projectFileName).lastModified().toString(
"hh:mm:ss, MMMM d, yyyy");
473 const QString lmAutoSave =
474 QFileInfo(
autosaveName).lastModified().toString(
"hh:mm:ss, MMMM d, yyyy");
476 QString message = QString(
"Project '%1' contains autosaved data.\n\n"
477 "Project saved at %2\nAutosave from %3")
483 "\nDo you want to restore from autosave?\n",
484 "Yes, please restore.",
"No, keep loading original");
ApplicationSettings * appSettings
global pointer to the instance
Defines class ApplicationSettings.
Defines class GUIHelpers functions.
Defines MessageService class.
Defines class NewProjectDialog.
ProjectDocument::Functionalities toFunctionalities(const QVariant &v)
QVariant toVariant(const ProjectDocument::Functionalities &f)
Defines class ProjectLoadProblemDialog.
Defines class ProjectManager.
Defines namespace GUI::Project::Utils.
Defines class SaveService.
SessionData * gSessionData
global pointer to the single instance
Defines struct SessionData.
bool defaultIsSingleSampleMode() const
bool defaultIsSingleInstrumentMode() const
bool useNativeFileDialog() const
QVariant defaultFunctionalities(const QVariant &absenceValue) const
The service to collect messages from different senders.
QStringList errors(bool includeSenders=false) const
QStringList warnings(bool includeSenders=false) const
new project dialog window
QString getWorkingDirectory() const
QString getProjectFileName() const
Project document class handles all data related to the opened project (sample, job,...
void modified()
Emitted for any modifications in the document.
The dialog to inform user about encountered problems during the loading of old project.
Handles activity related to opening/save projects.
bool saveProject(QString projectFileName="")
Processes save project request.
void setRecentlyUsedImportFilter1D(const QString &filter)
void createNewProject()
Calls dialog window to define project path and name.
void deleteCurrentProject()
void recentListModified()
QString untitledProjectName()
Will return 'Untitled' if the directory with such name doesn't exist in project path....
ProjectDocument::ReadResult loadProject(const QString &projectFileName, MessageService &messageService)
Load project data from file name. If autosave info exists, opens dialog for project restore.
void aboutToCloseDocument()
ProjectManager(QObject *parent)
static ProjectManager * instance()
QString m_importFilter2D
Recently used import filter for 2D files.
void riseProjectLoadProblemDialog(const MessageService &messageService)
QStringList m_recentProjects
void clearRecentProjects()
Clears list of recent projects.
void setAutosaveEnabled(bool value)
QString acquireProjectFileName()
Returns project file name from dialog. Returns empty string if dialog was canceled.
void setImportDirFromFilePath(const QString &filePath)
Sets user import directory in system settings.
QString recentlyUsedImportFilter2D() const
SaveService * m_saveService
void addToRecentProjects()
Add name of the current project to the name of recent projects.
QString m_importDirectory
void setRecentlyUsedImportFilter2D(const QString &filter)
bool closeCurrentProject()
Processes close current project request. Call save/discard/cancel dialog, if necessary....
void readSettings()
Reads settings of ProjectManager from global settings.
QString m_workingDirectory
Name of directory from there user prefer to import files.
void openProject(QString fileName="")
Opens existing project. If fileName is empty, will popup file selection dialog.
void setImportDir(const QString &dirname)
Sets user import directory in system settings.
QString userImportDir() const
Returns directory name which was used by the user to import files.
~ProjectManager() override
bool isAutosaveEnabled() const
static ProjectManager * s_instance
void newProject()
Processes new project request (close old project, rise dialog for project name, create project).
bool saveProjectAs()
Processes 'save project as' request.
QStringList recentProjects()
Returns list of recent projects, validates if projects still exists on disk.
QString m_importFilter1D
Recently used import filter for 1D files.
QString projectDir() const
Returns name of the current project directory.
void documentOpenedOrClosed(bool opened)
QString workingDirectory()
Returns default project path.
QString recentlyUsedImportFilter1D() const
bool restoreProjectDialog(const QString &projectFileName, QString autosaveName)
Name of directory where project directory was created.
void writeSettings()
Saves settings of ProjectManager in global settings.
void riseProjectLoadFailedDialog(const MessageService &messageService)
Provides save/autosave of ProjectDocument in a thread.
void setAutosaveEnabled(bool value)
void save(const QString &project_file_name)
bool isAutosaveEnabled() const
void setDocument(ProjectDocument *document)
Defines namespace GUI::Constants.
const int MAX_RECENT_PROJECTS
static QMainWindow * mainWindow
bool hasAutosavedData(const QString &projectFileName)
Returns true if project with given projectFileName contains autosaved data.
QString projectName(const QString &projectFileName)
Returns project name deduced from project file name.
QString autosaveName(const QString &projectFileName)
Returns name of project for autoSave from given project file name. E.g. from '/projects/Untitled2/Unt...
void warning(QWidget *parent, const QString &title, const QString &text, const QString &detailedText)
bool question(QWidget *parent, const QString &title, const QString &text, const QString &detailedText, const QString &yesText, const QString &noText)
std::optional< ProjectDocument * > projectDocument