From 3d188fa648b3feb091b0adcfa49c36037de5f9a2 Mon Sep 17 00:00:00 2001 From: Le Tan Date: Thu, 6 Sep 2018 21:08:46 +0800 Subject: [PATCH] add config insert_new_note_in_front --- src/resources/vnote.ini | 3 +++ src/vconfigmanager.cpp | 3 +++ src/vconfigmanager.h | 21 +++++++++++++++++++++ src/vdirectory.cpp | 14 +++++++++++--- src/vdirectory.h | 2 +- src/vfilelist.cpp | 4 +++- src/vnotefile.cpp | 5 +++-- src/vnotefile.h | 1 + 8 files changed, 46 insertions(+), 7 deletions(-) diff --git a/src/resources/vnote.ini b/src/resources/vnote.ini index 771d92fc..e8de370f 100644 --- a/src/resources/vnote.ini +++ b/src/resources/vnote.ini @@ -255,6 +255,9 @@ smart_live_preview=3 ; Support multiple keyboard layout multiple_keyboard_layout=true +; Whether insert new note in front +insert_new_note_in_front=false + [editor] ; Auto indent as previous line auto_indent=true diff --git a/src/vconfigmanager.cpp b/src/vconfigmanager.cpp index 07e964ef..3aa0cf04 100644 --- a/src/vconfigmanager.cpp +++ b/src/vconfigmanager.cpp @@ -317,6 +317,9 @@ void VConfigManager::initialize() m_multipleKeyboardLayout = getConfigFromSettings("global", "multiple_keyboard_layout").toBool(); + m_insertNewNoteInFront = getConfigFromSettings("global", + "insert_new_note_in_front").toBool(); + initEditorConfigs(); } diff --git a/src/vconfigmanager.h b/src/vconfigmanager.h index 0422a361..705f662d 100644 --- a/src/vconfigmanager.h +++ b/src/vconfigmanager.h @@ -558,6 +558,9 @@ public: bool getMultipleKeyboardLayout() const; + bool getInsertNewNoteInFront() const; + void setInsertNewNoteInFront(bool p_enabled); + private: // Look up a config from user and default settings. QVariant getConfigFromSettings(const QString §ion, const QString &key) const; @@ -1001,6 +1004,9 @@ private: // Support multiple keyboard layout. bool m_multipleKeyboardLayout; + // Whether insert new note in front. + bool m_insertNewNoteInFront; + // The name of the config file in each directory. static const QString c_dirConfigFile; @@ -2576,4 +2582,19 @@ inline bool VConfigManager::getMultipleKeyboardLayout() const { return m_multipleKeyboardLayout; } + +inline bool VConfigManager::getInsertNewNoteInFront() const +{ + return m_insertNewNoteInFront; +} + +inline void VConfigManager::setInsertNewNoteInFront(bool p_enabled) +{ + if (m_insertNewNoteInFront == p_enabled) { + return; + } + + m_insertNewNoteInFront = p_enabled; + setConfigToSettings("global", "insert_new_note_in_front", m_insertNewNoteInFront); +} #endif // VCONFIGMANAGER_H diff --git a/src/vdirectory.cpp b/src/vdirectory.cpp index 1f52c468..b5104fc4 100644 --- a/src/vdirectory.cpp +++ b/src/vdirectory.cpp @@ -289,7 +289,7 @@ bool VDirectory::containsFile(const VFile *p_file) const return false; } -VNoteFile *VDirectory::createFile(const QString &p_name) +VNoteFile *VDirectory::createFile(const QString &p_name, bool p_front) { Q_ASSERT(!p_name.isEmpty()); if (!open()) { @@ -312,11 +312,19 @@ VNoteFile *VDirectory::createFile(const QString &p_name) true, dateTime, dateTime); - m_files.append(ret); + int idx = -1; + if (p_front) { + m_files.prepend(ret); + idx = 0; + } else { + m_files.append(ret); + idx = m_files.size() - 1; + } + if (!writeToConfig()) { file.remove(); delete ret; - m_files.removeLast(); + m_files.remove(idx); return NULL; } diff --git a/src/vdirectory.h b/src/vdirectory.h index adec155d..13f72a85 100644 --- a/src/vdirectory.h +++ b/src/vdirectory.h @@ -37,7 +37,7 @@ public: // If current dir or its sub-dir contains @p_file. bool containsFile(const VFile *p_file) const; - VNoteFile *createFile(const QString &p_name); + VNoteFile *createFile(const QString &p_name, bool p_front); // Remove the file in the config and m_files without deleting it in the disk. // It won't change the parent of @p_file to enable it find its path. diff --git a/src/vfilelist.cpp b/src/vfilelist.cpp index a5d0644f..7add885f 100644 --- a/src/vfilelist.cpp +++ b/src/vfilelist.cpp @@ -349,7 +349,8 @@ void VFileList::newFile() true); VNewFileDialog dialog(tr("Create Note"), info, defaultName, m_directory, this); if (dialog.exec() == QDialog::Accepted) { - VNoteFile *file = m_directory->createFile(dialog.getNameInput()); + VNoteFile *file = m_directory->createFile(dialog.getNameInput(), + g_config->getInsertNewNoteInFront()); if (!file) { VUtils::showMessage(QMessageBox::Warning, tr("Warning"), tr("Fail to create note %2.") @@ -998,6 +999,7 @@ void VFileList::pasteFiles(VDirectory *p_destDir, fileName, file, p_isCut, + g_config->getInsertNewNoteInFront() ? 0 : -1, &destFile, &msg); if (!ret) { diff --git a/src/vnotefile.cpp b/src/vnotefile.cpp index e8b7c4c2..355f7a24 100644 --- a/src/vnotefile.cpp +++ b/src/vnotefile.cpp @@ -453,6 +453,7 @@ bool VNoteFile::copyFile(VDirectory *p_destDir, const QString &p_destName, VNoteFile *p_file, bool p_isCut, + int p_idx, VNoteFile **p_targetFile, QString *p_errMsg) { @@ -506,13 +507,13 @@ bool VNoteFile::copyFile(VDirectory *p_destDir, if (p_isCut) { srcDir->removeFile(p_file); p_file->setName(p_destName); - if (p_destDir->addFile(p_file, -1)) { + if (p_destDir->addFile(p_file, p_idx)) { destFile = p_file; } else { destFile = NULL; } } else { - destFile = p_destDir->addFile(p_destName, -1); + destFile = p_destDir->addFile(p_destName, p_idx); // Copy tags to this file. if (destFile) { const QStringList &tags = p_file->getTags(); diff --git a/src/vnotefile.h b/src/vnotefile.h index b95ef855..6e0b3076 100644 --- a/src/vnotefile.h +++ b/src/vnotefile.h @@ -135,6 +135,7 @@ public: const QString &p_destName, VNoteFile *p_file, bool p_isCut, + int p_idx, VNoteFile **p_targetFile, QString *p_errMsg = NULL);