mirror of
https://github.com/subsurface/subsurface.git
synced 2025-02-19 22:16:15 +00:00
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>
This commit is contained in:
parent
19d651a36b
commit
d6010b5155
4 changed files with 28 additions and 45 deletions
|
@ -351,7 +351,7 @@ bool DiveLocationFilterProxyModel::filterAcceptsRow(int source_row, const QModel
|
||||||
if (source_row == 0)
|
if (source_row == 0)
|
||||||
return true;
|
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());
|
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");
|
static const QIcon geoCode(":geotag-icon");
|
||||||
|
|
||||||
if (index.row() <= 1) { // two special cases.
|
if (index.row() <= 1) { // two special cases.
|
||||||
if (index.column() == UUID) {
|
if (index.column() == LocationInformationModel::UUID) {
|
||||||
return RECENTLY_ADDED_DIVESITE;
|
return RECENTLY_ADDED_DIVESITE;
|
||||||
}
|
}
|
||||||
switch (role) {
|
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.
|
// The dive sites are -2 because of the first two items.
|
||||||
struct dive_site *ds = get_dive_site(index.row() - 2);
|
struct dive_site *ds = get_dive_site(index.row() - 2);
|
||||||
switch (role) {
|
return LocationInformationModel::getDiveSiteData(ds, index.column(), 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();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int DiveLocationModel::columnCount(const QModelIndex&) const
|
int DiveLocationModel::columnCount(const QModelIndex&) const
|
||||||
{
|
{
|
||||||
return COLUMNS;
|
return LocationInformationModel::COLUMNS;
|
||||||
}
|
}
|
||||||
|
|
||||||
int DiveLocationModel::rowCount(const QModelIndex&) const
|
int DiveLocationModel::rowCount(const QModelIndex&) const
|
||||||
|
@ -453,10 +430,10 @@ DiveLocationLineEdit::DiveLocationLineEdit(QWidget *parent) : QLineEdit(parent),
|
||||||
location_line_edit = this;
|
location_line_edit = this;
|
||||||
|
|
||||||
proxy->setSourceModel(model);
|
proxy->setSourceModel(model);
|
||||||
proxy->setFilterKeyColumn(DiveLocationModel::NAME);
|
proxy->setFilterKeyColumn(LocationInformationModel::NAME);
|
||||||
|
|
||||||
view->setModel(proxy);
|
view->setModel(proxy);
|
||||||
view->setModelColumn(DiveLocationModel::NAME);
|
view->setModelColumn(LocationInformationModel::NAME);
|
||||||
view->setItemDelegate(new LocationFilterDelegate());
|
view->setItemDelegate(new LocationFilterDelegate());
|
||||||
view->setEditTriggers(QAbstractItemView::NoEditTriggers);
|
view->setEditTriggers(QAbstractItemView::NoEditTriggers);
|
||||||
view->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
|
view->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
|
||||||
|
@ -525,10 +502,10 @@ void DiveLocationLineEdit::focusOutEvent(QFocusEvent *ev)
|
||||||
void DiveLocationLineEdit::itemActivated(const QModelIndex &index)
|
void DiveLocationLineEdit::itemActivated(const QModelIndex &index)
|
||||||
{
|
{
|
||||||
QModelIndex idx = index;
|
QModelIndex idx = index;
|
||||||
if (index.column() == DiveLocationModel::UUID)
|
if (index.column() == LocationInformationModel::UUID)
|
||||||
idx = index.model()->index(index.row(), DiveLocationModel::NAME);
|
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();
|
uint32_t uuid = uuidIndex.data().toInt();
|
||||||
currType = uuid == 1 ? NEW_DIVE_SITE : EXISTING_DIVE_SITE;
|
currType = uuid == 1 ? NEW_DIVE_SITE : EXISTING_DIVE_SITE;
|
||||||
currUuid = uuid;
|
currUuid = uuid;
|
||||||
|
@ -566,8 +543,8 @@ void DiveLocationLineEdit::setTemporaryDiveSiteName(const QString&)
|
||||||
// a dive site to be generated. The first entry is simply the entered
|
// 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
|
// text. The second entry is the first known dive site name starting
|
||||||
// with the entered text.
|
// with the entered text.
|
||||||
QModelIndex i0 = model->index(0, DiveLocationModel::NAME);
|
QModelIndex i0 = model->index(0, LocationInformationModel::NAME);
|
||||||
QModelIndex i1 = model->index(1, DiveLocationModel::NAME);
|
QModelIndex i1 = model->index(1, LocationInformationModel::NAME);
|
||||||
model->setData(i0, text());
|
model->setData(i0, text());
|
||||||
|
|
||||||
// Note: if i1_name stays empty, the line will automatically
|
// Note: if i1_name stays empty, the line will automatically
|
||||||
|
|
|
@ -63,7 +63,6 @@ public:
|
||||||
class DiveLocationModel : public QAbstractTableModel {
|
class DiveLocationModel : public QAbstractTableModel {
|
||||||
Q_OBJECT
|
Q_OBJECT
|
||||||
public:
|
public:
|
||||||
enum columns{UUID, NAME, LATITUDE, LONGITUDE, DESCRIPTION, NOTES, COLUMNS};
|
|
||||||
DiveLocationModel(QObject *o = 0);
|
DiveLocationModel(QObject *o = 0);
|
||||||
void resetModel();
|
void resetModel();
|
||||||
QVariant data(const QModelIndex& index, int role = Qt::DisplayRole) const;
|
QVariant data(const QModelIndex& index, int role = Qt::DisplayRole) const;
|
||||||
|
|
|
@ -32,20 +32,15 @@ int LocationInformationModel::rowCount(const QModelIndex&) const
|
||||||
return dive_site_table.nr;
|
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)
|
if (!ds)
|
||||||
return QVariant();
|
return QVariant();
|
||||||
|
|
||||||
switch(role) {
|
switch(role) {
|
||||||
case Qt::EditRole:
|
case Qt::EditRole:
|
||||||
case Qt::DisplayRole :
|
case Qt::DisplayRole :
|
||||||
switch(index.column()) {
|
switch(column) {
|
||||||
case UUID: return ds->uuid;
|
case UUID: return ds->uuid;
|
||||||
case NAME: return ds->name;
|
case NAME: return ds->name;
|
||||||
case LATITUDE: return ds->latitude.udeg;
|
case LATITUDE: return ds->latitude.udeg;
|
||||||
|
@ -70,6 +65,15 @@ QVariant LocationInformationModel::data(const QModelIndex &index, int role) cons
|
||||||
return QVariant();
|
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()
|
void LocationInformationModel::update()
|
||||||
{
|
{
|
||||||
beginResetModel();
|
beginResetModel();
|
||||||
|
|
|
@ -8,18 +8,21 @@
|
||||||
#include "core/units.h"
|
#include "core/units.h"
|
||||||
#include "ssrfsortfilterproxymodel.h"
|
#include "ssrfsortfilterproxymodel.h"
|
||||||
|
|
||||||
class QLineEdit;
|
|
||||||
|
|
||||||
#define RECENTLY_ADDED_DIVESITE 1
|
#define RECENTLY_ADDED_DIVESITE 1
|
||||||
|
|
||||||
bool filter_same_gps_cb (QAbstractItemModel *m, int sourceRow, const QModelIndex& parent);
|
bool filter_same_gps_cb (QAbstractItemModel *m, int sourceRow, const QModelIndex& parent);
|
||||||
|
|
||||||
|
|
||||||
class LocationInformationModel : public QAbstractTableModel {
|
class LocationInformationModel : public QAbstractTableModel {
|
||||||
Q_OBJECT
|
Q_OBJECT
|
||||||
public:
|
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 Columns { UUID, NAME, LATITUDE, LONGITUDE, COORDS, DESCRIPTION, NOTES, TAXONOMY_1, TAXONOMY_2, TAXONOMY_3, COLUMNS};
|
||||||
enum Roles { UUID_ROLE = Qt::UserRole + 1 };
|
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();
|
static LocationInformationModel *instance();
|
||||||
int columnCount(const QModelIndex &parent) const;
|
int columnCount(const QModelIndex &parent) const;
|
||||||
int rowCount(const QModelIndex &parent = QModelIndex()) const;
|
int rowCount(const QModelIndex &parent = QModelIndex()) const;
|
||||||
|
|
Loading…
Add table
Reference in a new issue