From 9322092e415946e212f94b3e0a4e330f4b7c07d6 Mon Sep 17 00:00:00 2001 From: Berthold Stoeger Date: Sat, 31 Aug 2019 23:17:04 +0200 Subject: Cleanup: simplify role handling in MapLocationModel To connect a model to QML, one is supposed to provide a QHash 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 --- qt-models/maplocationmodel.cpp | 19 +++++++------------ qt-models/maplocationmodel.h | 7 ------- 2 files changed, 7 insertions(+), 19 deletions(-) (limited to 'qt-models') diff --git a/qt-models/maplocationmodel.cpp b/qt-models/maplocationmodel.cpp index 4ca4a3a53..57fc76179 100644 --- a/qt-models/maplocationmodel.cpp +++ b/qt-models/maplocationmodel.cpp @@ -10,12 +10,6 @@ #include #include -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 MapLocationModel::roleNames() const { - return m_roles; + QHash 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 diff --git a/qt-models/maplocationmodel.h b/qt-models/maplocationmodel.h index 4b2066186..ce1f999fb 100644 --- a/qt-models/maplocationmodel.h +++ b/qt-models/maplocationmodel.h @@ -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 m_mapLocations; - QHash m_roles; QVector m_selectedDs; signals: -- cgit v1.2.3-70-g09d2