Check Qt version for Qt5 related features
[quandoparte] / application / stationlistproxymodel.h
1 #ifndef STATIONLISTPROXYMODEL_H
2 #define STATIONLISTPROXYMODEL_H
3
4 /*
5
6 Copyright (C) 2011 Luciano Montanaro <mikelima@cirulla.net>
7
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
12
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16 General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with this program; see the file COPYING.  If not, write to
20 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
21 Boston, MA 02110-1301, USA.
22
23 */
24
25 #include <QtGlobal>
26 #include <QSortFilterProxyModel>
27 #include <QtLocation/QGeoCoordinate>
28 #include <QtLocation/QGeoPositionInfoSource>
29 #include <QMetaType>
30 #include <QStringList>
31
32 #if (QT_VERSION < QT_VERSION_CHECK(5, 0, 0))
33 QTM_USE_NAMESPACE
34 #endif
35
36 class StationListProxyModel : public QSortFilterProxyModel
37 {
38     Q_OBJECT
39     Q_PROPERTY(QString searchPattern READ searchPattern WRITE setSearchPattern)
40     Q_PROPERTY(SortingMode sortingMode READ sortingMode WRITE setSortingMode NOTIFY sortingModeChanged)
41     Q_ENUMS(SortingMode)
42 public:
43
44     enum SortingMode {
45         AlphaSorting,
46         DistanceSorting,
47         RecentUsageSorting
48     };
49
50     explicit StationListProxyModel(QObject *parent = 0);
51
52     QString searchPattern() const;
53     void setSearchPattern(const QString &pattern);
54
55     SortingMode sortingMode();
56     void setSortingMode(SortingMode mode);
57
58     Q_INVOKABLE void setUserPosition(const QGeoCoordinate &pos);
59     Q_INVOKABLE void setRecentStations(const QStringList &stations);
60     Q_INVOKABLE void setRecentOnlyFilter(bool);
61
62 signals:
63     void sortingModeChanged(SortingMode mode);
64
65 protected:
66     virtual bool lessThan(const QModelIndex &left, const QModelIndex &right) const;
67     virtual bool filterAcceptsRow(int sourceRow,
68             const QModelIndex &sourceParent) const;
69
70 private:
71     void forceSortingMode(SortingMode mode);
72
73 private slots:
74     void updatePosition(const QGeoPositionInfo &update);
75     void updateRecentStations(void);
76 private:
77     QString m_searchPattern;
78     QGeoPositionInfoSource *positionInfoSource;
79     QGeoCoordinate m_here;
80     QStringList m_stations;
81     SortingMode m_sortingMode;
82     bool m_filterRecentOnly;
83 };
84
85 Q_DECLARE_METATYPE(StationListProxyModel::SortingMode)
86
87 #endif // STATIONLISTPROXYMODEL_H