Navit SVN

/work/compile/navit/src/navit/gui/qml/ngqpoint.h

00001 #ifndef NAVIT_GUI_QML_POINT_H
00002 #define NAVIT_GUI_QML_POINT_H
00003 
00004 static void
00005 get_direction(char *buffer, int angle, int mode)
00006 {
00007         angle=angle%360;
00008         switch (mode) {
00009         case 0:
00010                 sprintf(buffer,"%d",angle);
00011                 break;
00012         case 1:
00013                 if (angle < 69 || angle > 291)
00014                         *buffer++='N';
00015                 if (angle > 111 && angle < 249)
00016                         *buffer++='S';
00017                 if (angle > 22 && angle < 158)
00018                         *buffer++='E';
00019                 if (angle > 202 && angle < 338)
00020                         *buffer++='W';
00021                 *buffer++='\0';
00022                 break;
00023         case 2:
00024                 angle=(angle+15)/30;
00025                 if (! angle)
00026                         angle=12;
00027                 sprintf(buffer,"%d H", angle);
00028                 break;
00029         }
00030 }
00031 
00032 enum NGQPointTypes {MapPoint,Bookmark,Position,Destination,PointOfInterest};
00033 
00034 class NGQPoint : public QObject {
00035         Q_OBJECT;
00036 
00037     Q_PROPERTY(QString coordString READ coordString CONSTANT);
00038     Q_PROPERTY(QString pointName READ pointName CONSTANT);
00039     Q_PROPERTY(QString pointType READ pointType CONSTANT);
00040     Q_PROPERTY(QUrl pointUrl READ pointUrl CONSTANT);
00041 public:
00042     NGQPoint(struct gui_priv* this_,struct point* p,NGQPointTypes type=MapPoint,QObject *parent=NULL) : QObject(parent) {
00043         this->object=this_;
00044         this->item.map=0;
00045         transform_reverse(navit_get_trans(this->object->nav), p, &co);
00046         transform_to_geo(transform_get_projection(navit_get_trans(this->object->nav)), &co, &g);
00047         c.pro = transform_get_projection(navit_get_trans(this->object->nav));
00048         c.x = co.x;
00049         c.y = co.y;
00050         this->p.x=p->x;
00051         this->p.y=p->y;
00052         this->type=type;
00053 
00054         this->name=this->_coordName();
00055         this->coord=this->_coordString();
00056     }
00057     NGQPoint(struct gui_priv* this_,struct coord* c,NGQPointTypes type=Bookmark,QObject *parent=NULL) : QObject(parent) {
00058         this->object=this_;
00059         this->item.map=0;
00060         this->co.x=c->x;
00061         this->co.y=c->y;
00062         transform_to_geo(transform_get_projection(navit_get_trans(this->object->nav)), &co, &g);
00063         this->c.pro = transform_get_projection(navit_get_trans(this->object->nav));
00064         this->c.x = c->x;
00065         this->c.y = c->y;
00066         this->type=type;
00067 
00068         this->name=this->_coordName();
00069         this->coord=this->_coordString();
00070     }
00071 
00072     NGQPoint(struct gui_priv* this_,struct pcoord* pc,NGQPointTypes type=Bookmark,QObject *parent=NULL) : QObject(parent) {
00073         this->object=this_;
00074         this->item.map=0;
00075         this->c.pro = pc->pro;
00076         this->c.x = pc->x;
00077         this->c.y = pc->y;
00078         this->co.x=pc->x;
00079         this->co.y=pc->y;
00080         transform_to_geo(this->c.pro, &co, &g);
00081         this->type=type;
00082 
00083         this->name=this->_coordName();
00084         this->coord=this->_coordString();
00085     }
00086 
00087     NGQPoint(struct gui_priv* this_,struct coord* c,QString name,NGQPointTypes type=Bookmark,QObject *parent=NULL) : QObject(parent) {
00088         this->object=this_;
00089         this->item.map=0;
00090         this->co.x=c->x;
00091         this->co.y=c->y;
00092         transform_to_geo(transform_get_projection(navit_get_trans(this->object->nav)), &co, &g);
00093         this->c.pro = transform_get_projection(navit_get_trans(this->object->nav));
00094         this->c.x = c->x;
00095         this->c.y = c->y;
00096         this->type=type;
00097         
00098         this->name=name;
00099         this->coord=this->_coordString();
00100     }
00101 
00102     struct pcoord* pc() { return &c; }
00103 public slots:
00104     void setNewPoint(QString coord,NGQPointTypes type=PointOfInterest) {
00105             this->item.map=0;
00106             QStringList coordSplit=coord.split(" ",QString::SkipEmptyParts);
00107             this->co.x=coordSplit[0].toInt();
00108             this->co.y=coordSplit[1].toInt();
00109             transform_to_geo(transform_get_projection(navit_get_trans(this->object->nav)), &co, &g);
00110             this->c.pro = transform_get_projection(navit_get_trans(this->object->nav));
00111             this->c.x = coordSplit[0].toInt();
00112             this->c.y = coordSplit[1].toInt();
00113             this->type=type;
00114 
00115             this->name=this->_coordName();
00116             this->coord=this->_coordString();
00117     }
00118     QString pointName() {
00119             return this->name;    
00120     }
00121     QString coordString() {
00122             return this->coord;
00123     }
00124     QString pointType() {
00125             switch(this->type) {
00126             case MapPoint:
00127                     return QString("MapPoint");
00128             case Bookmark:
00129                     return QString("Bookmark");
00130             case Position:
00131                     return QString("Position");
00132             case Destination:
00133                     return QString("Destination");
00134             case PointOfInterest:
00135                     return QString("PointOfInterest");
00136             }
00137             return QString("");
00138     }
00139     QUrl pointUrl() {
00140             return this->url;
00141     }
00142     QString getInformation() {
00143             struct map_rect *mr;
00144             struct item* item;
00145             struct attr attr;
00146 
00147             QDomDocument retDoc;
00148             QDomElement entries;
00149             entries=retDoc.createElement("point");
00150             retDoc.appendChild(entries);
00151 
00152             if (this->type!=Bookmark and this->item.map) {
00153                     mr=map_rect_new(this->item.map, NULL);
00154                     item = map_rect_get_item_byid(mr, this->item.id_hi, this->item.id_lo);
00155                     if (item) {
00156                             while(item_attr_get(item, attr_any, &attr)) {
00157                                      entries.appendChild(this->_fieldValueHelper(retDoc,QString::fromLocal8Bit(attr_to_name(attr.type)), QString::fromLocal8Bit(attr_to_text(&attr,this->item.map, 1))));
00158                             }
00159                     }
00160                     map_rect_destroy(mr);
00161             }
00162             return retDoc.toString();
00163     }
00164     QString getPOI(const QString &attr_name) {
00165             struct attr attr;
00166             struct item* item;
00167             struct mapset_handle *h;
00168             struct map_selection *sel,*selm;
00169             struct map_rect *mr;
00170             struct map *m;
00171             int idist,dist;
00172             struct coord center;
00173             QDomDocument retDoc(attr_name);
00174             QDomElement entries;
00175             char dirbuf[32];
00176 
00177             if (!gui_get_attr(this->object->gui,attr_radius,&attr,NULL)) {
00178                     return QString();
00179             }
00180 
00181             dist=attr.u.num*1000;
00182 
00183             sel=map_selection_rect_new(&this->c, dist, 18);
00184             center.x=this->c.x;
00185             center.y=this->c.y;
00186             h=mapset_open(navit_get_mapset(this->object->nav));
00187 
00188             entries=retDoc.createElement(attr_name);
00189             retDoc.appendChild(entries);
00190 
00191             while ((m=mapset_next(h, 1))) {
00192                     selm=map_selection_dup_pro(sel, this->c.pro, map_projection(m));
00193                     mr=map_rect_new(m, selm);
00194                     if (mr) {
00195                             while ((item=map_rect_get_item(mr))) {
00196                                     struct coord c;
00197                                     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) {
00198                                             char* label;
00199                                             QString rs;
00200                                             if (item_attr_get(item, attr_label, &attr)) {
00201                                                     label=map_convert_string(m, attr.u.str);
00202                                                      if (QString(item_to_name(item->type)).startsWith(QString("poi_"))) {
00203                                                              rs=QString::fromLocal8Bit(item_to_name(item->type));
00204                                                              rs=rs.remove(QString("poi_"));
00205                                                              rs+=QString(" ")+QString::fromLocal8Bit(label);
00206 
00207                                                      } else if (QString(item_to_name(item->type)).startsWith(QString("poly_"))) {
00208                                                              rs=QString::fromLocal8Bit(item_to_name(item->type));
00209                                                              rs=rs.remove(QString("poly_"));
00210                                                              rs+=QString(" ")+QString::fromLocal8Bit(label);
00211 
00212                                                      } else if (QString(item_to_name(item->type)).startsWith(QString("street_"))) {
00213                                                              rs="Street ";
00214                                                              rs+=QString::fromLocal8Bit(label);
00215                                                      }
00216                                                      map_convert_free(label);
00217                                             } else
00218                                                     rs=item_to_name(item->type);
00219                                             get_direction(dirbuf, transform_get_angle_delta(&center, &c, 0), 1);
00220                                             if (rs.length()>0) {
00221                                                     QDomElement entry=retDoc.createElement("point");
00222                                                     QDomElement nameTag=retDoc.createElement("name");
00223                                                     QDomElement typeTag=retDoc.createElement("type");
00224                                                     QDomElement distTag=retDoc.createElement("distance");
00225                                                     QDomElement directTag=retDoc.createElement("direction");
00226                                                     QDomElement coordsTag=retDoc.createElement("coords");
00227                                                     QDomText nameT=retDoc.createTextNode(rs);
00228                                                     QDomText typeT=retDoc.createTextNode(QString(item_to_name(item->type)));
00229                                                     QDomText distT=retDoc.createTextNode(QString::number(idist/1000));
00230                                                     QDomText directT=retDoc.createTextNode(dirbuf);
00231                                                     QDomText coordsT=retDoc.createTextNode(QString("%1 %2").arg(c.x).arg(c.y));
00232                                                     nameTag.appendChild(nameT);
00233                                                     typeTag.appendChild(typeT);
00234                                                     distTag.appendChild(distT);
00235                                                     directTag.appendChild(directT);
00236                                                     coordsTag.appendChild(coordsT);
00237                                                     entry.appendChild(nameTag);
00238                                                     entry.appendChild(typeTag);
00239                                                     entry.appendChild(distTag);
00240                                                     entry.appendChild(directTag);
00241                                                     entry.appendChild(coordsTag);
00242                                                     entries.appendChild(entry);
00243                                             }
00244                                     }
00245                             }
00246                     }
00247                     map_selection_destroy(selm);
00248             }
00249             map_selection_destroy(sel);
00250             mapset_close(h);
00251             dbg(2,"%s\n",retDoc.toString().toLocal8Bit().constData());
00252             return retDoc.toString();
00253     }
00254 protected:
00255         QDomElement _fieldValueHelper(QDomDocument doc, QString field,QString value) {
00256                 QDomElement fieldTag=doc.createElement(field);
00257                 QDomText valueText=doc.createTextNode(value);
00258                 fieldTag.appendChild(valueText);
00259                 return fieldTag;
00260         }
00261         QString _coordString() {
00262                 char latc='N',lngc='E';
00263                 int lat_deg,lat_min,lat_sec;
00264                 int lng_deg,lng_min,lng_sec;
00265                 struct coord_geo g=this->g;
00266 
00267                 if (g.lat < 0) {
00268                         g.lat=-g.lat;
00269                         latc='S';
00270                 }
00271                 if (g.lng < 0) {
00272                         g.lng=-g.lng;
00273                         lngc='W';
00274                 }
00275                 lat_deg=g.lat;
00276                 lat_min=fmod(g.lat*60,60);
00277                 lat_sec=fmod(g.lat*3600,60);
00278                 lng_deg=g.lng;
00279                 lng_min=fmod(g.lng*60,60);
00280                 lng_sec=fmod(g.lng*3600,60);
00281                 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);
00282         }
00283         QString _coordName() {
00284                 int dist=10;
00285                 struct mapset *ms;
00286                 struct mapset_handle *h;
00287                 struct map_rect *mr;
00288                 struct map *m;
00289                 struct item *item;
00290                 struct street_data *data;
00291                 struct map_selection sel;
00292                 struct transformation *trans;
00293                 enum projection pro;
00294                 struct attr attr;
00295                 char *label;
00296                 QString ret;
00297 
00298                 trans=navit_get_trans(this->object->nav);
00299                 pro=transform_get_projection(trans);
00300                 transform_from_geo(pro, &g, &co);
00301                 ms=navit_get_mapset(this->object->nav);
00302                 sel.next=NULL;
00303                 sel.u.c_rect.lu.x=c.x-dist;
00304                 sel.u.c_rect.lu.y=c.y+dist;
00305                 sel.u.c_rect.rl.x=c.x+dist;
00306                 sel.u.c_rect.rl.y=c.y-dist;
00307                 sel.order=18;
00308                 sel.range=item_range_all;
00309                 h=mapset_open(ms);
00310                 while ((m=mapset_next(h,1))) {
00311                         mr=map_rect_new(m, &sel);
00312                         if (! mr)
00313                                 continue;
00314                         while ((item=map_rect_get_item(mr))) {                             
00315                                 data=street_get_data(item);
00316                                 if (transform_within_dist_item(&co, item->type, data->c, data->count, dist)) {                                     
00317                                         if (item_attr_get(item, attr_label, &attr)) {
00318                                                 label=map_convert_string(m, attr.u.str);
00319                                                 this->item=*item;             
00320                                                 this->_setUrl(item);
00321                                                  if (QString(item_to_name(item->type)).startsWith(QString("poi_"))) {
00322                                                          ret=QString::fromLocal8Bit(item_to_name(item->type));
00323                                                          ret=ret.remove(QString("poi_"));
00324                                                          ret+=QString(" ")+QString::fromLocal8Bit(label);
00325                                                  }
00326                                                  if (QString(item_to_name(item->type)).startsWith(QString("poly_"))) {
00327                                                          ret=QString::fromLocal8Bit(item_to_name(item->type));
00328                                                          ret=ret.remove(QString("poly_"));
00329                                                          ret+=QString(" ")+QString::fromLocal8Bit(label);
00330                                                  }
00331                                                  if (QString(item_to_name(item->type)).startsWith(QString("street_"))) {
00332                                                          ret="Street ";
00333                                                          ret+=QString::fromLocal8Bit(label);
00334                                                  }
00335                                                  map_convert_free(label);
00336                                                  street_data_free(data);
00337                                                  map_rect_destroy(mr);
00338                                                  mapset_close(h);
00339                                                  return ret;
00340                                         } else
00341                                                 this->item=*item;
00342                                                 this->_setUrl(item);
00343                                                 ret=item_to_name(item->type);
00344                                 }
00345                                 street_data_free(data);
00346                         }
00347                         map_rect_destroy(mr);
00348                 }
00349                 mapset_close(h);
00350                 return ret;
00351         }
00352         void _setUrl(struct item *item) {
00353                 struct attr attr;
00354                 if (item_attr_get(item,attr_osm_nodeid,&attr)) {
00355                         url.setUrl(QString("http://www.openstreetmap.org/browse/node/%1").arg(*attr.u.num64));
00356                 } else if (item_attr_get(item,attr_osm_wayid,&attr)) {
00357                         url.setUrl(QString("http://www.openstreetmap.org/browse/way/%1").arg(*attr.u.num64));
00358                 } else if (item_attr_get(item,attr_osm_relationid,&attr)) {
00359                         url.setUrl(QString("http://www.openstreetmap.org/browse/relation/%1").arg(*attr.u.num64));
00360                 } else {
00361                         url.clear();
00362                 }
00363         }
00364 private:
00365     struct gui_priv* object;
00366 
00367     NGQPointTypes type;
00368     struct coord_geo g;
00369     struct coord co;
00370     struct pcoord c;
00371     struct point p;
00372 
00373     struct item item;
00374 
00375     QString name;
00376     QString coord;
00377     QUrl url;
00378 };
00379 
00380 #include "ngqpoint.moc"
00381 
00382 #endif /* NAVIT_GUI_QML_POINT_H */