summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorArno <arno@disconnect.de>2018-02-02 18:13:27 +0100
committerArno <arno@disconnect.de>2018-02-02 18:13:27 +0100
commit2f1e930668c1a2205a09ee835908d28b2bdedcdd (patch)
tree7f9331fd80e45e27994a0a5ebc2e61e4dc69be23
parent3a4b40917ba45e3dd34e14816d12ca8a6bd510f7 (diff)
downloadShemovCleaner-2f1e930668c1a2205a09ee835908d28b2bdedcdd.tar.gz
ShemovCleaner-2f1e930668c1a2205a09ee835908d28b2bdedcdd.tar.bz2
ShemovCleaner-2f1e930668c1a2205a09ee835908d28b2bdedcdd.zip
Implement context menu for search results
Subclass QTreeView to show a custom context menu. Since the depth of some items is > 1, add an option to expand and collapse a node recursively.
-rw-r--r--ShemovCleaner.pro6
-rw-r--r--searchdialog.cpp5
-rw-r--r--searchdialog.h5
-rw-r--r--searchview.cpp53
-rw-r--r--searchview.h28
5 files changed, 91 insertions, 6 deletions
diff --git a/ShemovCleaner.pro b/ShemovCleaner.pro
index e03b164..8f59657 100644
--- a/ShemovCleaner.pro
+++ b/ShemovCleaner.pro
@@ -37,7 +37,8 @@ HEADERS += \
viewer.h \
cachedfiledata.h \
itemselectionwidget.h \
- searchdialog.h
+ searchdialog.h \
+ searchview.h
SOURCES += \
configurationwidget.cpp \
filecopier.cpp \
@@ -56,5 +57,6 @@ SOURCES += \
viewer.cpp \
cachedfiledata.cpp \
itemselectionwidget.cpp \
- searchdialog.cpp
+ searchdialog.cpp \
+ searchview.cpp
RESOURCES += shemovcleaner.qrc
diff --git a/searchdialog.cpp b/searchdialog.cpp
index 1f59839..8c255c8 100644
--- a/searchdialog.cpp
+++ b/searchdialog.cpp
@@ -15,6 +15,7 @@
#include <QApplication>
#include "searchdialog.h"
+#include "searchview.h"
#include "helper.h"
SearchDialog::SearchDialog(QWidget *parent, Qt::WindowFlags f) : QDialog(parent, f){
@@ -39,7 +40,7 @@ SearchDialog::SearchDialog(QWidget *parent, Qt::WindowFlags f) : QDialog(parent,
mResM = new QStandardItemModel;
QSortFilterProxyModel *resMProxy = new QSortFilterProxyModel;
resMProxy->setSourceModel(mResM);
- mResV = new QTreeView;
+ mResV = new SearchView;
mResV->setModel(resMProxy);
connect(mResV->selectionModel(), &QItemSelectionModel::currentChanged, this, &SearchDialog::doResult);
QHBoxLayout *resGBL = new QHBoxLayout;
@@ -49,7 +50,7 @@ SearchDialog::SearchDialog(QWidget *parent, Qt::WindowFlags f) : QDialog(parent,
mDataM = new QStandardItemModel;
QSortFilterProxyModel *dataMProxy = new QSortFilterProxyModel;
dataMProxy->setSourceModel(mDataM);
- mDataV = new QTreeView;
+ mDataV = new SearchView;
mDataV->setModel(dataMProxy);
QHBoxLayout *dataGBL = new QHBoxLayout;
dataGBL->addWidget(mDataV);
diff --git a/searchdialog.h b/searchdialog.h
index 0ef4aad..b5b9ba4 100644
--- a/searchdialog.h
+++ b/searchdialog.h
@@ -8,6 +8,7 @@ class QComboBox;
class QTreeView;
class QStandardItemModel;
class QStandardItem;
+class SearchView;
class SearchDialog : public QDialog {
Q_OBJECT
@@ -37,8 +38,8 @@ class SearchDialog : public QDialog {
void doActorGenres(QStandardItem *item);
QLineEdit *mSearch;
QComboBox *mTypeSel;
- QTreeView *mResV;
- QTreeView *mDataV;
+ SearchView *mResV;
+ SearchView *mDataV;
QStandardItemModel *mResM;
QStandardItemModel *mDataM;
};
diff --git a/searchview.cpp b/searchview.cpp
new file mode 100644
index 0000000..a4ba25b
--- /dev/null
+++ b/searchview.cpp
@@ -0,0 +1,53 @@
+#include <QMenu>
+#include <QAction>
+#include <QContextMenuEvent>
+
+#include "searchview.h"
+#include "helper.h"
+
+SearchView::SearchView(QWidget *parent) : QTreeView(parent){
+ mCtxMenu = new QMenu(this);
+ QAction *expandCurA = new QAction(Helper::icon(QColor(255,85,255), QChar(0x21B3)), tr("Expand"), this);
+ connect(expandCurA, &QAction::triggered, this, &SearchView::expandNode);
+ mCtxMenu->addAction(expandCurA);
+ QAction *collapseCurA = new QAction(Helper::icon(QColor(255,85,255), QChar(0x21B0)), tr("Collapse"), this);
+ connect(collapseCurA, &QAction::triggered, this, &SearchView::collapseNode);
+ mCtxMenu->addAction(collapseCurA);
+ mCtxMenu->addSeparator();
+ QAction *expandAllA = new QAction(Helper::icon(QColor(255,85,255), QChar(0x2198)), tr("Expand all"), this);
+ connect(expandAllA, &QAction::triggered, this, &SearchView::expandAll);
+ mCtxMenu->addAction(expandAllA);
+ QAction *collapseAllA = new QAction(Helper::icon(QColor(255,85,255), QChar(0x2196)), tr("Collapse all"), this);
+ connect(collapseAllA, &QAction::triggered, this, &SearchView::collapseAll);
+ mCtxMenu->addAction(collapseAllA);
+}
+
+void SearchView::expandNode(){
+ QModelIndex idx = currentIndex();
+ expand(idx);
+ doNodeR(idx, Expand);
+}
+
+void SearchView::collapseNode(){
+ QModelIndex idx = currentIndex();
+ collapse(idx);
+ doNodeR(idx, Collapse);
+}
+
+void SearchView::contextMenuEvent(QContextMenuEvent *e){
+ mCtxMenu->exec(e->globalPos());
+}
+
+void SearchView::doNodeR(const QModelIndex &parent, int nAction){
+ for(int i = 0; i < model()->rowCount(parent); ++i){
+ QModelIndex cur = model()->index(i, 0, parent);
+ if(nAction == Expand){
+ expand(cur);
+ }else if(nAction == Collapse){
+ collapse(cur);
+ }
+ if(model()->hasChildren(cur)){
+ doNodeR(cur, nAction);
+ }
+ }
+}
diff --git a/searchview.h b/searchview.h
new file mode 100644
index 0000000..33cd02c
--- /dev/null
+++ b/searchview.h
@@ -0,0 +1,28 @@
+#ifndef SEARCHVIEW_H
+#define SEARCHVIEW_H
+
+#include <QTreeView>
+
+class QMenu;
+class QContextMenuEvent;
+
+class SearchView : public QTreeView {
+ Q_OBJECT
+ public:
+ enum NodeAction { Expand, Collapse };
+ explicit SearchView(QWidget *parent = 0);
+
+ public slots:
+ void expandNode();
+ void collapseNode();
+
+ protected:
+ virtual void contextMenuEvent(QContextMenuEvent *e);
+
+ private:
+ void doNodeR(const QModelIndex &parent, int nAction);
+ QMenu *mCtxMenu;
+
+};
+
+#endif // SEARCHVIEW_H