BornAgain  1.19.0
Simulate and fit neutron and x-ray scattering at grazing incidence
userinteractor.cpp
Go to the documentation of this file.
1 // ************************************************************************************************
2 //
3 // BornAgain: simulate and fit reflection and scattering
4 //
5 //! @file gui2/welcomeview/userinteractor.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 
19 #include "mvvm/utils/fileutils.h"
20 #include <QFileDialog>
21 #include <QMessageBox>
22 #include <map>
23 
24 using namespace ModelView;
25 
26 namespace {
27 //! Map of standard Qt answers to what ProjectManager expects.
28 std::map<QMessageBox::StandardButton, SaveChangesAnswer> answer_map()
29 {
30  std::map<QMessageBox::StandardButton, SaveChangesAnswer> result = {
31  {QMessageBox::Save, SaveChangesAnswer::SAVE},
32  {QMessageBox::Discard, SaveChangesAnswer::DISCARD},
33  {QMessageBox::Cancel, SaveChangesAnswer::CANCEL}};
34  return result;
35 }
36 } // namespace
37 
38 namespace gui2 {
39 
40 UserInteractor::UserInteractor(RecentProjectSettings* settings, QWidget* parent)
41  : m_settings(settings), m_parent(parent)
42 {
43 }
44 
45 //! Returns directory on disk selected by the user via QFileDialog.
46 //! Checks if selected directory can be the project directory.
47 
49 {
50  auto dirname = selectDir();
51 
52  if (dirname.empty()) // no valid selection
53  return {};
54 
56  QMessageBox msgBox;
57  msgBox.setText(
58  "Selected directory doesn't look like a project directory, choose another one");
59  msgBox.exec();
60  return {};
61  }
62 
63  return dirname;
64 }
65 
66 //! Returns new directory on disk created by the user via QFileDialog.
67 
69 
70 {
71  auto dirname = selectDir();
72 
73  if (dirname.empty()) // no valid selection
74  return {};
75 
76  if (!ModelView::Utils::is_empty(dirname)) {
77  QMessageBox msgBox;
78  msgBox.setText("The selected directory is not empty, choose another one.");
79  msgBox.exec();
80  return {};
81  }
82 
83  return dirname;
84 }
85 
86 //! Returns save/cancel/discard changes choice provided by the user.
87 
89 {
90  static auto translate = answer_map();
91 
92  QMessageBox msgBox;
93  msgBox.setText("The project has been modified.");
94  msgBox.setInformativeText("Do you want to save your changes?");
95  msgBox.setStandardButtons(QMessageBox::Save | QMessageBox::Discard | QMessageBox::Cancel);
96  msgBox.setDefaultButton(QMessageBox::Save);
97  auto ret = static_cast<QMessageBox::StandardButton>(msgBox.exec());
98  return translate[ret];
99 }
100 
101 //! Summon dialog to select directory on disk. If selection is not empty,
102 //! save parent directory for later re-use.
103 
104 std::string UserInteractor::selectDir() const
105 {
106  QString dirname = QFileDialog::getExistingDirectory(
107  m_parent, "Select directory", m_settings->currentWorkdir(),
108  QFileDialog::DontResolveSymlinks | QFileDialog::ShowDirsOnly);
109 
110  if (!dirname.isEmpty())
112 
113  return dirname.toStdString();
114 }
115 
116 } // namespace gui2
Collection of settings for RecentProjectWidget.
void updateWorkdirFromSelection(const QString &dirname)
Updates current workdir value from user selection.
QString currentWorkdir() const
Returns current workdir.
RecentProjectSettings * m_settings
std::string selectDir() const
Summon dialog to select directory on disk.
ModelView::SaveChangesAnswer onSaveChangesRequest()
Returns save/cancel/discard changes choice provided by the user.
std::string onCreateDirRequest()
Returns new directory on disk created by the user via QFileDialog.
std::string onSelectDirRequest()
Returns directory on disk selected by the user via QFileDialog.
Defines class CLASS?
MVVM_MODEL_EXPORT bool IsPossibleProjectDir(const std::string &project_dir)
Returns 'true' if given directory might be a project directory.
MVVM_MODEL_EXPORT bool is_empty(const std::string &path)
Returns true if the file indicated by 'path' refers to empty file or directory.
Definition: fileutils.cpp:121
materialitems.h Collection of materials to populate MaterialModel.
SaveChangesAnswer
Possible user answers on question "Project was modified".
Definition: project_types.h:28
Based on Qt example "codeeditor" Copyright (C) 2016 The Qt Company Ltd.
Definition: app_constants.h:20
Defines class CLASS?
Defines class CLASS?
Defines class CLASS?
Defines class CLASS?