BornAgain  1.19.0
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/coregui/mainwindow/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 
17 #include <QFileDialog>
18 #include <QGroupBox>
19 #include <QLabel>
20 #include <QPushButton>
21 #include <QVBoxLayout>
22 
23 NewProjectDialog::NewProjectDialog(QWidget* parent, Mode mode, const QString& workingDirectory,
24  const QString& projectName)
25  : QDialog(parent)
26  , m_projectNameEdit(0)
27  , m_workDirEdit(0)
28  , m_browseButton(0)
29  , m_warningLabel(0)
30  , m_cancelButton(0)
31  , m_createButton(0)
32  , m_valid_projectName(true)
33  , m_valid_projectPath(true)
34 
35 {
36  setMinimumSize(480, 280);
37  setWindowTitle(mode == CREATE ? "New project" : "Save project");
38 
39  QLabel* nameLabel = new QLabel("Project name:");
40  m_projectNameEdit = new QLineEdit;
41  m_projectNameEdit->setText("Untitled");
42  connect(m_projectNameEdit, &QLineEdit::textEdited, this,
44  nameLabel->setBuddy(m_projectNameEdit);
45 
46  QLabel* parentDirLabel = new QLabel(mode == CREATE ? "Create in:" : "Save in:");
47  m_workDirEdit = new QLineEdit;
48  m_workDirEdit->setText(QDir::toNativeSeparators(QDir::homePath()));
49  connect(m_workDirEdit, &QLineEdit::textEdited, this,
51  parentDirLabel->setBuddy(m_workDirEdit);
52 
53  m_browseButton = new QPushButton("Browse");
54  connect(m_browseButton, &QPushButton::clicked, this, &NewProjectDialog::onBrowseDirectory);
55 
56  m_warningLabel = new QLabel();
57 
58  m_createButton = new QPushButton(mode == CREATE ? "Create" : "Save");
59  connect(m_createButton, &QPushButton::clicked, this, &NewProjectDialog::createProjectDir);
60  m_createButton->setDefault(true);
61  m_cancelButton = new QPushButton("Cancel");
62  connect(m_cancelButton, &QPushButton::clicked, this, &NewProjectDialog::reject);
63 
64  QGroupBox* projectGroup = new QGroupBox("Project name and location");
65 
66  QGridLayout* layout = new QGridLayout;
67  layout->addWidget(nameLabel, 0, 0);
68  layout->addWidget(m_projectNameEdit, 0, 1);
69  layout->addWidget(parentDirLabel, 1, 0);
70  layout->addWidget(m_workDirEdit, 1, 1);
71  layout->addWidget(m_browseButton, 1, 2);
72 
73  projectGroup->setLayout(layout);
74 
75  QHBoxLayout* buttonsLayout = new QHBoxLayout;
76  buttonsLayout->addStretch(1);
77  buttonsLayout->addWidget(m_createButton);
78  buttonsLayout->addWidget(m_cancelButton);
79 
80  QVBoxLayout* mainLayout = new QVBoxLayout;
81  mainLayout->addWidget(projectGroup);
82  mainLayout->addWidget(m_warningLabel);
83  mainLayout->addStretch();
84  mainLayout->addLayout(buttonsLayout);
85 
86  setLayout(mainLayout);
87 
88  setWorkingDirectory(workingDirectory);
90 }
91 
93 {
94  return QDir::fromNativeSeparators(m_workDirEdit->text());
95 }
96 
97 void NewProjectDialog::setWorkingDirectory(const QString& text)
98 {
99  m_workDirEdit->setText(QDir::toNativeSeparators(text));
100 }
101 
102 void NewProjectDialog::setProjectName(const QString& text)
103 {
104  return m_projectNameEdit->setText(text);
105 }
106 
108 {
109  QString projectDir = getWorkingDirectory() + QString("/") + getProjectName();
110  QString projectFile = getProjectName() + ProjectDocument::projectFileExtension();
111  QString result = projectDir + QString("/") + projectFile;
112  return result;
113 }
114 
115 //! calls directory selection dialog
117 {
118  QString dirname = QFileDialog::getExistingDirectory(
119  this, "Select directory", getWorkingDirectory(),
120  QFileDialog::DontResolveSymlinks | QFileDialog::ShowDirsOnly);
121 
122  if (!dirname.isEmpty()) {
123  checkIfProjectPathIsValid(dirname);
125  }
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  if (m_valid_projectName) {
157  palette.setColor(QPalette::Text, Qt::black);
158  } else {
159  palette.setColor(QPalette::Text, Qt::darkRed);
160  }
161  m_projectNameEdit->setPalette(palette);
162 }
163 
164 //! sets flags wether project path is valid and then updates color of LineEdit
165 //! and warning message
167 {
168  m_valid_projectPath = status;
169  QPalette palette;
170  if (m_valid_projectPath) {
171  palette.setColor(QPalette::Text, Qt::black);
172  } else {
173  palette.setColor(QPalette::Text, Qt::darkRed);
174  }
175  m_workDirEdit->setPalette(palette);
176 }
177 
178 //! updates warning label depending on validity of project name and path
180 {
182  m_createButton->setEnabled(true);
183  m_warningLabel->setText("");
184  } else if (!m_valid_projectPath) {
185  m_createButton->setEnabled(false);
186  m_warningLabel->setText("<font color='darkRed'> The path '"
187  + QDir::toNativeSeparators(getWorkingDirectory())
188  + "' does not exist. </font>");
189  } else if (!m_valid_projectName) {
190  m_createButton->setEnabled(false);
191  if (getProjectName().isEmpty()) {
192  m_warningLabel->setText("<font color='darkRed'> Please specify project name. </font>");
193  } else {
194  m_warningLabel->setText("<font color='darkRed'> The directory '" + getProjectName()
195  + "' already exists. </font>");
196  }
197  }
198 }
199 
200 //! creates directory with selected ProjectName in selected ProjectPath
202 {
203  QDir parentDir = getWorkingDirectory();
204  if (!parentDir.mkdir(getProjectName())) {
205  m_warningLabel->setText("<font color='darkRed'> Can't make subdirectory' '"
206  + getProjectName() + "' in '"
207  + QDir::toNativeSeparators(getWorkingDirectory()) + "' </font>");
208  } else {
209  accept();
210  }
211 }
void checkIfProjectPathIsValid(const QString &dirname)
Checks whether ProjectPath is valid and sets warning state accordingly.
NewProjectDialog(QWidget *parent, Mode mode, const QString &workingDirectory="", const QString &projectName="")
QLineEdit * m_workDirEdit
void checkIfProjectNameIsValid(const QString &projectName)
Checks whether project name is valid and sets warning state accordingly.
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
void setProjectName(const QString &text)
QString getProjectFileName() const
void onBrowseDirectory()
calls directory selection dialog
QString getProjectName() const
static QString projectFileExtension()
bool exists(const QString &fileName)
Returns true if file exists.
QString projectName(const QString &projectFileName)
Returns project name deduced from project file name.
QString projectDir(const QString &projectFileName)
Returns project directory deduced from project file name.
Defines class NewProjectDialog.
Defines class ProjectDocument.