Browse Source

support sorting notebooks

Le Tan 6 years ago
parent
commit
b97b908e0b
3 changed files with 72 additions and 1 deletions
  1. 3 1
      src/dialog/vfileinfodialog.cpp
  2. 66 0
      src/vnotebookselector.cpp
  3. 3 0
      src/vnotebookselector.h

+ 3 - 1
src/dialog/vfileinfodialog.cpp

@@ -65,7 +65,9 @@ void VFileInfoDialog::setupUI(const QString &p_title, const QString &p_info)
 
     // Tags.
     QLineEdit *tagEdit = new QLineEdit(m_file->getTags().join(", "));
-    tagEdit->setToolTip(tr("Tags of this note separated by ,"));
+    QString tagTip = tr("Add tags to a note at the right bottom status bar when it is opened");
+    tagEdit->setPlaceholderText(tagTip);
+    tagEdit->setToolTip(tr("Tags of this note separated by , (%1)").arg(tagTip));
     tagEdit->setReadOnly(true);
 
     QFormLayout *topLayout = new QFormLayout();

+ 66 - 0
src/vnotebookselector.cpp

@@ -24,6 +24,7 @@
 #include "vmainwindow.h"
 #include "utils/vimnavigationforwidget.h"
 #include "utils/viconutils.h"
+#include "dialog/vsortdialog.h"
 
 extern VConfigManager *g_config;
 
@@ -428,6 +429,16 @@ void VNotebookSelector::popupListContextMenuRequested(QPoint p_pos)
             this, SLOT(deleteNotebook()));
     menu.addAction(deleteNotebookAct);
 
+    if (m_notebooks.size() > 1) {
+        QAction *sortAct = new QAction(VIconUtils::menuIcon(":/resources/icons/sort.svg"),
+                                       tr("&Sort"),
+                                       &menu);
+        sortAct->setToolTip(tr("Sort notebooks"));
+        connect(sortAct, SIGNAL(triggered(bool)),
+                this, SLOT(sortItems()));
+        menu.addAction(sortAct);
+    }
+
     if (nb->isValid()) {
         menu.addSeparator();
 
@@ -680,3 +691,58 @@ VNotebook *VNotebookSelector::currentNotebook() const
 {
     return getNotebook(currentIndex());
 }
+
+void VNotebookSelector::sortItems()
+{
+    if (m_notebooks.size() < 2) {
+        return;
+    }
+
+    VSortDialog dialog(tr("Sort Notebooks"),
+                       tr("Sort notebooks in the configuration file."),
+                       this);
+    QTreeWidget *tree = dialog.getTreeWidget();
+    tree->clear();
+    tree->setColumnCount(1);
+    QStringList headers;
+    headers << tr("Name");
+    tree->setHeaderLabels(headers);
+    for (int i = 0; i < m_notebooks.size(); ++i) {
+        QStringList cols;
+        cols << m_notebooks[i]->getName();
+        QTreeWidgetItem *item = new QTreeWidgetItem(tree, cols);
+        item->setData(0, Qt::UserRole, i);
+    }
+
+    dialog.treeUpdated();
+
+    if (dialog.exec()) {
+        QVector<QVariant> data = dialog.getSortedData();
+        Q_ASSERT(data.size() == m_notebooks.size());
+        QVector<int> sortedIdx(data.size(), -1);
+        for (int i = 0; i < data.size(); ++i) {
+            sortedIdx[i] = data[i].toInt();
+        }
+
+        // Sort m_notebooks.
+        auto ori = m_notebooks;
+        auto curNotebook = currentNotebook();
+        int curNotebookIdx = -1;
+        for (int i = 0; i < sortedIdx.size(); ++i) {
+            m_notebooks[i] = ori[sortedIdx[i]];
+            if (m_notebooks[i] == curNotebook) {
+                curNotebookIdx = i;
+            }
+        }
+
+        Q_ASSERT(ori.size() == m_notebooks.size());
+        Q_ASSERT(curNotebookIdx != -1);
+
+        g_config->setNotebooks(m_notebooks);
+        g_config->setCurNotebookIndex(curNotebookIdx);
+
+        update();
+
+        setCurrentItemToNotebook(m_notebooks[curNotebookIdx]);
+    }
+}

+ 3 - 0
src/vnotebookselector.h

@@ -65,6 +65,9 @@ private slots:
     // View and edit notebook information of selected notebook.
     void editNotebookInfo();
 
+    // Sort notebooks.
+    void sortItems();
+
 private:
     // Update Combox from m_notebooks.
     void updateComboBox();