Navit SVN

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

00001 #ifndef NAVIT_GUI_QML_SEARCHPROXY_H
00002 #define NAVIT_GUI_QML_SEARCHPROXY_H
00003 
00004 void __setNewPoint(struct gui_priv* this_,struct pcoord* pc, NGQPointTypes type);
00005 
00006 class NGQProxySearch : public NGQProxy {
00007         Q_OBJECT;
00008 
00009         Q_PROPERTY(QString countryName READ countryName WRITE setCountryName NOTIFY countryNameSignal);
00010         Q_PROPERTY(QString countryISO2 READ countryISO2 WRITE setCountryISO2 NOTIFY countryISO2Signal);
00011         Q_PROPERTY(QString townName READ townName WRITE setTownName NOTIFY townNameSignal);
00012         Q_PROPERTY(QString streetName READ streetName WRITE setStreetName NOTIFY streetNameSignal);
00013 
00014         Q_PROPERTY(QString searchContext READ searchContext WRITE setSearchContext);
00015 
00016 public:
00017         NGQProxySearch(struct gui_priv* this_,QObject* parent) : NGQProxy(this_,parent) {
00018                 struct attr search_attr, country_name, country_iso2, *country_attr;
00019                 struct item *item;
00020                 struct country_search *cs;
00021                 struct tracking *tracking;
00022                 struct search_list_result *res;
00023 
00024                 this->sl=search_list_new(navit_get_mapset(this->object->nav));
00025                 this->search_context="country";
00026 
00027                 country_attr=country_default();
00028                 tracking=navit_get_tracking(this->object->nav);
00029                 if (tracking && tracking_get_attr(tracking, attr_country_id, &search_attr, NULL))
00030                         country_attr=&search_attr;
00031                 if (country_attr) {
00032                         cs=country_search_new(country_attr, 0);
00033                         item=country_search_get_item(cs);
00034                         if (item && item_attr_get(item, attr_country_name, &country_name)) {
00035                                 search_attr.type=attr_country_all;
00036                                 dbg(0,"country %s\n", country_name.u.str);
00037                                 this->country_name=QString::fromLocal8Bit(country_name.u.str);
00038                                 search_attr.u.str=country_name.u.str;
00039                                 search_list_search(this->sl, &search_attr, 0);
00040                                 while((res=search_list_get_result(this->sl)));
00041                                 if (item_attr_get(item, attr_country_iso2, &country_iso2)) {
00042                                         this->country_iso2=QString::fromLocal8Bit(country_iso2.u.str);
00043                                 }
00044                         }
00045                         country_search_destroy(cs);
00046                 } else {
00047                         dbg(0,"warning: no default country found\n");
00048                         if (!this->country_iso2.isEmpty()) {
00049                                 dbg(0,"attempting to use country '%s'\n",this->country_iso2.toStdString().c_str());
00050                                 search_attr.type=attr_country_iso2;
00051                                 search_attr.u.str=(char*)this->country_iso2.toStdString().c_str();
00052                                 search_list_search(this->sl, &search_attr, 0);
00053                                 while((res=search_list_get_result(this->sl)));
00054                         }
00055                 }               
00056         }
00057         ~NGQProxySearch() {
00058                 search_list_destroy(this->sl);
00059         }
00060 
00061 signals:
00062         void countryNameSignal(QString);
00063         void countryISO2Signal(QString);
00064         void townNameSignal(QString);
00065         void streetNameSignal(QString);
00066 
00067 public slots:
00068         void setPointToResult() {
00069                 struct attr attr;
00070                 struct search_list_result *res;
00071 
00072                 if (this->street_name.length()>0) {
00073                         attr.type=attr_street_name;
00074                         attr.u.str=this->street_name.toLocal8Bit().data();
00075                 } else if (this->town_name.length()>0) {
00076                         attr.type=attr_town_or_district_name;
00077                         attr.u.str=this->town_name.toLocal8Bit().data();
00078                 } else if (this->country_name.length()>0) {
00079                         attr.type=attr_country_name;
00080                         attr.u.str=this->country_name.toLocal8Bit().data();
00081                 }
00082                 search_list_search(this->sl,&attr,0);
00083                 if ((res=search_list_get_result(this->sl))) {
00084                         __setNewPoint(this->object,res->c,PointOfInterest);
00085                 }
00086                 return;
00087         }
00088         QString searchXml() {
00089                 NGQStandardItemModel* ret=new NGQStandardItemModel(this);
00090                 struct attr attr;
00091                 struct search_list_result *res;
00092                 int counter=0;
00093                 QDomDocument retDoc;
00094                 QDomElement entries;
00095 
00096                 entries=retDoc.createElement("search");
00097                 retDoc.appendChild(entries);
00098 
00099                 if (this->search_context=="country") {
00100                         attr.type=attr_country_name;
00101                         attr.u.str=this->country_name.toLocal8Bit().data();
00102                 }
00103                 if (this->search_context=="town") {
00104                         if (this->town_name.length()<3) {
00105                                 return retDoc.toString();
00106                         }
00107                         attr.type=attr_town_or_district_name;
00108                         attr.u.str=this->town_name.toLocal8Bit().data();
00109                 }
00110                 if (this->search_context=="street") {
00111                         attr.type=attr_street_name;
00112                         attr.u.str=this->street_name.toLocal8Bit().data();
00113                 }
00114 
00115                 search_list_search(this->sl,&attr,1);
00116 
00117                 while ((res=search_list_get_result(this->sl))) {
00118                         QStandardItem* curItem=new QStandardItem();
00119                         QDomElement entry=retDoc.createElement("item");
00120                         entries.appendChild(entry);
00121                         //Result processing depends on search type
00122                         if (this->search_context=="country") {
00123                                         entry.appendChild(this->_fieldValueHelper(retDoc,QString("id"), QString::number(counter)));
00124                                         entry.appendChild(this->_fieldValueHelper(retDoc,QString("name"), QString::fromLocal8Bit(res->country->name)));
00125                                         entry.appendChild(this->_fieldValueHelper(retDoc,QString("icon"), QString("country_%1%2").arg(res->country->iso2).arg(".svgz")));
00126                         }
00127                         if (this->search_context=="town") {
00128                                 entry.appendChild(this->_fieldValueHelper(retDoc,QString("id"), QString::number(counter)));
00129                                 if (res->town->common.town_name) {
00130                                         entry.appendChild(this->_fieldValueHelper(retDoc,QString("name"),QString::fromLocal8Bit(res->town->common.town_name)));
00131                                 }
00132                                 if (res->town->common.district_name) {
00133                                         entry.appendChild(this->_fieldValueHelper(retDoc,QString("name"), QString::fromLocal8Bit(res->town->common.district_name)));
00134                                 } 
00135                         }
00136                         if (this->search_context=="street") {
00137                                 entry.appendChild(this->_fieldValueHelper(retDoc,QString("id"), QString::number(counter)));
00138                                 entry.appendChild(this->_fieldValueHelper(retDoc,QString("name"),QString::fromLocal8Bit(res->street->name)));
00139                         }
00140                         counter++;
00141                         ret->appendRow(curItem);
00142                 }
00143 
00144                 return retDoc.toString();
00145         }
00146         QString countryName() {
00147                 return this->country_name;
00148         }
00149         void setCountryName(QString countryName) {
00150                 this->country_name=countryName;
00151                 struct attr attr;
00152                 struct search_list_result *res;
00153                 
00154                 //We need to update ISO2 
00155                 attr.type=attr_country_name;
00156                 attr.u.str=countryName.toLocal8Bit().data();
00157                 search_list_search(this->sl,&attr,0);
00158                 while ((res=search_list_get_result(this->sl))) {
00159                                 this->setCountryISO2(QString::fromLocal8Bit(res->country->iso2));
00160                 }
00161                 //...and current town
00162                 this->town_name="";
00163                 this->street_name="";
00164 
00165                 countryNameSignal(countryName);
00166         }
00167         QString countryISO2() {
00168                 return this->country_iso2;
00169         }
00170         void setCountryISO2(QString countryISO2) {
00171                 this->country_iso2=countryISO2;
00172                 countryISO2Signal(countryISO2);
00173         }
00174         QString townName() {
00175                 return this->town_name;
00176         }
00177         void setTownName(QString townName) {
00178                 struct attr attr;
00179 
00180                 this->town_name=townName;
00181 
00182                 //Specialize search
00183                 attr.type=attr_town_or_district_name;
00184                 attr.u.str=townName.toLocal8Bit().data();
00185                 search_list_search(this->sl,&attr,0);
00186      
00187                 //...and street
00188                 this->street_name="";
00189 
00190                 townNameSignal(townName);
00191         }
00192         QString streetName() {
00193                 return this->street_name;
00194         }
00195         void setStreetName(QString streetName) {
00196                 struct attr attr;
00197 
00198                 this->street_name=streetName;
00199 
00200                 //Specialize search
00201                 attr.type=attr_street_name;
00202                 attr.u.str=streetName.toLocal8Bit().data();
00203                 search_list_search(this->sl,&attr,0);
00204 
00205                 streetNameSignal(streetName);
00206         }
00207         QString searchContext() {
00208                 return this->search_context;
00209         }
00210         void setSearchContext(QString searchContext) {
00211                 this->search_context=searchContext;
00212         }
00213 
00214 protected:
00215         virtual int getAttrFunc(enum attr_type type, struct attr *attr, struct attr_iter *iter) {
00216                 return 0;
00217         }
00218         virtual int setAttrFunc(struct attr *attr) {
00219                 return 0;
00220         }
00221 private:
00222         struct search_list *sl;
00223         QString search_context;
00224         QString country_iso2,country_name,town_name,street_name;
00225 };
00226 
00227 #include "searchProxy.moc"
00228 
00229 #endif /* NAVIT_GUI_QML_SEARCHPROXY_H */