BornAgain  1.19.79
Simulate and fit neutron and x-ray scattering at grazing incidence
NewProjectDialog.cpp
Go to the documentation of this file.
1 // ************************************************************************************************
2 //
3 // BornAgain: simulate and fit reflection and scattering
4 //
5 //! @file GUI/View/Project/NewProjectDialog.cpp
6 //! @brief Implements class NewProjectDialog
7 //!
8 //! @homepage http://www.bornagainproject.org
9 //! @license GNU General Public License v3 or higher (see COPYING)
10 //! @copyright Forschungszentrum Jülich GmbH 2018
11 //! @authors Scientific Computing Group at MLZ (see CITATION, AUTHORS)
12 //
13 // ************************************************************************************************
14 
19 #include <QFileDialog>
20 #include <QGroupBox>
21 #include <QLabel>
22 #include <QPushButton>
23 #include <QVBoxLayout>
24 
25 NewProjectDialog::NewProjectDialog(QWidget* parent, const QString& workingDirectory,
26  const QString& projectName)
27  : QDialog(parent)
28  , m_projectNameEdit(nullptr)
29  , m_workDirEdit(nullptr)
30  , m_browseButton(nullptr)
31  , m_warningLabel(nullptr)
32  , m_cancelButton(nullptr)
33  , m_createButton(nullptr)
34  , m_valid_projectName(true)
35  , m_valid_projectPath(true)
36 
37 {
38  setMinimumSize(480, 280);
39  setWindowTitle("Save project");
40 
41  auto* nameLabel = new QLabel("Project name:");
42  m_projectNameEdit = new QLineEdit;
44  connect(m_projectNameEdit, &QLineEdit::textEdited, this,
46  nameLabel->setBuddy(m_projectNameEdit);
47 
48  auto* parentDirLabel = new QLabel("Save in:");
49  m_workDirEdit = new QLineEdit;
50  m_workDirEdit->setText(QDir::toNativeSeparators(QDir::homePath()));
51  connect(m_workDirEdit, &QLineEdit::textEdited, this,
53  parentDirLabel->setBuddy(m_workDirEdit);
54 
55  m_browseButton = new QPushButton("Browse");
56  connect(m_browseButton, &QPushButton::clicked, this, &NewProjectDialog::onBrowseDirectory);
57 
58  m_warningLabel = new QLabel();
59 
60  m_createButton = new QPushButton("Save");
61  connect(m_createButton, &QPushButton::clicked, this, &NewProjectDialog::createProjectDir);
62  m_createButton->setDefault(true);
63  m_cancelButton = new QPushButton("Cancel");
64  connect(m_cancelButton, &QPushButton::clicked, this, &NewProjectDialog::reject);
65 
66  auto* projectGroup = new QGroupBox("Project name and location");
67 
68  auto* layout = new QGridLayout;
69  layout->addWidget(nameLabel, 0, 0);
70  layout->addWidget(m_projectNameEdit, 0, 1);
71  layout->addWidget(parentDirLabel, 1, 0);
72  layout->addWidget(m_workDirEdit, 1, 1);
73  layout->addWidget(m_browseButton, 1, 2);
74 
75  projectGroup->setLayout(layout);
76 
77  auto* buttonsLayout = new QHBoxLayout;
78  buttonsLayout->addStretch(1);
79  buttonsLayout->addWidget(m_createButton);
80  buttonsLayout->addWidget(m_cancelButton);
81 
82  auto* mainLayout = new QVBoxLayout;
83  mainLayout->addWidget(projectGroup);
84  mainLayout->addWidget(m_warningLabel);
85  mainLayout->addStretch();
86  mainLayout->addLayout(buttonsLayout);
87 
88  setLayout(mainLayout);
89 
90  setWorkingDirectory(workingDirectory);
91 }
92 
94 {
95  return QDir::fromNativeSeparators(m_workDirEdit->text());
96 }
97 
98 void NewProjectDialog::setWorkingDirectory(const QString& text)
99 {
100  m_workDirEdit->setText(QDir::toNativeSeparators(text));
101 }
102 
104 {
105  QString projectDir = getWorkingDirectory() + QString("/") + getProjectName();
107  return projectDir + QString("/") + projectFile;
108 }
109 
110 //! calls directory selection dialog
112 {
113  QFileDialog::Options options = QFileDialog::DontResolveSymlinks | QFileDialog::ShowDirsOnly;
114 
116  options |= QFileDialog::DontUseNativeDialog;
117 
118  const QString dirname =
119  QFileDialog::getExistingDirectory(this, "Select directory", getWorkingDirectory(), options);
120 
121  if (dirname.isEmpty())
122  return;
123 
124  checkIfProjectPathIsValid(dirname);
126 }
127 
128 //! Checks whether ProjectPath is valid and sets warning state accordingly. Corresponding directory
129 //! should exists.
131 {
132  if (QFile::exists(dirname)) {
133  setValidProjectPath(true);
134  setWorkingDirectory(dirname);
135  } else {
136  setValidProjectPath(false);
137  }
139 }
140 
141 //! Checks whether project name is valid and sets warning state accordingly. There should not be the
142 //! directory with such name in ProjectPath
144 {
145  const QDir projectDir = getWorkingDirectory() + "/" + projectName;
146  setValidProjectName(!projectDir.exists());
148 }
149 
150 //! sets flags whether project name is valid and then updates color of LineEdit
151 //! and warning message
153 {
154  m_valid_projectName = status;
155  QPalette palette;
156  palette.setColor(QPalette::Text, m_valid_projectPath ? Qt::black : Qt::darkRed);
157  m_projectNameEdit->setPalette(palette);
158 }
159 
160 //! sets flags wether project path is valid and then updates color of LineEdit
161 //! and warning message
163 {
164  m_valid_projectPath = status;
165  QPalette palette;
166  palette.setColor(QPalette::Text, m_valid_projectPath ? Qt::black : Qt::darkRed);
167  m_workDirEdit->setPalette(palette);
168 }
169 
170 //! updates warning label depending on validity of project name and path
172 {
174  m_createButton->setEnabled(true);
175  m_warningLabel->setText("");
176  } else if (!m_valid_projectPath) {
177  m_createButton->setEnabled(false);
178  m_warningLabel->setText("<font color='darkRed'> The path '"
179  + QDir::toNativeSeparators(getWorkingDirectory())
180  + "' does not exist. </font>");
181  } else if (!m_valid_projectName) {
182  m_createButton->setEnabled(false);
183  if (getProjectName().isEmpty())
184  m_warningLabel->setText("<font color='darkRed'> Please specify project name. </font>");
185  else {
186  m_warningLabel->setText("<font color='darkRed'> The directory '" + getProjectName()
187  + "' already exists. </font>");
188  }
189  }
190 }
191 
192 //! creates directory with selected ProjectName in selected ProjectPath
194 {
195  QDir parentDir = getWorkingDirectory();
196  if (!parentDir.mkdir(getProjectName())) {
197  m_warningLabel->setText("<font color='darkRed'> Can't make subdirectory' '"
198  + getProjectName() + "' in '"
199  + QDir::toNativeSeparators(getWorkingDirectory()) + "' </font>");
200  } else {
201  accept();
202  }
203 }
ApplicationSettings * appSettings
global pointer to the instance
Defines class ApplicationSettings.
Defines class NewProjectDialog.
Defines class ProjectDocument.
Defines namespace GUI::Project::Utils.
bool useNativeFileDialog() const
void checkIfProjectPathIsValid(const QString &dirname)
Checks whether ProjectPath is valid and sets warning state accordingly. Corresponding directory shoul...
QLineEdit * m_workDirEdit
void checkIfProjectNameIsValid(const QString &projectName)
Checks whether project name is valid and sets warning state accordingly. There should not be the dire...
QLineEdit * m_projectNameEdit
void setValidProjectName(bool status)
sets flags whether project name is valid and then updates color of LineEdit and warning message
QPushButton * m_cancelButton
void updateWarningStatus()
updates warning label depending on validity of project name and path
QPushButton * m_createButton
QString getWorkingDirectory() const
QPushButton * m_browseButton
void createProjectDir()
creates directory with selected ProjectName in selected ProjectPath
void setWorkingDirectory(const QString &text)
void setValidProjectPath(bool status)
sets flags wether project path is valid and then updates color of LineEdit and warning message
QString getProjectFileName() const
void onBrowseDirectory()
calls directory selection dialog
QString getProjectName() const
NewProjectDialog(QWidget *parent, const QString &workingDirectory="", const QString &projectName="")
constexpr const char * projectFileExtension
Definition: ProjectUtils.h:24
QString projectDir(const QString &projectFileName)
Returns project directory deduced from project file name.
QString projectName(const QString &projectFileName)
Returns project name deduced from project file name.