BornAgain  1.19.0
Simulate and fit neutron and x-ray scattering at grazing incidence
projectutils.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/projectutils.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 
18 #include "mvvm/project/project.h"
20 #include "mvvm/utils/fileutils.h"
21 #include <cctype>
22 
23 namespace {
24 const std::string json_extention = ".json";
25 const std::string untitled_name = "Untitled";
26 } // namespace
27 
28 namespace ModelView {
29 
30 //! Suggests file name which can be used to store json content of given model.
31 //! Uses the model type to construct a filename: MaterialModel -> materialmodel.json
32 
34 {
35  std::string result = model.modelType();
36  std::transform(result.begin(), result.end(), result.begin(), ::tolower);
37  return result + json_extention;
38 }
39 
40 //! Returns 'true' if given directory might be a project directory.
41 //! This simplified check counts number of files with json extention.
42 
43 bool ProjectUtils::IsPossibleProjectDir(const std::string& project_dir)
44 {
45  return !Utils::FindFiles(project_dir, json_extention).empty();
46 }
47 
48 //! Creates new untitled project.
49 
50 std::unique_ptr<ProjectInterface> ProjectUtils::CreateUntitledProject(const ProjectContext& context)
51 {
52  return std::make_unique<Project>(context);
53 }
54 
55 //! Returns a MainWindow title for given project.
56 
58 {
59  return ProjectWindowTitle(project.projectDir(), project.isModified());
60 }
61 
62 //! Returns a title composed from last part of project path, and `is_modified` flag.
63 //! Project without projectDir will be "Untitled", modified project will be "*Untitled".
64 //! Project with projectDir in "/home/user/project1" will get title "project1".
65 
66 std::string ProjectUtils::ProjectWindowTitle(const std::string& project_dir, bool is_modified)
67 {
68  auto pos = project_dir.find_last_of('/');
69  auto project_name = (pos == std::string::npos ? untitled_name : project_dir.substr(pos + 1));
70  auto unsaved_status = is_modified ? "*" : "";
71  return unsaved_status + project_name;
72 }
73 
74 } // namespace ModelView
Defines class CLASS?
Interface to manipulate projects on disk.
Main class to hold hierarchy of SessionItem objects.
Definition: sessionmodel.h:37
std::string modelType() const
Returns model type.
Defines class CLASS?
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::unique_ptr< ProjectInterface > CreateUntitledProject(const ProjectContext &context)
Creates new untitled project.
MVVM_MODEL_EXPORT bool IsPossibleProjectDir(const std::string &project_dir)
Returns 'true' if given directory might be a project directory.
MVVM_MODEL_EXPORT std::string ProjectWindowTitle(const ProjectInterface &project)
Returns a MainWindow title for given project.
MVVM_MODEL_EXPORT std::vector< std::string > FindFiles(const std::string &dirname, const std::string &ext)
Returns list of files with given extention found in given directory.
Definition: fileutils.cpp:87
materialitems.h Collection of materials to populate MaterialModel.
@ project
selective copying for saving/loading the project (tags and data created by item, updated from JSON)
Defines class CLASS?
Defines class CLASS?
Defines class CLASS?
Defines class CLASS?
Provides necessary information for Project construction.
Definition: project_types.h:32