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-03-12 21:35:43 +00:00
|
|
|
#include "desktop-widgets/mainwindow.h" // to place message box
|
|
|
|
#include "desktop-widgets/command.h"
|
|
|
|
#include <QMessageBox>
|
|
|
|
#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);
|
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-12 16:28:43 +00:00
|
|
|
bool DiveSiteSortedModel::filterAcceptsRow(int sourceRow, const QModelIndex &source_parent) const
|
|
|
|
{
|
|
|
|
// TODO: filtering
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
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();
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-16 10:35:44 +00:00
|
|
|
// TODO: Remove or edit. It doesn't make sense to call the model here, which calls the undo command,
|
2019-03-12 21:35:43 +00:00
|
|
|
// which in turn calls the model.
|
|
|
|
void DiveSiteSortedModel::remove(const QModelIndex &index)
|
|
|
|
{
|
2019-03-12 22:51:39 +00:00
|
|
|
struct dive_site *ds = getDiveSite(index);
|
2019-03-12 21:35:43 +00:00
|
|
|
if (!ds)
|
|
|
|
return;
|
2019-03-16 10:35:44 +00:00
|
|
|
switch (index.column()) {
|
|
|
|
case LocationInformationModel::EDIT:
|
|
|
|
MainWindow::instance()->editDiveSite(ds);
|
|
|
|
break;
|
|
|
|
case LocationInformationModel::REMOVE:
|
|
|
|
if (ds->dives.nr > 0 &&
|
|
|
|
QMessageBox::warning(MainWindow::instance(), tr("Delete dive site?"),
|
|
|
|
tr("This dive site has %n dive(s). Do you really want to delete it?\n", "", ds->dives.nr),
|
|
|
|
QMessageBox::Yes|QMessageBox::No) == QMessageBox::No)
|
|
|
|
return;
|
|
|
|
Command::deleteDiveSites(QVector<dive_site *>{ds});
|
|
|
|
break;
|
|
|
|
}
|
2019-03-12 21:35:43 +00:00
|
|
|
}
|
2019-03-12 22:51:39 +00:00
|
|
|
#endif // SUBSURFACE_MOBILE
|
2019-03-12 21:35:43 +00:00
|
|
|
|
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 *>();
|
2018-10-25 06:02:06 +00:00
|
|
|
if (ds == ignoreDs || ds == RECENTLY_ADDED_DIVESITE)
|
2018-10-08 17:01:45 +00:00
|
|
|
return false;
|
2015-08-31 23:59:13 +00:00
|
|
|
|
2018-10-20 18:12:15 +00:00
|
|
|
return ds && same_location(&ds->location, &location);
|
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),
|
2018-10-20 18:12:15 +00:00
|
|
|
location({{0},{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
|
|
|
}
|