Browse Source

UniversalEntry: Ctrl+S to sort items

Le Tan 7 years ago
parent
commit
0749ce178e

+ 5 - 0
src/iuniversalentry.h

@@ -74,6 +74,11 @@ public:
         Q_UNUSED(p_id);
     }
 
+    virtual void sort(int p_id)
+    {
+        Q_UNUSED(p_id);
+    }
+
     void setWidgetParent(QWidget *p_parent)
     {
         m_widgetParent = p_parent;

+ 9 - 0
src/vdoublerowitemwidget.cpp

@@ -35,3 +35,12 @@ void VDoubleRowItemWidget::setText(const QString &p_firstText,
         m_secondLabel->hide();
     }
 }
+
+VDoubleRowItemWidget *VDoubleRowItemWidget::cloneWidget(VDoubleRowItemWidget *p_widget,
+                                                        QWidget *p_parent)
+{
+    VDoubleRowItemWidget *widget = new VDoubleRowItemWidget(p_parent);
+    widget->setText(p_widget->m_firstLabel->text(), p_widget->m_secondLabel->text());
+
+    return widget;
+}

+ 3 - 0
src/vdoublerowitemwidget.h

@@ -13,6 +13,9 @@ public:
 
     void setText(const QString &p_firstText, const QString &p_secondText);
 
+    static VDoubleRowItemWidget *cloneWidget(VDoubleRowItemWidget *p_widget,
+                                             QWidget *p_parent = nullptr);
+
 private:
     QLabel *m_firstLabel;
     QLabel *m_secondLabel;

+ 27 - 15
src/vlistwidgetdoublerows.cpp

@@ -2,6 +2,7 @@
 
 #include <QListWidgetItem>
 #include <QScrollBar>
+#include <QDebug>
 
 #include "vdoublerowitemwidget.h"
 
@@ -10,17 +11,10 @@ VListWidgetDoubleRows::VListWidgetDoubleRows(QWidget *p_parent)
 {
 }
 
-QListWidgetItem *VListWidgetDoubleRows::addItem(const QIcon &p_icon,
-                                                const QString &p_firstRow,
-                                                const QString &p_secondRow)
-{
-    return VListWidgetDoubleRows::insertItem(count(), p_icon, p_firstRow, p_secondRow);
-}
-
-QListWidgetItem *VListWidgetDoubleRows::insertItem(int p_row,
-                                                   const QIcon &p_icon,
-                                                   const QString &p_firstRow,
-                                                   const QString &p_secondRow)
+QListWidgetItem *VListWidgetDoubleRows::insertDoubleRowsItem(int p_row,
+                                                             const QIcon &p_icon,
+                                                             const QString &p_firstRow,
+                                                             const QString &p_secondRow)
 {
     VDoubleRowItemWidget *itemWidget = new VDoubleRowItemWidget(this);
     itemWidget->setText(p_firstRow, p_secondRow);
@@ -42,8 +36,8 @@ QListWidgetItem *VListWidgetDoubleRows::insertItem(int p_row,
 
     item->setSizeHint(sz);
 
-    VListWidget::insertItem(p_row, item);
-    VListWidget::setItemWidget(item, itemWidget);
+    insertItem(p_row, item);
+    setItemWidget(item, itemWidget);
     return item;
 }
 
@@ -52,8 +46,9 @@ void VListWidgetDoubleRows::clearAll()
     // Delete the item widget for each item.
     int cnt = count();
     for (int i = 0; i < cnt; ++i) {
-        QWidget *wid = itemWidget(item(i));
-        removeItemWidget(item(i));
+        QListWidgetItem *it = item(i);
+        QWidget *wid = itemWidget(it);
+        removeItemWidget(it);
         delete wid;
     }
 
@@ -61,3 +56,20 @@ void VListWidgetDoubleRows::clearAll()
 
     setIconSize(QSize());
 }
+
+void VListWidgetDoubleRows::moveItem(int p_srcRow, int p_destRow)
+{
+    QListWidgetItem *it = item(p_srcRow);
+    QWidget *wid = itemWidget(it);
+
+    takeItem(p_srcRow);
+    insertItem(p_destRow, it);
+
+    if (wid) {
+        QWidget *newWid = VDoubleRowItemWidget::cloneWidget(static_cast<VDoubleRowItemWidget *>(wid), this);
+        removeItemWidget(it);
+        delete wid;
+
+        setItemWidget(it, newWid);
+    }
+}

