diff --git a/desktop-widgets/filterwidget2.cpp b/desktop-widgets/filterwidget2.cpp index 7b26c420e..c51bef29a 100644 --- a/desktop-widgets/filterwidget2.cpp +++ b/desktop-widgets/filterwidget2.cpp @@ -27,10 +27,16 @@ FilterWidget2::FilterWidget2(QWidget* parent) : ui.loadSetButton->setPopupMode(QToolButton::InstantPopup); + ui.presetTable->setModel(FilterPresetModel::instance()); + ui.presetTable->setSelectionBehavior(QAbstractItemView::SelectRows); + ui.presetTable->setSelectionMode(QAbstractItemView::SingleSelection); + connect(ui.clear, &QToolButton::clicked, this, &FilterWidget2::clearFilter); connect(ui.close, &QToolButton::clicked, this, &FilterWidget2::closeFilter); connect(ui.fullText, &QLineEdit::textChanged, this, &FilterWidget2::updateFilter); connect(ui.fulltextStringMode, QOverload::of(&QComboBox::currentIndexChanged), this, &FilterWidget2::updateFilter); + connect(ui.presetTable, &QTableView::clicked, this, &FilterWidget2::presetClicked); + connect(ui.presetTable->selectionModel(), &QItemSelectionModel::selectionChanged, this, &FilterWidget2::presetSelected); connect(&constraintModel, &FilterConstraintModel::rowsInserted, this, &FilterWidget2::constraintAdded); connect(&constraintModel, &FilterConstraintModel::rowsRemoved, this, &FilterWidget2::constraintRemoved); @@ -65,13 +71,22 @@ void FilterWidget2::updatePresetMenu() } ui.loadSetButton->setEnabled(true); for (int i = 0; i < count; ++i) { - QModelIndex idx = model->index(i, 0); + QModelIndex idx = model->index(i, FilterPresetModel::NAME); QString name = model->data(idx, Qt::DisplayRole).value(); - loadFilterPresetMenu->addAction(name, [this,i]() { loadPreset(i); }); + loadFilterPresetMenu->addAction(name, [this,i,model]() { selectPreset(i); }); } ui.loadSetButton->setMenu(loadFilterPresetMenu.get()); } +void FilterWidget2::selectPreset(int i) +{ + QAbstractItemModel *model = FilterPresetModel::instance(); + QItemSelectionModel *selectionModel = ui.presetTable->selectionModel(); + QModelIndex idx = model->index(i, 0); + selectionModel->reset(); + selectionModel->select(idx, QItemSelectionModel::Select | QItemSelectionModel::Rows); +} + void FilterWidget2::loadPreset(int index) { FilterData filter = filter_preset_get(index); @@ -103,6 +118,25 @@ void FilterWidget2::constraintRemoved(const QModelIndex &parent, int first, int updateFilter(); } +void FilterWidget2::presetClicked(const QModelIndex &index) +{ + if (!index.isValid()) + return; + + if (index.column() == FilterPresetModel::REMOVE) + Command::removeFilterPreset(index.row()); +} + +void FilterWidget2::presetSelected(const QItemSelection &selected, const QItemSelection &) +{ + if (selected.indexes().isEmpty()) + return clearFilter(); + const QModelIndex index = selected.indexes()[0]; + if (!index.isValid()) + return clearFilter(); + loadPreset(index.row()); +} + void FilterWidget2::constraintChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight, const QVector &roles) { // Note: this may appear strange, but we don't update the widget if we get @@ -126,6 +160,7 @@ void FilterWidget2::constraintsReset() void FilterWidget2::clearFilter() { ignoreSignal = true; // Prevent signals to force filter recalculation (TODO: check if necessary) + ui.presetTable->selectionModel()->reset(); // Note: we use reset(), because that doesn't emit signals. ui.fulltextStringMode->setCurrentIndex((int)StringFilterMode::STARTSWITH); ui.fullText->clear(); ignoreSignal = false; diff --git a/desktop-widgets/filterwidget2.h b/desktop-widgets/filterwidget2.h index 55d65fab2..a727c964c 100644 --- a/desktop-widgets/filterwidget2.h +++ b/desktop-widgets/filterwidget2.h @@ -35,6 +35,8 @@ private slots: void constraintChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight, const QVector &roles); void constraintsReset(); void updatePresetMenu(); + void presetClicked(const QModelIndex &index); + void presetSelected(const QItemSelection &selected, const QItemSelection &); void on_addSetButton_clicked(); private: @@ -47,6 +49,8 @@ private: FilterData createFilterData() const; void setFilterData(const FilterData &filterData); void loadPreset(int index); + void selectPreset(int i); + void clearFilterData(); std::unique_ptr loadFilterPresetMenu; }; diff --git a/desktop-widgets/filterwidget2.ui b/desktop-widgets/filterwidget2.ui index e83e6918d..d49f21fc7 100644 --- a/desktop-widgets/filterwidget2.ui +++ b/desktop-widgets/filterwidget2.ui @@ -19,200 +19,213 @@ Form - - - - - QFrame::NoFrame + + + + + 0 - - QFrame::Plain - - - true - - - - - 0 - 0 - 486 - 487 - + + + Filter + + + QFrame::NoFrame - - - - - - - - true - - - - Filter - - - - - - - - - - 0 - 0 - - - - Add constraint - - - - - - - Qt::Horizontal - - - - - - - - 0 - 0 - - - - Save set - - - - - - - - 0 - 0 - - - - Load set - - - - - - - Qt::Horizontal - - - - - - - - 0 - 0 - - - - Reset - - - - :edit-clear-icon:edit-clear-icon - - - Qt::ToolButtonTextBesideIcon - - - - - - - - 0 - 0 - - - - Close - - - - :filter-close:filter-close - - - Qt::ToolButtonTextBesideIcon - - - - - - - - - Fulltext - - - - 0 - 0 - - - - - - - - - - - 0 - 0 - - - - - Substring + + QFrame::Plain + + + true + + + + + 0 + 0 + 486 + 487 + + + + + + + + + + true + + + + Filter + + + + + + + + + + 0 + 0 + - - - Starts with + Add constraint + + + + + + + Qt::Horizontal + + + + + + + + 0 + 0 + - - - Full word + Save set - - - - - - - - - - - - - - Qt::Vertical - - - - 20 - 40 - - - - - + + + + + + + 0 + 0 + + + + Load set + + + + + + + Qt::Horizontal + + + + + + + + 0 + 0 + + + + Reset + + + + :edit-clear-icon:edit-clear-icon + + + Qt::ToolButtonTextBesideIcon + + + + + + + + 0 + 0 + + + + Close + + + + :filter-close:filter-close + + + Qt::ToolButtonTextBesideIcon + + + + + + + + + Fulltext + + + + 0 + 0 + + + + + + + + + + + 0 + 0 + + + + + Substring + + + + + Starts with + + + + + Full word + + + + + + + + + + + + + + + Qt::Vertical + + + + 20 + 40 + + + + + + + + + + Filter sets +