Skip to content

Commit

Permalink
Merge branch 'feature/searchBar' of github.com:emsec/hal into feature…
Browse files Browse the repository at this point in the history
…/searchBar
  • Loading branch information
neoneela committed Oct 14, 2023
2 parents 2d74494 + 1488fbd commit eee3798
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 21 deletions.
14 changes: 9 additions & 5 deletions plugins/gui/include/gui/module_dialog/module_select_model.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,16 @@

#pragma once

#include "hal_core/defines.h"
#include "gui/gui_utils/sort.h"
#include "hal_core/defines.h"

#include <QAbstractTableModel>
#include <QSortFilterProxyModel>
#include <QTableView>
#include <QColor>
#include <QList>
#include <QDebug>
#include <QList>
#include <QSortFilterProxyModel>
#include <QTableView>
#include <gui/searchbar/search_proxy_model.h>

namespace hal {

Expand Down Expand Up @@ -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

Expand All @@ -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;
Expand Down
18 changes: 17 additions & 1 deletion plugins/gui/src/module_dialog/module_dialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -93,10 +97,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);
Expand All @@ -107,6 +117,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()
Expand Down Expand Up @@ -221,6 +232,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();
Expand Down
34 changes: 33 additions & 1 deletion plugins/gui/src/module_dialog/module_select_model.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
;
}
Expand Down Expand Up @@ -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<int> 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()
Expand Down
18 changes: 5 additions & 13 deletions plugins/gui/src/searchbar/searchbar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
Expand All @@ -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;
Expand Down
6 changes: 5 additions & 1 deletion plugins/gui/src/searchbar/searchoptions_dialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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

}
Expand Down

0 comments on commit eee3798

Please sign in to comment.