Marble

MapThemeModel.cpp
1// SPDX-License-Identifier: LGPL-2.1-or-later
2//
3// SPDX-FileCopyrightText: 2012 Dennis Nienhüser <nienhueser@kde.org>
4//
5
6#include "MapThemeModel.h"
7
8#include "GeoSceneDocument.h"
9#include "GeoSceneHead.h"
10#include "GeoSceneZoom.h"
11#include "MapThemeManager.h"
12
13#include <QDebug>
14#include <QModelIndex>
15#include <QStandardItemModel>
16
17MapThemeModel::MapThemeModel(QObject *parent)
18 : QSortFilterProxyModel(parent)
19 , m_themeManager(new Marble::MapThemeManager(this))
20 , m_mapThemeFilters(MapThemeModel::AnyTheme)
21{
22 setSourceModel(m_themeManager->mapThemeModel());
23 handleChangedThemes();
24 connect(m_themeManager, &Marble::MapThemeManager::themesChanged, this, &MapThemeModel::handleChangedThemes);
25}
26
27QHash<int, QByteArray> MapThemeModel::roleNames() const
28{
29 return {
30 {Qt::DisplayRole, "themeName"},
31 {Qt::DecorationRole, "iconName"},
32 {Qt::UserRole + 1, "mapThemeId"},
33 {Qt::UserRole + 2, "description"},
34 };
35}
36
37QString MapThemeModel::name(const QString &id) const
38{
39 for (int i = 0; i < rowCount(); ++i) {
40 if (data(index(i, 0, QModelIndex()), Qt::UserRole + 1).toString() == id) {
41 return data(index(i, 0, QModelIndex())).toString();
42 }
43 }
44 return {};
45}
46
47int MapThemeModel::indexOf(const QString &id) const
48{
49 for (int i = 0; i < rowCount(); ++i) {
50 if (data(index(i, 0, QModelIndex()), Qt::UserRole + 1).toString() == id) {
51 return i;
52 }
53 }
54 return -1;
55}
56
57MapThemeModel::MapThemeFilters MapThemeModel::mapThemeFilter() const
58{
59 return m_mapThemeFilters;
60}
61
62void MapThemeModel::setMapThemeFilter(MapThemeFilters filters)
63{
64 if (filters != m_mapThemeFilters) {
65 m_mapThemeFilters = filters;
66 Q_EMIT mapThemeFilterChanged();
67 }
68}
69
70bool MapThemeModel::filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const
71{
72 QModelIndex const index = sourceModel()->index(sourceRow, 0, sourceParent);
73 QString const themeId = index.data(Qt::UserRole + 1).toString();
74 if (m_mapThemeFilters & MapThemeModel::HighZoom && m_streetMapThemeIds.contains(themeId)) {
75 return false;
76 }
77
78 if (m_mapThemeFilters & MapThemeModel::LowZoom && !m_streetMapThemeIds.contains(themeId)) {
79 return false;
80 }
81
82 if (m_mapThemeFilters & MapThemeModel::Terrestrial && themeId.startsWith(QLatin1StringView("earth/"))) {
83 return false;
84 }
85
86 if (m_mapThemeFilters & MapThemeModel::Extraterrestrial && !themeId.startsWith(QLatin1StringView("earth/"))) {
87 return false;
88 }
89
90 return true;
91}
92
93void MapThemeModel::handleChangedThemes()
94{
95 /** @todo Extend .dgml spec by categories to simplify this
96 * The map theme model items should specify the planet and a set of
97 * categories/tags (arbitrary strings) to simplify filtering for specific
98 * map theme properties.
99 * E.g. the item for earth/openstreetmap/openstreetmap.dgml should have
100 * the planet set to earth and categories/tags like "OpenStreetMap, street map"
101 */
102
103 m_streetMapThemeIds.clear();
104 QStringList const themes = m_themeManager->mapThemeIds();
105 for (const QString &theme : themes) {
107 if (document && document->head()->zoom()->maximum() > 3000) {
108 m_streetMapThemeIds << document->head()->mapThemeId();
109 delete document;
110 }
111 }
112
115}
116
117#include "moc_MapThemeModel.cpp"
Provides access to all map themes installed locally.
A container for features parsed from the DGML file.
QStringList mapThemeIds() const
Returns a list of all locally available map theme IDs.
void themesChanged()
This signal will be emitted, when the themes change.
static GeoSceneDocument * loadMapTheme(const QString &mapThemeStringID)
Returns the map theme as a GeoSceneDocument object.
char * toString(const EngineQuery &query)
Binds a QML item to a specific geodetic location in screen coordinates.
void clear()
bool contains(const AT &value) const const
QVariant data(int role) const const
Q_EMITQ_EMIT
virtual QVariant data(const QModelIndex &index, int role) const const override
virtual QModelIndex index(int row, int column, const QModelIndex &parent) const const override
virtual int rowCount(const QModelIndex &parent) const const override
bool startsWith(QChar c, Qt::CaseSensitivity cs) const const
DisplayRole
QFuture< ArgsType< Signal > > connect(Sender *sender, Signal signal)
QString toString() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Sat Dec 21 2024 17:04:13 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.