mirror of
https://gitee.com/vnotex/vnote.git
synced 2025-07-05 13:59:52 +08:00
refactor VDirInfoDialog and VNewDirDialog
This commit is contained in:
parent
59d0e82e66
commit
3daa463a6d
@ -1,16 +1,23 @@
|
||||
#include <QtWidgets>
|
||||
#include "vdirinfodialog.h"
|
||||
#include "vdirectory.h"
|
||||
#include "vconfigmanager.h"
|
||||
|
||||
VDirInfoDialog::VDirInfoDialog(const QString &title, const QString &info,
|
||||
const QString &defaultName,
|
||||
extern VConfigManager *g_config;
|
||||
|
||||
VDirInfoDialog::VDirInfoDialog(const QString &title,
|
||||
const QString &info,
|
||||
const VDirectory *directory,
|
||||
VDirectory *parentDirectory,
|
||||
QWidget *parent)
|
||||
: QDialog(parent), infoLabel(NULL), title(title), info(info), defaultName(defaultName)
|
||||
: QDialog(parent), infoLabel(NULL), title(title), info(info),
|
||||
m_directory(directory), m_parentDirectory(parentDirectory)
|
||||
{
|
||||
setupUI();
|
||||
|
||||
connect(nameEdit, &QLineEdit::textChanged, this, &VDirInfoDialog::enableOkButton);
|
||||
connect(nameEdit, &QLineEdit::textChanged, this, &VDirInfoDialog::handleInputChanged);
|
||||
|
||||
enableOkButton();
|
||||
handleInputChanged();
|
||||
}
|
||||
|
||||
void VDirInfoDialog::setupUI()
|
||||
@ -19,10 +26,14 @@ void VDirInfoDialog::setupUI()
|
||||
infoLabel = new QLabel(info);
|
||||
}
|
||||
nameLabel = new QLabel(tr("Folder &name:"));
|
||||
nameEdit = new QLineEdit(defaultName);
|
||||
nameEdit = new QLineEdit(m_directory->getName());
|
||||
nameEdit->selectAll();
|
||||
nameLabel->setBuddy(nameEdit);
|
||||
|
||||
m_warnLabel = new QLabel();
|
||||
m_warnLabel->setWordWrap(true);
|
||||
m_warnLabel->hide();
|
||||
|
||||
// Ok is the default button.
|
||||
m_btnBox = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel);
|
||||
connect(m_btnBox, &QDialogButtonBox::accepted, this, &QDialog::accept);
|
||||
@ -40,6 +51,7 @@ void VDirInfoDialog::setupUI()
|
||||
mainLayout->addWidget(infoLabel);
|
||||
}
|
||||
mainLayout->addLayout(topLayout);
|
||||
mainLayout->addWidget(m_warnLabel);
|
||||
mainLayout->addWidget(m_btnBox);
|
||||
mainLayout->setSizeConstraint(QLayout::SetFixedSize);
|
||||
setLayout(mainLayout);
|
||||
@ -47,10 +59,28 @@ void VDirInfoDialog::setupUI()
|
||||
setWindowTitle(title);
|
||||
}
|
||||
|
||||
void VDirInfoDialog::enableOkButton()
|
||||
void VDirInfoDialog::handleInputChanged()
|
||||
{
|
||||
bool showWarnLabel = false;
|
||||
QString name = nameEdit->text();
|
||||
bool nameOk = !name.isEmpty();
|
||||
if (nameOk && name != m_directory->getName()) {
|
||||
// Check if the name conflicts with existing directory name.
|
||||
// Case-insensitive when creating note.
|
||||
if (m_parentDirectory->findSubDirectory(name, false)) {
|
||||
nameOk = false;
|
||||
showWarnLabel = true;
|
||||
QString nameConflictText = tr("<span style=\"%1\">WARNING</span>: Name (case-insensitive) already exists. "
|
||||
"Please choose another name.")
|
||||
.arg(g_config->c_warningTextStyle);
|
||||
m_warnLabel->setText(nameConflictText);
|
||||
}
|
||||
}
|
||||
|
||||
m_warnLabel->setVisible(showWarnLabel);
|
||||
|
||||
QPushButton *okBtn = m_btnBox->button(QDialogButtonBox::Ok);
|
||||
okBtn->setEnabled(!nameEdit->text().isEmpty());
|
||||
okBtn->setEnabled(nameOk);
|
||||
}
|
||||
|
||||
QString VDirInfoDialog::getNameInput() const
|
||||
|
@ -7,17 +7,22 @@ class QLabel;
|
||||
class QLineEdit;
|
||||
class QDialogButtonBox;
|
||||
class QString;
|
||||
class VDirectory;
|
||||
|
||||
class VDirInfoDialog : public QDialog
|
||||
{
|
||||
Q_OBJECT
|
||||
public:
|
||||
VDirInfoDialog(const QString &title, const QString &info, const QString &defaultName,
|
||||
VDirInfoDialog(const QString &title,
|
||||
const QString &info,
|
||||
const VDirectory *directory,
|
||||
VDirectory *parentDirectory,
|
||||
QWidget *parent = 0);
|
||||
|
||||
QString getNameInput() const;
|
||||
|
||||
private slots:
|
||||
void enableOkButton();
|
||||
void handleInputChanged();
|
||||
|
||||
private:
|
||||
void setupUI();
|
||||
@ -25,10 +30,13 @@ private:
|
||||
QLabel *infoLabel;
|
||||
QLabel *nameLabel;
|
||||
QLineEdit *nameEdit;
|
||||
QLabel *m_warnLabel;
|
||||
QDialogButtonBox *m_btnBox;
|
||||
|
||||
QString title;
|
||||
QString info;
|
||||
QString defaultName;
|
||||
|
||||
VDirectory *m_parentDirectory;
|
||||
const VDirectory *m_directory;
|
||||
};
|
||||
#endif // VDIRINFODIALOG_H
|
||||
|
@ -65,7 +65,7 @@ void VFileInfoDialog::handleInputChanged()
|
||||
QString name = nameEdit->text();
|
||||
bool nameOk = !name.isEmpty();
|
||||
if (nameOk && name != m_file->getName()) {
|
||||
// Check if the name conflicts with existing notebook name.
|
||||
// Check if the name conflicts with existing note name.
|
||||
// Case-insensitive when creating note.
|
||||
if (m_directory->findFile(name, false)) {
|
||||
nameOk = false;
|
||||
|
@ -1,13 +1,23 @@
|
||||
#include <QtWidgets>
|
||||
#include "vnewdirdialog.h"
|
||||
#include "vdirectory.h"
|
||||
#include "vconfigmanager.h"
|
||||
|
||||
VNewDirDialog::VNewDirDialog(const QString &title, const QString &info, const QString &name, const QString &defaultName,
|
||||
extern VConfigManager *g_config;
|
||||
|
||||
VNewDirDialog::VNewDirDialog(const QString &title,
|
||||
const QString &info,
|
||||
const QString &defaultName,
|
||||
VDirectory *directory,
|
||||
QWidget *parent)
|
||||
: QDialog(parent), title(title), info(info), name(name), defaultName(defaultName)
|
||||
: QDialog(parent), title(title), info(info), defaultName(defaultName),
|
||||
m_directory(directory)
|
||||
{
|
||||
setupUI();
|
||||
|
||||
connect(nameEdit, &QLineEdit::textChanged, this, &VNewDirDialog::enableOkButton);
|
||||
connect(nameEdit, &QLineEdit::textChanged, this, &VNewDirDialog::handleInputChanged);
|
||||
|
||||
handleInputChanged();
|
||||
}
|
||||
|
||||
void VNewDirDialog::setupUI()
|
||||
@ -18,11 +28,15 @@ void VNewDirDialog::setupUI()
|
||||
infoLabel->setWordWrap(true);
|
||||
}
|
||||
|
||||
nameLabel = new QLabel(name);
|
||||
QLabel *nameLabel = new QLabel("Folder &name:");
|
||||
nameEdit = new QLineEdit(defaultName);
|
||||
nameEdit->selectAll();
|
||||
nameLabel->setBuddy(nameEdit);
|
||||
|
||||
m_warnLabel = new QLabel();
|
||||
m_warnLabel->setWordWrap(true);
|
||||
m_warnLabel->hide();
|
||||
|
||||
// Ok is the default button.
|
||||
m_btnBox = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel);
|
||||
connect(m_btnBox, &QDialogButtonBox::accepted, this, &QDialog::accept);
|
||||
@ -40,16 +54,35 @@ void VNewDirDialog::setupUI()
|
||||
mainLayout->addWidget(infoLabel);
|
||||
}
|
||||
mainLayout->addLayout(topLayout);
|
||||
mainLayout->addWidget(m_warnLabel);
|
||||
mainLayout->addWidget(m_btnBox);
|
||||
mainLayout->setSizeConstraint(QLayout::SetFixedSize);
|
||||
setLayout(mainLayout);
|
||||
setWindowTitle(title);
|
||||
}
|
||||
|
||||
void VNewDirDialog::enableOkButton(const QString &editText)
|
||||
void VNewDirDialog::handleInputChanged()
|
||||
{
|
||||
bool showWarnLabel = false;
|
||||
QString name = nameEdit->text();
|
||||
bool nameOk = !name.isEmpty();
|
||||
if (nameOk) {
|
||||
// Check if the name conflicts with existing directory name.
|
||||
// Case-insensitive when creating folder.
|
||||
if (m_directory->findSubDirectory(name, false)) {
|
||||
nameOk = false;
|
||||
showWarnLabel = true;
|
||||
QString nameConflictText = tr("<span style=\"%1\">WARNING</span>: Name (case-insensitive) already exists. "
|
||||
"Please choose another name.")
|
||||
.arg(g_config->c_warningTextStyle);
|
||||
m_warnLabel->setText(nameConflictText);
|
||||
}
|
||||
}
|
||||
|
||||
m_warnLabel->setVisible(showWarnLabel);
|
||||
|
||||
QPushButton *okBtn = m_btnBox->button(QDialogButtonBox::Ok);
|
||||
okBtn->setEnabled(!editText.isEmpty());
|
||||
okBtn->setEnabled(nameOk);
|
||||
}
|
||||
|
||||
QString VNewDirDialog::getNameInput() const
|
||||
|
@ -7,29 +7,36 @@ class QLabel;
|
||||
class QLineEdit;
|
||||
class QDialogButtonBox;
|
||||
class QString;
|
||||
class VDirectory;
|
||||
|
||||
class VNewDirDialog : public QDialog
|
||||
{
|
||||
Q_OBJECT
|
||||
public:
|
||||
VNewDirDialog(const QString &title, const QString &info, const QString &name,
|
||||
const QString &defaultName, QWidget *parent = 0);
|
||||
VNewDirDialog(const QString &title,
|
||||
const QString &info,
|
||||
const QString &defaultName,
|
||||
VDirectory *directory,
|
||||
QWidget *parent = 0);
|
||||
|
||||
QString getNameInput() const;
|
||||
|
||||
private slots:
|
||||
void enableOkButton(const QString &editText);
|
||||
void handleInputChanged();
|
||||
|
||||
private:
|
||||
void setupUI();
|
||||
|
||||
QLabel *nameLabel;
|
||||
QLineEdit *nameEdit;
|
||||
QDialogButtonBox *m_btnBox;
|
||||
|
||||
QLabel *m_warnLabel;
|
||||
|
||||
QString title;
|
||||
QString info;
|
||||
QString name;
|
||||
QString defaultName;
|
||||
|
||||
VDirectory *m_directory;
|
||||
};
|
||||
|
||||
#endif // VNEWDIRDIALOG_H
|
||||
|
@ -77,7 +77,7 @@ void VNewFileDialog::handleInputChanged()
|
||||
QString name = nameEdit->text();
|
||||
bool nameOk = !name.isEmpty();
|
||||
if (nameOk) {
|
||||
// Check if the name conflicts with existing notebook name.
|
||||
// Check if the name conflicts with existing note name.
|
||||
// Case-insensitive when creating note.
|
||||
if (m_directory->findFile(name, false)) {
|
||||
nameOk = false;
|
||||
|
@ -388,21 +388,11 @@ void VDirectoryTree::newSubDirectory()
|
||||
|
||||
QString info = tr("Create a subfolder in <span style=\"%1\">%2</span>.")
|
||||
.arg(g_config->c_dataTextStyle).arg(curDir->getName());
|
||||
QString text(tr("Folder &name:"));
|
||||
QString defaultText("new_folder");
|
||||
|
||||
do {
|
||||
VNewDirDialog dialog(tr("Create Folder"), info, text, defaultText, this);
|
||||
QString defaultName("new_folder");
|
||||
defaultName = VUtils::getFileNameWithSequence(curDir->fetchPath(), defaultName);
|
||||
VNewDirDialog dialog(tr("Create Folder"), info, defaultName, curDir, this);
|
||||
if (dialog.exec() == QDialog::Accepted) {
|
||||
QString name = dialog.getNameInput();
|
||||
// Case-insensitive.
|
||||
if (curDir->findSubDirectory(name, false)) {
|
||||
info = tr("Name (case-insensitive) already exists in "
|
||||
"<span style=\"%1\">%2</span>. Please choose another name.")
|
||||
.arg(g_config->c_dataTextStyle).arg(curDir->getName());
|
||||
defaultText = name;
|
||||
continue;
|
||||
}
|
||||
|
||||
VDirectory *subDir = curDir->createSubDirectory(name);
|
||||
if (!subDir) {
|
||||
@ -412,11 +402,10 @@ void VDirectoryTree::newSubDirectory()
|
||||
QMessageBox::Ok, QMessageBox::Ok, this);
|
||||
return;
|
||||
}
|
||||
|
||||
updateItemChildren(curItem);
|
||||
locateDirectory(subDir);
|
||||
}
|
||||
break;
|
||||
} while (true);
|
||||
}
|
||||
|
||||
void VDirectoryTree::newRootDirectory()
|
||||
@ -426,20 +415,13 @@ void VDirectoryTree::newRootDirectory()
|
||||
}
|
||||
QString info = tr("Create a root folder in notebook <span style=\"%1\">%2</span>.")
|
||||
.arg(g_config->c_dataTextStyle).arg(m_notebook->getName());
|
||||
QString text(tr("Folder &name:"));
|
||||
QString defaultText("new_folder");
|
||||
VDirectory *rootDir = m_notebook->getRootDir();
|
||||
do {
|
||||
VNewDirDialog dialog(tr("Create Root Folder"), info, text, defaultText, this);
|
||||
QString defaultName("new_folder");
|
||||
defaultName = VUtils::getFileNameWithSequence(rootDir->fetchPath(), defaultName);
|
||||
VNewDirDialog dialog(tr("Create Root Folder"), info, defaultName, rootDir, this);
|
||||
if (dialog.exec() == QDialog::Accepted) {
|
||||
QString name = dialog.getNameInput();
|
||||
if (rootDir->findSubDirectory(name, false)) {
|
||||
info = tr("Name (case-insensitive) already exists in "
|
||||
"notebook <span style=\"%1\">%2</span>. Please choose another name.")
|
||||
.arg(g_config->c_dataTextStyle).arg(m_notebook->getName());
|
||||
defaultText = name;
|
||||
continue;
|
||||
}
|
||||
|
||||
VDirectory *subDir = rootDir->createSubDirectory(name);
|
||||
if (!subDir) {
|
||||
VUtils::showMessage(QMessageBox::Warning, tr("Warning"),
|
||||
@ -448,11 +430,10 @@ void VDirectoryTree::newRootDirectory()
|
||||
QMessageBox::Ok, QMessageBox::Ok, this);
|
||||
return;
|
||||
}
|
||||
|
||||
updateItemChildren(NULL);
|
||||
locateDirectory(subDir);
|
||||
}
|
||||
break;
|
||||
} while (true);
|
||||
}
|
||||
|
||||
void VDirectoryTree::deleteDirectory()
|
||||
@ -503,23 +484,14 @@ void VDirectoryTree::editDirectoryInfo()
|
||||
VDirectory *curDir = getVDirectory(curItem);
|
||||
VDirectory *parentDir = curDir->getParentDirectory();
|
||||
QString curName = curDir->getName();
|
||||
QString info;
|
||||
QString defaultName = curName;
|
||||
|
||||
do {
|
||||
VDirInfoDialog dialog(tr("Folder Information"), info, defaultName, this);
|
||||
VDirInfoDialog dialog(tr("Folder Information"), "", curDir, parentDir, this);
|
||||
if (dialog.exec() == QDialog::Accepted) {
|
||||
QString name = dialog.getNameInput();
|
||||
if (name == curName) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (parentDir->findSubDirectory(name, false)) {
|
||||
info = "Name (case-insensitive) already exists. Please choose another name.";
|
||||
defaultName = name;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!curDir->rename(name)) {
|
||||
VUtils::showMessage(QMessageBox::Warning, tr("Warning"),
|
||||
tr("Fail to rename folder <span style=\"%1\">%2</span>.")
|
||||
@ -527,12 +499,11 @@ void VDirectoryTree::editDirectoryInfo()
|
||||
QMessageBox::Ok, QMessageBox::Ok, this);
|
||||
return;
|
||||
}
|
||||
|
||||
curItem->setText(0, name);
|
||||
curItem->setToolTip(0, name);
|
||||
emit directoryUpdated(curDir);
|
||||
}
|
||||
break;
|
||||
} while (true);
|
||||
}
|
||||
|
||||
void VDirectoryTree::openDirectoryLocation() const
|
||||
|
Loading…
x
Reference in New Issue
Block a user