summaryrefslogtreecommitdiff
path: root/gui/qml/ngqpoint.h
blob: 6cdb48517cff90df73522ae83d5cc53901e4743e (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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
#ifndef NAVIT_GUI_QML_POINT_H
#define NAVIT_GUI_QML_POINT_H

enum NGQPointTypes {MapPoint,Bookmark,Position,Destination};

class NGQPoint : public QObject {
	Q_OBJECT;

    Q_PROPERTY(QString coordString READ coordString CONSTANT);
    Q_PROPERTY(QString pointName READ pointName CONSTANT);
    Q_PROPERTY(QString pointType READ pointType CONSTANT);
public:
    NGQPoint(struct gui_priv* this_,struct point* p,NGQPointTypes type=MapPoint,QObject *parent=NULL) : QObject(parent) {
        this->object=this_;
        transform_reverse(navit_get_trans(this->object->nav), p, &co);
        transform_to_geo(transform_get_projection(navit_get_trans(this->object->nav)), &co, &g);
        c.pro = transform_get_projection(navit_get_trans(this->object->nav));
        c.x = co.x;
        c.y = co.y;
        this->p.x=p->x;
        this->p.y=p->y;
        this->type=type;

        this->name=this->_coordName();
        this->coord=this->_coordString();
    }
    NGQPoint(struct gui_priv* this_,struct coord* c,NGQPointTypes type=Bookmark,QObject *parent=NULL) : QObject(parent) {
        this->object=this_;
        this->co.x=c->x;
        this->co.y=c->y;
        transform_to_geo(transform_get_projection(navit_get_trans(this->object->nav)), &co, &g);
        this->c.pro = transform_get_projection(navit_get_trans(this->object->nav));
        this->c.x = c->x;
        this->c.y = c->y;
        this->type=type;

        this->name=this->_coordName();
        this->coord=this->_coordString();
    }

    NGQPoint(struct gui_priv* this_,struct coord* c,QString name,NGQPointTypes type=Bookmark,QObject *parent=NULL) : QObject(parent) {
        this->object=this_;
        this->co.x=c->x;
        this->co.y=c->y;
        transform_to_geo(transform_get_projection(navit_get_trans(this->object->nav)), &co, &g);
        this->c.pro = transform_get_projection(navit_get_trans(this->object->nav));
        this->c.x = c->x;
        this->c.y = c->y;
        this->type=type;
        
        this->name=name;
        this->coord=this->_coordString();
    }

    struct pcoord* pc() { return &c; }
public slots:
    QString pointName() {
            return this->name;    
    }
    QString coordString() {
            return this->coord;
    }
    QString pointType() {
            switch(this->type) {
            case MapPoint:
                    return QString("MapPoint");
            case Bookmark:
                    return QString("Bookmark");
            case Position:
                    return QString("Position");
            case Destination:
                    return QString("Destination");
            }
            return QString("");
    }
    QString getAttrList(const QString &attr_name) {
            struct attr attr;
            struct item* item;
            struct mapset_handle *h;
            struct map_selection *sel,*selm;
            struct map_rect *mr;
            struct map *m;
            int idist,dist;
            struct coord center;
            QDomDocument retDoc(attr_name);
            QDomElement entries;

            if (!gui_get_attr(this->object->gui,attr_radius,&attr,NULL)) {
                    return QString();
            }

            dist=attr.u.num*1000;

            sel=map_selection_rect_new(&this->c, dist, 18);
            center.x=this->c.x;
            center.y=this->c.y;
            h=mapset_open(navit_get_mapset(this->object->nav));

            entries=retDoc.createElement(attr_name);
            retDoc.appendChild(entries);

            while ((m=mapset_next(h, 1))) {
                    selm=map_selection_dup_pro(sel, this->c.pro, map_projection(m));
                    mr=map_rect_new(m, selm);
                    if (mr) {
                            while ((item=map_rect_get_item(mr))) {
                                    struct coord c;
                                    if ( item_coord_get_pro(item, &c, 1, this->c.pro) && coord_rect_contains(&sel->u.c_rect, &c) && (idist=transform_distance(this->c.pro, &center, &c)) < dist && item->type<type_line) {
                                            char* label;
                                            QString rs;
                                            if (item_attr_get(item, attr_label, &attr)) {
                                                    label=map_convert_string(m, attr.u.str);
                                                     if (QString(item_to_name(item->type)).startsWith(QString("poi_"))) {
                                                             rs=QString::fromLocal8Bit(item_to_name(item->type));
                                                             rs=rs.remove(QString("poi_"));
                                                             rs+=QString(" ")+QString::fromLocal8Bit(label);

                                                     } else if (QString(item_to_name(item->type)).startsWith(QString("poly_"))) {
                                                             rs=QString::fromLocal8Bit(item_to_name(item->type));
                                                             rs=rs.remove(QString("poly_"));
                                                             rs+=QString(" ")+QString::fromLocal8Bit(label);

                                                     } else if (QString(item_to_name(item->type)).startsWith(QString("street_"))) {
                                                             rs="Street ";
                                                             rs+=QString::fromLocal8Bit(label);
                                                     }
                                                     map_convert_free(label);
                                            } else
                                                    rs=item_to_name(item->type);
                                            if (rs.length()>0) {
                                                    QDomElement entry=retDoc.createElement("point");
                                                    QDomElement nameTag=retDoc.createElement("name");
                                                    QDomElement typeTag=retDoc.createElement("type");
                                                    QDomText nameT=retDoc.createTextNode(rs);
                                                    QDomText typeT=retDoc.createTextNode(QString(item_to_name(item->type)));
                                                    nameTag.appendChild(nameT);
                                                    typeTag.appendChild(typeT);
                                                    entry.appendChild(nameTag);
                                                    entry.appendChild(typeTag);
                                                    entries.appendChild(entry);
                                            }
                                    }
                            }
                    }
                    map_selection_destroy(selm);
            }
            map_selection_destroy(sel);
            mapset_close(h);
           
            return retDoc.toString();
    }
protected:
        QString _coordString() {
                char latc='N',lngc='E';
                int lat_deg,lat_min,lat_sec;
                int lng_deg,lng_min,lng_sec;
                struct coord_geo g=this->g;

                if (g.lat < 0) {
                        g.lat=-g.lat;
                        latc='S';
                }
                if (g.lng < 0) {
                        g.lng=-g.lng;
                        lngc='W';
                }
                lat_deg=g.lat;
                lat_min=fmod(g.lat*60,60);
                lat_sec=fmod(g.lat*3600,60);
                lng_deg=g.lng;
                lng_min=fmod(g.lng*60,60);
                lng_sec=fmod(g.lng*3600,60);
                return QString(QString::fromLocal8Bit("%1°%2'%3\" %4%5%6°%7'%8\" %9")).arg(lat_deg).arg(lat_min).arg(lat_sec).arg(latc).arg(' ').arg(lng_deg).arg(lng_min).arg(lng_sec).arg(lngc);
        }
        QString _coordName() {
                int dist=10;
                struct mapset *ms;
                struct mapset_handle *h;
                struct map_rect *mr;
                struct map *m;
                struct item *item;
                struct street_data *data;
                struct map_selection sel;
                struct transformation *trans;
                enum projection pro;
                struct attr attr;
                char *label;
                QString ret;

                trans=navit_get_trans(this->object->nav);
                pro=transform_get_projection(trans);
                transform_from_geo(pro, &g, &co);
                ms=navit_get_mapset(this->object->nav);
                sel.next=NULL;
                sel.u.c_rect.lu.x=c.x-dist;
                sel.u.c_rect.lu.y=c.y+dist;
                sel.u.c_rect.rl.x=c.x+dist;
                sel.u.c_rect.rl.y=c.y-dist;
                sel.order=18;
                sel.range=item_range_all;
                h=mapset_open(ms);
                while ((m=mapset_next(h,1))) {
                        mr=map_rect_new(m, &sel);
                        if (! mr)
                                continue;
                        while ((item=map_rect_get_item(mr))) {                             
                                data=street_get_data(item);
                                if (transform_within_dist_item(&co, item->type, data->c, data->count, dist)) {                                     
                                        if (item_attr_get(item, attr_label, &attr)) {
                                                label=map_convert_string(m, attr.u.str);
                                                 if (QString(item_to_name(item->type)).startsWith(QString("poi_"))) {
                                                         ret=QString::fromLocal8Bit(item_to_name(item->type));
                                                         ret=ret.remove(QString("poi_"));
                                                         ret+=QString(" ")+QString::fromLocal8Bit(label);
                                                         map_convert_free(label);
                                                         street_data_free(data);
                                                         map_rect_destroy(mr);
                                                         mapset_close(h);
                                                         return ret;
                                                 }
                                                 if (QString(item_to_name(item->type)).startsWith(QString("poly_"))) {
                                                         ret=QString::fromLocal8Bit(item_to_name(item->type));
                                                         ret=ret.remove(QString("poly_"));
                                                         ret+=QString(" ")+QString::fromLocal8Bit(label);
                                                         map_convert_free(label);
                                                         street_data_free(data);
                                                         map_rect_destroy(mr);
                                                         mapset_close(h);
                                                         return ret;                                                     
                                                 }
                                                 if (QString(item_to_name(item->type)).startsWith(QString("street_"))) {
                                                         ret="Street ";
                                                         ret+=QString::fromLocal8Bit(label);
                                                         map_convert_free(label);
                                                         street_data_free(data);
                                                         map_rect_destroy(mr);
                                                         mapset_close(h);
                                                         return ret;                                                      
                                                 }
                                                 map_convert_free(label);
                                        } else
                                                ret=item_to_name(item->type);
                                }
                                street_data_free(data);
                        }
                        map_rect_destroy(mr);
                }
                mapset_close(h);
                return ret;
        }
private:
    struct gui_priv* object;

    NGQPointTypes type;
    struct coord_geo g;
    struct coord co;
    struct pcoord c;
    struct point p;

    QString name;
    QString coord;
};

#include "ngqpoint.moc"

#endif /* NAVIT_GUI_QML_POINT_H */