diff options
Diffstat (limited to 'qt-models')
-rw-r--r-- | qt-models/divelistmodel.cpp | 29 | ||||
-rw-r--r-- | qt-models/divelistmodel.h | 12 |
2 files changed, 41 insertions, 0 deletions
diff --git a/qt-models/divelistmodel.cpp b/qt-models/divelistmodel.cpp index 6706d9137..114745fe7 100644 --- a/qt-models/divelistmodel.cpp +++ b/qt-models/divelistmodel.cpp @@ -2,6 +2,28 @@ #include "helpers.h" #include <QDateTime> +DiveListSortModel::DiveListSortModel(QObject *parent) : QSortFilterProxyModel(parent) +{ + +} + +int DiveListSortModel::getDiveId(int idx) +{ + DiveListModel *mySourceModel = qobject_cast<DiveListModel *>(sourceModel()); + return mySourceModel->getDiveId(mapToSource(index(idx,0)).row()); +} + +int DiveListSortModel::getIdxForId(int id) +{ + for (int i = 0; i < rowCount(); i++) { + QVariant v = data(index(i, 0), DiveListModel::DiveRole); + DiveObjectHelper *d = v.value<DiveObjectHelper *>(); + if (d->id() == id) + return i; + } + return -1; +} + DiveListModel *DiveListModel::m_instance = NULL; DiveListModel::DiveListModel(QObject *parent) : QAbstractListModel(parent) @@ -52,6 +74,13 @@ int DiveListModel::rowCount(const QModelIndex &) const return m_dives.count(); } +int DiveListModel::getDiveId(int idx) const +{ + if (idx < 0 || idx >= m_dives.count()) + return -1; + return m_dives[idx]->id(); +} + QVariant DiveListModel::data(const QModelIndex &index, int role) const { if(index.row() < 0 || index.row() > m_dives.count()) diff --git a/qt-models/divelistmodel.h b/qt-models/divelistmodel.h index 8f52680a4..675619640 100644 --- a/qt-models/divelistmodel.h +++ b/qt-models/divelistmodel.h @@ -2,11 +2,22 @@ #define DIVELISTMODEL_H #include <QAbstractListModel> +#include <QSortFilterProxyModel> #include "dive.h" #include "helpers.h" #include "subsurface-qt/DiveObjectHelper.h" +class DiveListSortModel : public QSortFilterProxyModel +{ + Q_OBJECT +public: + DiveListSortModel(QObject *parent = 0); +public slots: + int getDiveId(int idx); + int getIdxForId(int id); +}; + class DiveListModel : public QAbstractListModel { Q_OBJECT @@ -25,6 +36,7 @@ public: void updateDive(int i, dive *d); void clear(); int rowCount(const QModelIndex &parent = QModelIndex()) const; + int getDiveId(int idx) const; QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const; QHash<int, QByteArray> roleNames() const; QString startAddDive(); |