support created_time of notebook and folder

This commit is contained in:
Le Tan 2017-09-14 20:27:21 +08:00
parent 5c038e1f76
commit 4626673925
9 changed files with 100 additions and 38 deletions

View File

@ -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);

View File

@ -27,8 +27,6 @@ private slots:
private:
void setupUI();
QLabel *infoLabel;
QLabel *nameLabel;
QLineEdit *nameEdit;
QLabel *m_warnLabel;
QDialogButtonBox *m_btnBox;

View File

@ -11,7 +11,7 @@ VNotebookInfoDialog::VNotebookInfoDialog(const QString &p_title,
const VNotebook *p_notebook,
const QVector<VNotebook *> &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<VNotebook *>(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);

View File

@ -38,7 +38,6 @@ private:
const VNotebook *m_notebook;
QLabel *m_infoLabel;
QLineEdit *m_nameEdit;
QLineEdit *m_pathEdit;
QLineEdit *m_imageFolderEdit;

View File

@ -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;
}

View File

@ -6,6 +6,7 @@
#include <QVector>
#include <QPointer>
#include <QJsonObject>
#include <QDateTime>
#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<VNotebook> m_notebook;
// Name of this folder.
QString m_name;
// Owner of the sub-directories
QVector<VDirectory *> m_subDirs;
// Owner of the files
QVector<VFile *> 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 *> &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

View File

@ -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,

View File

@ -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();
}

View File

@ -3,6 +3,7 @@
#include <QObject>
#include <QString>
#include <QDateTime>
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();