2017-04-27 18:25:32 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0
|
2016-04-05 05:02:03 +00:00
|
|
|
#include "core/units.h"
|
|
|
|
#include "qt-models/divelocationmodel.h"
|
2019-03-10 15:03:39 +00:00
|
|
|
#include "core/subsurface-qt/DiveListNotifier.h"
|
2018-02-28 22:37:09 +00:00
|
|
|
#include "core/qthelper.h"
|
2019-03-04 22:20:29 +00:00
|
|
|
#include "core/divesite.h"
|
2019-03-09 21:32:16 +00:00
|
|
|
#include "core/metrics.h"
|
2019-03-12 21:35:43 +00:00
|
|
|
#ifndef SUBSURFACE_MOBILE
|
2019-03-16 10:35:44 +00:00
|
|
|
#include "cleanertablemodel.h" // for trashIcon() and editIcon()
|
2019-11-13 14:08:40 +00:00
|
|
|
#include "commands/command.h"
|
2019-03-12 21:35:43 +00:00
|
|
|
#endif
|
2015-07-14 21:43:47 +00:00
|
|
|
#include <QLineEdit>
|
|
|
|
#include <QIcon>
|
2017-04-25 21:16:46 +00:00
|
|
|
#include <core/gettextfromc.h>
|
2015-05-30 00:19:44 +00:00
|
|
|
|
2015-05-30 01:22:24 +00:00
|
|
|
LocationInformationModel *LocationInformationModel::instance()
|
|
|
|
{
|
|
|
|
static LocationInformationModel *self = new LocationInformationModel();
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
2018-10-09 07:18:08 +00:00
|
|
|
LocationInformationModel::LocationInformationModel(QObject *obj) : QAbstractTableModel(obj)
|
2015-05-30 00:19:44 +00:00
|
|
|
{
|
2019-03-10 15:03:39 +00:00
|
|
|
connect(&diveListNotifier, &DiveListNotifier::diveSiteDiveCountChanged, this, &LocationInformationModel::diveSiteDiveCountChanged);
|
2019-03-11 23:25:31 +00:00
|
|
|
connect(&diveListNotifier, &DiveListNotifier::diveSiteAdded, this, &LocationInformationModel::diveSiteAdded);
|
|
|
|
connect(&diveListNotifier, &DiveListNotifier::diveSiteDeleted, this, &LocationInformationModel::diveSiteDeleted);
|
2019-03-12 22:51:39 +00:00
|
|
|
connect(&diveListNotifier, &DiveListNotifier::diveSiteChanged, this, &LocationInformationModel::diveSiteChanged);
|
2019-03-20 20:46:58 +00:00
|
|
|
connect(&diveListNotifier, &DiveListNotifier::diveSiteDivesChanged, this, &LocationInformationModel::diveSiteDivesChanged);
|
2015-05-30 00:19:44 +00:00
|
|
|
}
|
|
|
|
|
2019-03-09 21:32:16 +00:00
|
|
|
int LocationInformationModel::columnCount(const QModelIndex &) const
|
2015-07-01 21:46:27 +00:00
|
|
|
{
|
|
|
|
return COLUMNS;
|
|
|
|
}
|
|
|
|
|
2019-03-09 21:32:16 +00:00
|
|
|
int LocationInformationModel::rowCount(const QModelIndex &) const
|
2015-05-30 00:19:44 +00:00
|
|
|
{
|
2018-10-09 07:18:08 +00:00
|
|
|
return dive_site_table.nr;
|
2015-07-16 21:08:08 +00:00
|
|
|
}
|
|
|
|
|
2019-03-09 21:32:16 +00: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 10:26:58 +00:00
|
|
|
QVariant LocationInformationModel::getDiveSiteData(const struct dive_site *ds, int column, int role)
|
2015-05-30 00:19:44 +00:00
|
|
|
{
|
2015-06-02 13:35:06 +00:00
|
|
|
if (!ds)
|
|
|
|
return QVariant();
|
|
|
|
|
2015-05-30 00:19:44 +00:00
|
|
|
switch(role) {
|
2015-07-14 21:43:47 +00:00
|
|
|
case Qt::EditRole:
|
2019-03-09 21:32:16 +00:00
|
|
|
case Qt::DisplayRole:
|
2018-10-09 10:26:58 +00:00
|
|
|
switch(column) {
|
2018-10-28 20:16:42 +00:00
|
|
|
case DIVESITE: return QVariant::fromValue<dive_site *>((dive_site *)ds); // Not nice: casting away const
|
2015-09-04 10:06:31 +00:00
|
|
|
case NAME: return ds->name;
|
2019-03-09 21:32:16 +00:00
|
|
|
case NUM_DIVES: return ds->dives.nr;
|
2019-03-14 21:07:48 +00:00
|
|
|
case LOCATION: return "TODO";
|
2015-07-01 21:46:27 +00:00
|
|
|
case DESCRIPTION: return ds->description;
|
|
|
|
case NOTES: return ds->name;
|
2019-03-14 07:26:50 +00:00
|
|
|
case TAXONOMY: return "TODO";
|
2015-07-01 21:46:27 +00:00
|
|
|
}
|
|
|
|
break;
|
2019-03-09 21:32:16 +00:00
|
|
|
case Qt::ToolTipRole:
|
|
|
|
switch(column) {
|
2019-03-16 10:35:44 +00:00
|
|
|
case EDIT: return tr("Click here to edit the divesite.");
|
2019-03-09 21:32:16 +00:00
|
|
|
case REMOVE: return tr("Clicking here will remove this divesite.");
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case Qt::DecorationRole:
|
|
|
|
switch(column) {
|
|
|
|
#ifndef SUBSURFACE_MOBILE
|
2019-03-16 10:35:44 +00:00
|
|
|
case EDIT: return editIcon();
|
2019-03-09 21:32:16 +00:00
|
|
|
case REMOVE: return trashIcon();
|
|
|
|
#endif
|
|
|
|
case NAME: return dive_site_has_gps_location(ds) ? QIcon(":geotag-icon") : QVariant();
|
|
|
|
}
|
|
|
|
break;
|
2018-10-24 14:34:43 +00:00
|
|
|
case DIVESITE_ROLE:
|
2018-10-28 20:16:42 +00:00
|
|
|
return QVariant::fromValue<dive_site *>((dive_site *)ds); // Not nice: casting away const
|
2015-05-30 00:19:44 +00:00
|
|
|
}
|
|
|
|
return QVariant();
|
|
|
|
}
|
|
|
|
|
2018-10-09 10:26:58 +00:00
|
|
|
QVariant LocationInformationModel::data(const QModelIndex &index, int role) const
|
|
|
|
{
|
|
|
|
if (!index.isValid())
|
|
|
|
return QVariant();
|
|
|
|
|
2019-02-26 21:26:11 +00:00
|
|
|
struct dive_site *ds = get_dive_site(index.row(), &dive_site_table);
|
2018-10-09 10:26:58 +00:00
|
|
|
return getDiveSiteData(ds, index.column(), role);
|
|
|
|
}
|
|
|
|
|
2015-05-30 00:19:44 +00:00
|
|
|
void LocationInformationModel::update()
|
|
|
|
{
|
2015-06-02 02:13:51 +00:00
|
|
|
beginResetModel();
|
|
|
|
endResetModel();
|
2015-05-30 00:19:44 +00:00
|
|
|
}
|
2015-06-01 19:58:23 +00:00
|
|
|
|
2019-03-10 15:03:39 +00: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));
|
|
|
|
}
|
|
|
|
|
2019-03-11 23:25:31 +00:00
|
|
|
void LocationInformationModel::diveSiteAdded(struct dive_site *, int idx)
|
|
|
|
{
|
|
|
|
if (idx < 0)
|
|
|
|
return;
|
|
|
|
beginInsertRows(QModelIndex(), idx, idx);
|
|
|
|
// Row has already been added by Undo-Command.
|
|
|
|
endInsertRows();
|
|
|
|
}
|
|
|
|
|
|
|
|
void LocationInformationModel::diveSiteDeleted(struct dive_site *, int idx)
|
|
|
|
{
|
|
|
|
if (idx < 0)
|
|
|
|
return;
|
|
|
|
beginRemoveRows(QModelIndex(), idx, idx);
|
|
|
|
// Row has already been added by Undo-Command.
|
|
|
|
endRemoveRows();
|
|
|
|
}
|
|
|
|
|
2019-03-12 22:51:39 +00:00
|
|
|
void LocationInformationModel::diveSiteChanged(struct dive_site *ds, int field)
|
|
|
|
{
|
|
|
|
int idx = get_divesite_idx(ds, &dive_site_table);
|
|
|
|
if (idx < 0)
|
|
|
|
return;
|
|
|
|
dataChanged(createIndex(idx, field), createIndex(idx, field));
|
|
|
|
}
|
|
|
|
|
2019-03-20 20:46:58 +00:00
|
|
|
void LocationInformationModel::diveSiteDivesChanged(struct dive_site *ds)
|
|
|
|
{
|
|
|
|
int idx = get_divesite_idx(ds, &dive_site_table);
|
|
|
|
if (idx < 0)
|
|
|
|
return;
|
|
|
|
dataChanged(createIndex(idx, NUM_DIVES), createIndex(idx, NUM_DIVES));
|
|
|
|
}
|
|
|
|
|
2019-03-12 16:28:43 +00:00
|
|
|
bool DiveSiteSortedModel::filterAcceptsRow(int sourceRow, const QModelIndex &source_parent) const
|
|
|
|
{
|
2019-03-24 16:11:29 +00:00
|
|
|
if (fullText.isEmpty())
|
|
|
|
return true;
|
|
|
|
|
|
|
|
if (sourceRow < 0 || sourceRow > dive_site_table.nr)
|
|
|
|
return false;
|
|
|
|
struct dive_site *ds = dive_site_table.dive_sites[sourceRow];
|
|
|
|
QString text = QString(ds->name) + QString(ds->description) + QString(ds->notes);
|
|
|
|
return text.contains(fullText, Qt::CaseInsensitive);
|
2019-03-12 16:28:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool DiveSiteSortedModel::lessThan(const QModelIndex &i1, const QModelIndex &i2) const
|
|
|
|
{
|
|
|
|
// The source indices correspond to indices in the global dive site table.
|
|
|
|
// Let's access them directly without going via the source model.
|
|
|
|
// Kind of dirty, but less effort.
|
|
|
|
struct dive_site *ds1 = get_dive_site(i1.row(), &dive_site_table);
|
|
|
|
struct dive_site *ds2 = get_dive_site(i2.row(), &dive_site_table);
|
|
|
|
if (!ds1 || !ds2) // Invalid dive sites compare as different
|
|
|
|
return false;
|
|
|
|
switch (i1.column()) {
|
|
|
|
case LocationInformationModel::NAME:
|
|
|
|
default:
|
|
|
|
return QString::localeAwareCompare(QString(ds1->name), QString(ds2->name)) < 0; // TODO: avoid copy
|
|
|
|
case LocationInformationModel::DESCRIPTION: {
|
|
|
|
int cmp = QString::localeAwareCompare(QString(ds1->description), QString(ds2->description)); // TODO: avoid copy
|
|
|
|
return cmp != 0 ? cmp < 0 :
|
|
|
|
QString::localeAwareCompare(QString(ds1->name), QString(ds2->name)) < 0; // TODO: avoid copy
|
|
|
|
}
|
|
|
|
case LocationInformationModel::NUM_DIVES: {
|
|
|
|
int cmp = ds1->dives.nr - ds2->dives.nr;
|
|
|
|
// Since by default nr dives is descending, invert sort direction of names, such that
|
|
|
|
// the names are listed as ascending.
|
|
|
|
return cmp != 0 ? cmp < 0 :
|
|
|
|
QString::localeAwareCompare(QString(ds1->name), QString(ds2->name)) < 0; // TODO: avoid copy
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
DiveSiteSortedModel::DiveSiteSortedModel()
|
|
|
|
{
|
|
|
|
setSourceModel(LocationInformationModel::instance());
|
|
|
|
}
|
|
|
|
|
|
|
|
QStringList DiveSiteSortedModel::allSiteNames() const
|
|
|
|
{
|
|
|
|
QStringList locationNames;
|
|
|
|
int num = rowCount();
|
|
|
|
for (int i = 0; i < num; i++) {
|
|
|
|
int idx = mapToSource(index(i, 0)).row();
|
2019-08-10 15:28:24 +00:00
|
|
|
// This shouldn't happen, but if model and core get out of sync,
|
|
|
|
// (more precisely: the core has more sites than the model is aware of),
|
|
|
|
// we might get an invalid index.
|
|
|
|
if (idx < 0 || idx > dive_site_table.nr) {
|
|
|
|
fprintf(stderr, "DiveSiteSortedModel::allSiteNames(): invalid index");
|
|
|
|
continue;
|
|
|
|
}
|
2019-03-12 16:28:43 +00:00
|
|
|
locationNames << QString(dive_site_table.dive_sites[idx]->name);
|
|
|
|
}
|
|
|
|
return locationNames;
|
|
|
|
}
|
|
|
|
|
2019-03-12 22:51:39 +00:00
|
|
|
struct dive_site *DiveSiteSortedModel::getDiveSite(const QModelIndex &idx)
|
|
|
|
{
|
|
|
|
return get_dive_site(mapToSource(idx).row(), &dive_site_table);
|
|
|
|
}
|
|
|
|
|
2019-03-12 21:35:43 +00:00
|
|
|
#ifndef SUBSURFACE_MOBILE
|
2019-03-12 22:51:39 +00:00
|
|
|
bool DiveSiteSortedModel::setData(const QModelIndex &index, const QVariant &value, int role)
|
|
|
|
{
|
|
|
|
struct dive_site *ds = getDiveSite(index);
|
|
|
|
if (!ds || value.isNull())
|
|
|
|
return false;
|
|
|
|
switch (index.column()) {
|
|
|
|
case LocationInformationModel::NAME:
|
|
|
|
Command::editDiveSiteName(ds, value.toString());
|
|
|
|
return true;
|
2019-03-13 19:10:22 +00:00
|
|
|
case LocationInformationModel::DESCRIPTION:
|
|
|
|
Command::editDiveSiteDescription(ds, value.toString());
|
|
|
|
return true;
|
2019-03-12 22:51:39 +00:00
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif // SUBSURFACE_MOBILE
|
2019-03-12 21:35:43 +00:00
|
|
|
|
2019-03-24 16:11:29 +00:00
|
|
|
void DiveSiteSortedModel::setFilter(const QString &text)
|
|
|
|
{
|
|
|
|
fullText = text.trimmed();
|
|
|
|
invalidateFilter();
|
|
|
|
}
|
|
|
|
|
2018-10-25 06:02:06 +00:00
|
|
|
GeoReferencingOptionsModel *GeoReferencingOptionsModel::instance()
|
|
|
|
{
|
2015-06-22 20:24:15 +00:00
|
|
|
static GeoReferencingOptionsModel *self = new GeoReferencingOptionsModel();
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
GeoReferencingOptionsModel::GeoReferencingOptionsModel(QObject *parent) : QStringListModel(parent)
|
|
|
|
{
|
|
|
|
QStringList list;
|
2015-07-01 19:32:46 +00:00
|
|
|
int i;
|
2015-07-02 17:21:35 +00:00
|
|
|
for (i = 0; i < TC_NR_CATEGORIES; i++)
|
2018-06-17 19:03:16 +00:00
|
|
|
list << gettextFromC::tr(taxonomy_category_names[i]);
|
2015-06-22 20:24:15 +00:00
|
|
|
setStringList(list);
|
|
|
|
}
|
2015-08-31 23:37:49 +00:00
|
|
|
|
2018-10-08 17:01:45 +00:00
|
|
|
bool GPSLocationInformationModel::filterAcceptsRow(int sourceRow, const QModelIndex &parent) const
|
2015-08-31 23:37:49 +00:00
|
|
|
{
|
2018-10-28 20:16:42 +00:00
|
|
|
struct dive_site *ds = sourceModel()->index(sourceRow, LocationInformationModel::DIVESITE, parent).data().value<dive_site *>();
|
2019-03-25 21:18:32 +00:00
|
|
|
if (!ds || ds == ignoreDs || ds == RECENTLY_ADDED_DIVESITE)
|
2018-10-08 17:01:45 +00:00
|
|
|
return false;
|
2015-08-31 23:59:13 +00:00
|
|
|
|
2019-03-25 21:18:32 +00:00
|
|
|
return distance <= 0 ? same_location(&ds->location, &location)
|
|
|
|
: (int64_t)get_distance(&ds->location, &location) * 1000 <= distance; // We need 64 bit to represent distances in mm
|
2018-10-08 17:01:45 +00:00
|
|
|
}
|
2015-08-31 23:59:13 +00:00
|
|
|
|
2018-10-08 17:01:45 +00:00
|
|
|
GPSLocationInformationModel::GPSLocationInformationModel(QObject *parent) : QSortFilterProxyModel(parent),
|
2018-10-25 06:02:06 +00:00
|
|
|
ignoreDs(nullptr),
|
2019-03-25 21:18:32 +00:00
|
|
|
location({{0},{0}}),
|
|
|
|
distance(0)
|
2018-10-08 17:01:45 +00:00
|
|
|
{
|
|
|
|
setSourceModel(LocationInformationModel::instance());
|
|
|
|
}
|
2015-08-31 23:37:49 +00:00
|
|
|
|
2018-10-25 06:02:06 +00:00
|
|
|
void GPSLocationInformationModel::set(const struct dive_site *ignoreDsIn, const location_t &locationIn)
|
2018-10-08 17:01:45 +00:00
|
|
|
{
|
2018-10-25 06:02:06 +00:00
|
|
|
ignoreDs = ignoreDsIn;
|
2018-10-20 18:12:15 +00:00
|
|
|
location = locationIn;
|
2018-10-08 17:01:45 +00:00
|
|
|
invalidate();
|
|
|
|
}
|
2015-10-09 17:33:31 +00:00
|
|
|
|
2018-10-20 18:12:15 +00:00
|
|
|
void GPSLocationInformationModel::setCoordinates(const location_t &locationIn)
|
2018-10-08 17:01:45 +00:00
|
|
|
{
|
2018-10-20 18:12:15 +00:00
|
|
|
location = locationIn;
|
2018-10-08 17:01:45 +00:00
|
|
|
invalidate();
|
2015-11-07 20:56:55 +00:00
|
|
|
}
|
2019-03-25 21:18:32 +00:00
|
|
|
|
|
|
|
void GPSLocationInformationModel::setDistance(int64_t dist)
|
|
|
|
{
|
|
|
|
distance = dist;
|
|
|
|
invalidate();
|
|
|
|
}
|