2017-07-29 05:01:33 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0
|
|
|
|
#include <QQmlContext>
|
|
|
|
#include <QDebug>
|
|
|
|
#include <QQuickItem>
|
|
|
|
#include <QModelIndex>
|
|
|
|
|
|
|
|
#include "mapwidget.h"
|
|
|
|
#include "core/divesite.h"
|
2020-04-25 18:54:12 +00:00
|
|
|
#include "core/selection.h"
|
2017-11-04 19:23:37 +00:00
|
|
|
#include "map-widget/qmlmapwidgethelper.h"
|
2017-07-29 05:01:33 +00:00
|
|
|
#include "qt-models/maplocationmodel.h"
|
2018-10-25 06:02:06 +00:00
|
|
|
#include "qt-models/divelocationmodel.h"
|
2019-11-13 14:08:40 +00:00
|
|
|
#include "commands/command.h"
|
2017-07-29 05:01:33 +00:00
|
|
|
|
2018-06-20 14:06:49 +00:00
|
|
|
static const QUrl urlMapWidget = QUrl(QStringLiteral("qrc:/qml/MapWidget.qml"));
|
|
|
|
static const QUrl urlMapWidgetError = QUrl(QStringLiteral("qrc:/qml/MapWidgetError.qml"));
|
2017-10-07 09:08:18 +00:00
|
|
|
static bool isReady = false;
|
2017-07-29 05:01:33 +00:00
|
|
|
|
2017-10-07 09:08:18 +00:00
|
|
|
#define CHECK_IS_READY_RETURN_VOID() \
|
|
|
|
if (!isReady) return
|
|
|
|
|
2017-07-29 05:01:33 +00:00
|
|
|
MapWidget *MapWidget::m_instance = NULL;
|
|
|
|
|
|
|
|
MapWidget::MapWidget(QWidget *parent) : QQuickWidget(parent)
|
|
|
|
{
|
2019-04-01 21:11:19 +00:00
|
|
|
m_rootItem = nullptr;
|
|
|
|
m_mapHelper = nullptr;
|
2017-10-07 09:08:18 +00:00
|
|
|
setResizeMode(QQuickWidget::SizeRootObjectToView);
|
2017-07-29 04:19:00 +00:00
|
|
|
connect(this, &QQuickWidget::statusChanged, this, &MapWidget::doneLoading);
|
2019-05-03 10:10:19 +00:00
|
|
|
connect(&diveListNotifier, &DiveListNotifier::divesChanged, this, &MapWidget::divesChanged);
|
2017-10-07 09:08:18 +00:00
|
|
|
setSource(urlMapWidget);
|
2017-07-29 04:19:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void MapWidget::doneLoading(QQuickWidget::Status status)
|
|
|
|
{
|
2017-10-07 09:08:18 +00:00
|
|
|
// the default map widget QML failed; load the error QML.
|
|
|
|
if (source() == urlMapWidget && status != QQuickWidget::Ready) {
|
|
|
|
qDebug() << urlMapWidget << "failed to load with status:" << status;
|
|
|
|
setSource(urlMapWidgetError);
|
|
|
|
return;
|
|
|
|
} else if (source() == urlMapWidgetError) { // the error QML finished loading.
|
2017-07-29 04:19:00 +00:00
|
|
|
return;
|
|
|
|
}
|
2017-07-29 05:01:33 +00:00
|
|
|
|
2017-10-07 09:08:18 +00:00
|
|
|
isReady = true;
|
2017-07-29 05:01:33 +00:00
|
|
|
m_rootItem = qobject_cast<QQuickItem *>(rootObject());
|
|
|
|
m_mapHelper = rootObject()->findChild<MapWidgetHelper *>();
|
2019-05-02 20:41:24 +00:00
|
|
|
connect(m_mapHelper, &MapWidgetHelper::selectedDivesChanged, this, &MapWidget::selectedDivesChanged);
|
2019-03-14 22:28:45 +00:00
|
|
|
connect(m_mapHelper, &MapWidgetHelper::coordinatesChanged, this, &MapWidget::coordinatesChanged);
|
2017-07-29 05:01:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void MapWidget::centerOnDiveSite(struct dive_site *ds)
|
|
|
|
{
|
2017-10-07 09:08:18 +00:00
|
|
|
CHECK_IS_READY_RETURN_VOID();
|
2019-05-05 12:58:04 +00:00
|
|
|
m_mapHelper->centerOnDiveSite(ds);
|
2017-07-29 05:01:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void MapWidget::centerOnIndex(const QModelIndex& idx)
|
|
|
|
{
|
2017-10-07 09:08:18 +00:00
|
|
|
CHECK_IS_READY_RETURN_VOID();
|
2018-10-28 20:16:42 +00:00
|
|
|
dive_site *ds = idx.model()->index(idx.row(), LocationInformationModel::DIVESITE).data().value<dive_site *>();
|
2018-10-25 06:02:06 +00:00
|
|
|
if (!ds || ds == RECENTLY_ADDED_DIVESITE || !dive_site_has_gps_location(ds))
|
2019-05-03 10:18:25 +00:00
|
|
|
m_mapHelper->centerOnSelectedDiveSite();
|
2017-07-29 05:01:33 +00:00
|
|
|
else
|
|
|
|
centerOnDiveSite(ds);
|
|
|
|
}
|
|
|
|
|
|
|
|
void MapWidget::reload()
|
|
|
|
{
|
2017-10-07 09:08:18 +00:00
|
|
|
CHECK_IS_READY_RETURN_VOID();
|
2019-05-05 12:58:04 +00:00
|
|
|
m_mapHelper->reloadMapLocations();
|
|
|
|
m_mapHelper->centerOnSelectedDiveSite();
|
2017-07-29 05:01:33 +00:00
|
|
|
}
|
|
|
|
|
2019-08-30 13:25:59 +00:00
|
|
|
bool MapWidget::editMode() const
|
|
|
|
{
|
|
|
|
return isReady && m_mapHelper->editMode();
|
|
|
|
}
|
|
|
|
|
2019-08-30 15:38:54 +00:00
|
|
|
void MapWidget::setSelected(const QVector<dive_site *> &divesites)
|
|
|
|
{
|
|
|
|
CHECK_IS_READY_RETURN_VOID();
|
|
|
|
m_mapHelper->setSelected(divesites);
|
|
|
|
}
|
|
|
|
|
2019-08-30 14:51:59 +00:00
|
|
|
void MapWidget::selectionChanged()
|
|
|
|
{
|
|
|
|
CHECK_IS_READY_RETURN_VOID();
|
|
|
|
m_mapHelper->selectionChanged();
|
|
|
|
m_mapHelper->centerOnSelectedDiveSite();
|
|
|
|
}
|
|
|
|
|
2019-05-02 20:41:24 +00:00
|
|
|
void MapWidget::selectedDivesChanged(const QList<int> &list)
|
2017-07-29 05:01:33 +00:00
|
|
|
{
|
2017-10-07 09:08:18 +00:00
|
|
|
CHECK_IS_READY_RETURN_VOID();
|
2020-04-25 18:54:12 +00:00
|
|
|
// We get a list of dive indices, but the selection code wants a list of dives.
|
|
|
|
// Therefore, transform them here.
|
|
|
|
std::vector<dive *> selection;
|
|
|
|
selection.reserve(list.size());
|
|
|
|
for (int idx: list) {
|
|
|
|
if (dive *d = get_dive(idx))
|
|
|
|
selection.push_back(d);
|
|
|
|
}
|
|
|
|
setSelection(selection, current_dive);
|
2017-07-29 05:01:33 +00:00
|
|
|
}
|
|
|
|
|
2019-03-14 22:28:45 +00:00
|
|
|
void MapWidget::coordinatesChanged(struct dive_site *ds, const location_t &location)
|
2017-07-29 05:01:33 +00:00
|
|
|
{
|
2019-03-14 22:28:45 +00:00
|
|
|
Command::editDiveSiteLocation(ds, location);
|
2017-07-29 05:01:33 +00:00
|
|
|
}
|
|
|
|
|
2019-06-23 07:22:26 +00:00
|
|
|
void MapWidget::divesChanged(const QVector<dive *> &, DiveField field)
|
2019-05-03 10:10:19 +00:00
|
|
|
{
|
2019-10-13 10:44:39 +00:00
|
|
|
if (field.divesite)
|
2019-05-03 10:10:19 +00:00
|
|
|
reload();
|
|
|
|
}
|
|
|
|
|
2017-07-29 05:01:33 +00:00
|
|
|
MapWidget::~MapWidget()
|
|
|
|
{
|
|
|
|
m_instance = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
MapWidget *MapWidget::instance()
|
|
|
|
{
|
|
|
|
if (m_instance == NULL)
|
|
|
|
m_instance = new MapWidget();
|
|
|
|
return m_instance;
|
|
|
|
}
|