Marble

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

KDE's Doxygen guidelines are available online.