From 1488fbd9221f3c7fd829fa0bc2d89f263f0473d7 Mon Sep 17 00:00:00 2001 From: HerrKermet Date: Sat, 14 Oct 2023 15:54:50 +0200 Subject: [PATCH] Added SearchProxyModel to TableView inside module_dialog searchbar will now emit a startSearch signal if input is empty Dialog for column selection is now only shown if there are columns to choose from --- .../gui/module_dialog/module_select_model.h | 14 +++++--- .../gui/src/module_dialog/module_dialog.cpp | 18 +++++++++- .../src/module_dialog/module_select_model.cpp | 34 ++++++++++++++++++- plugins/gui/src/searchbar/searchbar.cpp | 18 +++------- .../src/searchbar/searchoptions_dialog.cpp | 6 +++- 5 files changed, 69 insertions(+), 21 deletions(-) diff --git a/plugins/gui/include/gui/module_dialog/module_select_model.h b/plugins/gui/include/gui/module_dialog/module_select_model.h index 1a68a843e70..e09ab2aa614 100644 --- a/plugins/gui/include/gui/module_dialog/module_select_model.h +++ b/plugins/gui/include/gui/module_dialog/module_select_model.h @@ -25,14 +25,16 @@ #pragma once -#include "hal_core/defines.h" #include "gui/gui_utils/sort.h" +#include "hal_core/defines.h" + #include -#include -#include #include -#include #include +#include +#include +#include +#include namespace hal { @@ -120,7 +122,7 @@ namespace hal { /** * @brief The ModuleSelectProxy class allows sorting and filtering of module tables */ - class ModuleSelectProxy : public QSortFilterProxyModel + class ModuleSelectProxy : public SearchProxyModel { Q_OBJECT @@ -130,10 +132,12 @@ namespace hal { public Q_SLOTS: void setSortMechanism(gui_utility::mSortMechanism sortMechanism); void searchTextChanged(const QString& txt); + void startSearch(QString text, int options) override; protected: static bool lessThan(const QColor& a, const QColor& b); bool lessThan(const QModelIndex &sourceLeft, const QModelIndex &sourceRight) const override; + bool filterAcceptsRow(int source_row, const QModelIndex& source_parent) const override; private: gui_utility::mSortMechanism mSortMechanism; diff --git a/plugins/gui/src/module_dialog/module_dialog.cpp b/plugins/gui/src/module_dialog/module_dialog.cpp index 3cc9ea7eabe..64a1112817b 100644 --- a/plugins/gui/src/module_dialog/module_dialog.cpp +++ b/plugins/gui/src/module_dialog/module_dialog.cpp @@ -85,6 +85,10 @@ namespace hal { mTreeView->setModel(mModuleTreeProxyModel); mTreeView->expandAll(); + mModuleTableProxyModel = new ModuleSelectProxy(this), + mModuleTableProxyModel->setSourceModel(mTableView->model()); + mTableView->setModel(mModuleTableProxyModel); + mButtonBox = new QDialogButtonBox(QDialogButtonBox::Ok|QDialogButtonBox::Cancel, this); layout->addWidget(mButtonBox, 3, 0, 1, 3, Qt::AlignHCenter); @@ -92,10 +96,16 @@ namespace hal { mToggleSearchbar->setShortcut(QKeySequence(ContentManager::sSettingSearch->value().toString())); addAction(mToggleSearchbar); - mTabWidget->setCurrentIndex(1); + mTabWidget->setCurrentIndex(0); enableButtons(); mSearchbar->hide(); + //get column names for searchbar + if(mTabWidget->currentWidget() == mTreeView) + mSearchbar->setColumnNames(mModuleTreeProxyModel->getColumnNames()); + else + mSearchbar->setColumnNames(mModuleTableProxyModel->getColumnNames()); + connect(mTabWidget, &QTabWidget::currentChanged, this, &ModuleDialog::handleCurrentTabChanged); connect(mToggleSearchbar, &QAction::triggered, this, &ModuleDialog::handleToggleSearchbar); connect(mSearchbar, &Searchbar::textEdited, this, &ModuleDialog::filter); @@ -106,6 +116,7 @@ namespace hal { connect(mTreeView->selectionModel(), &QItemSelectionModel::selectionChanged, this, &ModuleDialog::handleTreeSelectionChanged); connect(mSearchbar, &Searchbar::triggerNewSearch, mModuleTreeProxyModel, &ModuleProxyModel::startSearch); + connect(mSearchbar, &Searchbar::triggerNewSearch, mModuleTableProxyModel, &ModuleSelectProxy::startSearch); } void ModuleDialog::enableButtons() @@ -220,6 +231,11 @@ namespace hal { void ModuleDialog::handleCurrentTabChanged(int index) { Q_UNUSED(index); + //set columnNames for searchbar + if(mTabWidget->currentWidget() == mTreeView) + mSearchbar->setColumnNames(mModuleTreeProxyModel->getColumnNames()); + else + mSearchbar->setColumnNames(mModuleTableProxyModel->getColumnNames()); mTreeView->clearSelection(); mTableView->clearSelection(); mSearchbar->clear(); diff --git a/plugins/gui/src/module_dialog/module_select_model.cpp b/plugins/gui/src/module_dialog/module_select_model.cpp index 480ee75beab..c6204b5e28d 100644 --- a/plugins/gui/src/module_dialog/module_select_model.cpp +++ b/plugins/gui/src/module_dialog/module_select_model.cpp @@ -158,7 +158,7 @@ namespace hal } //---------------- PROXY ------------------------------------------ - ModuleSelectProxy::ModuleSelectProxy(QObject* parent) : QSortFilterProxyModel(parent), mSortMechanism(gui_utility::mSortMechanism::numerated) + ModuleSelectProxy::ModuleSelectProxy(QObject* parent) : SearchProxyModel(parent), mSortMechanism(gui_utility::mSortMechanism::numerated) { ; } @@ -200,6 +200,38 @@ namespace hal setFilterKeyColumn(-1); setFilterRegularExpression(txt); } + void ModuleSelectProxy::startSearch(QString text, int options) + { + mSearchString = text; + mSearchOptions = SearchOptions(options); + invalidateFilter(); + } + + bool ModuleSelectProxy::filterAcceptsRow(int source_row, const QModelIndex& source_parent) const + { + QList columns = mSearchOptions.getColumns(); + if(columns.empty()){ + //iterate over each column + for(int index = 1; index < 4; index++){ + QString entry = sourceModel()->index(source_row, index, source_parent).data().toString(); + if(isMatching(mSearchString, entry)) + { + return true; + } + } + return false; + }else + { + for(int index : columns) + { + //offset the index by one to account for missing color column + QString entry = sourceModel()->index(source_row, index + 1, source_parent).data().toString(); + if(isMatching(mSearchString, entry)) + return true; + } + return false; + } + } //---------------- EXCLUDE ---------------------------------------- ModuleSelectExclude::ModuleSelectExclude() diff --git a/plugins/gui/src/searchbar/searchbar.cpp b/plugins/gui/src/searchbar/searchbar.cpp index 9da014e36d3..6709352f109 100644 --- a/plugins/gui/src/searchbar/searchbar.cpp +++ b/plugins/gui/src/searchbar/searchbar.cpp @@ -205,7 +205,11 @@ namespace hal void Searchbar::handleTextEdited() { repolish(); - if(mIncrementalSearch && mLineEdit->text().length() >= mMinCharsToStartIncSearch) + //if the line is empty then start a search with the given filter + if(mLineEdit->text().isEmpty()){ + Q_EMIT triggerNewSearch(mLineEdit->text(), mCurrentOptions->toInt()); + } + else if(mIncrementalSearch && mLineEdit->text().length() >= mMinCharsToStartIncSearch) { Q_EMIT triggerNewSearch(mLineEdit->text(), mCurrentOptions->toInt()); } @@ -221,18 +225,6 @@ namespace hal clear(); } - /* - bool Searchbar::exactMatchChecked() - { - return mCurrentOptions->toInt()&&1; - } - - bool Searchbar::caseSensitiveChecked() - { - return mCurrentOptions->toInt()&&2; - } -*/ - void Searchbar::setEmitTextWithFlags(bool emitTextWithFlags) { mEmitTextWithFlags = emitTextWithFlags; diff --git a/plugins/gui/src/searchbar/searchoptions_dialog.cpp b/plugins/gui/src/searchbar/searchoptions_dialog.cpp index 88f2d072c3c..65325360c3e 100644 --- a/plugins/gui/src/searchbar/searchoptions_dialog.cpp +++ b/plugins/gui/src/searchbar/searchoptions_dialog.cpp @@ -118,8 +118,12 @@ namespace hal { for(QString name : columnNames) mColumnNames.append(name); + }else{ + mSearchInLabel->hide(); + mSelectColumnsBtn->hide(); } - mSelectColumnsBtn->setText(formatColumnButtonText(buildColumnButtonText())); + if(!columnNames.isEmpty()) + mSelectColumnsBtn->setText(formatColumnButtonText(buildColumnButtonText())); //TODO resize button after it updates its text }