Marble

MarblePlacemarkModel.cpp
1// SPDX-License-Identifier: LGPL-2.1-or-later
2//
3// SPDX-FileCopyrightText: 2006-2007 Torsten Rahn <tackat@kde.org>
4// SPDX-FileCopyrightText: 2007 Inge Wallin <ingwa@kde.org>
5//
6
7// Own
8#include "MarblePlacemarkModel.h"
9#include "MarblePlacemarkModel_P.h"
10
11// Qt
12#include <QElapsedTimer>
13#include <QImage>
14
15// Marble
16#include "GeoDataData.h"
17#include "GeoDataExtendedData.h"
18#include "GeoDataGeometry.h"
19#include "GeoDataIconStyle.h"
20#include "GeoDataPlacemark.h"
21#include "GeoDataStyle.h" // In geodata/data/
22#include "MarbleDebug.h"
23
24using namespace Marble;
25
26class Q_DECL_HIDDEN MarblePlacemarkModel::Private
27{
28public:
29 Private()
30 : m_size(0)
31 , m_placemarkContainer(nullptr)
32 {
33 }
34
35 ~Private() = default;
36
37 int m_size;
38 QList<GeoDataPlacemark *> *m_placemarkContainer;
39};
40
41// ---------------------------------------------------------------------------
42
44 : QAbstractListModel(parent)
45 , d(new Private)
46{
47}
48
53
54void MarblePlacemarkModel::setPlacemarkContainer(QList<GeoDataPlacemark *> *container)
55{
56 d->m_placemarkContainer = container;
57}
58
60{
61 if (!parent.isValid())
62 return d->m_size;
63 else
64 return 0;
65}
66
67int MarblePlacemarkModel::columnCount(const QModelIndex &parent) const
68{
69 if (!parent.isValid())
70 return 1;
71 else
72 return 0;
73}
74
76{
77 return {
78 {DescriptionRole, "description"},
79 {Qt::DisplayRole, "name"},
80 {Qt::DecorationRole, "icon"},
81 {IconPathRole, "iconPath"},
82 {PopularityIndexRole, "zoomLevel"},
83 {VisualCategoryRole, "visualCategory"},
84 {AreaRole, "area"},
85 {PopulationRole, "population"},
86 {CountryCodeRole, "countryCode"},
87 {StateRole, "state"},
88 {PopularityRole, "popularity"},
89 {GeoTypeRole, "role"},
90 {CoordinateRole, "coordinate"},
91 {StyleRole, "style"},
92 {GmtRole, "gmt"},
93 {DstRole, "dst"},
94 {GeometryRole, "geometry"},
95 {ObjectPointerRole, "objectPointer"},
96 {LongitudeRole, "longitude"},
97 {LatitudeRole, "latitude"},
98 };
99}
100
102{
103 if (!index.isValid())
104 return {};
105
106 if (index.row() >= d->m_placemarkContainer->size())
107 return {};
108
109 if (role == Qt::DisplayRole) {
110 return d->m_placemarkContainer->at(index.row())->name();
111 } else if (role == Qt::DecorationRole) {
112 return QVariant::fromValue(d->m_placemarkContainer->at(index.row())->style()->iconStyle().icon());
113 } else if (role == IconPathRole) {
114 return QVariant::fromValue(d->m_placemarkContainer->at(index.row())->style()->iconStyle().iconPath());
115 } else if (role == PopularityIndexRole) {
116 return d->m_placemarkContainer->at(index.row())->zoomLevel();
117 } else if (role == VisualCategoryRole) {
118 return d->m_placemarkContainer->at(index.row())->visualCategory();
119 } else if (role == AreaRole) {
120 return d->m_placemarkContainer->at(index.row())->area();
121 } else if (role == PopulationRole) {
122 return d->m_placemarkContainer->at(index.row())->population();
123 } else if (role == CountryCodeRole) {
124 return d->m_placemarkContainer->at(index.row())->countryCode();
125 } else if (role == StateRole) {
126 return d->m_placemarkContainer->at(index.row())->state();
127 } else if (role == PopularityRole) {
128 return d->m_placemarkContainer->at(index.row())->popularity();
129 } else if (role == DescriptionRole) {
130 return d->m_placemarkContainer->at(index.row())->description();
131 } else if (role == Qt::ToolTipRole) {
132 return d->m_placemarkContainer->at(index.row())->description();
133 } else if (role == GeoTypeRole) {
134 return d->m_placemarkContainer->at(index.row())->role();
135 } else if (role == CoordinateRole) {
136 return QVariant::fromValue(d->m_placemarkContainer->at(index.row())->coordinate());
137 } else if (role == StyleRole) {
138 return QVariant::fromValue(d->m_placemarkContainer->at(index.row())->style().data());
139 } else if (role == GmtRole) {
140 return QVariant::fromValue(d->m_placemarkContainer->at(index.row())->extendedData().value(QStringLiteral("gmt")).value());
141 } else if (role == DstRole) {
142 return QVariant::fromValue(d->m_placemarkContainer->at(index.row())->extendedData().value(QStringLiteral("dst")).value());
143 } else if (role == GeometryRole) {
144 return QVariant::fromValue(d->m_placemarkContainer->at(index.row())->geometry());
145 } else if (role == ObjectPointerRole) {
146 return QVariant::fromValue(dynamic_cast<GeoDataObject *>(d->m_placemarkContainer->at(index.row())));
147 } else if (role == LongitudeRole) {
148 return QVariant::fromValue(d->m_placemarkContainer->at(index.row())->coordinate().longitude(GeoDataCoordinates::Degree));
149 } else if (role == LatitudeRole) {
150 return QVariant::fromValue(d->m_placemarkContainer->at(index.row())->coordinate().latitude(GeoDataCoordinates::Degree));
151 } else
152 return {};
153}
154
155QModelIndexList MarblePlacemarkModel::approxMatch(const QModelIndex &start, int role, const QVariant &value, int hits, Qt::MatchFlags flags) const
156{
157 QList<QModelIndex> results;
158
159 int count = 0;
160
161 QModelIndex entryIndex;
162 QString listName;
163 QString queryString = value.toString().toLower();
164 QString simplifiedListName;
165
166 int row = start.row();
167 const int rowNum = rowCount();
168
169 while (row < rowNum && count != hits) {
171 entryIndex = index(row, 0);
172 listName = data(entryIndex, role).toString().toLower();
173 simplifiedListName = GeoString::deaccent(listName);
174
175 if (listName.startsWith(queryString) || simplifiedListName.startsWith(queryString)) {
176 results << entryIndex;
177 ++count;
178 }
179 }
180 ++row;
181 }
182
183 return results;
184}
185
187{
188 Q_UNUSED(start);
189
190 // performance wise a reset is far better when the provided list
191 // is significant. That is an issue because we have
192 // MarbleControlBox::m_sortproxy as a sorting customer.
193 // I leave the balance search as an exercise to the reader...
194
196 t.start();
197 // beginInsertRows( QModelIndex(), start, start + length );
198 d->m_size += length;
199 // endInsertRows();
202 Q_EMIT countChanged();
203 mDebug() << "addPlacemarks: Time elapsed:" << t.elapsed() << "ms for" << length << "Placemarks.";
204}
205
206void MarblePlacemarkModel::removePlacemarks(const QString &containerName, int start, int length)
207{
208 if (length > 0) {
210 t.start();
212 d->m_size -= length;
215 Q_EMIT countChanged();
216 mDebug() << "removePlacemarks(" << containerName << "): Time elapsed:" << t.elapsed() << "ms for" << length << "Placemarks.";
217 }
218}
219
220#include "moc_MarblePlacemarkModel.cpp"
A base class for all geodata objects.
This class represents a model of all place marks which are currently available through a given Placem...
QHash< int, QByteArray > roleNames() const override
Return the supported role names.
void addPlacemarks(int start, int length)
This method is used by the PlacemarkManager to add new place marks to the model.
int rowCount(const QModelIndex &parent=QModelIndex()) const override
Return the number of Placemarks in the Model.
MarblePlacemarkModel(QObject *parent=nullptr)
Creates a new place mark model.
@ GmtRole
The Greenwich Mean Time.
@ LongitudeRole
The longitude in degree (for use in QML)
@ PopularityIndexRole
The popularity index.
@ DstRole
The Daylight Saving Time.
@ LatitudeRole
The latitude in degree (for use in QML)
@ ObjectPointerRole
The pointer to a specific object.
@ GeoTypeRole
The geo type (e.g. city or mountain)
@ GeometryRole
The GeoDataGeometry geometry.
@ CoordinateRole
The GeoDataCoordinates coordinate.
@ IconPathRole
Path to the image, if known.
void removePlacemarks(const QString &containerName, int start, int length)
This method is used by the PlacemarkManager to remove place marks from the model.
~MarblePlacemarkModel() override
Destroys the place mark model.
QVariant data(const QModelIndex &index, int role) const override
Return the data according to the index.
Q_SCRIPTABLE Q_NOREPLY void start()
Binds a QML item to a specific geodetic location in screen coordinates.
void beginRemoveRows(const QModelIndex &parent, int first, int last)
void layoutChanged(const QList< QPersistentModelIndex > &parents, QAbstractItemModel::LayoutChangeHint hint)
virtual Qt::ItemFlags flags(const QModelIndex &index) const const override
virtual QModelIndex index(int row, int column, const QModelIndex &parent) const const override
qint64 elapsed() const const
bool isValid() const const
int row() const const
Q_EMITQ_EMIT
QObject * parent() const const
bool startsWith(QChar c, Qt::CaseSensitivity cs) const const
QString toLower() const const
DisplayRole
typedef MatchFlags
QVariant fromValue(T &&value)
QString toString() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Mon Nov 18 2024 12:15:45 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.