BornAgain  1.19.0
Simulate and fit neutron and x-ray scattering at grazing incidence
recentprojectsettings.cpp
Go to the documentation of this file.
1 // ************************************************************************************************
2 //
3 // BornAgain: simulate and fit reflection and scattering
4 //
5 //! @file gui2/welcomeview/recentprojectsettings.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 
16 #include "mvvm/utils/fileutils.h"
17 #include <QDir>
18 #include <QSettings>
19 
20 namespace {
21 const int max_recent_projects = 10;
22 
23 const QString group_key = "welcomeview";
24 const QString current_workdir_key = "currentworkdir";
25 const QString recent_projects_key = "recentprojects";
26 
27 const QString workdir_setting_name()
28 {
29  return group_key + "/" + current_workdir_key;
30 }
31 
32 const QString recent_projects_setting_name()
33 {
34  return group_key + "/" + recent_projects_key;
35 }
36 
37 } // namespace
38 
39 namespace gui2 {
40 
42 {
43  readSettings();
44 }
45 
47 {
48  writeSettings();
49 }
50 
51 //! Returns current workdir.
53 {
54  return m_currentWorkdir;
55 }
56 
57 //! Updates current workdir value from user selection.
58 //! Workdir will be set as parent director of selected `dirname`.
60 {
61  if (!dirname.isEmpty()) {
62  auto parent_path = ModelView::Utils::parent_path(dirname.toStdString());
63  m_currentWorkdir = QString::fromStdString(parent_path);
64  }
65 }
66 
67 //! Returns list of recent projects, validates if projects still exists on disk.
69 {
70  QStringList updatedList;
71  for (const auto& fileName : m_recentProjects) {
72  if (ModelView::Utils::exists(fileName.toStdString()))
73  updatedList.append(fileName);
74  }
75  m_recentProjects = updatedList;
76  return m_recentProjects;
77 }
78 
79 //! Adds directory to the list of recent projects.
80 void RecentProjectSettings::addToRecentProjects(const QString& dirname)
81 {
82  m_recentProjects.removeAll(dirname);
83  m_recentProjects.prepend(dirname);
84  while (m_recentProjects.size() > max_recent_projects)
85  m_recentProjects.removeLast();
86 }
87 
89 {
90  m_recentProjects.clear();
91 }
92 
93 //! Write all settings to file.
95 {
96  QSettings settings;
97  settings.setValue(workdir_setting_name(), m_currentWorkdir);
98  settings.setValue(recent_projects_setting_name(), m_recentProjects);
99 }
100 
101 //! Reads all settings from file.
103 {
104  QSettings settings;
105  m_currentWorkdir = QDir::homePath();
106 
107  if (settings.contains(workdir_setting_name()))
108  m_currentWorkdir = settings.value(workdir_setting_name()).toString();
109 
110  if (settings.contains(recent_projects_setting_name()))
111  m_recentProjects = settings.value(recent_projects_setting_name()).toStringList();
112 }
113 
114 } // namespace gui2
QStringList recentProjects()
Returns list of recent projects, validates if projects still exists on disk.
void updateWorkdirFromSelection(const QString &dirname)
Updates current workdir value from user selection.
QString currentWorkdir() const
Returns current workdir.
void addToRecentProjects(const QString &dirname)
Adds directory to the list of recent projects.
void readSettings()
Reads all settings from file.
void writeSettings()
Write all settings to file.
Defines class CLASS?
MVVM_MODEL_EXPORT std::string parent_path(const std::string &path)
Returns the path to the parent directory.
Definition: fileutils.cpp:111
MVVM_MODEL_EXPORT bool exists(const std::string &fileName)
Returns true if file exists.
Definition: fileutils.cpp:27
Based on Qt example "codeeditor" Copyright (C) 2016 The Qt Company Ltd.
Definition: app_constants.h:20
Defines class CLASS?