|
|
@@ -3,23 +3,26 @@
|
|
|
#include "vnewnotebookdialog.h"
|
|
|
#include "vconfigmanager.h"
|
|
|
#include "utils/vutils.h"
|
|
|
+#include "vnotebook.h"
|
|
|
|
|
|
extern VConfigManager vconfig;
|
|
|
|
|
|
VNewNotebookDialog::VNewNotebookDialog(const QString &title, const QString &info,
|
|
|
const QString &defaultName, const QString &defaultPath,
|
|
|
+ const QVector<VNotebook *> &p_notebooks,
|
|
|
QWidget *parent)
|
|
|
: QDialog(parent), infoLabel(NULL),
|
|
|
- title(title), info(info), defaultName(defaultName), defaultPath(defaultPath)
|
|
|
+ title(title), info(info), defaultName(defaultName), defaultPath(defaultPath),
|
|
|
+ m_importNotebook(false), m_manualPath(false), m_manualName(false),
|
|
|
+ m_notebooks(p_notebooks)
|
|
|
{
|
|
|
setupUI();
|
|
|
|
|
|
- connect(nameEdit, &QLineEdit::textChanged, this, &VNewNotebookDialog::enableOkButton);
|
|
|
- connect(pathEdit, &QLineEdit::textChanged, this, &VNewNotebookDialog::handlePathChanged);
|
|
|
+ connect(nameEdit, &QLineEdit::textChanged, this, &VNewNotebookDialog::handleInputChanged);
|
|
|
+ connect(pathEdit, &QLineEdit::textChanged, this, &VNewNotebookDialog::handleInputChanged);
|
|
|
connect(browseBtn, &QPushButton::clicked, this, &VNewNotebookDialog::handleBrowseBtnClicked);
|
|
|
|
|
|
- enableOkButton();
|
|
|
- checkRootFolder(pathEdit->text());
|
|
|
+ handleInputChanged();
|
|
|
}
|
|
|
|
|
|
void VNewNotebookDialog::setupUI()
|
|
|
@@ -38,11 +41,6 @@ void VNewNotebookDialog::setupUI()
|
|
|
pathLabel->setBuddy(pathEdit);
|
|
|
browseBtn = new QPushButton(tr("&Browse"));
|
|
|
|
|
|
- importCheck = new QCheckBox(tr("Import existing notebook"));
|
|
|
- importCheck->setToolTip(tr("When checked, VNote will read the configuration file to import an existing notebook"));
|
|
|
- connect(importCheck, &QCheckBox::stateChanged,
|
|
|
- this, &VNewNotebookDialog::importCheckChanged);
|
|
|
-
|
|
|
QLabel *imageFolderLabel = new QLabel(tr("&Image folder:"));
|
|
|
m_imageFolderEdit = new QLineEdit();
|
|
|
m_imageFolderEdit->setPlaceholderText(tr("Use global configuration (%1)")
|
|
|
@@ -61,9 +59,13 @@ void VNewNotebookDialog::setupUI()
|
|
|
topLayout->addWidget(pathLabel, 1, 0);
|
|
|
topLayout->addWidget(pathEdit, 1, 1);
|
|
|
topLayout->addWidget(browseBtn, 1, 2);
|
|
|
- topLayout->addWidget(importCheck, 2, 1);
|
|
|
- topLayout->addWidget(imageFolderLabel, 3, 0);
|
|
|
- topLayout->addWidget(m_imageFolderEdit, 3, 1);
|
|
|
+ topLayout->addWidget(imageFolderLabel, 2, 0);
|
|
|
+ topLayout->addWidget(m_imageFolderEdit, 2, 1);
|
|
|
+
|
|
|
+ // Warning label.
|
|
|
+ m_warnLabel = new QLabel();
|
|
|
+ m_warnLabel->setWordWrap(true);
|
|
|
+ m_warnLabel->hide();
|
|
|
|
|
|
// Ok is the default button.
|
|
|
m_btnBox = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel);
|
|
|
@@ -73,14 +75,6 @@ void VNewNotebookDialog::setupUI()
|
|
|
QPushButton *okBtn = m_btnBox->button(QDialogButtonBox::Ok);
|
|
|
pathEdit->setMinimumWidth(okBtn->sizeHint().width() * 3);
|
|
|
|
|
|
- // Warning label.
|
|
|
- m_warnLabel = new QLabel(tr("<span style=\"%1\">WARNING</span>: The folder you choose is NOT empty! "
|
|
|
- "It is highly recommended to use an EMPTY and EXCLUSIVE folder for a notebook. "
|
|
|
- "Ignore this warning if you do want to import an existing VNote notebook folder.")
|
|
|
- .arg(vconfig.c_warningTextStyle));
|
|
|
- m_warnLabel->setWordWrap(true);
|
|
|
- m_warnLabel->hide();
|
|
|
-
|
|
|
QVBoxLayout *mainLayout = new QVBoxLayout(this);
|
|
|
if (infoLabel) {
|
|
|
mainLayout->addWidget(infoLabel);
|
|
|
@@ -88,20 +82,13 @@ void VNewNotebookDialog::setupUI()
|
|
|
mainLayout->addLayout(topLayout);
|
|
|
mainLayout->addWidget(m_warnLabel);
|
|
|
mainLayout->addWidget(m_btnBox);
|
|
|
+
|
|
|
// Will set the parent of above widgets properly.
|
|
|
setLayout(mainLayout);
|
|
|
mainLayout->setSizeConstraint(QLayout::SetFixedSize);
|
|
|
setWindowTitle(title);
|
|
|
}
|
|
|
|
|
|
-void VNewNotebookDialog::enableOkButton()
|
|
|
-{
|
|
|
- QPushButton *okBtn = m_btnBox->button(QDialogButtonBox::Ok);
|
|
|
- okBtn->setEnabled(!pathEdit->text().isEmpty()
|
|
|
- && !nameEdit->text().isEmpty()
|
|
|
- && QDir(pathEdit->text()).exists());
|
|
|
-}
|
|
|
-
|
|
|
QString VNewNotebookDialog::getNameInput() const
|
|
|
{
|
|
|
return nameEdit->text();
|
|
|
@@ -109,27 +96,42 @@ QString VNewNotebookDialog::getNameInput() const
|
|
|
|
|
|
QString VNewNotebookDialog::getPathInput() const
|
|
|
{
|
|
|
- return pathEdit->text();
|
|
|
+ return QDir::cleanPath(pathEdit->text());
|
|
|
}
|
|
|
|
|
|
QString VNewNotebookDialog::getImageFolder() const
|
|
|
{
|
|
|
- return m_imageFolderEdit->text();
|
|
|
+ if (m_imageFolderEdit->isEnabled()) {
|
|
|
+ return m_imageFolderEdit->text();
|
|
|
+ } else {
|
|
|
+ return QString();
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
void VNewNotebookDialog::handleBrowseBtnClicked()
|
|
|
{
|
|
|
+ static QString defaultPath;
|
|
|
+ if (defaultPath.isEmpty()) {
|
|
|
+ defaultPath = vconfig.getVnoteNotebookFolderPath();
|
|
|
+ if (!QFileInfo::exists(defaultPath)) {
|
|
|
+ defaultPath = QDir::homePath();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
QString dirPath = QFileDialog::getExistingDirectory(this, tr("Select Root Folder Of The Notebook"),
|
|
|
- QDir::homePath(), QFileDialog::ShowDirsOnly | QFileDialog::DontResolveSymlinks);
|
|
|
+ defaultPath,
|
|
|
+ QFileDialog::ShowDirsOnly | QFileDialog::DontResolveSymlinks);
|
|
|
|
|
|
if (!dirPath.isEmpty()) {
|
|
|
+ m_manualPath = true;
|
|
|
pathEdit->setText(dirPath);
|
|
|
+ defaultPath = VUtils::basePathFromPath(dirPath);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-bool VNewNotebookDialog::getImportCheck() const
|
|
|
+bool VNewNotebookDialog::isImportExistingNotebook() const
|
|
|
{
|
|
|
- return importCheck->isChecked();
|
|
|
+ return m_importNotebook;
|
|
|
}
|
|
|
|
|
|
void VNewNotebookDialog::showEvent(QShowEvent *event)
|
|
|
@@ -138,38 +140,149 @@ void VNewNotebookDialog::showEvent(QShowEvent *event)
|
|
|
QDialog::showEvent(event);
|
|
|
}
|
|
|
|
|
|
-void VNewNotebookDialog::handlePathChanged(const QString &p_text)
|
|
|
+void VNewNotebookDialog::handleInputChanged()
|
|
|
{
|
|
|
- enableOkButton();
|
|
|
- checkRootFolder(p_text);
|
|
|
-}
|
|
|
+ QString warnText = tr("<span style=\"%1\">WARNING</span>: The folder chosen is NOT empty! "
|
|
|
+ "It is highly recommended to use an EMPTY and EXCLUSIVE folder for a new notebook.")
|
|
|
+ .arg(vconfig.c_warningTextStyle);
|
|
|
+ QString infoText = tr("<span style=\"%1\">INFO</span>: The folder chosen seems to be a root "
|
|
|
+ "folder of a notebook created by VNote before. "
|
|
|
+ "VNote will try to import it by reading the configuration file.")
|
|
|
+ .arg("font-weight:bold;");
|
|
|
+ bool pathOk = false;
|
|
|
+ bool configExist = false;
|
|
|
+ bool showWarnLabel = false;
|
|
|
|
|
|
-void VNewNotebookDialog::importCheckChanged(int p_state)
|
|
|
-{
|
|
|
- // If import existing notebook, disable setting new configs.
|
|
|
- bool checked = p_state == Qt::Checked;
|
|
|
+ // User has input some texts.
|
|
|
+ if (pathEdit->isModified()) {
|
|
|
+ m_manualPath = true;
|
|
|
+ }
|
|
|
+
|
|
|
+ if (nameEdit->isModified()) {
|
|
|
+ m_manualName = true;
|
|
|
+ }
|
|
|
|
|
|
- m_imageFolderEdit->setEnabled(!checked);
|
|
|
+ if (autoComplete()) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ QString path = pathEdit->text();
|
|
|
+ if (!path.isEmpty()) {
|
|
|
+ if (QFileInfo::exists(path)) {
|
|
|
+ QDir dir(path);
|
|
|
+ QStringList files = dir.entryList(QDir::NoDotAndDotDot | QDir::AllEntries | QDir::Hidden);
|
|
|
+ if (!files.isEmpty()) {
|
|
|
+ // Folder is not empty.
|
|
|
+ configExist = VConfigManager::directoryConfigExist(path);
|
|
|
+ showWarnLabel = true;
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ pathOk = true;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ if (configExist) {
|
|
|
+ pathOk = true;
|
|
|
+ m_warnLabel->setText(infoText);
|
|
|
+ } else {
|
|
|
+ m_warnLabel->setText(warnText);
|
|
|
+ }
|
|
|
+
|
|
|
+ // Try to validate if this is a legal path on the OS.
|
|
|
+ if (pathOk) {
|
|
|
+ pathOk = VUtils::checkPathLegal(path);
|
|
|
+ if (!pathOk) {
|
|
|
+ showWarnLabel = true;
|
|
|
+ QString tmp = tr("<span style=\"%1\">WARNING</span>: The path seems to be illegal. "
|
|
|
+ "Please choose another one.")
|
|
|
+ .arg(vconfig.c_warningTextStyle);
|
|
|
+ m_warnLabel->setText(tmp);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ if (pathOk) {
|
|
|
+ // Check if this path has been in VNote.
|
|
|
+ int idx = -1;
|
|
|
+ path = QDir::cleanPath(path);
|
|
|
+ for (idx = 0; idx < m_notebooks.size(); ++idx) {
|
|
|
+ if (QDir::cleanPath(m_notebooks[idx]->getPath()) == path) {
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ if (idx < m_notebooks.size()) {
|
|
|
+ pathOk = false;
|
|
|
+ showWarnLabel = true;
|
|
|
+ QString existText = tr("<span style=\"%1\">WARNING</span>: The folder chosen has already been a root folder "
|
|
|
+ "of existing notebook <span style=\"%2\">%3</span> in VNote.")
|
|
|
+ .arg(vconfig.c_warningTextStyle)
|
|
|
+ .arg(vconfig.c_dataTextStyle)
|
|
|
+ .arg(m_notebooks[idx]->getName());
|
|
|
+ m_warnLabel->setText(existText);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ QString name = nameEdit->text();
|
|
|
+ bool nameOk = !name.isEmpty();
|
|
|
+ if (pathOk && nameOk) {
|
|
|
+ // Check if the name conflicts with existing notebook name.
|
|
|
+ int idx = -1;
|
|
|
+ for (idx = 0; idx < m_notebooks.size(); ++idx) {
|
|
|
+ if (m_notebooks[idx]->getName() == name) {
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ if (idx < m_notebooks.size()) {
|
|
|
+ nameOk = false;
|
|
|
+ showWarnLabel = true;
|
|
|
+ QString nameConflictText = tr("<span style=\"%1\">WARNING</span>: Name already exists. "
|
|
|
+ "Please choose another name.")
|
|
|
+ .arg(vconfig.c_warningTextStyle);
|
|
|
+ m_warnLabel->setText(nameConflictText);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ m_warnLabel->setVisible(showWarnLabel);
|
|
|
+ m_importNotebook = configExist;
|
|
|
+ m_imageFolderEdit->setEnabled(!m_importNotebook);
|
|
|
+
|
|
|
+ QPushButton *okBtn = m_btnBox->button(QDialogButtonBox::Ok);
|
|
|
+ okBtn->setEnabled(nameOk && pathOk);
|
|
|
}
|
|
|
|
|
|
-void VNewNotebookDialog::checkRootFolder(const QString &p_path)
|
|
|
+bool VNewNotebookDialog::autoComplete()
|
|
|
{
|
|
|
- bool existConfig = false;
|
|
|
+ if (m_manualPath) {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
|
|
|
- if (!p_path.isEmpty()) {
|
|
|
- QDir dir(p_path);
|
|
|
- QStringList files = dir.entryList(QDir::NoDotAndDotDot | QDir::AllEntries | QDir::Hidden);
|
|
|
- if (!files.isEmpty()) {
|
|
|
- m_warnLabel->show();
|
|
|
- } else {
|
|
|
- m_warnLabel->hide();
|
|
|
+ QString vnoteFolder = vconfig.getVnoteNotebookFolderPath();
|
|
|
+ QString pathText = pathEdit->text();
|
|
|
+ if (!pathText.isEmpty() && vnoteFolder != VUtils::basePathFromPath(pathText)) {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ bool ret = false;
|
|
|
+ QString nameText = nameEdit->text();
|
|
|
+ if (nameText.isEmpty()) {
|
|
|
+ if (m_manualName) {
|
|
|
+ return false;
|
|
|
}
|
|
|
|
|
|
- existConfig = VConfigManager::directoryConfigExist(p_path);
|
|
|
+ // Get a folder name under vnoteFolder and set it as the name of the notebook.
|
|
|
+ QString name = "vnotebook";
|
|
|
+ name = VUtils::getFileNameWithSequence(vnoteFolder, name);
|
|
|
+ nameEdit->setText(name);
|
|
|
+ ret = true;
|
|
|
} else {
|
|
|
- m_warnLabel->hide();
|
|
|
+ // Use the name as the folder name under vnoteFolder.
|
|
|
+ QString autoPath = QDir::cleanPath(QDir(vnoteFolder).filePath(nameText));
|
|
|
+ if (autoPath != pathText) {
|
|
|
+ pathEdit->setText(autoPath);
|
|
|
+ ret = true;
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
- importCheck->setChecked(existConfig);
|
|
|
- importCheck->setEnabled(existConfig);
|
|
|
+ return ret;
|
|
|
}
|