diff options
author | Berthold Stoeger <bstoeger@mail.tuwien.ac.at> | 2018-10-09 12:26:58 +0200 |
---|---|---|
committer | Dirk Hohndel <dirk@hohndel.org> | 2018-10-09 21:00:44 -0700 |
commit | d6010b5155e7c1b03d1872eb3ed75c39e070ad55 (patch) | |
tree | fb8a33c76b4e9a4afd0dafebefa16bd55f76c199 | |
parent | 19d651a36b8f5c955ef3e147bbb3efa93028f959 (diff) | |
download | subsurface-d6010b5155e7c1b03d1872eb3ed75c39e070ad55.tar.gz |
Dive locations: factor out common code of models
For increased maintainability, use the same columns, roles and
the same accessor function for both dive-site models.
Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
-rw-r--r-- | desktop-widgets/locationinformation.cpp | 45 | ||||
-rw-r--r-- | desktop-widgets/locationinformation.h | 1 | ||||
-rw-r--r-- | qt-models/divelocationmodel.cpp | 18 | ||||
-rw-r--r-- | qt-models/divelocationmodel.h | 9 |
4 files changed, 28 insertions, 45 deletions
diff --git a/desktop-widgets/locationinformation.cpp b/desktop-widgets/locationinformation.cpp index 3ebc3d020..4118e6c65 100644 --- a/desktop-widgets/locationinformation.cpp +++ b/desktop-widgets/locationinformation.cpp @@ -351,7 +351,7 @@ bool DiveLocationFilterProxyModel::filterAcceptsRow(int source_row, const QModel if (source_row == 0) return true; - QString sourceString = sourceModel()->index(source_row, DiveLocationModel::NAME).data(Qt::DisplayRole).toString(); + QString sourceString = sourceModel()->index(source_row, LocationInformationModel::NAME).data(Qt::DisplayRole).toString(); return sourceString.toLower().contains(location_line_edit->text().toLower()); } @@ -377,7 +377,7 @@ QVariant DiveLocationModel::data(const QModelIndex &index, int role) const static const QIcon geoCode(":geotag-icon"); if (index.row() <= 1) { // two special cases. - if (index.column() == UUID) { + if (index.column() == LocationInformationModel::UUID) { return RECENTLY_ADDED_DIVESITE; } switch (role) { @@ -394,35 +394,12 @@ QVariant DiveLocationModel::data(const QModelIndex &index, int role) const // The dive sites are -2 because of the first two items. struct dive_site *ds = get_dive_site(index.row() - 2); - switch (role) { - case Qt::EditRole: - case Qt::DisplayRole: - switch (index.column()) { - case UUID: - return ds->uuid; - case NAME: - return ds->name; - case LATITUDE: - return ds->latitude.udeg; - case LONGITUDE: - return ds->longitude.udeg; - case DESCRIPTION: - return ds->description; - case NOTES: - return ds->name; - } - break; - case Qt::DecorationRole: { - if (dive_site_has_gps_location(ds)) - return geoCode; - } - } - return QVariant(); + return LocationInformationModel::getDiveSiteData(ds, index.column(), role); } int DiveLocationModel::columnCount(const QModelIndex&) const { - return COLUMNS; + return LocationInformationModel::COLUMNS; } int DiveLocationModel::rowCount(const QModelIndex&) const @@ -453,10 +430,10 @@ DiveLocationLineEdit::DiveLocationLineEdit(QWidget *parent) : QLineEdit(parent), location_line_edit = this; proxy->setSourceModel(model); - proxy->setFilterKeyColumn(DiveLocationModel::NAME); + proxy->setFilterKeyColumn(LocationInformationModel::NAME); view->setModel(proxy); - view->setModelColumn(DiveLocationModel::NAME); + view->setModelColumn(LocationInformationModel::NAME); view->setItemDelegate(new LocationFilterDelegate()); view->setEditTriggers(QAbstractItemView::NoEditTriggers); view->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff); @@ -525,10 +502,10 @@ void DiveLocationLineEdit::focusOutEvent(QFocusEvent *ev) void DiveLocationLineEdit::itemActivated(const QModelIndex &index) { QModelIndex idx = index; - if (index.column() == DiveLocationModel::UUID) - idx = index.model()->index(index.row(), DiveLocationModel::NAME); + if (index.column() == LocationInformationModel::UUID) + idx = index.model()->index(index.row(), LocationInformationModel::NAME); - QModelIndex uuidIndex = index.model()->index(index.row(), DiveLocationModel::UUID); + QModelIndex uuidIndex = index.model()->index(index.row(), LocationInformationModel::UUID); uint32_t uuid = uuidIndex.data().toInt(); currType = uuid == 1 ? NEW_DIVE_SITE : EXISTING_DIVE_SITE; currUuid = uuid; @@ -566,8 +543,8 @@ void DiveLocationLineEdit::setTemporaryDiveSiteName(const QString&) // a dive site to be generated. The first entry is simply the entered // text. The second entry is the first known dive site name starting // with the entered text. - QModelIndex i0 = model->index(0, DiveLocationModel::NAME); - QModelIndex i1 = model->index(1, DiveLocationModel::NAME); + QModelIndex i0 = model->index(0, LocationInformationModel::NAME); + QModelIndex i1 = model->index(1, LocationInformationModel::NAME); model->setData(i0, text()); // Note: if i1_name stays empty, the line will automatically diff --git a/desktop-widgets/locationinformation.h b/desktop-widgets/locationinformation.h index 3b88bd030..736e53e04 100644 --- a/desktop-widgets/locationinformation.h +++ b/desktop-widgets/locationinformation.h @@ -63,7 +63,6 @@ public: class DiveLocationModel : public QAbstractTableModel { Q_OBJECT public: - enum columns{UUID, NAME, LATITUDE, LONGITUDE, DESCRIPTION, NOTES, COLUMNS}; DiveLocationModel(QObject *o = 0); void resetModel(); QVariant data(const QModelIndex& index, int role = Qt::DisplayRole) const; diff --git a/qt-models/divelocationmodel.cpp b/qt-models/divelocationmodel.cpp index ba9dede0d..4411f5d98 100644 --- a/qt-models/divelocationmodel.cpp +++ b/qt-models/divelocationmodel.cpp @@ -32,20 +32,15 @@ int LocationInformationModel::rowCount(const QModelIndex&) const return dive_site_table.nr; } -QVariant LocationInformationModel::data(const QModelIndex &index, int role) const +QVariant LocationInformationModel::getDiveSiteData(const struct dive_site *ds, int column, int role) { - if (!index.isValid()) - return QVariant(); - - struct dive_site *ds = get_dive_site(index.row()); - if (!ds) return QVariant(); switch(role) { case Qt::EditRole: case Qt::DisplayRole : - switch(index.column()) { + switch(column) { case UUID: return ds->uuid; case NAME: return ds->name; case LATITUDE: return ds->latitude.udeg; @@ -70,6 +65,15 @@ QVariant LocationInformationModel::data(const QModelIndex &index, int role) cons return QVariant(); } +QVariant LocationInformationModel::data(const QModelIndex &index, int role) const +{ + if (!index.isValid()) + return QVariant(); + + struct dive_site *ds = get_dive_site(index.row()); + return getDiveSiteData(ds, index.column(), role); +} + void LocationInformationModel::update() { beginResetModel(); diff --git a/qt-models/divelocationmodel.h b/qt-models/divelocationmodel.h index 47164d2d4..8dbc8f3e6 100644 --- a/qt-models/divelocationmodel.h +++ b/qt-models/divelocationmodel.h @@ -8,18 +8,21 @@ #include "core/units.h" #include "ssrfsortfilterproxymodel.h" -class QLineEdit; - #define RECENTLY_ADDED_DIVESITE 1 bool filter_same_gps_cb (QAbstractItemModel *m, int sourceRow, const QModelIndex& parent); + class LocationInformationModel : public QAbstractTableModel { Q_OBJECT public: - LocationInformationModel(QObject *obj = 0); + // Common columns, roles and accessor function for all dive-site models. + // Thus, different views can connect to different models. enum Columns { UUID, NAME, LATITUDE, LONGITUDE, COORDS, DESCRIPTION, NOTES, TAXONOMY_1, TAXONOMY_2, TAXONOMY_3, COLUMNS}; enum Roles { UUID_ROLE = Qt::UserRole + 1 }; + static QVariant getDiveSiteData(const struct dive_site *ds, int column, int role); + + LocationInformationModel(QObject *obj = 0); static LocationInformationModel *instance(); int columnCount(const QModelIndex &parent) const; int rowCount(const QModelIndex &parent = QModelIndex()) const; |