BornAgain  1.19.0
Simulate and fit neutron and x-ray scattering at grazing incidence
projecthandler.cpp
Go to the documentation of this file.
1 // ************************************************************************************************
2 //
3 // BornAgain: simulate and fit reflection and scattering
4 //
5 //! @file gui2/welcomeview/projecthandler.cpp
6 //! @brief Implements class CLASS?
7 //!
8 //! @homepage http://www.bornagainproject.org
9 //! @license GNU General Public License v3 or higher (see COPYING)
10 //! @copyright Forschungszentrum Jülich GmbH 2020
11 //! @authors Scientific Computing Group at MLZ (see CITATION, AUTHORS)
12 //
13 // ************************************************************************************************
14 
23 #include <QMainWindow>
24 
25 using namespace ModelView;
26 
27 namespace gui2 {
28 
29 ProjectHandler::ProjectHandler(ApplicationModels* models, QWidget* parent)
30  : QObject(parent)
31  , m_recentProjectSettings(std::make_unique<RecentProjectSettings>())
32  , m_userInteractor(std::make_unique<UserInteractor>(m_recentProjectSettings.get(), parent))
33  , m_models(models)
34 {
37 }
38 
40 
41 //! Update names (name of the current project, recent project name list, notifies the world).
42 
44 {
47 }
48 
49 //! Returns 'true' if current project can be closed.
50 //! Internally will perform check for unsaved data, and proceed via save/discard/cancel dialog.
51 
53 {
54  return m_projectManager->closeCurrentProject();
55 }
56 
58 {
59  if (m_projectManager->createNewProject())
60  updateNames();
61 }
62 
63 void ProjectHandler::onOpenExistingProject(const QString& dirname)
64 {
65  if (m_projectManager->openExistingProject(dirname.toStdString()))
66  updateNames();
67 }
68 
70 {
71  if (m_projectManager->saveCurrentProject())
72  updateNames();
73 }
74 
76 {
77  if (m_projectManager->saveProjectAs())
78  updateNames();
79 }
80 
82 {
83  m_recentProjectSettings->clearRecentProjectsList();
84  updateNames();
85 }
86 
88 {
89  auto modified_callback = [this]() { updateCurrentProjectName(); };
90  auto models_callback = [this]() { return m_models->persistent_models(); };
91  ProjectContext project_context{modified_callback, models_callback};
92 
93  auto select_dir_callback = [this]() { return m_userInteractor->onSelectDirRequest(); };
94  auto create_dir_callback = [this]() { return m_userInteractor->onCreateDirRequest(); };
95  auto answer_callback = [this]() { return m_userInteractor->onSaveChangesRequest(); };
96  UserInteractionContext user_context{select_dir_callback, create_dir_callback, answer_callback};
97 
98  m_projectManager = CreateProjectManager(project_context, user_context);
99 }
100 
101 //! Updates the name of the current project on main window, notifies the world.
102 
104 {
105  const auto current_project_dir = QString::fromStdString(m_projectManager->currentProjectDir());
106  const auto is_modified = m_projectManager->isModified();
107 
108  // set main window title
109  auto title = ModelView::Utils::ProjectWindowTitle(current_project_dir, is_modified);
110  if (auto main_window = ModelView::Utils::FindMainWindow(); main_window)
111  main_window->setWindowTitle(title);
112 
113  currentProjectModified(current_project_dir, is_modified);
114 }
115 
116 //! Update recent project list in settings, notifies the world.
117 
119 {
120  m_recentProjectSettings->addToRecentProjects(
121  QString::fromStdString(m_projectManager->currentProjectDir()));
123 }
124 
125 } // namespace gui2
Defines class CLASS?
Main class to holds all models of GUI session.
std::vector< ModelView::SessionModel * > persistent_models() const override
Returns vector of models intended for saving on disk.
ApplicationModels * m_models
std::unique_ptr< ModelView::ProjectManagerInterface > m_projectManager
void recentProjectsListModified(const QStringList &projects)
void updateCurrentProjectName()
Updates the name of the current project on main window, notifies the world.
void onOpenExistingProject(const QString &dirname={})
std::unique_ptr< RecentProjectSettings > m_recentProjectSettings
void updateRecentProjectNames()
Update recent project list in settings, notifies the world.
~ProjectHandler() override
bool canCloseProject() const
Returns 'true' if current project can be closed.
std::unique_ptr< UserInteractor > m_userInteractor
void currentProjectModified(const QString &project_dir, bool is_modified)
void updateNames()
Update names (name of the current project, recent project name list, notifies the world).
Collection of settings for RecentProjectWidget.
Provide save/discard/cancel and similar dialogs on user request.
MVVM_VIEW_EXPORT QMainWindow * FindMainWindow()
Finds main window.
MVVM_VIEW_EXPORT QString ProjectWindowTitle(const QString &project_dir, bool is_modified)
Returns a title composed from last part of project path, and is_modified flag.
Definition: widgetutils.cpp:98
materialitems.h Collection of materials to populate MaterialModel.
std::unique_ptr< ProjectManagerInterface > CreateProjectManager(const ProjectContext &project_context, const UserInteractionContext &user_context)
Creates default ProjectManager to save and load models.
Based on Qt example "codeeditor" Copyright (C) 2016 The Qt Company Ltd.
Definition: app_constants.h:20
Definition: filesystem.h:81
Defines class CLASS?
Defines class CLASS?
Defines class CLASS?
Defines class CLASS?
Defines class CLASS?
Provides necessary information for Project construction.
Definition: project_types.h:32
Defines the context to interact with the user regarding save/save-as/create-new project scenarious.
Definition: project_types.h:48
Defines class CLASS?
Defines class CLASS?