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"
|
|
|
|
#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-08 19:16:40 +00:00
|
|
|
struct dive_site *ds = get_dive_site_by_uuid(idx.model()->index(idx.row(), 0).data().toUInt());
|
2017-07-29 05:01:33 +00:00
|
|
|
if (!ds || !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
|
|
|
}
|
|
|
|
|
|
|
|
void MapWidget::prepareForGetDiveCoordinates()
|
|
|
|
{
|
2017-10-07 09:08:18 +00:00
|
|
|
CHECK_IS_READY_RETURN_VOID();
|
2018-10-08 13:00:12 +00:00
|
|
|
m_mapHelper->enterEditMode();
|
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;
|
|
|
|
MainWindow::instance()->dive_list()->unselectDives();
|
|
|
|
if (!list.empty())
|
|
|
|
MainWindow::instance()->dive_list()->selectDives(list);
|
|
|
|
skipReload = false;
|
|
|
|
}
|
|
|
|
|
2018-10-08 11:51:57 +00:00
|
|
|
void MapWidget::coordinatesChangedLocal(degrees_t latitude, degrees_t longitude)
|
2017-07-29 05:01:33 +00:00
|
|
|
{
|
2017-10-07 09:08:18 +00:00
|
|
|
CHECK_IS_READY_RETURN_VOID();
|
2018-10-08 11:51:57 +00:00
|
|
|
emit coordinatesChanged(latitude, longitude);
|
2017-07-29 05:01:33 +00:00
|
|
|
}
|
|
|
|
|
2018-10-08 10:57:46 +00:00
|
|
|
void MapWidget::updateDiveSiteCoordinates(uint32_t uuid, degrees_t latitude, degrees_t longitude)
|
2017-11-09 16:43:21 +00:00
|
|
|
{
|
|
|
|
CHECK_IS_READY_RETURN_VOID();
|
2018-10-08 10:57:46 +00:00
|
|
|
m_mapHelper->updateDiveSiteCoordinates(uuid, latitude, longitude);
|
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;
|
|
|
|
}
|