From 536fc05dd6acd67a459fe6decbd67071ad388df0 Mon Sep 17 00:00:00 2001 From: Berthold Stoeger Date: Wed, 27 May 2020 08:17:06 +0200 Subject: [PATCH] filter: add (very primitive) filterpresetmodel Implement a trivial model to provide the filter preset names to the UI. Sadly, for now this features the QWidget/QML column / name dichotomy. However, in this simple case that shouldn't be too much of an issue. Signed-off-by: Berthold Stoeger --- packaging/ios/Subsurface-mobile.pro | 2 + qt-models/CMakeLists.txt | 2 + qt-models/filterpresetmodel.cpp | 59 +++++++++++++++++++++++++++++ qt-models/filterpresetmodel.h | 30 +++++++++++++++ 4 files changed, 93 insertions(+) create mode 100644 qt-models/filterpresetmodel.cpp create mode 100644 qt-models/filterpresetmodel.h diff --git a/packaging/ios/Subsurface-mobile.pro b/packaging/ios/Subsurface-mobile.pro index a34e4ad4b..d924f5c23 100644 --- a/packaging/ios/Subsurface-mobile.pro +++ b/packaging/ios/Subsurface-mobile.pro @@ -146,6 +146,7 @@ SOURCES += ../../subsurface-mobile-main.cpp \ ../../qt-models/models.cpp \ ../../qt-models/weightsysteminfomodel.cpp \ ../../qt-models/filterconstraintmodel.cpp \ + ../../qt-models/filterpresetmodel.cpp \ ../../profile-widget/qmlprofile.cpp \ ../../profile-widget/divecartesianaxis.cpp \ ../../profile-widget/diveeventitem.cpp \ @@ -297,6 +298,7 @@ HEADERS += \ ../../qt-models/models.h \ ../../qt-models/weightsysteminfomodel.h \ ../../qt-models/filterconstraintmodel.h \ + ../../qt-models/filterpresetmodel.h \ ../../profile-widget/qmlprofile.h \ ../../profile-widget/diveprofileitem.h \ ../../profile-widget/profilewidget2.h \ diff --git a/qt-models/CMakeLists.txt b/qt-models/CMakeLists.txt index 280abbd41..b42b6063b 100644 --- a/qt-models/CMakeLists.txt +++ b/qt-models/CMakeLists.txt @@ -21,6 +21,8 @@ set(SUBSURFACE_GENERIC_MODELS_LIB_SRCS divetripmodel.h filterconstraintmodel.cpp filterconstraintmodel.h + filterpresetmodel.cpp + filterpresetmodel.h maplocationmodel.cpp maplocationmodel.h models.cpp diff --git a/qt-models/filterpresetmodel.cpp b/qt-models/filterpresetmodel.cpp new file mode 100644 index 000000000..446dd5e16 --- /dev/null +++ b/qt-models/filterpresetmodel.cpp @@ -0,0 +1,59 @@ +// SPDX-License-Identifier: GPL-2.0 +#include "filterpresetmodel.h" +#include "core/filterconstraint.h" +#include "core/filterpreset.h" +#include "core/qthelper.h" +#include "core/subsurface-qt/divelistnotifier.h" + +FilterPresetModel::FilterPresetModel() +{ + setHeaderDataStrings(QStringList{ "", tr("Name") }); + connect(&diveListNotifier, &DiveListNotifier::dataReset, this, &FilterPresetModel::reset); +} + +FilterPresetModel::~FilterPresetModel() +{ +} + +FilterPresetModel *FilterPresetModel::instance() +{ + static FilterPresetModel self; + return &self; +} + +QVariant FilterPresetModel::data(const QModelIndex &index, int role) const +{ + if (!index.isValid() || index.row() >= filter_presets_count()) + return QVariant(); + + switch (role) { + case Qt::DisplayRole: + if (index.column() == NAME) + return filter_preset_name_qstring(index.row()); + break; + case Qt::DecorationRole: + if (index.column() == REMOVE) + return trashIcon(); + break; + case Qt::SizeHintRole: + if (index.column() == REMOVE) + return trashIcon().size(); + break; + case Qt::ToolTipRole: + if (index.column() == REMOVE) + return tr("Clicking here will remove this filter set."); + break; + } + return QVariant(); +} + +int FilterPresetModel::rowCount(const QModelIndex &parent) const +{ + return filter_presets_count(); +} + +void FilterPresetModel::reset() +{ + beginResetModel(); + endResetModel(); +} diff --git a/qt-models/filterpresetmodel.h b/qt-models/filterpresetmodel.h new file mode 100644 index 000000000..26543666a --- /dev/null +++ b/qt-models/filterpresetmodel.h @@ -0,0 +1,30 @@ +// SPDX-License-Identifier: GPL-2.0 +#ifndef FILTERPRESETMODEL_H +#define FILTERPRESETMODEL_H + +#include "cleanertablemodel.h" +#include "core/filterpreset.h" + +class FilterPresetModel : public CleanerTableModel { + Q_OBJECT +public: + // For QML we will have to define roles + enum Column { + REMOVE, + NAME + }; +private +slots: + void reset(); +public: + // there is one global filter preset list, therefore this model is a singleton + static FilterPresetModel *instance(); +private: + FilterPresetModel(); + ~FilterPresetModel(); + QVariant data(const QModelIndex &index, int role) const override; + int rowCount(const QModelIndex &parent) const override; + std::vector constraints; +}; + +#endif