Cleanup: simplify role handling in MapLocationModel

To connect a model to QML, one is supposed to provide a
  QHash<int, QByteArray> MapLocationModel::roleNames()
function that returns a role -> attribute-name hash.

That was realized by filling the hash in the constructor,
storing it as a member variable, using static strings that
were declared in the class-definition and defined in the
translation unit.

Adding a new role was a pain and the whole thing was totally
pointless as the attribute names were used nowhere else and
the roleNames() function is called only once.

Simply do, what we do everywhere else: initialize the hash
in the roleNames() function and use normal string literals.

Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
This commit is contained in:
Berthold Stoeger 2019-08-31 23:17:04 +02:00 committed by Dirk Hohndel
parent 4dd86cc205
commit 9322092e41
2 changed files with 7 additions and 19 deletions

View file

@ -10,12 +10,6 @@
#include <QDebug>
#include <algorithm>
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";
const char *MapLocation::PROPERTY_NAME_Z = "z";
#define MIN_DISTANCE_BETWEEN_DIVE_SITES_M 50.0
MapLocation::MapLocation() : m_ds(nullptr), m_selected(false)
@ -87,11 +81,6 @@ QVariant MapLocation::divesiteVariant()
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;
m_roles[MapLocation::Roles::RoleZ] = MapLocation::PROPERTY_NAME_Z;
connect(&diveListNotifier, &DiveListNotifier::diveSiteChanged, this, &MapLocationModel::diveSiteChanged);
}
@ -110,7 +99,13 @@ QVariant MapLocationModel::data(const QModelIndex & index, int role) const
QHash<int, QByteArray> MapLocationModel::roleNames() const
{
return m_roles;
QHash<int, QByteArray> roles;
roles[MapLocation::Roles::RoleDivesite] = "divesite";
roles[MapLocation::Roles::RoleCoordinate] = "coordinate";
roles[MapLocation::Roles::RoleName] = "name";
roles[MapLocation::Roles::RolePixmap] = "pixmap";
roles[MapLocation::Roles::RoleZ] = "z";
return roles;
}
int MapLocationModel::rowCount(const QModelIndex&) const

View file

@ -18,12 +18,6 @@ class MapLocation : public QObject
Q_PROPERTY(QString name MEMBER m_name)
public:
static const char *PROPERTY_NAME_COORDINATE;
static const char *PROPERTY_NAME_DIVESITE;
static const char *PROPERTY_NAME_NAME;
static const char *PROPERTY_NAME_PIXMAP;
static const char *PROPERTY_NAME_Z;
explicit MapLocation();
explicit MapLocation(struct dive_site *ds, QGeoCoordinate coord, QString name, bool selected);
@ -86,7 +80,6 @@ private slots:
private:
QVector<MapLocation *> m_mapLocations;
QHash<int, QByteArray> m_roles;
QVector<dive_site *> m_selectedDs;
signals: