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"
|
2018-02-28 22:37:09 +00:00
|
|
|
#include "core/qthelper.h"
|
2015-06-02 01:30:33 +00:00
|
|
|
#include <QDebug>
|
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
|
|
|
|
2017-11-26 22:48:41 +00:00
|
|
|
static bool dive_site_less_than(dive_site *a, dive_site *b)
|
2015-05-31 20:56:53 +00:00
|
|
|
{
|
2017-11-26 22:48:41 +00:00
|
|
|
return QString(a->name) < QString(b->name);
|
2015-05-31 20:56:53 +00:00
|
|
|
}
|
|
|
|
|
2015-05-30 01:22:24 +00:00
|
|
|
LocationInformationModel *LocationInformationModel::instance()
|
|
|
|
{
|
|
|
|
static LocationInformationModel *self = new LocationInformationModel();
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
2015-07-14 21:43:47 +00:00
|
|
|
LocationInformationModel::LocationInformationModel(QObject *obj) : QAbstractTableModel(obj),
|
|
|
|
internalRowCount(0),
|
|
|
|
textField(NULL)
|
2015-05-30 00:19:44 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2018-05-21 15:53:42 +00:00
|
|
|
int LocationInformationModel::columnCount(const QModelIndex&) const
|
2015-07-01 21:46:27 +00:00
|
|
|
{
|
|
|
|
return COLUMNS;
|
|
|
|
}
|
|
|
|
|
2018-05-21 15:53:42 +00:00
|
|
|
int LocationInformationModel::rowCount(const QModelIndex&) const
|
2015-05-30 00:19:44 +00:00
|
|
|
{
|
2015-07-16 18:19:31 +00:00
|
|
|
return internalRowCount + 2;
|
2015-05-30 00:19:44 +00:00
|
|
|
}
|
|
|
|
|
2015-07-16 21:08:08 +00:00
|
|
|
static struct dive_site *get_dive_site_name_start_which_str(const QString& str) {
|
|
|
|
struct dive_site *ds;
|
|
|
|
int i;
|
|
|
|
for_each_dive_site(i,ds) {
|
|
|
|
QString dsName(ds->name);
|
|
|
|
if (dsName.startsWith(str)) {
|
|
|
|
return ds;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2015-05-30 00:19:44 +00:00
|
|
|
QVariant LocationInformationModel::data(const QModelIndex &index, int role) const
|
|
|
|
{
|
|
|
|
if (!index.isValid())
|
|
|
|
return QVariant();
|
2015-07-14 21:43:47 +00:00
|
|
|
|
|
|
|
// Special case to handle the 'create dive site' with name.
|
2015-07-16 18:19:31 +00:00
|
|
|
if (index.row() < 2) {
|
2015-07-14 21:43:47 +00:00
|
|
|
if (index.column() == UUID)
|
2015-08-26 00:03:20 +00:00
|
|
|
return RECENTLY_ADDED_DIVESITE;
|
2015-07-14 21:43:47 +00:00
|
|
|
switch(role) {
|
|
|
|
case Qt::DisplayRole : {
|
2015-07-16 18:19:31 +00:00
|
|
|
if (index.row() == 1) {
|
2015-07-16 21:08:08 +00:00
|
|
|
struct dive_site *ds = get_dive_site_name_start_which_str(textField->text());
|
|
|
|
if(ds)
|
|
|
|
return ds->name;
|
2015-07-14 21:43:47 +00:00
|
|
|
}
|
|
|
|
return textField->text();
|
|
|
|
}
|
|
|
|
case Qt::ToolTipRole : {
|
2015-07-15 17:37:09 +00:00
|
|
|
return QString(tr("Create dive site with this name"));
|
2015-07-14 21:43:47 +00:00
|
|
|
}
|
2015-07-16 21:08:08 +00:00
|
|
|
case Qt::EditRole : {
|
|
|
|
if (index.row() == 1) {
|
|
|
|
struct dive_site *ds = get_dive_site_name_start_which_str(textField->text());
|
|
|
|
if (!ds)
|
2015-07-17 15:39:49 +00:00
|
|
|
return INVALID_DIVE_SITE_NAME;
|
2015-07-16 21:08:08 +00:00
|
|
|
if (QString(ds->name) == textField->text())
|
2015-07-17 15:39:49 +00:00
|
|
|
return INVALID_DIVE_SITE_NAME;
|
|
|
|
|
2015-07-16 21:08:08 +00:00
|
|
|
}
|
|
|
|
return textField->text();
|
|
|
|
}
|
2017-11-29 09:57:08 +00:00
|
|
|
case Qt::DecorationRole : return QIcon(":list-add-icon");
|
2015-07-14 21:43:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-16 18:19:31 +00:00
|
|
|
// The dive sites are -2 because of the first two items.
|
|
|
|
struct dive_site *ds = get_dive_site(index.row() - 2);
|
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:
|
2015-07-01 21:46:27 +00:00
|
|
|
case Qt::DisplayRole :
|
|
|
|
switch(index.column()) {
|
|
|
|
case UUID: return ds->uuid;
|
2015-09-04 10:06:31 +00:00
|
|
|
case NAME: return ds->name;
|
2015-07-01 21:46:27 +00:00
|
|
|
case LATITUDE: return ds->latitude.udeg;
|
|
|
|
case LONGITUDE: return ds->longitude.udeg;
|
|
|
|
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;
|
2015-07-14 21:43:47 +00:00
|
|
|
case Qt::DecorationRole : {
|
|
|
|
if (dive_site_has_gps_location(ds))
|
2017-11-29 09:57:08 +00:00
|
|
|
return QIcon(":geotag-icon");
|
2015-07-14 21:43:47 +00:00
|
|
|
else
|
|
|
|
return QVariant();
|
|
|
|
}
|
2015-09-01 00:35:17 +00:00
|
|
|
case UUID_ROLE:
|
|
|
|
return ds->uuid;
|
2015-05-30 00:19:44 +00:00
|
|
|
}
|
|
|
|
return QVariant();
|
|
|
|
}
|
|
|
|
|
2015-07-14 21:43:47 +00:00
|
|
|
void LocationInformationModel::setFirstRowTextField(QLineEdit *t)
|
|
|
|
{
|
|
|
|
textField = t;
|
|
|
|
}
|
|
|
|
|
2015-05-30 00:19:44 +00:00
|
|
|
void LocationInformationModel::update()
|
|
|
|
{
|
2015-06-02 02:13:51 +00:00
|
|
|
beginResetModel();
|
|
|
|
internalRowCount = dive_site_table.nr;
|
2015-06-04 15:45:45 +00:00
|
|
|
qSort(dive_site_table.dive_sites, dive_site_table.dive_sites + dive_site_table.nr, dive_site_less_than);
|
2018-01-28 14:21:28 +00:00
|
|
|
locationNames.clear();
|
|
|
|
for (int i = 0; i < internalRowCount; i++)
|
|
|
|
locationNames << QString(dive_site_table.dive_sites[i]->name);
|
2015-06-02 02:13:51 +00:00
|
|
|
endResetModel();
|
2015-05-30 00:19:44 +00:00
|
|
|
}
|
2015-06-01 19:58:23 +00:00
|
|
|
|
2018-01-28 14:21:28 +00:00
|
|
|
QStringList LocationInformationModel::allSiteNames() const
|
|
|
|
{
|
2018-02-17 20:21:16 +00:00
|
|
|
return locationNames;
|
2018-01-28 14:21:28 +00:00
|
|
|
}
|
|
|
|
|
2015-06-02 01:16:07 +00:00
|
|
|
bool LocationInformationModel::setData(const QModelIndex &index, const QVariant &value, int role)
|
|
|
|
{
|
2015-07-16 18:19:31 +00:00
|
|
|
if (!index.isValid() || index.row() < 2)
|
2015-06-02 01:16:07 +00:00
|
|
|
return false;
|
|
|
|
|
|
|
|
if (role != Qt::EditRole)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
struct dive_site *ds = get_dive_site(index.row());
|
|
|
|
free(ds->name);
|
2018-02-28 22:37:09 +00:00
|
|
|
ds->name = copy_qstring(value.toString());
|
2015-06-02 01:16:07 +00:00
|
|
|
emit dataChanged(index, index);
|
|
|
|
return true;
|
|
|
|
}
|
2015-06-02 02:13:51 +00:00
|
|
|
|
2018-05-21 15:53:42 +00:00
|
|
|
bool LocationInformationModel::removeRows(int row, int, const QModelIndex&)
|
2015-06-22 20:24:15 +00:00
|
|
|
{
|
2015-06-02 02:13:51 +00:00
|
|
|
if(row >= rowCount())
|
|
|
|
return false;
|
|
|
|
|
2015-07-16 18:19:31 +00:00
|
|
|
beginRemoveRows(QModelIndex(), row + 2, row + 2);
|
2015-06-02 02:13:51 +00:00
|
|
|
struct dive_site *ds = get_dive_site(row);
|
2015-06-02 13:35:06 +00:00
|
|
|
if (ds)
|
|
|
|
delete_dive_site(ds->uuid);
|
2015-06-04 03:13:09 +00:00
|
|
|
internalRowCount = dive_site_table.nr;
|
2015-06-02 02:13:51 +00:00
|
|
|
endRemoveRows();
|
|
|
|
return true;
|
|
|
|
}
|
2015-06-22 20:24:15 +00:00
|
|
|
|
|
|
|
GeoReferencingOptionsModel *GeoReferencingOptionsModel::instance() {
|
|
|
|
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++)
|
2017-04-25 21:16:46 +00:00
|
|
|
list << gettextFromC::instance()->trGettext(taxonomy_category_names[i]);
|
2015-06-22 20:24:15 +00:00
|
|
|
setStringList(list);
|
|
|
|
}
|
2015-08-31 23:37:49 +00:00
|
|
|
|
|
|
|
bool filter_same_gps_cb (QAbstractItemModel *model, int sourceRow, const QModelIndex& parent)
|
|
|
|
{
|
|
|
|
int ref_lat = displayed_dive_site.latitude.udeg;
|
|
|
|
int ref_lon = displayed_dive_site.longitude.udeg;
|
2015-11-07 20:56:55 +00:00
|
|
|
uint32_t ref_uuid = displayed_dive_site.uuid;
|
2015-08-31 23:59:13 +00:00
|
|
|
QSortFilterProxyModel *self = (QSortFilterProxyModel*) model;
|
|
|
|
|
2015-11-07 20:56:55 +00:00
|
|
|
uint32_t ds_uuid = self->sourceModel()->index(sourceRow, LocationInformationModel::UUID, parent).data().toUInt();
|
2015-08-31 23:59:13 +00:00
|
|
|
struct dive_site *ds = get_dive_site_by_uuid(ds_uuid);
|
|
|
|
|
|
|
|
if (!ds)
|
|
|
|
return false;
|
2015-08-31 23:37:49 +00:00
|
|
|
|
2015-10-09 17:33:31 +00:00
|
|
|
if (ds->latitude.udeg == 0 || ds->longitude.udeg == 0)
|
|
|
|
return false;
|
|
|
|
|
2018-02-17 20:21:16 +00:00
|
|
|
return ds->latitude.udeg == ref_lat && ds->longitude.udeg == ref_lon && ds->uuid != ref_uuid;
|
2015-11-07 20:56:55 +00:00
|
|
|
}
|