Browse Source

add config insert_new_note_in_front

Le Tan 7 years ago
parent
commit
3d188fa648
8 changed files with 46 additions and 7 deletions
  1. 3 0
      src/resources/vnote.ini
  2. 3 0
      src/vconfigmanager.cpp
  3. 21 0
      src/vconfigmanager.h
  4. 11 3
      src/vdirectory.cpp
  5. 1 1
      src/vdirectory.h
  6. 3 1
      src/vfilelist.cpp
  7. 3 2
      src/vnotefile.cpp
  8. 1 0
      src/vnotefile.h

+ 3 - 0
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

+ 3 - 0
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();
 }
 

+ 21 - 0
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 &section, 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

+ 11 - 3
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;
     }
 

+ 1 - 1
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.

+ 3 - 1
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 <span style=\"%1\">%2</span>.")
@@ -998,6 +999,7 @@ void VFileList::pasteFiles(VDirectory *p_destDir,
                                        fileName,
                                        file,
                                        p_isCut,
+                                       g_config->getInsertNewNoteInFront() ? 0 : -1,
                                        &destFile,
                                        &msg);
         if (!ret) {

+ 3 - 2
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();

+ 1 - 0
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);