From 3afe6efd51edaa02a989418c825182f41b7c8ab6 Mon Sep 17 00:00:00 2001 From: Arno Date: Mon, 2 Apr 2018 21:36:19 +0200 Subject: FSWidget: Implement select and deselect files Select files by CTRL+k, deselect everything with CTRL+k (for kill). Select all with the global shortcut CTRL+a. Learned some interesting things: First, QAction shortcuts won't work if you don't add them to any actions(), even if it has a parent. So subclass QTreeView for the context menu and add an InvisibleAction enum. Don't show the QAction if it has that flag in data(). This has a nice side effect: the context menu isn't shown any more when you right-click outside the FSView. Second: The debugger was quite confused with equal SLOT names in different classes when using the new connect syntax. It dropped me off in totally wrong classes, confusing me at first, too :( --- fsview.cpp | 16 ++++++++++++++++ fsview.h | 16 ++++++++++++++++ fswidget.cpp | 36 ++++++++++++++++++++++++++++-------- fswidget.h | 7 +++---- shemov.pro | 6 ++++-- 5 files changed, 67 insertions(+), 14 deletions(-) create mode 100644 fsview.cpp create mode 100644 fsview.h diff --git a/fsview.cpp b/fsview.cpp new file mode 100644 index 0000000..6b0b7c3 --- /dev/null +++ b/fsview.cpp @@ -0,0 +1,16 @@ +#include +#include + +#include "fsview.h" + +FSView::FSView(QWidget *parent) : QTreeView(parent) {} + +void FSView::contextMenuEvent(QContextMenuEvent *e){ + QMenu contextMenu(this); + for(QAction *a : actions()){ + if(a->data() != InvisibleAction){ + contextMenu.addAction(a); + } + } + contextMenu.exec(e->globalPos()); +} diff --git a/fsview.h b/fsview.h new file mode 100644 index 0000000..380ae5f --- /dev/null +++ b/fsview.h @@ -0,0 +1,16 @@ +#ifndef FSVIEW_H +#define FSVIEW_H + +#include + +class FSView : public QTreeView { + Q_OBJECT + public: + enum { InvisibleAction }; + explicit FSView(QWidget *parent = nullptr); + + protected: + virtual void contextMenuEvent(QContextMenuEvent *e); +}; + +#endif // FSVIEW_H diff --git a/fswidget.cpp b/fswidget.cpp index 53df90f..8624902 100644 --- a/fswidget.cpp +++ b/fswidget.cpp @@ -24,6 +24,7 @@ #include "newmoviewizard.h" #include "newpicsdialog.h" #include "fsproxy.h" +#include "fsview.h" #include "viewer.h" FSWidget::FSWidget(QWidget *parent) : QWidget(parent) { @@ -82,6 +83,14 @@ void FSWidget::setupWidget(){ } QAction *playRepeatMA = new QAction(tr("Play repeat"), this); playRepeatMA->setMenu(repeatMenu); + QAction *selectFilterA = new QAction(tr("Select by filter..."), this); + connect(selectFilterA, &QAction::triggered, this, &FSWidget::selectFilter); + selectFilterA->setShortcut(tr("CTRL+j")); + selectFilterA->setData(FSView::InvisibleAction); + QAction *unselectAllA = new QAction(tr("unselect all..."), this); + connect(unselectAllA, &QAction::triggered, [=] { mFileView->selectionModel()->clear(); }); + unselectAllA->setShortcut(tr("CTRL+k")); + unselectAllA->setData(FSView::InvisibleAction); QIcon plusIcon = Helper::icon(QColor(255,85,255), '+'); QIcon minusIcon = Helper::icon(QColor(255,85,255), '-'); @@ -133,7 +142,7 @@ void FSWidget::setupWidget(){ topWL->addWidget(mFilterCB); topWL->addWidget(filterTB); - mFileView = new QTreeView; + mFileView = new FSView; mFileView->setAlternatingRowColors(true); mFileView->setSortingEnabled(true); mFileView->setUniformRowHeights(true); @@ -146,7 +155,7 @@ void FSWidget::setupWidget(){ mFileView->setModel(mProxy); mFileView->sortByColumn(0, Qt::AscendingOrder); - addActions(QList() << playSelectedA << playRepeatMA << Helper::createSeparator(this) << backA << forwardA << Helper::createSeparator(this) << refreshA << deleteFilesA << Helper::createSeparator(this) << archiveMovieA << archivePicsA << unpackA << previewA); + mFileView->addActions(QList() << playSelectedA << playRepeatMA << Helper::createSeparator(this) << backA << forwardA << Helper::createSeparator(this) << refreshA << deleteFilesA << Helper::createSeparator(this) << archiveMovieA << archivePicsA << unpackA << previewA << selectFilterA << unselectAllA); QVBoxLayout *mainLayout = new QVBoxLayout; mainLayout->addLayout(topWL); @@ -160,12 +169,6 @@ FSWidget::~FSWidget(){ writeSettings(); } -void FSWidget::contextMenuEvent(QContextMenuEvent *e){ - QMenu contextMenu(this); - contextMenu.addActions(actions()); - contextMenu.exec(e->globalPos()); -} - void FSWidget::readSettings(){ QSettings s; QStringList dirs = s.value("fs/dirs").toStringList(); @@ -522,3 +525,20 @@ void FSWidget::playSelected(int count){ } QProcess::startDetached(playerData.first, args); } + +void FSWidget::selectFilter(){ + bool ok; + QString retval = QInputDialog::getText(this, tr("File selection by regex!"), tr("Select"), QLineEdit::Normal, "mkv$", &ok); + if(!ok){ + return; + } + mFileView->selectionModel()->clearSelection(); + QRegExp re(retval); + for(int i = 0; i < mProxy->rowCount(); ++i){ + QModelIndex nameIdx = mProxy->index(i, 0); + if(re.indexIn(nameIdx.data().toString()) != -1){ + mFileView->selectionModel()->select(nameIdx, QItemSelectionModel::Select | QItemSelectionModel::Rows); + } + } + +} diff --git a/fswidget.h b/fswidget.h index e6760fe..252c487 100644 --- a/fswidget.h +++ b/fswidget.h @@ -13,6 +13,7 @@ class NewMovieWizard; class NewPicsDialog; class Viewer; class FSProxy; +class FSView; class FSWidget : public QWidget { Q_OBJECT @@ -21,9 +22,6 @@ class FSWidget : public QWidget { explicit FSWidget(QWidget *parent = nullptr); ~FSWidget(); - protected: - virtual void contextMenuEvent(QContextMenuEvent *e); - public slots: void readSettings(); void writeSettings(); @@ -43,6 +41,7 @@ class FSWidget : public QWidget { void doubleClicked(const QModelIndex &idx); void preview(); void playSelected(int count = 1); + void selectFilter(); signals: void message(QString msg); @@ -51,7 +50,7 @@ class FSWidget : public QWidget { void setupWidget(); QComboBox *mDirCB; QComboBox *mFilterCB; - QTreeView *mFileView; + FSView *mFileView; QStandardItemModel *mModel; FSProxy *mProxy; NewMovieWizard *mMovieWizard; diff --git a/shemov.pro b/shemov.pro index cea8c6b..8414179 100644 --- a/shemov.pro +++ b/shemov.pro @@ -50,7 +50,8 @@ SOURCES = main.cpp \ randomtab.cpp \ fswidget.cpp \ fsproxy.cpp \ - viewer.cpp + viewer.cpp \ + fsview.cpp HEADERS = \ filesystemdirproxy.h \ filesystemwidget.h \ @@ -95,7 +96,8 @@ HEADERS = \ randomtab.h \ fswidget.h \ fsproxy.h \ - viewer.h + viewer.h \ + fsview.h LIBS += -lmagic -lXfixes -lX11 -lMagick++-6.Q16HDRI INCLUDEPATH += /usr/include/ImageMagick-6/ RESOURCES = shemov.qrc -- cgit v1.2.3-70-g09d2