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