From a8c63604ae549dae6add2cdcaee2faae8747ee03 Mon Sep 17 00:00:00 2001 From: Niels Date: Thu, 4 Nov 2010 21:01:35 +0100 Subject: [PATCH] use monav plugin to do address look ups --- app.pro | 4 +- mainwidget.cpp | 22 ++-- mainwidget.h | 7 +- mapwidget.cpp | 6 +- mapwidget.h | 2 +- routingwidget.cpp | 170 ------------------------------ searchwidget.cpp | 156 +++++++++++++++++++++++++++ routingwidget.h => searchwidget.h | 45 ++++---- 8 files changed, 201 insertions(+), 211 deletions(-) delete mode 100644 routingwidget.cpp create mode 100644 searchwidget.cpp rename routingwidget.h => searchwidget.h (56%) diff --git a/app.pro b/app.pro index 7ee5afd..4570be3 100644 --- a/app.pro +++ b/app.pro @@ -20,7 +20,7 @@ SOURCES += main.cpp \ mapwidget.cpp \ markerlist.cpp \ downloadwidget.cpp \ - routingwidget.cpp \ + searchwidget.cpp \ gpsclient.cpp HEADERS += mainwidget.h \ @@ -36,5 +36,5 @@ HEADERS += mainwidget.h \ mapwidget.h \ markerlist.h \ downloadwidget.h \ - routingwidget.h \ + searchwidget.h \ gpsclient.h diff --git a/mainwidget.cpp b/mainwidget.cpp index 347f061..ae342bb 100644 --- a/mainwidget.cpp +++ b/mainwidget.cpp @@ -22,7 +22,7 @@ #include "downloadwidget.h" #include "mapwidget.h" #include "markerlist.h" -#include "routingwidget.h" +#include "searchwidget.h" #include "batterylayer.h" #include "gpslayer.h" @@ -42,7 +42,7 @@ MainWidget::MainWidget(QWidget *parent) m_map(new MapWidget(this)), m_markerList(new MarkerList(this)), m_dlWidget(new DownloadWidget(this)), - m_routingWidget(new RoutingWidget(this)) + m_search(new SearchWidget(this)) { QHBoxLayout *layout = new QHBoxLayout(this); layout->setContentsMargins(0, 0, 0, 0); @@ -73,7 +73,7 @@ MainWidget::MainWidget(QWidget *parent) connect(m_map, SIGNAL(close()), this, SIGNAL(close())); connect(m_map, SIGNAL(showMarkerList()), this, SLOT(showList())); connect(m_map, SIGNAL(downloadArea(int, QRectF)), this, SLOT(downloadArea(int, QRectF))); - connect(m_map, SIGNAL(route(QPointF, QPointF)), this, SLOT(findRoute(QPointF, QPointF))); + connect(m_map, SIGNAL(search()), this, SLOT(search())); m_stack->insertWidget(0, m_map); connect(m_markerList, SIGNAL(back()), this, SLOT(showMap())); @@ -83,8 +83,9 @@ MainWidget::MainWidget(QWidget *parent) connect(m_dlWidget, SIGNAL(back()), this, SLOT(showMap())); m_stack->insertWidget(2, m_dlWidget); - connect(m_routingWidget, SIGNAL(back()), this, SLOT(showMap())); - m_stack->insertWidget(3, m_routingWidget); + connect(m_search, SIGNAL(back()), this, SLOT(showMap())); + connect(m_search, SIGNAL(centerOn(qreal, qreal)), this, SLOT(showMap(qreal, qreal))); + m_stack->insertWidget(3, m_search); resize(320, 240); } @@ -121,6 +122,12 @@ void MainWidget::showMap() m_stack->setCurrentIndex(0); } +void MainWidget::showMap(qreal lon, qreal lat) +{ + m_map->centerOnGeoPos(lon, lat); + showMap(); +} + void MainWidget::downloadArea(int level, const QRectF &rect) { m_dlWidget->setStartLevel(level); @@ -128,11 +135,8 @@ void MainWidget::downloadArea(int level, const QRectF &rect) m_stack->setCurrentIndex(2); } -void MainWidget::findRoute(const QPointF &from, const QPointF &to) +void MainWidget::search() { - m_routingWidget->setFrom(from); - m_routingWidget->setTo(to); - m_stack->setCurrentIndex(3); } diff --git a/mainwidget.h b/mainwidget.h index 471c000..a08e1ae 100644 --- a/mainwidget.h +++ b/mainwidget.h @@ -27,7 +27,7 @@ class DownloadWidget; class MapWidget; class MarkerList; -class RoutingWidget; +class SearchWidget; class MainWidget : public QWidget { @@ -45,15 +45,16 @@ private slots: void showList(); void markerAdded(const QString &name); void showMap(); + void showMap(qreal lon, qreal lat); void downloadArea(int level, const QRectF &rect); - void findRoute(const QPointF &from, const QPointF &to); + void search(); private: QStackedWidget *m_stack; MapWidget *m_map; MarkerList *m_markerList; DownloadWidget *m_dlWidget; - RoutingWidget *m_routingWidget; + SearchWidget *m_search; }; diff --git a/mapwidget.cpp b/mapwidget.cpp index 577a357..bf37d65 100644 --- a/mapwidget.cpp +++ b/mapwidget.cpp @@ -297,10 +297,10 @@ void MapWidget::keyPressEvent(QKeyEvent *event) } break; } - case Qt::Key_R: + case Qt::Key_F: { - if (event->modifiers() == Qt::NoModifier) { - // emit route(m_routeStart, m_routeEnd); + if (event->modifiers() == Qt::ControlModifier) { + emit search(); } break; } diff --git a/mapwidget.h b/mapwidget.h index a15cea9..a5b316b 100644 --- a/mapwidget.h +++ b/mapwidget.h @@ -49,7 +49,7 @@ signals: void close(); void showMarkerList(); void downloadArea(int level, const QRectF &rect); - void route(const QPointF &from, const QPointF &to); + void search(); protected: virtual void resizeEvent(QResizeEvent *event); diff --git a/routingwidget.cpp b/routingwidget.cpp deleted file mode 100644 index aa98e7c..0000000 --- a/routingwidget.cpp +++ /dev/null @@ -1,170 +0,0 @@ -/* - * Copyright 2010 Niels Kummerfeldt - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 51 Franklin Street, Fifth Floor, - * Boston, MA 02110-1301 USA - */ - -#include "routingwidget.h" - -#include "projection.h" - -#include -#include -#include -#include -#include -#include -#include - -RoutingWidget::RoutingWidget(QWidget *parent) - : QWidget(parent), - m_name(new QLineEdit("route.gpx", this)), - m_transport(new QComboBox(this)), - m_quickest(new QRadioButton("Quickest", this)), - m_shortest(new QRadioButton("Shortest", this)), - m_routino(), - m_dir(), - m_prefix(), - m_profiles(), - m_swap(), - m_from(), - m_to(), - m_fromLabel(new QLabel(this)), - m_toLabel(new QLabel(this)) -{ - QGridLayout *layout = new QGridLayout(this); - layout->setContentsMargins(5, 5, 5, 5); - layout->setRowStretch(6, 1); - - QLabel *label = new QLabel("From:"); - layout->addWidget(label, 0, 0); - - layout->addWidget(m_fromLabel, 0, 1, 1, 2); - - label = new QLabel("To:"); - layout->addWidget(label, 1, 0); - - layout->addWidget(m_toLabel, 1, 1, 1, 2); - - label = new QLabel("Name:"); - layout->addWidget(label, 2, 0); - - layout->addWidget(m_name, 2, 1, 1, 2); - - label = new QLabel("Transport:"); - layout->addWidget(label, 3, 0); - - m_transport->addItems(QStringList() << "motorcar" << "motorbike" << "bicycle" << "foot"); - layout->addWidget(m_transport, 3, 1, 1, 2); - - label = new QLabel("Route:"); - layout->addWidget(label, 4, 0); - - m_shortest->setChecked(true); - layout->addWidget(m_shortest, 4, 1, 1, 2); - - layout->addWidget(m_quickest, 5, 1, 1, 2); - - QPushButton *back = new QPushButton("&Show map", this); - back->setShortcut(QKeySequence(Qt::ALT + Qt::Key_S)); - layout->addWidget(back, 7, 1); - connect(back, SIGNAL(clicked()), this, SIGNAL(back())); - - QPushButton *find = new QPushButton("&Find route", this); - find->setShortcut(QKeySequence(Qt::ALT + Qt::Key_F)); - layout->addWidget(find, 7, 2); - connect(find, SIGNAL(clicked()), this, SLOT(findRoute())); - - QFile run("/tmp/findroute.sh"); - if (run.open(QFile::WriteOnly)) { - QTextStream out(&run); - out << "#!/bin/sh\n"; - run.close(); - } - run.setPermissions(QFile::ReadOwner | QFile::WriteOwner | QFile::ExeOwner); - - loadConfig(); - - resize(320, 240); -} - -RoutingWidget::~RoutingWidget() -{ -} - -void RoutingWidget::setFrom(const QPointF &from) -{ - m_from = from; - m_fromLabel->setText(Projection::geo2string(from)); -} - -void RoutingWidget::setTo(const QPointF &to) -{ - m_to = to; - m_toLabel->setText(Projection::geo2string(to)); -} - -void RoutingWidget::findRoute() -{ - QFile run("/tmp/findroute.sh"); - if (run.open(QFile::WriteOnly)) { - QString file; - QString name = m_name->text(); - QString command; - QTextStream out(&run); - out << "#!/bin/sh\n"; - if (!m_swap.isEmpty()) { - out << "swapon "+m_swap+"\n"; - } - command = m_routino; - command.append(QString(" --dir=%1").arg(m_dir)); - command.append(QString(" --prefix=%1").arg(m_prefix)); - command.append(QString(" --profiles=%1").arg(m_profiles)); - command.append(QString(" --lon1=%1 --lat1=%2 --lon2=%3 --lat2=%4") - .arg(m_from.x()).arg(m_from.y()).arg(m_to.x()).arg(m_to.y())); - command.append(" --output-gpx-track"); - if (m_shortest->isChecked()) { - command.append(" --shortest"); - file = "shortest-track.gpx"; - } else { - command.append(" --quickest"); - file = "quickest-track.gpx"; - } - out << command+"\n"; - if (!m_swap.isEmpty()) { - out << "swapon "+m_swap+"\n"; - } - out << QString("mv %1 %2\n").arg(file).arg(name);; - out << QString("exec ./NanoMap.sh %1\n").arg(name); - qApp->quit(); - } -} - -void RoutingWidget::loadConfig() -{ - QSettings set(QDir::homePath()+"/Maps/nanomap.conf", QSettings::NativeFormat); - - set.beginGroup("routino"); - - m_routino = set.value("router", "router").toString(); - m_dir = set.value("dir", ".").toString(); - m_prefix = set.value("prefix", "world").toString(); - m_profiles = set.value("profiles", "/usr/share/routino/routino-profiles.xml").toString(); - m_swap = set.value("swap", "").toString(); - - set.endGroup(); -} - diff --git a/searchwidget.cpp b/searchwidget.cpp new file mode 100644 index 0000000..a43bb9c --- /dev/null +++ b/searchwidget.cpp @@ -0,0 +1,156 @@ +/* + * Copyright 2010 Niels Kummerfeldt + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, + * Boston, MA 02110-1301 USA + */ + +#include "searchwidget.h" + +#include +#include +#include +#include +#include +#include +#include + +SearchWidget::SearchWidget(QWidget *parent) + : QWidget(parent), + m_addrLookup(0), + m_cityCoordinates(), + m_loaded(false), + m_city(new QLineEdit(this)), + m_cityList(new QListWidget(this)), + m_street(new QLineEdit(this)), + m_streetList(new QListWidget(this)) +{ + QGridLayout *layout = new QGridLayout(this); + layout->setContentsMargins(0, 0, 0, 0); + + m_city->setPlaceholderText("Enter city name"); + layout->addWidget(m_city, 0, 0, 1, 2); + connect(m_city, SIGNAL(textEdited(QString)), this, SLOT(cityChanged(QString))); + connect(m_city, SIGNAL(editingFinished()), this, SLOT(cityEntered())); + + layout->addWidget(m_cityList, 1, 0, 1, 2); + connect(m_cityList, SIGNAL(itemActivated(QListWidgetItem*)), + this, SLOT(citySelected(QListWidgetItem*))); + + m_street->setPlaceholderText("Enter street name"); + layout->addWidget(m_street, 2, 0, 1, 2); + connect(m_street, SIGNAL(textEdited(QString)), this, SLOT(streetChanged(QString))); + + layout->addWidget(m_streetList, 3, 0, 1, 2); + connect(m_streetList, SIGNAL(itemActivated(QListWidgetItem*)), + this, SLOT(streetSelected(QListWidgetItem*))); + + QPushButton *back = new QPushButton("&Cancel", this); + layout->addWidget(back, 4, 1); + connect(back, SIGNAL(clicked()), this, SIGNAL(back())); + + QSettings set(QDir::homePath()+"/Maps/nanomap.conf", QSettings::NativeFormat); + set.beginGroup("monav"); + QString dataDir = set.value("datadir").toString(); + QString addrLookupLib = set.value("addresslookup", + "/usr/lib/monav/libunicodetournamenttrieclient.so").toString(); + set.endGroup(); + + QSettings pluginSettings(dataDir+"/plugins.ini", QSettings::IniFormat); + QString addrLookupName = pluginSettings.value("addressLookup").toString(); + + QPluginLoader Loader(addrLookupLib); + QObject *plugin = Loader.instance(); + if (plugin) { + m_addrLookup = qobject_cast(plugin); + if (m_addrLookup) { + m_addrLookup->SetInputDirectory(dataDir); + m_loaded = m_addrLookup->LoadData(); + } + } + + resize(320, 240); +} + +SearchWidget::~SearchWidget() +{ +} + +void SearchWidget::cityChanged(const QString &city) +{ + if (m_loaded) { + QStringList suggestions, inputSuggestions; + bool found = m_addrLookup->GetPlaceSuggestions(city, 20, &suggestions, &inputSuggestions); + if (found) { + QCompleter *c = new QCompleter(suggestions, this); + c->setCompletionMode(QCompleter::PopupCompletion); + c->setCaseSensitivity(Qt::CaseInsensitive); + m_city->setCompleter(c); + } + } +} + +void SearchWidget::cityEntered() +{ + if (m_loaded) { + QVector placeIDs; + QVector placeCoordinates; + if (m_addrLookup->GetPlaceData(m_city->text(), &placeIDs, &placeCoordinates)) { + QListWidgetItem *item; + m_cityList->clear(); + m_cityCoordinates.clear(); + for (int i = 0; i < placeIDs.count(); ++i) { + GPSCoordinate coord = placeCoordinates.at(i).ToGPSCoordinate(); + QString name = QString("%1: %2 / %3").arg(m_city->text()) + .arg(coord.latitude).arg(coord.longitude); + item = new QListWidgetItem(name, m_cityList); + item->setData(Qt::UserRole, placeIDs.at(i)); + m_cityList->addItem(item); + m_cityCoordinates.insert(placeIDs.at(i), coord); + } + } + } +} + +void SearchWidget::citySelected(QListWidgetItem *item) +{ + int id = item->data(Qt::UserRole).toInt(); + GPSCoordinate c = m_cityCoordinates.value(id); + emit centerOn(c.longitude, c.latitude); +} + +void SearchWidget::streetChanged(const QString &street) +{ + if (m_loaded) { + m_addrLookup->SelectPlace(m_cityList->currentItem()->data(Qt::UserRole).toInt()); + QStringList suggestions, inputSuggestions; + bool found = m_addrLookup->GetStreetSuggestions(street, 20, &suggestions, &inputSuggestions); + if (found) { + m_streetList->clear(); + m_streetList->insertItems(0, suggestions); + } + } +} + +void SearchWidget::streetSelected(QListWidgetItem *item) +{ + QVector segmentLength; + QVector coordinates; + if (m_addrLookup->GetStreetData(item->text(), &segmentLength, &coordinates)) { + GPSCoordinate coord = coordinates.first().ToGPSCoordinate(); + emit centerOn(coord.longitude, coord.latitude); + } +} + diff --git a/routingwidget.h b/searchwidget.h similarity index 56% rename from routingwidget.h rename to searchwidget.h index 4eaf7da..d91eb23 100644 --- a/routingwidget.h +++ b/searchwidget.h @@ -17,42 +17,41 @@ * Boston, MA 02110-1301 USA */ -#ifndef ROUTINGWIDGET_H -#define ROUTINGWIDGET_H +#ifndef SEARCHWIDGET_H +#define SEARCHWIDGET_H -#include -#include #include -#include -#include +#include -class RoutingWidget : public QWidget +#include "interfaces/iaddresslookup.h" + +class SearchWidget : public QWidget { Q_OBJECT public: - RoutingWidget(QWidget *parent = 0); - ~RoutingWidget(); - - void setFrom(const QPointF &from); - void setTo(const QPointF &to); + SearchWidget(QWidget *parent = 0); + ~SearchWidget(); signals: void back(); + void centerOn(qreal lon, qreal lat); private slots: - void findRoute(); + void cityChanged(const QString &city); + void cityEntered(); + void citySelected(QListWidgetItem *item); + void streetChanged(const QString &street); + void streetSelected(QListWidgetItem *item); private: - void loadConfig(); - - QLineEdit *m_name; - QComboBox *m_transport; - QRadioButton *m_quickest; - QRadioButton *m_shortest; - QString m_routino, m_dir, m_prefix, m_profiles, m_swap; - QPointF m_from, m_to; - QLabel *m_fromLabel, *m_toLabel; + IAddressLookup *m_addrLookup; + QHash m_cityCoordinates; + bool m_loaded; + QLineEdit *m_city; + QListWidget *m_cityList; + QLineEdit *m_street; + QListWidget *m_streetList; }; -#endif // ROUTINGWIDGET_H +#endif // SEARCHWIDGET_H