Marble

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

KDE's Doxygen guidelines are available online.