Root/
1 | /* |
2 | * Copyright 2010-2011 Niels Kummerfeldt <niels.kummerfeldt@tu-harburg.de> |
3 | * |
4 | * This program is free software; you can redistribute it and/or modify |
5 | * it under the terms of the GNU General Public License as published by |
6 | * the Free Software Foundation; either version 2 of the License, or |
7 | * (at your option) any later version. |
8 | * |
9 | * This program is distributed in the hope that it will be useful, |
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
12 | * GNU General Public License for more details. |
13 | * |
14 | * You should have received a copy of the GNU General Public License |
15 | * along with this program; if not, write to the Free Software |
16 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, |
17 | * Boston, MA 02110-1301 USA |
18 | */ |
19 | |
20 | #include "searchwidget.h" |
21 | |
22 | #include <QtCore/QDebug> |
23 | #include <QtCore/QDir> |
24 | #include <QtCore/QPluginLoader> |
25 | #include <QtCore/QSettings> |
26 | #include <QtGui/QCompleter> |
27 | #include <QtGui/QLayout> |
28 | #include <QtGui/QPushButton> |
29 | |
30 | SearchWidget::SearchWidget(QWidget *parent) |
31 | : QWidget(parent), |
32 | m_addrLookup(0), |
33 | m_cityCoordinates(), |
34 | m_loaded(false), |
35 | m_city(new QLineEdit(this)), |
36 | m_cityList(new QListWidget(this)), |
37 | m_street(new QLineEdit(this)), |
38 | m_streetList(new QListWidget(this)) |
39 | { |
40 | QGridLayout *layout = new QGridLayout(this); |
41 | layout->setContentsMargins(0, 0, 0, 0); |
42 | |
43 | layout->addWidget(m_city, 0, 0, 1, 2); |
44 | connect(m_city, SIGNAL(textEdited(QString)), this, SLOT(cityChanged(QString))); |
45 | connect(m_city, SIGNAL(editingFinished()), this, SLOT(cityEntered())); |
46 | |
47 | layout->addWidget(m_cityList, 1, 0, 1, 2); |
48 | connect(m_cityList, SIGNAL(itemActivated(QListWidgetItem*)), |
49 | this, SLOT(citySelected(QListWidgetItem*))); |
50 | |
51 | #if QT_VERSION >= 0x040700 |
52 | m_city->setPlaceholderText("Enter city name"); |
53 | m_street->setPlaceholderText("Enter street name"); |
54 | #endif |
55 | layout->addWidget(m_street, 2, 0, 1, 2); |
56 | connect(m_street, SIGNAL(textEdited(QString)), this, SLOT(streetChanged(QString))); |
57 | |
58 | layout->addWidget(m_streetList, 3, 0, 1, 2); |
59 | connect(m_streetList, SIGNAL(itemActivated(QListWidgetItem*)), |
60 | this, SLOT(streetSelected(QListWidgetItem*))); |
61 | |
62 | QPushButton *back = new QPushButton("&Back", this); |
63 | back->setIcon(QIcon(":cancel.png")); |
64 | layout->addWidget(back, 4, 1); |
65 | connect(back, SIGNAL(clicked()), this, SIGNAL(back())); |
66 | |
67 | QSettings set(QDir::homePath()+"/Maps/nanomap.conf", QSettings::NativeFormat); |
68 | set.beginGroup("monav"); |
69 | QString dataDir = set.value("datadir").toString(); |
70 | QString addrLookupLib = set.value("addresslookup", |
71 | "/usr/lib/monav/libunicodetournamenttrieclient.so").toString(); |
72 | set.endGroup(); |
73 | |
74 | QSettings pluginSettings(dataDir+"/plugins.ini", QSettings::IniFormat); |
75 | QString addrLookupName = pluginSettings.value("addressLookup").toString(); |
76 | |
77 | QPluginLoader Loader(addrLookupLib); |
78 | QObject *plugin = Loader.instance(); |
79 | if (plugin) { |
80 | m_addrLookup = qobject_cast<IAddressLookup*>(plugin); |
81 | if (m_addrLookup) { |
82 | m_addrLookup->SetInputDirectory(dataDir); |
83 | m_loaded = m_addrLookup->LoadData(); |
84 | } |
85 | } |
86 | |
87 | resize(320, 240); |
88 | } |
89 | |
90 | SearchWidget::~SearchWidget() |
91 | { |
92 | } |
93 | |
94 | void SearchWidget::cityChanged(const QString &city) |
95 | { |
96 | if (m_loaded) { |
97 | QStringList suggestions, inputSuggestions; |
98 | bool found = m_addrLookup->GetPlaceSuggestions(city, 20, &suggestions, &inputSuggestions); |
99 | if (found) { |
100 | QCompleter *c = new QCompleter(suggestions, this); |
101 | c->setCompletionMode(QCompleter::PopupCompletion); |
102 | c->setCaseSensitivity(Qt::CaseInsensitive); |
103 | m_city->setCompleter(c); |
104 | } |
105 | } |
106 | } |
107 | |
108 | void SearchWidget::cityEntered() |
109 | { |
110 | if (m_loaded) { |
111 | QVector<int> placeIDs; |
112 | QVector<UnsignedCoordinate> placeCoordinates; |
113 | if (m_addrLookup->GetPlaceData(m_city->text(), &placeIDs, &placeCoordinates)) { |
114 | QListWidgetItem *item; |
115 | m_cityList->clear(); |
116 | m_cityCoordinates.clear(); |
117 | for (int i = 0; i < placeIDs.count(); ++i) { |
118 | GPSCoordinate coord = placeCoordinates.at(i).ToGPSCoordinate(); |
119 | QString name = QString("%1: %2 / %3").arg(m_city->text()) |
120 | .arg(coord.latitude).arg(coord.longitude); |
121 | item = new QListWidgetItem(name, m_cityList); |
122 | item->setData(Qt::UserRole, placeIDs.at(i)); |
123 | m_cityList->addItem(item); |
124 | m_cityCoordinates.insert(placeIDs.at(i), coord); |
125 | } |
126 | } |
127 | } |
128 | } |
129 | |
130 | void SearchWidget::citySelected(QListWidgetItem *item) |
131 | { |
132 | int id = item->data(Qt::UserRole).toInt(); |
133 | GPSCoordinate c = m_cityCoordinates.value(id); |
134 | emit centerOn(c.longitude, c.latitude); |
135 | } |
136 | |
137 | void SearchWidget::streetChanged(const QString &street) |
138 | { |
139 | if (m_loaded) { |
140 | QListWidgetItem *item = m_cityList->currentItem(); |
141 | if (item) { |
142 | m_addrLookup->SelectPlace(item->data(Qt::UserRole).toInt()); |
143 | } |
144 | QStringList suggestions, inputSuggestions; |
145 | bool found = m_addrLookup->GetStreetSuggestions(street, 20, &suggestions, &inputSuggestions); |
146 | if (found) { |
147 | m_streetList->clear(); |
148 | m_streetList->insertItems(0, suggestions); |
149 | } |
150 | } |
151 | } |
152 | |
153 | void SearchWidget::streetSelected(QListWidgetItem *item) |
154 | { |
155 | QVector<int> segmentLength; |
156 | QVector<UnsignedCoordinate> coordinates; |
157 | if (m_addrLookup->GetStreetData(item->text(), &segmentLength, &coordinates)) { |
158 | GPSCoordinate coord = coordinates.first().ToGPSCoordinate(); |
159 | emit centerOn(coord.longitude, coord.latitude); |
160 | } |
161 | } |
162 | |
163 |
Branches:
master