diff options
author | Berthold Stoeger <bstoeger@mail.tuwien.ac.at> | 2020-05-27 23:09:30 +0200 |
---|---|---|
committer | Dirk Hohndel <dirk@hohndel.org> | 2020-09-29 16:13:03 -0700 |
commit | 581eb1f563df9e7fa72371d936521e0bd55d9f18 (patch) | |
tree | b269b6566f85aadcc0951fc8d40a0cbd2cdbf7e5 | |
parent | 2f5223035a9d43b933b0baf64823dab84b5d8cea (diff) | |
download | subsurface-581eb1f563df9e7fa72371d936521e0bd55d9f18.tar.gz |
filter: create a primitive "create filter preset" dialog
The dialog asks the user for a name and warns if the name
already exists, i.e. an old filter preset will be overwritten.
Possibly, this should contain an auto-completion facility in
the case that the user wants to overwrite old presets.
Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
-rw-r--r-- | desktop-widgets/CMakeLists.txt | 1 | ||||
-rw-r--r-- | desktop-widgets/addfilterpreset.ui | 87 | ||||
-rw-r--r-- | desktop-widgets/filterwidget2.cpp | 29 | ||||
-rw-r--r-- | desktop-widgets/filterwidget2.h | 2 | ||||
-rw-r--r-- | desktop-widgets/filterwidget2.ui | 58 | ||||
-rw-r--r-- | desktop-widgets/simplewidgets.cpp | 26 | ||||
-rw-r--r-- | desktop-widgets/simplewidgets.h | 13 |
7 files changed, 193 insertions, 23 deletions
diff --git a/desktop-widgets/CMakeLists.txt b/desktop-widgets/CMakeLists.txt index a9a6a59a7..4a3259318 100644 --- a/desktop-widgets/CMakeLists.txt +++ b/desktop-widgets/CMakeLists.txt @@ -16,6 +16,7 @@ add_subdirectory(preferences) set (SUBSURFACE_UI about.ui + addfilterpreset.ui btdeviceselectiondialog.ui configuredivecomputerdialog.ui divecomponentselection.ui diff --git a/desktop-widgets/addfilterpreset.ui b/desktop-widgets/addfilterpreset.ui new file mode 100644 index 000000000..120c5e547 --- /dev/null +++ b/desktop-widgets/addfilterpreset.ui @@ -0,0 +1,87 @@ +<?xml version="1.0" encoding="UTF-8"?> +<ui version="4.0"> + <class>AddFilterPresetDialog</class> + <widget class="QDialog" name="AddFilterPresetDialog"> + <property name="windowModality"> + <enum>Qt::WindowModal</enum> + </property> + <property name="geometry"> + <rect> + <x>0</x> + <y>0</y> + <width>211</width> + <height>127</height> + </rect> + </property> + <property name="windowTitle"> + <string>Save filter set</string> + </property> + <property name="windowIcon"> + <iconset> + <normalon>:subsurface-icon</normalon> + </iconset> + </property> + <layout class="QVBoxLayout" name="verticalLayout_2"> + <property name="spacing"> + <number>1</number> + </property> + <property name="leftMargin"> + <number>3</number> + </property> + <property name="topMargin"> + <number>3</number> + </property> + <property name="rightMargin"> + <number>3</number> + </property> + <property name="bottomMargin"> + <number>3</number> + </property> + <item> + <widget class="QGroupBox" name="groupBox"> + <property name="title"> + <string>Save filter set</string> + </property> + <layout class="QVBoxLayout" name="verticalLayout"> + <property name="leftMargin"> + <number>0</number> + </property> + <property name="topMargin"> + <number>0</number> + </property> + <property name="rightMargin"> + <number>0</number> + </property> + <property name="bottomMargin"> + <number>0</number> + </property> + <item> + <widget class="QLineEdit" name="name"/> + </item> + <item> + <widget class="QLabel" name="duplicateWarning"> + <property name="text"> + <string>Warning: this will overwrite an existing filter set.</string> + </property> + </widget> + </item> + </layout> + </widget> + </item> + <item> + <widget class="QDialogButtonBox" name="buttonBox"> + <property name="orientation"> + <enum>Qt::Horizontal</enum> + </property> + <property name="standardButtons"> + <set>QDialogButtonBox::Cancel|QDialogButtonBox::Ok</set> + </property> + </widget> + </item> + </layout> + </widget> + <resources> + <include location="../subsurface.qrc"/> + </resources> + <connections/> +</ui> diff --git a/desktop-widgets/filterwidget2.cpp b/desktop-widgets/filterwidget2.cpp index f1cd3263d..490892cc1 100644 --- a/desktop-widgets/filterwidget2.cpp +++ b/desktop-widgets/filterwidget2.cpp @@ -2,9 +2,11 @@ #include "desktop-widgets/filterconstraintwidget.h" #include "desktop-widgets/simplewidgets.h" #include "desktop-widgets/mainwindow.h" +#include "commands/command.h" #include "core/qthelper.h" #include "core/divelist.h" #include "core/settings/qPrefUnit.h" +#include "core/filterpreset.h" #include <QDoubleSpinBox> @@ -100,19 +102,38 @@ void FilterWidget2::closeFilter() MainWindow::instance()->setApplicationState(ApplicationState::Default); } -void FilterWidget2::updateFilter() +FilterData FilterWidget2::createFilterData() const { - if (ignoreSignal) - return; - FilterData filterData; filterData.fulltextStringMode = (StringFilterMode)ui.fulltextStringMode->currentIndex(); filterData.fullText = ui.fullText->text(); filterData.constraints = constraintModel.getConstraints(); + return filterData; +} + +void FilterWidget2::updateFilter() +{ + if (ignoreSignal) + return; + + FilterData filterData = createFilterData(); validFilter = filterData.validFilter(); DiveFilter::instance()->setFilter(filterData); } +void FilterWidget2::on_addSetButton_clicked() +{ + AddFilterPresetDialog dialog(this); + QString name = dialog.doit(); + if (name.isEmpty()) + return; + int idx = filter_preset_id(name); + if (idx >= 0) + Command::editFilterPreset(idx, createFilterData()); + else + Command::createFilterPreset(name, createFilterData()); +} + void FilterWidget2::showEvent(QShowEvent *event) { QWidget::showEvent(event); diff --git a/desktop-widgets/filterwidget2.h b/desktop-widgets/filterwidget2.h index 462200bf8..334b9737c 100644 --- a/desktop-widgets/filterwidget2.h +++ b/desktop-widgets/filterwidget2.h @@ -33,6 +33,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 on_addSetButton_clicked(); private: bool ignoreSignal; @@ -41,6 +42,7 @@ private: bool validFilter; void addConstraint(filter_constraint_type type); std::vector<std::unique_ptr<FilterConstraintWidget>> constraintWidgets; + FilterData createFilterData() const; }; #endif diff --git a/desktop-widgets/filterwidget2.ui b/desktop-widgets/filterwidget2.ui index f6b3d65f6..ca5d7693a 100644 --- a/desktop-widgets/filterwidget2.ui +++ b/desktop-widgets/filterwidget2.ui @@ -58,7 +58,7 @@ <item row="0" column="2" colspan="3"> <layout class="QHBoxLayout" name="titleLineLayout"> <item alignment="Qt::AlignLeft"> - <widget class="QToolButton" name="clear"> + <widget class="QToolButton" name="addConstraintButton"> <property name="sizePolicy"> <sizepolicy hsizetype="Preferred" vsizetype="Preferred"> <horstretch>0</horstretch> @@ -66,19 +66,39 @@ </sizepolicy> </property> <property name="text"> - <string>Reset</string> + <string>Add constraint</string> </property> - <property name="icon"> - <iconset> - <normaloff>:edit-clear-icon</normaloff>:edit-clear-icon</iconset> + </widget> + </item> + <item> + <spacer name="titleLineSpacer1"> + <property name="orientation"> + <enum>Qt::Horizontal</enum> </property> - <property name="toolButtonStyle"> - <enum>Qt::ToolButtonTextBesideIcon</enum> + </spacer> + </item> + <item> + <widget class="QToolButton" name="addSetButton"> + <property name="sizePolicy"> + <sizepolicy hsizetype="Preferred" vsizetype="Preferred"> + <horstretch>0</horstretch> + <verstretch>0</verstretch> + </sizepolicy> + </property> + <property name="text"> + <string>Save set</string> </property> </widget> </item> + <item> + <spacer name="titleLineSpacer2"> + <property name="orientation"> + <enum>Qt::Horizontal</enum> + </property> + </spacer> + </item> <item alignment="Qt::AlignLeft"> - <widget class="QToolButton" name="close"> + <widget class="QToolButton" name="clear"> <property name="sizePolicy"> <sizepolicy hsizetype="Preferred" vsizetype="Preferred"> <horstretch>0</horstretch> @@ -86,11 +106,11 @@ </sizepolicy> </property> <property name="text"> - <string>Close</string> + <string>Reset</string> </property> <property name="icon"> <iconset> - <normaloff>:filter-close</normaloff>:filter-close</iconset> + <normaloff>:edit-clear-icon</normaloff>:edit-clear-icon</iconset> </property> <property name="toolButtonStyle"> <enum>Qt::ToolButtonTextBesideIcon</enum> @@ -98,7 +118,7 @@ </widget> </item> <item alignment="Qt::AlignLeft"> - <widget class="QToolButton" name="addConstraintButton"> + <widget class="QToolButton" name="close"> <property name="sizePolicy"> <sizepolicy hsizetype="Preferred" vsizetype="Preferred"> <horstretch>0</horstretch> @@ -106,16 +126,16 @@ </sizepolicy> </property> <property name="text"> - <string>Add constraint</string> + <string>Close</string> </property> - </widget> - </item> - <item> - <spacer name="titleLineSpacer"> - <property name="orientation"> - <enum>Qt::Horizontal</enum> + <property name="icon"> + <iconset> + <normaloff>:filter-close</normaloff>:filter-close</iconset> </property> - </spacer> + <property name="toolButtonStyle"> + <enum>Qt::ToolButtonTextBesideIcon</enum> + </property> + </widget> </item> </layout> </item> diff --git a/desktop-widgets/simplewidgets.cpp b/desktop-widgets/simplewidgets.cpp index b18c43bf5..153d3239e 100644 --- a/desktop-widgets/simplewidgets.cpp +++ b/desktop-widgets/simplewidgets.cpp @@ -12,6 +12,7 @@ #include <QClipboard> #include "core/file.h" +#include "core/filterpreset.h" #include "core/divesite.h" #include "desktop-widgets/mainwindow.h" #include "core/qthelper.h" @@ -478,6 +479,31 @@ void DiveComponentSelection::buttonClicked(QAbstractButton *button) } } +AddFilterPresetDialog::AddFilterPresetDialog(QWidget *parent) +{ + ui.setupUi(this); + connect(ui.name, &QLineEdit::textChanged, this, &AddFilterPresetDialog::nameChanged); + connect(ui.buttonBox, &QDialogButtonBox::accepted, this, &AddFilterPresetDialog::accept); + connect(ui.buttonBox, &QDialogButtonBox::rejected, this, &AddFilterPresetDialog::reject); + nameChanged(ui.name->text()); +} + +void AddFilterPresetDialog::nameChanged(const QString &text) +{ + QString trimmed = text.trimmed(); + bool isEmpty = trimmed.isEmpty(); + bool exists = !isEmpty && filter_preset_id(trimmed) >= 0; + ui.duplicateWarning->setVisible(exists); + ui.buttonBox->button(QDialogButtonBox::Ok)->setEnabled(!isEmpty); +} + +QString AddFilterPresetDialog::doit() +{ + if (exec() == QDialog::Accepted) + return ui.name->text().trimmed(); + return QString(); +} + TextHyperlinkEventFilter::TextHyperlinkEventFilter(QTextEdit *txtEdit) : QObject(txtEdit), textEdit(txtEdit), scrollView(textEdit->viewport()) diff --git a/desktop-widgets/simplewidgets.h b/desktop-widgets/simplewidgets.h index 6075aa660..d68c14bd4 100644 --- a/desktop-widgets/simplewidgets.h +++ b/desktop-widgets/simplewidgets.h @@ -20,6 +20,7 @@ class FilterModelBase; #include "ui_urldialog.h" #include "ui_divecomponentselection.h" #include "ui_listfilter.h" +#include "ui_addfilterpreset.h" #include "core/exif.h" #include "core/dive.h" @@ -138,6 +139,18 @@ private: struct dive_components *what; }; +class AddFilterPresetDialog : public QDialog { + Q_OBJECT +public: + explicit AddFilterPresetDialog(QWidget *parent); + QString doit(); // returns name of filter preset or empty string if user cancelled the dialog +private +slots: + void nameChanged(const QString &text); +private: + Ui::AddFilterPresetDialog ui; +}; + class TextHyperlinkEventFilter : public QObject { Q_OBJECT public: |