libosmscout  1.1.1
SearchLocationModel.h
Go to the documentation of this file.
1 #ifndef OSMSCOUT_CLIENT_QT_SEARCHLOCATIONMODEL_H
2 #define OSMSCOUT_CLIENT_QT_SEARCHLOCATIONMODEL_H
3 
4 /*
5  OSMScout - a Qt backend for libosmscout and libosmscout-map
6  Copyright (C) 2014 Tim Teulings
7  Copyright (C) 2016 Lukáš Karas
8 
9  This library is free software; you can redistribute it and/or
10  modify it under the terms of the GNU Lesser General Public
11  License as published by the Free Software Foundation; either
12  version 2.1 of the License, or (at your option) any later version.
13 
14  This library is distributed in the hope that it will be useful,
15  but WITHOUT ANY WARRANTY; without even the implied warranty of
16  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17  Lesser General Public License for more details.
18 
19  You should have received a copy of the GNU Lesser General Public
20  License along with this library; if not, write to the Free Software
21  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22  */
23 
24 #include <QObject>
25 #include <QAbstractListModel>
26 #include <QJSValue>
27 
28 #include <osmscout/GeoCoord.h>
29 #include <osmscout/LocationEntry.h>
31 #include <osmscout/SearchModule.h>
32 
34 
35 namespace osmscout {
36 
42 class OSMSCOUT_CLIENT_QT_API LocationListModel : public QAbstractListModel
43 {
44  Q_OBJECT
45 
49  Q_PROPERTY(int count READ rowCount NOTIFY countChanged)
50 
51 
54  Q_PROPERTY(bool searching READ isSearching NOTIFY SearchingChanged)
55 
62  Q_PROPERTY(double lat READ GetLat WRITE SetLat)
63 
67  Q_PROPERTY(double lon READ GetLon WRITE SetLon)
68 
72  Q_PROPERTY(int resultLimit READ GetResultLimit WRITE SetResultLimit)
73 
77  Q_PROPERTY(QString pattern READ getPattern WRITE setPattern)
78 
97  Q_PROPERTY(QJSValue compare READ getCompare WRITE setCompare)
98 
123  Q_PROPERTY(QJSValue equals READ getEquals WRITE setEquals)
124 
125 signals:
126  void SearchRequested(const QString searchPattern,
127  int limit,
128  osmscout::GeoCoord searchCenter,
129  AdminRegionInfoRef defaultRegion,
130  osmscout::BreakerRef breaker);
131  void SearchingChanged(bool);
132  void countChanged(int);
133  void regionLookupRequested(osmscout::GeoCoord);
134 
135 public slots:
136  void setPattern(const QString& pattern);
137  void onSearchResult(const QString searchPattern,
138  const QList<LocationEntry>);
139  void onSearchFinished(const QString searchPattern, bool error);
140 
141  void onLocationAdminRegions(const osmscout::GeoCoord,QList<AdminRegionInfoRef>);
142  void onLocationAdminRegionFinished(const osmscout::GeoCoord);
143 
144 private:
145  QString pattern;
146  QString lastRequestPattern;
147  QList<LocationEntryRef> locations;
148  bool searching;
149  SearchModule* searchModule;
150  LookupModule* lookupModule;
151  osmscout::GeoCoord searchCenter;
152  int resultLimit;
153  osmscout::BreakerRef breaker;
154  AdminRegionInfoRef defaultRegion;
155  AdminRegionInfoRef lastRequestDefaultRegion;
156  QJSValue compareFn;
157  QJSValue equalsFn;
158 
159 public:
160  enum Roles {
161  LabelRole = Qt::UserRole,
162  TypeRole = Qt::UserRole +1,
163  RegionRole = Qt::UserRole +2,
164  LatRole = Qt::UserRole +3,
165  LonRole = Qt::UserRole +4,
166  DistanceRole = Qt::UserRole +5,
167  BearingRole = Qt::UserRole +6,
168  LocationObjectRole = Qt::UserRole +7
169  };
170 
171 public:
172  explicit LocationListModel(QObject* parent = nullptr);
173  ~LocationListModel() override;
174 
175  QJSValue getCompare() const {
176  return compareFn;
177  }
178 
179  void setCompare(const QJSValue &fn){
180  compareFn=fn;
181  }
182 
183  QJSValue getEquals() const{
184  return equalsFn;
185  }
186 
187  void setEquals(const QJSValue &fn){
188  equalsFn=fn;
189  }
190 
191  Q_INVOKABLE QVariant data(const QModelIndex &index, int role) const override;
192 
193  Q_INVOKABLE int rowCount(const QModelIndex &parent = QModelIndex()) const override;
194 
195  Q_INVOKABLE Qt::ItemFlags flags(const QModelIndex &index) const override;
196 
197  QHash<int, QByteArray> roleNames() const override;
198 
199  Q_INVOKABLE QObject* get(int row) const;
200 
201  inline bool isSearching() const {
202  return searching;
203  }
204 
205  inline double GetLat() const {
206  return searchCenter.GetLat();
207  }
208 
209  inline double GetLon() const {
210  return searchCenter.GetLon();
211  }
212 
213  inline void SetLat(double lat){
214  if (lat!=searchCenter.GetLat()) {
215  searchCenter.Set(lat, searchCenter.GetLon());
216  lookupRegion();
217  }
218  }
219 
220  inline void SetLon(double lon){
221  if (lon!=searchCenter.GetLon()) {
222  searchCenter.Set(searchCenter.GetLat(), lon);
223  lookupRegion();
224  }
225  }
226 
227  inline int GetResultLimit() const {
228  return resultLimit;
229  }
230 
231  inline void SetResultLimit(int limit){
232  resultLimit=limit;
233  }
234 
235  inline QString getPattern() const {
236  return pattern;
237  }
238 
239 private:
240  void lookupRegion();
241 
242 };
243 
244 }
245 
246 #endif
void SetResultLimit(int limit)
Definition: SearchLocationModel.h:231
#define OSMSCOUT_CLIENT_QT_API
Definition: ClientQtImportExport.h:45
std::shared_ptr< LocationEntry > LocationEntryRef
Definition: LocationEntry.h:112
std::shared_ptr< Breaker > BreakerRef
Definition: Breaker.h:65
QJSValue getCompare() const
Definition: SearchLocationModel.h:175
QJSValue getEquals() const
Definition: SearchLocationModel.h:183
Definition: SearchModule.h:137
void SetLon(double lon)
Definition: SearchLocationModel.h:220
double GetLat() const
Definition: SearchLocationModel.h:205
void setEquals(const QJSValue &fn)
Definition: SearchLocationModel.h:187
void setCompare(const QJSValue &fn)
Definition: SearchLocationModel.h:179
Definition: Area.h:38
Roles
Definition: SearchLocationModel.h:160
void SetLat(double lat)
Definition: SearchLocationModel.h:213
bool isSearching() const
Definition: SearchLocationModel.h:201
Definition: LocationEntry.h:42
Definition: SearchLocationModel.h:42
std::shared_ptr< AdminRegionInfo > AdminRegionInfoRef
Definition: LookupModule.h:43
double GetLon() const
Definition: SearchLocationModel.h:209
int GetResultLimit() const
Definition: SearchLocationModel.h:227
Definition: LookupModule.h:48
QString getPattern() const
Definition: SearchLocationModel.h:235