summaryrefslogtreecommitdiff
path: root/navit/gui/qt5_qml/navitsearchmodel.h
blob: 2c6ab823c3922cb19ed17f517fd361bbec2de23d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
#ifndef NAVITSEARCHMODEL_H
#define NAVITSEARCHMODEL_H

#include <QAbstractItemModel>
#include <QDebug>
#include <QThread>
#include <QThreadPool>
#include <QRunnable>
#include <QMutex>

#include "navitinstance.h"
#include "navithelper.h"

#include <glib.h>
extern "C" {
#include "config.h"
#include "item.h" /* needs to be first, as attr.h depends on it */
#include "navit.h"

#include "coord.h"
#include "attr.h"
#include "xmlconfig.h" // for NAVIT_OBJECT
#include "layout.h"
#include "map.h"
#include "transform.h"

#include "mapset.h"
#include "callback.h"
#include "search.h"
#include "country.h"
#include "track.h"

#include "graphics.h"
}

Q_DECLARE_METATYPE(struct item *)

struct Address{
    QString country;
    QString town;
    QString street;
    QString house;
};
struct SearchResult{
    int index;
    QString distance;
    QString addressString;
    QString icon;
    QString label;
    struct pcoord coords;
    struct Address address;
};

Q_DECLARE_METATYPE(SearchResult)

class SearchWorker : public QThread
{
    Q_OBJECT
public:
    SearchWorker (NavitInstance * navit, struct search_list *searchResultList, QString searchQuery, enum attr_type search_type);
    void run() override;
signals:
    void gotSearchResult(SearchResult result);
private:
    NavitInstance * m_navitInstance;
    struct search_list *m_searchResultList;
    QString m_searchQuery;
    enum attr_type m_search_type;
};

class NavitSearchModel : public QAbstractItemModel
{
    Q_OBJECT
    Q_PROPERTY(NavitInstance * navit MEMBER m_navitInstance WRITE setNavit)
    Q_PROPERTY(SearchType currentSearchType READ getSearchType WRITE changeSearchType NOTIFY searchTypeChanged)
    Q_PROPERTY(QString searchQuery MEMBER m_searchQuery NOTIFY searchQueryChanged)
    Q_PROPERTY(QString country READ getCountry NOTIFY addressChanged)
    Q_PROPERTY(QString town READ getTown NOTIFY addressChanged)
    Q_PROPERTY(QString street READ getStreet NOTIFY addressChanged)
    Q_PROPERTY(QString house READ getHouse NOTIFY addressChanged)

public:
    enum SearchModelRoles {
        LabelRole = Qt::UserRole + 1,
        NameRole,
        IconRole,
        AddressRole,
        DistanceRole
    };

    enum SearchType {
        SearchCountry = attr_country_all,
        SearchTown = attr_town_or_district_name,//attr_town_or_district_name or attr_town_postal
        SearchStreet = attr_street_name,
        SearchHouse = attr_house_number
    };

    Q_ENUMS(SearchType)
    explicit NavitSearchModel(QObject *parent = 0);
    ~NavitSearchModel ();
    int rowCount(const QModelIndex & parent = QModelIndex()) const override;
    QHash<int, QByteArray> roleNames() const override;
    QVariant data(const QModelIndex & index, int role) const override;
    Qt::ItemFlags flags(const QModelIndex &index) const override;
    bool setData(const QModelIndex &index, const QVariant &value, int role = Qt::EditRole) override;

    QModelIndex index(int row, int column, const QModelIndex &parent = QModelIndex()) const override;
    QModelIndex parent(const QModelIndex &child) const override;
    int columnCount(const QModelIndex &parent = QModelIndex()) const override;

    void setNavit(NavitInstance * navit);
    void search_list_set_default_country();
public slots:
    void setAsDestination(int index);
    void setAsPosition(int index);
    void addAsBookmark(int index);
    void addStop(int index,  int position);
    void setAddressAsDestination();
    void setAddressAsPosition();
    void addAddressAsBookmark();
    void addAddressStop(int position);
    void viewAddressOnMap();
    void viewResultOnMap(int index);
    void remove(int index);
    void select(int index);
    void search();
    void reset();
signals:
    void searchTypeChanged();
    void searchQueryChanged();
    void addressChanged();
private slots:
    void receiveSearchResult(SearchResult result);
private:
    NavitInstance *m_navitInstance = nullptr;

    QList<SearchResult> m_searchResults;

    QString m_country = "United Kingdom";

    struct search_list *m_searchResultList;

    char *m_country_iso2 = nullptr;

    enum SearchType m_search_type;

    QString m_searchQuery;
    QString m_searchQueryCountry;
    QString m_searchQueryTown;
    QString m_searchQueryStreet;

    SearchResult m_address;

    SearchWorker *m_searchWorker = nullptr;

    void update();
    void stopSearchWorker(bool clearModel = false);
    void setDefaultCountry();
    void changeSearchType(enum SearchType searchType, bool restoreQuery = true);
    void searchResultsToMap(QList<SearchResult> results);
    enum SearchType getSearchType();

    void viewOnMap(SearchResult pointMap);

    QString getCountry(){
        return m_address.address.country;
    }
    QString getTown(){
        return m_address.address.town;
    }
    QString getStreet(){
        return m_address.address.street;
    }
    QString getHouse(){
        return m_address.address.house;
    }
};

#endif // NAVITSEARCHMODEL_H