Map: generate pixmap name in model

Experimentation has shown that the image of a flag will
only be changed after dataChanged() if it is a simple
property. The old code had a complex QML expression and
then - for some reason - it didn't work.

To give us better control over the flags and avoid full
reloads of the map therefore introduce a model-property
pixmap name. The name depends on whether the site is
selected and if not, whether we are in divesite-edit mode.
This makes the code rather convoluted. Firstly, we have
to save whether the site is selected in the map-item.
Secondly we have to access the global map-widget, which
in turn has to go to the map-widget helper (layering
violation!).

Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
This commit is contained in:
Berthold Stoeger 2019-08-30 15:25:59 +02:00 committed by Dirk Hohndel
parent 28cb75b73d
commit bce31ab862
7 changed files with 44 additions and 8 deletions

View file

@ -4,6 +4,7 @@
#include "core/divesite.h"
#ifndef SUBSURFACE_MOBILE
#include "qt-models/filtermodels.h"
#include "desktop-widgets/mapwidget.h"
#endif
#include <QDebug>
@ -12,18 +13,31 @@
const char *MapLocation::PROPERTY_NAME_COORDINATE = "coordinate";
const char *MapLocation::PROPERTY_NAME_DIVESITE = "divesite";
const char *MapLocation::PROPERTY_NAME_NAME = "name";
const char *MapLocation::PROPERTY_NAME_PIXMAP = "pixmap";
#define MIN_DISTANCE_BETWEEN_DIVE_SITES_M 50.0
MapLocation::MapLocation() : m_ds(nullptr)
MapLocation::MapLocation() : m_ds(nullptr), m_selected(false)
{
}
MapLocation::MapLocation(struct dive_site *ds, QGeoCoordinate coord, QString name) :
m_ds(ds), m_coordinate(coord), m_name(name)
MapLocation::MapLocation(struct dive_site *ds, QGeoCoordinate coord, QString name, bool selected) :
m_ds(ds), m_coordinate(coord), m_name(name), m_selected(selected)
{
}
// Check whether we are in divesite-edit mode. This doesn't
// exist on mobile. And on desktop we have to access the MapWidget.
// Simplify this!
static bool inEditMode()
{
#ifdef SUBSURFACE_MOBILE
return false;
#else
return MapWidget::instance()->editMode();
#endif
}
QVariant MapLocation::getRole(int role) const
{
switch (role) {
@ -33,6 +47,10 @@ QVariant MapLocation::getRole(int role) const
return QVariant::fromValue(m_coordinate);
case Roles::RoleName:
return QVariant::fromValue(m_name);
case Roles::RolePixmap:
return m_selected ? QString("qrc:///dive-location-marker-selected-icon") :
inEditMode() ? QString("qrc:///dive-location-marker-inactive-icon") :
QString("qrc:///dive-location-marker-icon");
default:
return QVariant();
}
@ -69,6 +87,7 @@ MapLocationModel::MapLocationModel(QObject *parent) : QAbstractListModel(parent)
m_roles[MapLocation::Roles::RoleDivesite] = MapLocation::PROPERTY_NAME_DIVESITE;
m_roles[MapLocation::Roles::RoleCoordinate] = MapLocation::PROPERTY_NAME_COORDINATE;
m_roles[MapLocation::Roles::RoleName] = MapLocation::PROPERTY_NAME_NAME;
m_roles[MapLocation::Roles::RolePixmap] = MapLocation::PROPERTY_NAME_PIXMAP;
connect(&diveListNotifier, &DiveListNotifier::diveSiteChanged, this, &MapLocationModel::diveSiteChanged);
}
@ -184,7 +203,8 @@ void MapLocationModel::reload(QObject *map)
continue;
}
}
MapLocation *location = new MapLocation(ds, dsCoord, name);
bool selected = m_selectedDs.contains(ds);
MapLocation *location = new MapLocation(ds, dsCoord, name, selected);
m_mapLocations.append(location);
if (!diveSiteMode)
locationNameMap[name] = location;
@ -243,6 +263,5 @@ void MapLocationModel::diveSiteChanged(struct dive_site *ds, int field)
break;
}
emit dataChanged(createIndex(row, 0), createIndex(row, 0));
}