From 4626673925d1ac2af1412ddbdad2ba0a3ce862e2 Mon Sep 17 00:00:00 2001 From: Le Tan Date: Thu, 14 Sep 2017 20:27:21 +0800 Subject: [PATCH] support created_time of notebook and folder --- src/dialog/vdirinfodialog.cpp | 19 +++++++++----- src/dialog/vdirinfodialog.h | 2 -- src/dialog/vnotebookinfodialog.cpp | 42 ++++++++++++++++-------------- src/dialog/vnotebookinfodialog.h | 1 - src/vdirectory.cpp | 27 +++++++++++++++---- src/vdirectory.h | 25 +++++++++++++++++- src/vmainwindow.cpp | 2 +- src/vnotebook.cpp | 16 +++++++++++- src/vnotebook.h | 4 +++ 9 files changed, 100 insertions(+), 38 deletions(-) diff --git a/src/dialog/vdirinfodialog.cpp b/src/dialog/vdirinfodialog.cpp index 608f0961..b384021e 100644 --- a/src/dialog/vdirinfodialog.cpp +++ b/src/dialog/vdirinfodialog.cpp @@ -10,7 +10,7 @@ VDirInfoDialog::VDirInfoDialog(const QString &title, const VDirectory *directory, VDirectory *parentDirectory, QWidget *parent) - : QDialog(parent), infoLabel(NULL), title(title), info(info), + : QDialog(parent), title(title), info(info), m_directory(directory), m_parentDirectory(parentDirectory) { setupUI(); @@ -22,13 +22,22 @@ VDirInfoDialog::VDirInfoDialog(const QString &title, void VDirInfoDialog::setupUI() { + QLabel *infoLabel = NULL; if (!info.isEmpty()) { infoLabel = new QLabel(info); } - nameLabel = new QLabel(tr("Folder &name:")); + nameEdit = new QLineEdit(m_directory->getName()); nameEdit->selectAll(); - nameLabel->setBuddy(nameEdit); + + // Created time. + QString createdTimeStr = m_directory->getCreatedTimeUtc().toLocalTime() + .toString(Qt::DefaultLocaleLongDate); + QLabel *createdTimeLabel = new QLabel(createdTimeStr); + + QFormLayout *topLayout = new QFormLayout(); + topLayout->addRow(tr("Folder &name:"), nameEdit); + topLayout->addRow(tr("Created time:"), createdTimeLabel); m_warnLabel = new QLabel(); m_warnLabel->setWordWrap(true); @@ -39,10 +48,6 @@ void VDirInfoDialog::setupUI() connect(m_btnBox, &QDialogButtonBox::accepted, this, &QDialog::accept); connect(m_btnBox, &QDialogButtonBox::rejected, this, &QDialog::reject); - QHBoxLayout *topLayout = new QHBoxLayout(); - topLayout->addWidget(nameLabel); - topLayout->addWidget(nameEdit); - QPushButton *okBtn = m_btnBox->button(QDialogButtonBox::Ok); nameEdit->setMinimumWidth(okBtn->sizeHint().width() * 3); diff --git a/src/dialog/vdirinfodialog.h b/src/dialog/vdirinfodialog.h index 7e477e57..558334df 100644 --- a/src/dialog/vdirinfodialog.h +++ b/src/dialog/vdirinfodialog.h @@ -27,8 +27,6 @@ private slots: private: void setupUI(); - QLabel *infoLabel; - QLabel *nameLabel; QLineEdit *nameEdit; QLabel *m_warnLabel; QDialogButtonBox *m_btnBox; diff --git a/src/dialog/vnotebookinfodialog.cpp b/src/dialog/vnotebookinfodialog.cpp index 897abd61..15f650c4 100644 --- a/src/dialog/vnotebookinfodialog.cpp +++ b/src/dialog/vnotebookinfodialog.cpp @@ -11,7 +11,7 @@ VNotebookInfoDialog::VNotebookInfoDialog(const QString &p_title, const VNotebook *p_notebook, const QVector &p_notebooks, QWidget *p_parent) - : QDialog(p_parent), m_notebook(p_notebook), m_infoLabel(NULL), + : QDialog(p_parent), m_notebook(p_notebook), m_notebooks(p_notebooks) { setupUI(p_title, p_info); @@ -24,43 +24,44 @@ VNotebookInfoDialog::VNotebookInfoDialog(const QString &p_title, void VNotebookInfoDialog::setupUI(const QString &p_title, const QString &p_info) { + QLabel *infoLabel = NULL; if (!p_info.isEmpty()) { - m_infoLabel = new QLabel(p_info); + infoLabel = new QLabel(p_info); } - QLabel *nameLabel = new QLabel(tr("Notebook &name:")); m_nameEdit = new QLineEdit(m_notebook->getName()); m_nameEdit->selectAll(); - nameLabel->setBuddy(m_nameEdit); - QLabel *pathLabel = new QLabel(tr("Notebook &root folder:")); m_pathEdit = new QLineEdit(m_notebook->getPath()); - pathLabel->setBuddy(m_pathEdit); m_pathEdit->setReadOnly(true); - QLabel *imageFolderLabel = new QLabel(tr("&Image folder:")); m_imageFolderEdit = new QLineEdit(m_notebook->getImageFolderConfig()); m_imageFolderEdit->setPlaceholderText(tr("Use global configuration (%1)") .arg(g_config->getImageFolder())); - imageFolderLabel->setBuddy(m_imageFolderEdit); - QString imageFolderTip = tr("Set the name of the folder for all the notes of this notebook to store images " - "(empty to use global configuration)"); - m_imageFolderEdit->setToolTip(imageFolderTip); - imageFolderLabel->setToolTip(imageFolderTip); + m_imageFolderEdit->setToolTip(tr("Set the name of the folder for all the notes of this notebook to store images " + "(empty to use global configuration)")); QValidator *validator = new QRegExpValidator(QRegExp(VUtils::c_fileNameRegExp), m_imageFolderEdit); m_imageFolderEdit->setValidator(validator); + QLabel *imageFolderLabel = new QLabel(tr("&Image folder:")); + imageFolderLabel->setBuddy(m_imageFolderEdit); + imageFolderLabel->setToolTip(m_imageFolderEdit->toolTip()); + + // Created time. + QString createdTimeStr = const_cast(m_notebook)->getCreatedTimeUtc().toLocalTime() + .toString(Qt::DefaultLocaleLongDate); + QLabel *createdTimeLabel = new QLabel(createdTimeStr); + + QFormLayout *topLayout = new QFormLayout(); + topLayout->addRow(tr("Notebook &name:"), m_nameEdit); + topLayout->addRow(tr("Notebook &root folder:"), m_pathEdit); + topLayout->addRow(imageFolderLabel, m_imageFolderEdit); + topLayout->addRow(tr("Created time:"), createdTimeLabel); // Warning label. m_warnLabel = new QLabel(); m_warnLabel->setWordWrap(true); m_warnLabel->hide(); - QFormLayout *topLayout = new QFormLayout(); - topLayout->addRow(nameLabel, m_nameEdit); - topLayout->addRow(pathLabel, m_pathEdit); - topLayout->addRow(imageFolderLabel, m_imageFolderEdit); - topLayout->addRow(m_warnLabel); - // Ok is the default button. m_btnBox = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel); connect(m_btnBox, &QDialogButtonBox::accepted, this, &QDialog::accept); @@ -70,11 +71,12 @@ void VNotebookInfoDialog::setupUI(const QString &p_title, const QString &p_info) m_pathEdit->setMinimumWidth(okBtn->sizeHint().width() * 3); QVBoxLayout *mainLayout = new QVBoxLayout(); - if (m_infoLabel) { - mainLayout->addWidget(m_infoLabel); + if (infoLabel) { + mainLayout->addWidget(infoLabel); } mainLayout->addLayout(topLayout); + mainLayout->addWidget(m_warnLabel); mainLayout->addWidget(m_btnBox); setLayout(mainLayout); diff --git a/src/dialog/vnotebookinfodialog.h b/src/dialog/vnotebookinfodialog.h index a43c77ee..5e9eaf88 100644 --- a/src/dialog/vnotebookinfodialog.h +++ b/src/dialog/vnotebookinfodialog.h @@ -38,7 +38,6 @@ private: const VNotebook *m_notebook; - QLabel *m_infoLabel; QLineEdit *m_nameEdit; QLineEdit *m_pathEdit; QLineEdit *m_imageFolderEdit; diff --git a/src/vdirectory.cpp b/src/vdirectory.cpp index a525a804..514411d9 100644 --- a/src/vdirectory.cpp +++ b/src/vdirectory.cpp @@ -10,9 +10,15 @@ extern VConfigManager *g_config; VDirectory::VDirectory(VNotebook *p_notebook, - const QString &p_name, QObject *p_parent) - : QObject(p_parent), m_notebook(p_notebook), m_name(p_name), m_opened(false), - m_expanded(false) + const QString &p_name, + QObject *p_parent, + QDateTime p_createdTimeUtc) + : QObject(p_parent), + m_notebook(p_notebook), + m_name(p_name), + m_opened(false), + m_expanded(false), + m_createdTimeUtc(p_createdTimeUtc) { } @@ -31,6 +37,10 @@ bool VDirectory::open() return false; } + // created_time + m_createdTimeUtc = QDateTime::fromString(configJson[DirConfig::c_createdTime].toString(), + Qt::ISODate); + // [sub_directories] section QJsonArray dirJson = configJson[DirConfig::c_subDirectories].toArray(); for (int i = 0; i < dirJson.size(); ++i) { @@ -114,6 +124,7 @@ QJsonObject VDirectory::toConfigJson() const { QJsonObject dirJson; dirJson[DirConfig::c_version] = "1"; + dirJson[DirConfig::c_createdTime] = m_createdTimeUtc.toString(Qt::ISODate); QJsonArray subDirs; for (int i = 0; i < m_subDirs.size(); ++i) { @@ -187,7 +198,10 @@ VDirectory *VDirectory::createSubDirectory(const QString &p_name) return NULL; } - VDirectory *ret = new VDirectory(m_notebook, p_name, this); + VDirectory *ret = new VDirectory(m_notebook, + p_name, + this, + QDateTime::currentDateTimeUtc()); if (!ret->writeToConfig()) { dir.rmdir(p_name); delete ret; @@ -382,7 +396,10 @@ VDirectory *VDirectory::addSubDirectory(const QString &p_name, int p_index) return NULL; } - VDirectory *dir = new VDirectory(m_notebook, p_name, this); + VDirectory *dir = new VDirectory(m_notebook, + p_name, + this, + QDateTime::currentDateTimeUtc()); if (!dir) { return NULL; } diff --git a/src/vdirectory.h b/src/vdirectory.h index 88e7d47a..e0c40ba1 100644 --- a/src/vdirectory.h +++ b/src/vdirectory.h @@ -6,6 +6,7 @@ #include #include #include +#include #include "vnotebook.h" class VFile; @@ -15,7 +16,10 @@ class VDirectory : public QObject Q_OBJECT public: VDirectory(VNotebook *p_notebook, - const QString &p_name, QObject *p_parent = 0); + const QString &p_name, + QObject *p_parent = 0, + QDateTime p_createdTimeUtc = QDateTime()); + bool open(); void close(); VDirectory *createSubDirectory(const QString &p_name); @@ -94,6 +98,8 @@ public: // Try to load file given relative path @p_filePath. VFile *tryLoadFile(QStringList &p_filePath); + QDateTime getCreatedTimeUtc() const; + private: // Get the path of @p_dir recursively QString fetchPath(const VDirectory *p_dir) const; @@ -117,15 +123,27 @@ private: // Add the directory in the config and m_subDirs. If @p_index is -1, add it at the end. bool addSubDirectory(VDirectory *p_dir, int p_index); + // Notebook containing this folder. QPointer m_notebook; + + // Name of this folder. QString m_name; + // Owner of the sub-directories QVector m_subDirs; + // Owner of the files QVector m_files; + + // Whether the directory has been opened. bool m_opened; + // Whether expanded in the directory tree. bool m_expanded; + + // UTC time when creating this directory. + // Loaded after open(). + QDateTime m_createdTimeUtc; }; inline const QVector &VDirectory::getSubDirs() const @@ -193,4 +211,9 @@ inline bool VDirectory::isExpanded() const return m_expanded; } +inline QDateTime VDirectory::getCreatedTimeUtc() const +{ + return m_createdTimeUtc; +} + #endif // VDIRECTORY_H diff --git a/src/vmainwindow.cpp b/src/vmainwindow.cpp index a1967aae..319d63d6 100644 --- a/src/vmainwindow.cpp +++ b/src/vmainwindow.cpp @@ -589,7 +589,7 @@ void VMainWindow::initMarkdownMenu() markdownMenu->addAction(codeBlockAct); codeBlockAct->setChecked(g_config->getEnableCodeBlockHighlight()); - QAction *lineNumberAct = new QAction(tr("Display Line Number in Code Blocks"), this); + QAction *lineNumberAct = new QAction(tr("Display Line Number In Code Blocks"), this); lineNumberAct->setToolTip(tr("Enable line number in code blocks in read mode")); lineNumberAct->setCheckable(true); connect(lineNumberAct, &QAction::triggered, diff --git a/src/vnotebook.cpp b/src/vnotebook.cpp index a472d849..c5aeb972 100644 --- a/src/vnotebook.cpp +++ b/src/vnotebook.cpp @@ -12,7 +12,10 @@ VNotebook::VNotebook(const QString &name, const QString &path, QObject *parent) : QObject(parent), m_name(name) { m_path = QDir::cleanPath(path); - m_rootDir = new VDirectory(this, VUtils::directoryNameFromPath(path)); + m_rootDir = new VDirectory(this, + VUtils::directoryNameFromPath(path), + NULL, + QDateTime::currentDateTimeUtc()); } VNotebook::~VNotebook() @@ -241,3 +244,14 @@ bool VNotebook::isOpened() const { return m_rootDir->isOpened(); } + +QDateTime VNotebook::getCreatedTimeUtc() +{ + if (!isOpened()) { + if (!open()) { + return QDateTime(); + } + } + + return m_rootDir->getCreatedTimeUtc(); +} diff --git a/src/vnotebook.h b/src/vnotebook.h index c01153a0..3bc0501a 100644 --- a/src/vnotebook.h +++ b/src/vnotebook.h @@ -3,6 +3,7 @@ #include #include +#include class VDirectory; class VFile; @@ -66,6 +67,9 @@ public: // Return only the info of notebook part in json. QJsonObject toConfigJsonNotebook() const; + // Need to check if this notebook has been opened. + QDateTime getCreatedTimeUtc(); + signals: void contentChanged();