diff options
author | Berthold Stoeger <bstoeger@mail.tuwien.ac.at> | 2020-05-28 07:31:51 +0200 |
---|---|---|
committer | Dirk Hohndel <dirk@hohndel.org> | 2020-09-29 16:13:03 -0700 |
commit | c6bd2a7ffb1388f56d54fe67042bd0c9f44eaf9a (patch) | |
tree | a7e4fd0d4481d38ca77f73a6b7964bc9f84cd1bd | |
parent | 51959d0feb9fa560f388b4f1204f5028fb80f70d (diff) | |
download | subsurface-c6bd2a7ffb1388f56d54fe67042bd0c9f44eaf9a.tar.gz |
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 <bstoeger@mail.tuwien.ac.at>
-rw-r--r-- | desktop-widgets/filterwidget2.cpp | 48 | ||||
-rw-r--r-- | desktop-widgets/filterwidget2.h | 5 | ||||
-rw-r--r-- | desktop-widgets/filterwidget2.ui | 13 |
3 files changed, 63 insertions, 3 deletions
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 <QDoubleSpinBox> +#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<QString>(); + 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) diff --git a/desktop-widgets/filterwidget2.h b/desktop-widgets/filterwidget2.h index 334b9737c..55d65fab2 100644 --- a/desktop-widgets/filterwidget2.h +++ b/desktop-widgets/filterwidget2.h @@ -12,6 +12,7 @@ #include "qt-models/filterconstraintmodel.h" class FilterConstraintWidget; +class QMenu; class FilterWidget2 : public QWidget { Q_OBJECT @@ -33,6 +34,7 @@ private slots: void constraintRemoved(const QModelIndex &parent, int first, int last); void constraintChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight, const QVector<int> &roles); void constraintsReset(); + void updatePresetMenu(); void on_addSetButton_clicked(); private: @@ -43,6 +45,9 @@ private: void addConstraint(filter_constraint_type type); std::vector<std::unique_ptr<FilterConstraintWidget>> constraintWidgets; FilterData createFilterData() const; + void setFilterData(const FilterData &filterData); + void loadPreset(int index); + std::unique_ptr<QMenu> loadFilterPresetMenu; }; #endif diff --git a/desktop-widgets/filterwidget2.ui b/desktop-widgets/filterwidget2.ui index ca5d7693a..e83e6918d 100644 --- a/desktop-widgets/filterwidget2.ui +++ b/desktop-widgets/filterwidget2.ui @@ -91,6 +91,19 @@ </widget> </item> <item> + <widget class="QToolButton" name="loadSetButton"> + <property name="sizePolicy"> + <sizepolicy hsizetype="Preferred" vsizetype="Preferred"> + <horstretch>0</horstretch> + <verstretch>0</verstretch> + </sizepolicy> + </property> + <property name="text"> + <string>Load set</string> + </property> + </widget> + </item> + <item> <spacer name="titleLineSpacer2"> <property name="orientation"> <enum>Qt::Horizontal</enum> |