1 // NavaidSearchModel.hxx - expose navaids via a QabstractListModel
2 //
3 // Written by James Turner, started July 2018.
4 //
5 // Copyright (C) 2018 James Turner <james@flightgear.org>
6 //
7 // This program is free software; you can redistribute it and/or
8 // modify it under the terms of the GNU General Public License as
9 // published by the Free Software Foundation; either version 2 of the
10 // License, or (at your option) any later version.
11 //
12 // This program is distributed in the hope that it will be useful, but
13 // WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15 // General Public License for more details.
16 //
17 // You should have received a copy of the GNU General Public License
18 // along with this program; if not, write to the Free Software
19 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
20 
21 #ifndef NAVAIDSEARCHMODEL_HXX
22 #define NAVAIDSEARCHMODEL_HXX
23 
24 #include <QAbstractListModel>
25 
26 #include "LauncherController.hxx"
27 
28 #include <Navaids/positioned.hxx>
29 #include <Navaids/NavDataCache.hxx>
30 
31 class NavaidSearchModel : public QAbstractListModel
32 {
33     Q_OBJECT
34 
35     Q_PROPERTY(bool isSearchActive READ isSearchActive NOTIFY searchActiveChanged)
36     Q_PROPERTY(bool haveExistingSearch READ haveExistingSearch NOTIFY haveExistingSearchChanged)
37     Q_PROPERTY(bool airportsOnly MEMBER m_airportsOnly NOTIFY airportsOnlyChanged)
38     Q_PROPERTY(int maxResults MEMBER m_maxResults NOTIFY maxResultsChanged)
39     Q_PROPERTY(int numResults READ numResults NOTIFY searchActiveChanged)
40 
41     Q_PROPERTY(qlonglong exactMatch READ exactMatch NOTIFY searchActiveChanged)
42 
43     enum Roles {
44         GeodRole = Qt::UserRole + 1,
45         GuidRole = Qt::UserRole + 2,
46         IdentRole = Qt::UserRole + 3,
47         NameRole = Qt::UserRole + 4,
48         IconRole = Qt::UserRole + 5,
49         TypeRole = Qt::UserRole + 6,
50         NavFrequencyRole = Qt::UserRole + 7
51     };
52 
53 public:
54     NavaidSearchModel(QObject* parent = nullptr);
55 
56     enum AircraftType
57     {
58         Unknown = LauncherController::Unknown,
59         Airplane = LauncherController::Airplane,
60         Seaplane = LauncherController::Seaplane,
61         Helicopter = LauncherController::Helicopter,
62         Airship = LauncherController::Airship
63     };
64 
65     Q_ENUMS(AircraftType)
66 
67     Q_INVOKABLE void setSearch(QString t, AircraftType aircraft = Unknown);
68 
69     Q_INVOKABLE void clear();
70 
71     Q_INVOKABLE qlonglong guidAtIndex(int index) const;
72 
isSearchActive() const73     bool isSearchActive() const
74     {
75         return m_searchActive;
76     }
77 
78     bool haveExistingSearch() const;
79 
80     int rowCount(const QModelIndex&) const override;
81 
82     QVariant data(const QModelIndex& index, int role) const override;
83 
84     FGPositionedRef itemAtRow(unsigned int row) const;
85 
86     void setItems(const FGPositionedList& items);
87 
88     QHash<int, QByteArray> roleNames() const override;
89 
90     qlonglong exactMatch() const;
91 
92     int numResults() const;
93 Q_SIGNALS:
94     void searchComplete();
95     void searchActiveChanged();
96     void haveExistingSearchChanged();
97     void airportsOnlyChanged();
98     void maxResultsChanged();
99 
100 private slots:
101     void onSearchResultsPoll();
102 
103 private:
104     void resort();
105 
106     PositionedIDVec m_ids;
107     mutable FGPositionedList m_items;
108     bool m_searchActive = false;
109     bool m_airportsOnly = false;
110     int m_maxResults = 0;
111     QScopedPointer<flightgear::NavDataCache::ThreadedGUISearch> m_search;
112 };
113 
114 
115 #endif // NAVAIDSEARCHMODEL_HXX
116