BornAgain  1.19.0
Simulate and fit neutron and x-ray scattering at grazing incidence
project.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/project.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 
15 #include "mvvm/project/project.h"
20 #include "mvvm/utils/fileutils.h"
21 #include <functional>
22 
23 using namespace ModelView;
24 
26  std::string m_project_dir;
29 
30  ProjectImpl(const ProjectContext& context)
31  : m_context(context)
32  , m_change_controller(context.m_models_callback(), context.m_modified_callback)
33  {
34  }
35 
36  //! Returns list of models which are subject to save/load.
37  std::vector<SessionModel*> models() const { return m_context.m_models_callback(); }
38 
39  //! Processes all models one by one and either save or load them to/from given directory.
40  //! Template parameter `method` specifies ModelDocumentInterface's method to use.
41  template <typename T> bool process(const std::string& dirname, T method)
42  {
43  if (!Utils::exists(dirname))
44  return false;
45 
46  for (auto model : models()) {
47  auto document = CreateJsonDocument({model});
48  auto filename = Utils::join(dirname, ProjectUtils::SuggestFileName(*model));
49  std::invoke(method, document, filename);
50  }
51  m_project_dir = dirname;
53  return true;
54  }
55 };
56 
57 Project::Project(const ProjectContext& context) : p_impl(std::make_unique<ProjectImpl>(context)) {}
58 
59 Project::~Project() = default;
60 
61 //! Returns the full path to a project directory. It is a name where the project has been last time
62 //! saved, or loaded from.
63 
64 std::string Project::projectDir() const
65 {
66  return p_impl->m_project_dir;
67 }
68 
69 //! Saves all models to a given directory. Directory should exist.
70 //! Provided name will become 'projectDir'.
71 
72 bool Project::save(const std::string& dirname) const
73 {
74  return p_impl->process(dirname, &ModelDocumentInterface::save);
75 }
76 
77 //! Loads all models from the given directory.
78 bool Project::load(const std::string& dirname)
79 {
80  return p_impl->process(dirname, &ModelDocumentInterface::load);
81 }
82 
83 bool Project::isModified() const
84 {
85  return p_impl->m_change_controller.hasChanged();
86 }
virtual void load(const std::string &file_name)=0
virtual void save(const std::string &file_name) const =0
void resetChanged()
Reset controller to initial state, pretending that no changes has been registered.
std::string projectDir() const override
Returns the full path to a project directory.
Definition: project.cpp:64
Project(const ProjectContext &context)
Definition: project.cpp:57
bool load(const std::string &dirname) override
Loads all models from the given directory.
Definition: project.cpp:78
bool save(const std::string &dirname) const override
Saves all models to a given directory.
Definition: project.cpp:72
bool isModified() const override
Definition: project.cpp:83
std::unique_ptr< ProjectImpl > p_impl
Definition: project.h:42
Defines class CLASS?
Defines class CLASS?
std::string filename(const std::string &path)
Returns path without directory part ("Foo/Bar/Doz.int.gz" -> "Doz.int.gz")
MVVM_MODEL_EXPORT std::string SuggestFileName(const SessionModel &model)
Suggests file name which can be used to store json content of given model.
MVVM_MODEL_EXPORT std::string join(const std::string &part1, const std::string &part2)
Joins two path elements into the path.
Definition: fileutils.cpp:37
MVVM_MODEL_EXPORT bool exists(const std::string &fileName)
Returns true if file exists.
Definition: fileutils.cpp:27
materialitems.h Collection of materials to populate MaterialModel.
std::unique_ptr< ModelDocumentInterface > CreateJsonDocument(const std::vector< SessionModel * > &models)
Creates JsonDocument to save and load models.
Definition: filesystem.h:81
Defines class CLASS?
Defines class CLASS?
Defines class CLASS?
Defines class CLASS?
Provides necessary information for Project construction.
Definition: project_types.h:32
models_callback_t m_models_callback
Definition: project_types.h:42
ProjectChangedController m_change_controller
Definition: project.cpp:28
ProjectImpl(const ProjectContext &context)
Definition: project.cpp:30
bool process(const std::string &dirname, T method)
Processes all models one by one and either save or load them to/from given directory.
Definition: project.cpp:41
std::vector< SessionModel * > models() const
Returns list of models which are subject to save/load.
Definition: project.cpp:37