From 0749ce178ec57d18bd3c8b06aa44371d9491b9e1 Mon Sep 17 00:00:00 2001 From: Le Tan Date: Sun, 1 Apr 2018 12:33:02 +0800 Subject: [PATCH] UniversalEntry: Ctrl+S to sort items --- src/iuniversalentry.h | 5 ++++ src/vdoublerowitemwidget.cpp | 9 +++++++ src/vdoublerowitemwidget.h | 3 +++ src/vlistwidgetdoublerows.cpp | 42 ++++++++++++++++++----------- src/vlistwidgetdoublerows.h | 12 ++++----- src/vsearchue.cpp | 51 ++++++++++++++++++++++++++++++++++- src/vsearchue.h | 2 ++ src/vuniversalentry.cpp | 11 ++++++++ 8 files changed, 112 insertions(+), 23 deletions(-) diff --git a/src/iuniversalentry.h b/src/iuniversalentry.h index 9798974d..40f55c95 100644 --- a/src/iuniversalentry.h +++ b/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; diff --git a/src/vdoublerowitemwidget.cpp b/src/vdoublerowitemwidget.cpp index 3ae9ad4b..058223d0 100644 --- a/src/vdoublerowitemwidget.cpp +++ b/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; +} diff --git a/src/vdoublerowitemwidget.h b/src/vdoublerowitemwidget.h index 5bdad563..7136cd19 100644 --- a/src/vdoublerowitemwidget.h +++ b/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; diff --git a/src/vlistwidgetdoublerows.cpp b/src/vlistwidgetdoublerows.cpp index 2cc860b7..aef79141 100644 --- a/src/vlistwidgetdoublerows.cpp +++ b/src/vlistwidgetdoublerows.cpp @@ -2,6 +2,7 @@ #include #include +#include #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(wid), this); + removeItemWidget(it); + delete wid; + + setItemWidget(it, newWid); + } +} diff --git a/src/vlistwidgetdoublerows.h b/src/vlistwidgetdoublerows.h index b0575ce8..02faab7e 100644 --- a/src/vlistwidgetdoublerows.h +++ b/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 *insertDoubleRowsItem(int p_row, + 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); + void moveItem(int p_srcRow, int p_destRow); void clearAll() Q_DECL_OVERRIDE; }; diff --git a/src/vsearchue.cpp b/src/vsearchue.cpp index 1e724b8b..d6aa5f29 100644 --- a/src/vsearchue.cpp +++ b/src/vsearchue.cpp @@ -642,7 +642,7 @@ void VSearchUE::appendItemToList(const QSharedPointer &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; + } +} diff --git a/src/vsearchue.h b/src/vsearchue.h index 3c5d113e..93dd8ee6 100644 --- a/src/vsearchue.h +++ b/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; diff --git a/src/vuniversalentry.cpp b/src/vuniversalentry.cpp index 5c27ce7e..f0e6049c 100644 --- a/src/vuniversalentry.cpp +++ b/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: