BornAgain  1.19.0
Simulate and fit neutron and x-ray scattering at grazing incidence
projectmanager.cpp
Go to the documentation of this file.
1 // ************************************************************************************************
2 //
3 // qt-mvvm: Model-view-view-model framework for large GUI applications
4 //
5 //! @file mvvm/model/mvvm/project/projectmanager.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 Gennady Pospelov et al, Scientific Computing Group at MLZ (see CITATION, AUTHORS)
12 //
13 // ************************************************************************************************
14 
19 
20 using namespace ModelView;
21 
22 namespace {
23 const bool succeeded = true;
24 const bool failed = false;
25 } // namespace
26 
28  std::unique_ptr<ProjectInterface> m_current_project;
30 
31  ProjectManagerImpl(ProjectContext context) : m_project_context(std::move(context))
32  {
34  }
35 
36  //! Closes current project. Used in assumption that project was already saved.
38  {
39  m_current_project = ProjectUtils::CreateUntitledProject(m_project_context);
40  }
41 
42  //! Returns true if the project has directory already defined.
43  bool projectHasDir() const { return !m_current_project->projectDir().empty(); }
44 
45  //! Saves project in project directory. If directory is not defined
46  bool saveCurrentProject() { return saveCurrentProjectAs(m_current_project->projectDir()); }
47 
48  //! Saves the project into a given directory.
49  bool saveCurrentProjectAs(const std::string& dirname)
50  {
51  return m_current_project->save(dirname);
52  }
53 
54  //! Loads the project from a given directory.
55  bool loadFrom(const std::string& dirname) { return m_current_project->load(dirname); }
56 
57  //! Returns true if project has been modified after the last save.
58  bool isModified() const { return m_current_project->isModified(); }
59 };
60 
61 //! Constructor for ProjectManager.
62 
64  : p_impl(std::make_unique<ProjectManagerImpl>(context))
65 {
66 }
67 
69 
70 //! Creates a new project, returns 'true' in the case of success.
71 //! Current project has to be in a saved state, otherwise will return false.
72 
73 bool ProjectManager::createNewProject(const std::string& dirname)
74 {
75  if (p_impl->isModified())
76  return failed;
77  p_impl->createNewProject();
78  return p_impl->saveCurrentProjectAs(dirname);
79 }
80 
81 //! Saves current project, returns 'true' in the case of success.
82 //! The project should have a project directory defined to succeed.
83 
85 {
86  if (!p_impl->projectHasDir())
87  return failed;
88  return p_impl->saveCurrentProject();
89 }
90 
91 //! Saves the project under a given directory, returns true in the case of success.
92 //! The directory should exist already.
93 
94 bool ProjectManager::saveProjectAs(const std::string& dirname)
95 {
96  return p_impl->saveCurrentProjectAs(dirname);
97 }
98 
99 //! Opens existing project, returns 'true' in the case of success.
100 //! Current project should be in a saved state, new project should exist.
101 
102 bool ProjectManager::openExistingProject(const std::string& dirname)
103 {
104  if (p_impl->isModified())
105  return failed;
106  p_impl->createNewProject();
107  return p_impl->loadFrom(dirname);
108 }
109 
110 //! Returns current project directory.
111 
113 {
114  return p_impl->m_current_project ? p_impl->m_current_project->projectDir() : std::string();
115 }
116 
117 //! Returns true if project was modified since last save.
118 
120 {
121  return p_impl->isModified();
122 }
123 
124 //! Closes current project (without saving).
125 //! No checks whether it is modified or not being performed.
126 
128 {
129  // no special operation is required to close the project
130  p_impl->createNewProject(); // ready for further actions
131  return succeeded;
132 }
bool closeCurrentProject() const override
Closes current project (without saving).
bool openExistingProject(const std::string &dirname) override
Opens existing project, returns 'true' in the case of success.
bool saveProjectAs(const std::string &dirname) override
Saves the project under a given directory, returns true in the case of success.
bool saveCurrentProject() override
Saves current project, returns 'true' in the case of success.
std::unique_ptr< ProjectManagerImpl > p_impl
bool createNewProject(const std::string &dirname) override
Creates a new project, returns 'true' in the case of success.
ProjectManager(const ProjectContext &context)
Constructor for ProjectManager.
std::string currentProjectDir() const override
Returns current project directory.
bool isModified() const override
Returns true if project was modified since last save.
Defines class CLASS?
MVVM_MODEL_EXPORT std::unique_ptr< ProjectInterface > CreateUntitledProject(const ProjectContext &context)
Creates new untitled project.
materialitems.h Collection of materials to populate MaterialModel.
Definition: filesystem.h:81
Defines class CLASS?
Defines class CLASS?
Defines class CLASS?
Provides necessary information for Project construction.
Definition: project_types.h:32
std::unique_ptr< ProjectInterface > m_current_project
void createNewProject()
Closes current project. Used in assumption that project was already saved.
ProjectManagerImpl(ProjectContext context)
bool projectHasDir() const
Returns true if the project has directory already defined.
bool saveCurrentProjectAs(const std::string &dirname)
Saves the project into a given directory.
bool loadFrom(const std::string &dirname)
Loads the project from a given directory.
bool saveCurrentProject()
Saves project in project directory. If directory is not defined.
bool isModified() const
Returns true if project has been modified after the last save.