aboutsummaryrefslogtreecommitdiffstats
path: root/qt-models
diff options
context:
space:
mode:
authorGravatar Tomaz Canabrava <tomaz.canabrava@intel.com>2015-08-31 20:19:06 -0300
committerGravatar Dirk Hohndel <dirk@hohndel.org>2015-08-31 18:41:14 -0700
commitab32295f8906ae5b6334f7abc8d05032b68ae577 (patch)
tree7905a5fbcefc438d12253f0ff65312b785008dff /qt-models
parent5db6d415986ffacf199f75e10d277934d3ff5a7b (diff)
downloadsubsurface-ab32295f8906ae5b6334f7abc8d05032b68ae577.tar.gz
Add a new model: SsrfFilterProxyModel
Add a new Model, it's the QSortFilterProxyModel that accepts functions to filter / sort. so instead of creating a new class for each different sorting (overkill), now we can just create a function and pass to this class. I'll rework the filtering system of subsurface to use this - in the meantime I've created this to ease the creation of another filter: the dive sites by gps coordinates. Signed-off-by: Tomaz Canabrava <tomaz.canabrava@intel.com> Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
Diffstat (limited to 'qt-models')
-rw-r--r--qt-models/ssrfsortfilterproxymodel.cpp42
-rw-r--r--qt-models/ssrfsortfilterproxymodel.h33
2 files changed, 75 insertions, 0 deletions
diff --git a/qt-models/ssrfsortfilterproxymodel.cpp b/qt-models/ssrfsortfilterproxymodel.cpp
new file mode 100644
index 000000000..1a8c835dc
--- /dev/null
+++ b/qt-models/ssrfsortfilterproxymodel.cpp
@@ -0,0 +1,42 @@
+#include "ssrfsortfilterproxymodel.h"
+
+SsrfSortFilterProxyModel::SsrfSortFilterProxyModel(QObject *parent)
+: QSortFilterProxyModel(parent), less_than(0), accepts_col(0), accepts_row(0)
+{
+}
+
+bool SsrfSortFilterProxyModel::lessThan(const QModelIndex& source_left, const QModelIndex& source_right) const
+{
+ Q_ASSERT(less_than);
+ const QAbstractItemModel *self = this;
+ return less_than(const_cast<QAbstractItemModel*>(self), source_left, source_right);
+}
+
+bool SsrfSortFilterProxyModel::filterAcceptsRow(int source_row, const QModelIndex& source_parent) const
+{
+ Q_ASSERT(accepts_row);
+ const QAbstractItemModel *self = this;
+ return accepts_row(const_cast<QAbstractItemModel*>(self), source_row, source_parent);
+}
+
+bool SsrfSortFilterProxyModel::filterAcceptsColumn(int source_column, const QModelIndex& source_parent) const
+{
+ Q_ASSERT(accepts_col);
+ const QAbstractItemModel *self = this;
+ return accepts_col(const_cast<QAbstractItemModel*>(self), source_column, source_parent);
+}
+
+void SsrfSortFilterProxyModel::setLessThan(less_than_cb func)
+{
+ less_than = func;
+}
+
+void SsrfSortFilterProxyModel::setFilterRow(filter_accepts_row_cb func)
+{
+ accepts_row = func;
+}
+
+void SsrfSortFilterProxyModel::setFilterCol(filter_accepts_col_cb func)
+{
+ accepts_col = func;
+}
diff --git a/qt-models/ssrfsortfilterproxymodel.h b/qt-models/ssrfsortfilterproxymodel.h
new file mode 100644
index 000000000..6427a35dc
--- /dev/null
+++ b/qt-models/ssrfsortfilterproxymodel.h
@@ -0,0 +1,33 @@
+#ifndef SSRFSORTFILTERPROXYMODEL_H
+#define SSRFSORTFILTERPROXYMODEL_H
+
+#include <QSortFilterProxyModel>
+
+typedef bool (*filter_accepts_col_cb) (QAbstractItemModel *model,int sourceRow, const QModelIndex& parent);
+typedef bool (*filter_accepts_row_cb) (QAbstractItemModel *model,int sourceRow, const QModelIndex& parent);
+typedef bool (*less_than_cb) (QAbstractItemModel *model, const QModelIndex& left, const QModelIndex& right);
+
+/* Use this class when you wanna a quick filter.
+ * instead of creating a new class, just create a new instance of this class
+ * and plug your callback.
+ */
+class SsrfSortFilterProxyModel : public QSortFilterProxyModel {
+ Q_OBJECT
+
+public:
+ SsrfSortFilterProxyModel(QObject *parent = 0);
+ virtual bool lessThan(const QModelIndex& source_left, const QModelIndex& source_right) const;
+ virtual bool filterAcceptsRow(int source_row, const QModelIndex& source_parent) const;
+ virtual bool filterAcceptsColumn(int source_column, const QModelIndex& source_parent) const;
+
+ void setLessThan(less_than_cb func);
+ void setFilterRow(filter_accepts_row_cb func);
+ void setFilterCol(filter_accepts_col_cb func);
+
+private:
+ less_than_cb less_than;
+ filter_accepts_col_cb accepts_col;
+ filter_accepts_row_cb accepts_row;
+};
+
+#endif