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"
|
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"
|
2017-07-29 05:01:33 +00:00
|
|
|
#include "mainwindow.h"
|
|
|
|
#include "divelistview.h"
|
|
|
|
|
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
|
|
|
static bool skipReload = false;
|
|
|
|
|
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)
|
|
|
|
{
|
2017-12-28 19:05:04 +00:00
|
|
|
m_rootItem = Q_NULLPTR;
|
|
|
|
m_mapHelper = Q_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);
|
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 *>();
|
|
|
|
connect(m_mapHelper, SIGNAL(selectedDivesChanged(QList<int>)),
|
2017-07-29 04:19:00 +00:00
|
|
|
this, SLOT(selectedDivesChanged(QList<int>)));
|
2018-10-08 11:51:57 +00:00
|
|
|
connect(m_mapHelper, &MapWidgetHelper::coordinatesChanged, this, &MapWidget::coordinatesChangedLocal);
|
2017-07-29 05:01:33 +00:00
|
|
|
}
|
|
|
|
|
2018-10-08 19:16:40 +00:00
|
|
|
void MapWidget::centerOnSelectedDiveSite()
|
|
|
|
{
|
|
|
|
CHECK_IS_READY_RETURN_VOID();
|
|
|
|
if (!skipReload)
|
|
|
|
m_mapHelper->centerOnSelectedDiveSite();
|
|
|
|
}
|
|
|
|
|
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();
|
2017-07-29 05:01:33 +00:00
|
|
|
if (!skipReload)
|
|
|
|
m_mapHelper->centerOnDiveSite(ds);
|
|
|
|
}
|
|
|
|
|
|
|
|
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))
|
2018-10-08 19:16:40 +00:00
|
|
|
centerOnSelectedDiveSite();
|
2017-07-29 05:01:33 +00:00
|
|
|
else
|
|
|
|
centerOnDiveSite(ds);
|
|
|
|
}
|
|
|
|
|
|
|
|
void MapWidget::repopulateLabels()
|
|
|
|
{
|
2017-10-07 09:08:18 +00:00
|
|
|
CHECK_IS_READY_RETURN_VOID();
|
2017-07-29 05:01:33 +00:00
|
|
|
m_mapHelper->reloadMapLocations();
|
|
|
|
}
|
|
|
|
|
|
|
|
void MapWidget::reload()
|
|
|
|
{
|
2017-10-07 09:08:18 +00:00
|
|
|
CHECK_IS_READY_RETURN_VOID();
|
2018-10-08 13:00:12 +00:00
|
|
|
m_mapHelper->exitEditMode();
|
2017-07-29 05:01:33 +00:00
|
|
|
if (!skipReload)
|
|
|
|
m_mapHelper->reloadMapLocations();
|
|
|
|
}
|
|
|
|
|
|
|
|
void MapWidget::endGetDiveCoordinates()
|
|
|
|
{
|
2017-10-07 09:08:18 +00:00
|
|
|
CHECK_IS_READY_RETURN_VOID();
|
2018-10-08 13:00:12 +00:00
|
|
|
m_mapHelper->exitEditMode();
|
2017-07-29 05:01:33 +00:00
|
|
|
}
|
|
|
|
|
2018-10-25 08:07:45 +00:00
|
|
|
void MapWidget::prepareForGetDiveCoordinates(struct dive_site *ds)
|
2017-07-29 05:01:33 +00:00
|
|
|
{
|
2017-10-07 09:08:18 +00:00
|
|
|
CHECK_IS_READY_RETURN_VOID();
|
2018-10-25 08:07:45 +00:00
|
|
|
m_mapHelper->enterEditMode(ds);
|
2017-07-29 05:01:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void MapWidget::selectedDivesChanged(QList<int> list)
|
|
|
|
{
|
2017-10-07 09:08:18 +00:00
|
|
|
CHECK_IS_READY_RETURN_VOID();
|
2017-07-29 05:01:33 +00:00
|
|
|
skipReload = true;
|
2018-10-12 14:13:42 +00:00
|
|
|
MainWindow::instance()->diveList->unselectDives();
|
2017-07-29 05:01:33 +00:00
|
|
|
if (!list.empty())
|
2018-10-12 14:13:42 +00:00
|
|
|
MainWindow::instance()->diveList->selectDives(list);
|
2017-07-29 05:01:33 +00:00
|
|
|
skipReload = false;
|
|
|
|
}
|
|
|
|
|
2018-10-20 18:12:15 +00:00
|
|
|
void MapWidget::coordinatesChangedLocal(const location_t &location)
|
2017-07-29 05:01:33 +00:00
|
|
|
{
|
2017-10-07 09:08:18 +00:00
|
|
|
CHECK_IS_READY_RETURN_VOID();
|
2018-10-20 18:12:15 +00:00
|
|
|
emit coordinatesChanged(location);
|
2017-07-29 05:01:33 +00:00
|
|
|
}
|
|
|
|
|
2018-10-25 10:23:19 +00:00
|
|
|
void MapWidget::updateDiveSiteCoordinates(struct dive_site *ds, const location_t &location)
|
2017-11-09 16:43:21 +00:00
|
|
|
{
|
|
|
|
CHECK_IS_READY_RETURN_VOID();
|
2018-10-25 10:23:19 +00:00
|
|
|
m_mapHelper->updateDiveSiteCoordinates(ds, location);
|
2017-11-09 16:43:21 +00:00
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|