From c6bd2a7ffb1388f56d54fe67042bd0c9f44eaf9a Mon Sep 17 00:00:00 2001 From: Berthold Stoeger Date: Thu, 28 May 2020 07:31:51 +0200 Subject: filter: connect load filter preset functionality to ui Add a button to the filter preset widget that allows the user to load a previously saved filter preset. Signed-off-by: Berthold Stoeger --- desktop-widgets/filterwidget2.cpp | 48 ++++++++++++++++++++++++++++++++++++--- 1 file changed, 45 insertions(+), 3 deletions(-) (limited to 'desktop-widgets/filterwidget2.cpp') diff --git a/desktop-widgets/filterwidget2.cpp b/desktop-widgets/filterwidget2.cpp index 490892cc1..7b26c420e 100644 --- a/desktop-widgets/filterwidget2.cpp +++ b/desktop-widgets/filterwidget2.cpp @@ -6,9 +6,7 @@ #include "core/qthelper.h" #include "core/divelist.h" #include "core/settings/qPrefUnit.h" -#include "core/filterpreset.h" - -#include +#include "qt-models/filterpresetmodel.h" FilterWidget2::FilterWidget2(QWidget* parent) : QWidget(parent), @@ -27,6 +25,8 @@ FilterWidget2::FilterWidget2(QWidget* parent) : ui.addConstraintButton->setPopupMode(QToolButton::InstantPopup); ui.constraintTable->setColumnStretch(4, 1); // The fifth column is were the actual constraint resides - stretch that. + ui.loadSetButton->setPopupMode(QToolButton::InstantPopup); + connect(ui.clear, &QToolButton::clicked, this, &FilterWidget2::clearFilter); connect(ui.close, &QToolButton::clicked, this, &FilterWidget2::closeFilter); connect(ui.fullText, &QLineEdit::textChanged, this, &FilterWidget2::updateFilter); @@ -37,13 +37,48 @@ FilterWidget2::FilterWidget2(QWidget* parent) : connect(&constraintModel, &FilterConstraintModel::dataChanged, this, &FilterWidget2::constraintChanged); connect(&constraintModel, &FilterConstraintModel::modelReset, this, &FilterWidget2::constraintsReset); + // QDataWidgetMapper might be the more civilized way to keep the menus up to data. + // For now, let's be blunt and fully reload the context menu if the presets list changes. + // This gives us more flexibility in populating the menus. + QAbstractItemModel *presetModel = FilterPresetModel::instance(); + connect(presetModel, &QAbstractItemModel::rowsInserted, this, &FilterWidget2::updatePresetMenu); + connect(presetModel, &QAbstractItemModel::rowsRemoved, this, &FilterWidget2::updatePresetMenu); + connect(presetModel, &QAbstractItemModel::dataChanged, this, &FilterWidget2::updatePresetMenu); + connect(presetModel, &QAbstractItemModel::modelReset, this, &FilterWidget2::updatePresetMenu); + clearFilter(); + updatePresetMenu(); } FilterWidget2::~FilterWidget2() { } +void FilterWidget2::updatePresetMenu() +{ + loadFilterPresetMenu.reset(new QMenu); + QAbstractItemModel *model = FilterPresetModel::instance(); + int count = model->rowCount(QModelIndex()); + if (count == 0) { + ui.loadSetButton->setEnabled(false); + return; + } + ui.loadSetButton->setEnabled(true); + for (int i = 0; i < count; ++i) { + QModelIndex idx = model->index(i, 0); + QString name = model->data(idx, Qt::DisplayRole).value(); + loadFilterPresetMenu->addAction(name, [this,i]() { loadPreset(i); }); + } + ui.loadSetButton->setMenu(loadFilterPresetMenu.get()); +} + +void FilterWidget2::loadPreset(int index) +{ + FilterData filter = filter_preset_get(index); + setFilterData(filter); + updateFilter(); +} + void FilterWidget2::constraintAdded(const QModelIndex &parent, int first, int last) { if (parent.isValid() || last < first) @@ -111,6 +146,13 @@ FilterData FilterWidget2::createFilterData() const return filterData; } +void FilterWidget2::setFilterData(const FilterData &filterData) +{ + ui.fulltextStringMode->setCurrentIndex((int)filterData.fulltextStringMode); + ui.fullText->setText(filterData.fullText.originalQuery); + constraintModel.reload(filterData.constraints); +} + void FilterWidget2::updateFilter() { if (ignoreSignal) -- cgit v1.2.3-70-g09d2