2017-04-27 20:25:32 +02:00
|
|
|
// SPDX-License-Identifier: GPL-2.0
|
2016-04-04 22:02:03 -07:00
|
|
|
#include "core/units.h"
|
|
|
|
#include "qt-models/divelocationmodel.h"
|
2019-03-10 16:03:39 +01:00
|
|
|
#include "core/subsurface-qt/DiveListNotifier.h"
|
2018-02-28 23:37:09 +01:00
|
|
|
#include "core/qthelper.h"
|
2019-03-04 23:20:29 +01:00
|
|
|
#include "core/divesite.h"
|
2019-03-09 22:32:16 +01:00
|
|
|
#include "core/metrics.h"
|
|
|
|
#include "cleanertablemodel.h" // for trashIcon();
|
2015-06-01 22:30:33 -03:00
|
|
|
#include <QDebug>
|
2015-07-14 18:43:47 -03:00
|
|
|
#include <QLineEdit>
|
|
|
|
#include <QIcon>
|
2017-04-25 23:16:46 +02:00
|
|
|
#include <core/gettextfromc.h>
|
2015-05-29 21:19:44 -03:00
|
|
|
|
2017-11-26 23:48:41 +01:00
|
|
|
static bool dive_site_less_than(dive_site *a, dive_site *b)
|
2015-05-31 17:56:53 -03:00
|
|
|
{
|
2017-11-26 23:48:41 +01:00
|
|
|
return QString(a->name) < QString(b->name);
|
2015-05-31 17:56:53 -03:00
|
|
|
}
|
|
|
|
|
2015-05-29 22:22:24 -03:00
|
|
|
LocationInformationModel *LocationInformationModel::instance()
|
|
|
|
{
|
|
|
|
static LocationInformationModel *self = new LocationInformationModel();
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
2018-10-09 09:18:08 +02:00
|
|
|
LocationInformationModel::LocationInformationModel(QObject *obj) : QAbstractTableModel(obj)
|
2015-05-29 21:19:44 -03:00
|
|
|
{
|
2019-03-10 16:03:39 +01:00
|
|
|
connect(&diveListNotifier, &DiveListNotifier::diveSiteDiveCountChanged, this, &LocationInformationModel::diveSiteDiveCountChanged);
|
2015-05-29 21:19:44 -03:00
|
|
|
}
|
|
|
|
|
2019-03-09 22:32:16 +01:00
|
|
|
int LocationInformationModel::columnCount(const QModelIndex &) const
|
2015-07-01 18:46:27 -03:00
|
|
|
{
|
|
|
|
return COLUMNS;
|
|
|
|
}
|
|
|
|
|
2019-03-09 22:32:16 +01:00
|
|
|
int LocationInformationModel::rowCount(const QModelIndex &) const
|
2015-05-29 21:19:44 -03:00
|
|
|
{
|
2018-10-09 09:18:08 +02:00
|
|
|
return dive_site_table.nr;
|
2015-07-16 18:08:08 -03:00
|
|
|
}
|
|
|
|
|
2019-03-09 22:32:16 +01:00
|
|
|
QVariant LocationInformationModel::headerData(int section, Qt::Orientation orientation, int role) const
|
|
|
|
{
|
|
|
|
if (orientation == Qt::Vertical)
|
|
|
|
return QVariant();
|
|
|
|
|
|
|
|
switch (role) {
|
|
|
|
case Qt::TextAlignmentRole:
|
|
|
|
return int(Qt::AlignLeft | Qt::AlignVCenter);
|
|
|
|
case Qt::FontRole:
|
|
|
|
return defaultModelFont();
|
|
|
|
case Qt::InitialSortOrderRole:
|
|
|
|
// By default, sort number of dives descending, everything else ascending.
|
|
|
|
return section == NUM_DIVES ? Qt::DescendingOrder : Qt::AscendingOrder;
|
|
|
|
case Qt::DisplayRole:
|
|
|
|
case Qt::ToolTipRole:
|
|
|
|
switch (section) {
|
|
|
|
case NAME:
|
|
|
|
return tr("Name");
|
|
|
|
case DESCRIPTION:
|
|
|
|
return tr("Description");
|
|
|
|
case NUM_DIVES:
|
|
|
|
return tr("# of dives");
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return QVariant();
|
|
|
|
}
|
|
|
|
|
|
|
|
Qt::ItemFlags LocationInformationModel::flags(const QModelIndex &index) const
|
|
|
|
{
|
|
|
|
switch (index.column()) {
|
|
|
|
case REMOVE:
|
|
|
|
return Qt::ItemIsEnabled;
|
|
|
|
case NAME:
|
|
|
|
case DESCRIPTION:
|
|
|
|
return QAbstractItemModel::flags(index) | Qt::ItemIsEditable;
|
|
|
|
}
|
|
|
|
return QAbstractItemModel::flags(index);
|
|
|
|
}
|
|
|
|
|
2018-10-09 12:26:58 +02:00
|
|
|
QVariant LocationInformationModel::getDiveSiteData(const struct dive_site *ds, int column, int role)
|
2015-05-29 21:19:44 -03:00
|
|
|
{
|
2015-06-02 15:35:06 +02:00
|
|
|
if (!ds)
|
|
|
|
return QVariant();
|
|
|
|
|
2015-05-29 21:19:44 -03:00
|
|
|
switch(role) {
|
2015-07-14 18:43:47 -03:00
|
|
|
case Qt::EditRole:
|
2019-03-09 22:32:16 +01:00
|
|
|
case Qt::DisplayRole:
|
2018-10-09 12:26:58 +02:00
|
|
|
switch(column) {
|
2018-10-28 21:16:42 +01:00
|
|
|
case DIVESITE: return QVariant::fromValue<dive_site *>((dive_site *)ds); // Not nice: casting away const
|
2015-09-04 12:06:31 +02:00
|
|
|
case NAME: return ds->name;
|
2019-03-09 22:32:16 +01:00
|
|
|
case NUM_DIVES: return ds->dives.nr;
|
2018-10-20 14:12:15 -04:00
|
|
|
case LATITUDE: return ds->location.lat.udeg;
|
|
|
|
case LONGITUDE: return ds->location.lon.udeg;
|
2015-07-01 18:46:27 -03:00
|
|
|
case COORDS: return "TODO";
|
|
|
|
case DESCRIPTION: return ds->description;
|
|
|
|
case NOTES: return ds->name;
|
|
|
|
case TAXONOMY_1: return "TODO";
|
|
|
|
case TAXONOMY_2: return "TODO";
|
|
|
|
case TAXONOMY_3: return "TODO";
|
|
|
|
}
|
|
|
|
break;
|
2019-03-09 22:32:16 +01:00
|
|
|
case Qt::ToolTipRole:
|
|
|
|
switch(column) {
|
|
|
|
case REMOVE: return tr("Clicking here will remove this divesite.");
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case Qt::DecorationRole:
|
|
|
|
switch(column) {
|
|
|
|
#ifndef SUBSURFACE_MOBILE
|
|
|
|
case REMOVE: return trashIcon();
|
|
|
|
#endif
|
|
|
|
case NAME: return dive_site_has_gps_location(ds) ? QIcon(":geotag-icon") : QVariant();
|
|
|
|
}
|
|
|
|
break;
|
2018-10-24 16:34:43 +02:00
|
|
|
case DIVESITE_ROLE:
|
2018-10-28 21:16:42 +01:00
|
|
|
return QVariant::fromValue<dive_site *>((dive_site *)ds); // Not nice: casting away const
|
2015-05-29 21:19:44 -03:00
|
|
|
}
|
|
|
|
return QVariant();
|
|
|
|
}
|
|
|
|
|
2018-10-09 12:26:58 +02:00
|
|
|
QVariant LocationInformationModel::data(const QModelIndex &index, int role) const
|
|
|
|
{
|
|
|
|
if (!index.isValid())
|
|
|
|
return QVariant();
|
|
|
|
|
2019-02-26 22:26:11 +01:00
|
|
|
struct dive_site *ds = get_dive_site(index.row(), &dive_site_table);
|
2018-10-09 12:26:58 +02:00
|
|
|
return getDiveSiteData(ds, index.column(), role);
|
|
|
|
}
|
|
|
|
|
2015-05-29 21:19:44 -03:00
|
|
|
void LocationInformationModel::update()
|
|
|
|
{
|
2015-06-01 23:13:51 -03:00
|
|
|
beginResetModel();
|
2019-04-03 20:21:53 +02:00
|
|
|
std::sort(dive_site_table.dive_sites, dive_site_table.dive_sites + dive_site_table.nr, dive_site_less_than);
|
2018-01-28 15:21:28 +01:00
|
|
|
locationNames.clear();
|
2018-10-09 09:18:08 +02:00
|
|
|
for (int i = 0; i < dive_site_table.nr; i++)
|
2018-01-28 15:21:28 +01:00
|
|
|
locationNames << QString(dive_site_table.dive_sites[i]->name);
|
2015-06-01 23:13:51 -03:00
|
|
|
endResetModel();
|
2015-05-29 21:19:44 -03:00
|
|
|
}
|
2015-06-01 16:58:23 -03:00
|
|
|
|
2018-01-28 15:21:28 +01:00
|
|
|
QStringList LocationInformationModel::allSiteNames() const
|
|
|
|
{
|
2018-02-17 21:21:16 +01:00
|
|
|
return locationNames;
|
2018-01-28 15:21:28 +01:00
|
|
|
}
|
|
|
|
|
2018-05-21 17:53:42 +02:00
|
|
|
bool LocationInformationModel::removeRows(int row, int, const QModelIndex&)
|
2015-06-22 17:24:15 -03:00
|
|
|
{
|
2015-06-01 23:13:51 -03:00
|
|
|
if(row >= rowCount())
|
|
|
|
return false;
|
|
|
|
|
2018-10-09 09:07:04 +02:00
|
|
|
beginRemoveRows(QModelIndex(), row, row);
|
2019-02-26 22:26:11 +01:00
|
|
|
struct dive_site *ds = get_dive_site(row, &dive_site_table);
|
2015-06-02 15:35:06 +02:00
|
|
|
if (ds)
|
2019-02-26 22:26:11 +01:00
|
|
|
delete_dive_site(ds, &dive_site_table);
|
2015-06-01 23:13:51 -03:00
|
|
|
endRemoveRows();
|
|
|
|
return true;
|
|
|
|
}
|
2015-06-22 17:24:15 -03:00
|
|
|
|
2019-03-10 16:03:39 +01:00
|
|
|
void LocationInformationModel::diveSiteDiveCountChanged(dive_site *ds)
|
|
|
|
{
|
|
|
|
int idx = get_divesite_idx(ds, &dive_site_table);
|
|
|
|
if (idx >= 0)
|
|
|
|
dataChanged(createIndex(idx, NUM_DIVES), createIndex(idx, NUM_DIVES));
|
|
|
|
}
|
|
|
|
|
2018-10-25 08:02:06 +02:00
|
|
|
GeoReferencingOptionsModel *GeoReferencingOptionsModel::instance()
|
|
|
|
{
|
2015-06-22 17:24:15 -03:00
|
|
|
static GeoReferencingOptionsModel *self = new GeoReferencingOptionsModel();
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
GeoReferencingOptionsModel::GeoReferencingOptionsModel(QObject *parent) : QStringListModel(parent)
|
|
|
|
{
|
|
|
|
QStringList list;
|
2015-07-01 12:32:46 -07:00
|
|
|
int i;
|
2015-07-02 10:21:35 -07:00
|
|
|
for (i = 0; i < TC_NR_CATEGORIES; i++)
|
2018-06-17 21:03:16 +02:00
|
|
|
list << gettextFromC::tr(taxonomy_category_names[i]);
|
2015-06-22 17:24:15 -03:00
|
|
|
setStringList(list);
|
|
|
|
}
|
2015-08-31 20:37:49 -03:00
|
|
|
|
2018-10-08 19:01:45 +02:00
|
|
|
bool GPSLocationInformationModel::filterAcceptsRow(int sourceRow, const QModelIndex &parent) const
|
2015-08-31 20:37:49 -03:00
|
|
|
{
|
2018-10-28 21:16:42 +01:00
|
|
|
struct dive_site *ds = sourceModel()->index(sourceRow, LocationInformationModel::DIVESITE, parent).data().value<dive_site *>();
|
2018-10-25 08:02:06 +02:00
|
|
|
if (ds == ignoreDs || ds == RECENTLY_ADDED_DIVESITE)
|
2018-10-08 19:01:45 +02:00
|
|
|
return false;
|
2015-08-31 20:59:13 -03:00
|
|
|
|
2018-10-20 14:12:15 -04:00
|
|
|
return ds && same_location(&ds->location, &location);
|
2018-10-08 19:01:45 +02:00
|
|
|
}
|
2015-08-31 20:59:13 -03:00
|
|
|
|
2018-10-08 19:01:45 +02:00
|
|
|
GPSLocationInformationModel::GPSLocationInformationModel(QObject *parent) : QSortFilterProxyModel(parent),
|
2018-10-25 08:02:06 +02:00
|
|
|
ignoreDs(nullptr),
|
2018-10-20 14:12:15 -04:00
|
|
|
location({{0},{0}})
|
2018-10-08 19:01:45 +02:00
|
|
|
{
|
|
|
|
setSourceModel(LocationInformationModel::instance());
|
|
|
|
}
|
2015-08-31 20:37:49 -03:00
|
|
|
|
2018-10-25 08:02:06 +02:00
|
|
|
void GPSLocationInformationModel::set(const struct dive_site *ignoreDsIn, const location_t &locationIn)
|
2018-10-08 19:01:45 +02:00
|
|
|
{
|
2018-10-25 08:02:06 +02:00
|
|
|
ignoreDs = ignoreDsIn;
|
2018-10-20 14:12:15 -04:00
|
|
|
location = locationIn;
|
2018-10-08 19:01:45 +02:00
|
|
|
invalidate();
|
|
|
|
}
|
2015-10-09 14:33:31 -03:00
|
|
|
|
2018-10-20 14:12:15 -04:00
|
|
|
void GPSLocationInformationModel::setCoordinates(const location_t &locationIn)
|
2018-10-08 19:01:45 +02:00
|
|
|
{
|
2018-10-20 14:12:15 -04:00
|
|
|
location = locationIn;
|
2018-10-08 19:01:45 +02:00
|
|
|
invalidate();
|
2015-11-07 22:56:55 +02:00
|
|
|
}
|