2017-07-29 05:01:33 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0
|
|
|
|
#include <QQmlContext>
|
|
|
|
#include <QQuickItem>
|
|
|
|
#include <QModelIndex>
|
|
|
|
|
|
|
|
#include "mapwidget.h"
|
|
|
|
#include "core/divesite.h"
|
2024-03-26 19:23:50 +00:00
|
|
|
#include "core/errorhelper.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
|
|
|
|
|
2022-08-17 01:20:11 +00:00
|
|
|
MapWidget *MapWidget::m_instance = nullptr;
|
2017-07-29 05:01:33 +00:00
|
|
|
|
|
|
|
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);
|
cleanup: invert control-flow when resetting the core structures
To reset the core data structures, the mobile and desktop UIs
were calling into the dive-list models, which then reset the
core data structures, themselves and the unrelated
locationinformation model. The UI code then reset various other
things, such as the TankInformation model or the map. . This was
unsatisfying from a control-flow perspective, as the models should
display the core data, not act on it. Moreover, this meant lots
of intricate intermodule-dependencies.
Thus, straighten up the control flow: give the C core the
possibility to send a "all data reset" event. And do that
in those functions that reset the core data structures.
Let each module react to this event by itself. This removes
inter-module dependencies. For example, the MainWindow now
doesn't have to reset the TankInfoModel or the MapWidget.
Then, to reset the core data structures, let the UI code
simply directly call the respective core functions.
Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
2020-05-04 22:12:36 +00:00
|
|
|
connect(&diveListNotifier, &DiveListNotifier::dataReset, this, &MapWidget::reload);
|
2022-08-17 00:17:04 +00:00
|
|
|
connect(&diveListNotifier, &DiveListNotifier::settingsChanged, this, &MapWidget::reload);
|
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) {
|
2024-03-26 19:23:50 +00:00
|
|
|
report_info("%s failed to load with status: %d", qPrintable(urlMapWidget.toString()), static_cast<int>(status));
|
2017-10-07 09:08:18 +00:00
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
2024-05-11 11:21:53 +00:00
|
|
|
void MapWidget::setSelected(std::vector<dive_site *> divesites)
|
2019-08-30 15:38:54 +00:00
|
|
|
{
|
|
|
|
CHECK_IS_READY_RETURN_VOID();
|
2024-05-11 11:21:53 +00:00
|
|
|
m_mapHelper->setSelected(std::move(divesites));
|
2019-08-30 14:51:59 +00:00
|
|
|
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);
|
|
|
|
}
|
2024-05-11 11:21:53 +00:00
|
|
|
setSelection(std::move(selection), current_dive, -1);
|
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();
|
|
|
|
}
|
|
|
|
|
2020-12-18 11:01:36 +00:00
|
|
|
// Sadly, for reasons out of our control, we can't use a normal singleton for the
|
|
|
|
// map widget: In a standard singleton, the object is freed after main() exits.
|
|
|
|
// However, if there is an animation running (map zooming), the thread is
|
2022-09-03 21:06:01 +00:00
|
|
|
// terminated when the QApplication object is destroyed, which is before main()
|
2020-12-18 11:01:36 +00:00
|
|
|
// exits. The thread has a QQmlAnimationTimer that is freed. However, the map widget
|
|
|
|
// then tries to free the object itself, leading to a crash. Clearly, a bug in
|
|
|
|
// the QML MapWidget / QtQuick ecosystem.
|
|
|
|
// To solve this, the mainwindow will destroy the map widget and in the destructor
|
|
|
|
// the reference is cleared. Sad.
|
2017-07-29 05:01:33 +00:00
|
|
|
MapWidget::~MapWidget()
|
|
|
|
{
|
2022-08-17 01:20:11 +00:00
|
|
|
m_instance = nullptr;
|
2017-07-29 05:01:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
MapWidget *MapWidget::instance()
|
|
|
|
{
|
2022-08-17 01:20:11 +00:00
|
|
|
if (m_instance == nullptr)
|
2017-07-29 05:01:33 +00:00
|
|
|
m_instance = new MapWidget();
|
|
|
|
return m_instance;
|
|
|
|
}
|