+ 6 - 8
src/vlistwidgetdoublerows.h

@@ -14,14 +14,12 @@ class VListWidgetDoubleRows : public VListWidget
 public:
     explicit VListWidgetDoubleRows(QWidget *p_parent = nullptr);
 
-    QListWidgetItem *addItem(const QIcon &p_icon,
-                             const QString &p_firstRow,
-                             const QString &p_secondRow);
-
-    QListWidgetItem *insertItem(int p_row,
-                                const QIcon &p_icon,
-                                const QString &p_firstRow,
-                                const QString &p_secondRow);
+    QListWidgetItem *insertDoubleRowsItem(int p_row,
+                                          const QIcon &p_icon,
+                                          const QString &p_firstRow,
+                                          const QString &p_secondRow);
+
+    void moveItem(int p_srcRow, int p_destRow);
 
     void clearAll() Q_DECL_OVERRIDE;
 };

+ 50 - 1
src/vsearchue.cpp

@@ -642,7 +642,7 @@ void VSearchUE::appendItemToList(const QSharedPointer<VSearchResultItem> &p_item
         break;
     }
 
-    QListWidgetItem *item = m_listWidget->insertItem(row, *icon, first, second);
+    QListWidgetItem *item = m_listWidget->insertDoubleRowsItem(row, *icon, first, second);
     item->setData(Qt::UserRole, m_data.size() - 1);
     item->setToolTip(p_item->m_path);
 
@@ -932,3 +932,52 @@ void VSearchUE::toggleItemExpanded(int p_id)
         break;
     }
 }
+
+void VSearchUE::sort(int p_id)
+{
+    static bool noteFirst = false;
+
+    switch (p_id) {
+    case ID::Name_Notebook_AllNotebook:
+    case ID::Name_FolderNote_AllNotebook:
+    case ID::Name_FolderNote_CurrentNotebook:
+    case ID::Name_FolderNote_CurrentFolder:
+    case ID::Name_Note_Buffer:
+    case ID::Path_FolderNote_AllNotebook:
+    case ID::Path_FolderNote_CurrentNotebook:
+    {
+        int cnt = m_listWidget->count();
+        if (noteFirst) {
+            int idx = cnt - 1;
+            while (true) {
+                if (itemResultData(m_listWidget->item(idx))->m_type != VSearchResultItem::Note) {
+                    // Move it to the first row.
+                    m_listWidget->moveItem(idx, 0);
+                } else {
+                    break;
+                }
+            }
+        } else {
+            int idx = 0;
+            while (true) {
+                if (itemResultData(m_listWidget->item(idx))->m_type != VSearchResultItem::Note) {
+                    // Move it to the last row.
+                    m_listWidget->moveItem(idx, cnt - 1);
+                } else {
+                    break;
+                }
+            }
+        }
+
+        if (cnt) {
+            m_listWidget->setCurrentRow(0);
+        }
+
+        noteFirst = !noteFirst;
+        break;
+    }
+
+    default:
+        break;
+    }
+}

+ 2 - 0
src/vsearchue.h

@@ -77,6 +77,8 @@ public:
 
     void toggleItemExpanded(int p_id) Q_DECL_OVERRIDE;
 
+    void sort(int p_id) Q_DECL_OVERRIDE;
+
 protected:
     void init() Q_DECL_OVERRIDE;
 

+ 11 - 0
src/vuniversalentry.cpp

@@ -353,6 +353,17 @@ void VUniversalEntry::keyPressEvent(QKeyEvent *p_event)
             return;
         }
 
+    case Qt::Key_S:
+        if (VUtils::isControlModifierForVim(modifiers)) {
+            // Ctrl+S to sort the items.
+            // UE could just alternate among all the sort modes.
+            if (m_lastEntry) {
+                m_lastEntry->m_entry->sort(m_lastEntry->m_id);
+            }
+
+            return;
+        }
+
         break;
 
     default: