2 * I Am Here(iamhere) - an application to find user's location
4 * Copyright (C) 2010 Vytarani Mathane <vytarani.mathane@gmail.com>.
6 * This file is part of I Am Here.
8 * I Am Here is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation, either version 3 of the License, or
11 * (at your option) any later version.
13 * I Am Here is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with I Am Here. If not, see <http://www.gnu.org/licenses/>.
23 #include <QVBoxLayout>
24 #include <QPushButton>
25 #include <QGeoPositionInfoSource>
26 #include <QGeoSearchManager>
27 #include <QGeoServiceProvider>
28 #include <QNetworkProxyFactory>
32 using namespace QtMobility;
33 Window::Window(QWidget * parent)
37 m_source = QGeoPositionInfoSource::createDefaultSource(this);
39 QMessageBox::information(this, tr("Info"),
40 tr("Not a valid geo source!!"));
43 connect(m_source, SIGNAL(positionUpdated(QGeoPositionInfo)),
44 this, SLOT(positionUpdated(QGeoPositionInfo)));
45 connect(m_source, SIGNAL(updateTimeout()), this,
46 SLOT(updateTimeout()));
47 m_source->startUpdates();
49 m_serviceProvider = new QGeoServiceProvider("nokia");
50 if (m_serviceProvider->error() == QGeoServiceProvider::NoError) {
51 m_searchManager = m_serviceProvider->searchManager();
52 if (m_searchManager) {
53 QObject::connect(m_searchManager,
54 SIGNAL(finished(QGeoSearchReply *)),
57 (QGeoSearchReply *)));
58 QObject::connect(m_searchManager,
61 QGeoSearchReply::Error,
65 QGeoSearchReply::Error,
70 setWindowTitle(tr("I am Here"));
71 //m_source->startUpdates();
72 QWidget *layout_widget = new QWidget(this);
73 //layout_widget->setMinimumSize(550,380);
74 //layout_widget->setMaximumSize(550,380);
75 QString image_name = ":/man1.png";
76 QImage image(image_name, 0);
77 m_imagelabel = new QLabel(layout_widget);
78 m_imagelabel->setPixmap(QPixmap::fromImage(image, Qt::ColorOnly));
79 QWidget *hlayout_widget = new QWidget(this);
80 QHBoxLayout *hlayout = new QHBoxLayout(hlayout_widget);
81 QPushButton *button1 = new QPushButton(tr("Try Again"), hlayout_widget);
82 QPushButton *button2 = new QPushButton(tr("Close"), hlayout_widget);
83 button1->setFixedSize(200, 50);
84 //QPalette palette = button1->palette();
85 //palette.setColor(QPalette::Button,QColor(Qt::darkGreen));
86 //button1->setPalette(palette);
87 //button1->setPalette(QPalette(QPalette::Button,QColor(Qt::darkGreen)));
88 //button1->setAutoFillBackground(true);
89 button1->setStyleSheet("background-color: darkGreen");
90 button2->setFixedSize(200, 50);
91 //button2->setAutoFillBackground(true);
92 button2->setStyleSheet("background-color: darkGreen");
93 hlayout->addWidget(button1);
94 hlayout->addWidget(button2);
95 m_textlabel = new QLabel(layout_widget);
96 QVBoxLayout *layout = new QVBoxLayout(layout_widget);
97 layout->addWidget(m_textlabel);
98 layout->addWidget(m_imagelabel);
99 layout->addWidget(hlayout_widget);
100 setCentralWidget(layout_widget);
101 setPalette(QPalette(Qt::white));
102 m_textlabel->setPalette(QPalette(Qt::yellow));
103 m_textlabel->setAutoFillBackground(true);
104 connect(button1, SIGNAL(pressed()), this, SLOT(handlePressed()));
105 connect(button2, SIGNAL(pressed()), this, SLOT(handlePressedClose()));
106 m_timer = new QTimer(this);
107 connect(m_timer, SIGNAL(timeout()), this, SLOT(advance()));
108 m_timer->start(1000);
111 void Window::handlePressed()
115 m_textlabel->clear();
116 m_source->stopUpdates();
117 m_source->startUpdates();
118 m_timer->start(1000);
120 QMessageBox::information(this, tr("Info"),
121 tr("Source not present!!"));
124 void Window::handlePressedClose()
129 void Window::replyFinished(QGeoSearchReply * reply)
131 QList < QGeoPlace > places = reply->places();
133 if (!places[0].address().isEmpty()) {
134 s = places[0].address().postCode();
136 s.append(places[0].address().thoroughfareName());
138 s.append(places[0].address().thoroughfareNumber());
140 s.append(places[0].address().county());
142 s.append(places[0].address().city());
144 s.append(places[0].address().district());
146 s.append(places[0].address().state());
148 s.append(places[0].address().country());
150 s.sprintf("places length is %d", places.length());
152 m_textlabel->setText(s);
154 QString image_n = ":/bulb.png";
155 QImage image(image_n, 0);
156 m_imagelabel->setPixmap(QPixmap::fromImage(image, Qt::ColorOnly));
157 this->activateWindow();
160 void Window::resultsError(QGeoSearchReply * reply,
161 QGeoSearchReply::Error errorCode, QString errorString)
163 m_source->stopUpdates();
164 QString s = errorString;
168 QMessageBox::information(this, tr("Info"),
169 tr("Try again after conneting to internet!"));
173 void Window::positionUpdated(const QGeoPositionInfo & info)
176 m_coord = info.coordinate();
177 if (m_searchManager) {
178 if (m_searchManager->supportsGeocoding())
180 QGeoSearchReply *reply =
181 m_searchManager->geocode(m_coord);
183 QMessageBox::information(this, tr("Info"),
184 tr("search manager not present!!"));
187 void Window::advance()
189 this->activateWindow();
191 QString image_n = ":/man2.png";
192 QImage image(image_n, 0);
193 m_imagelabel->setPixmap(QPixmap::
194 fromImage(image, Qt::ColorOnly));
197 QString image_na = ":/man1.png";
198 QImage image(image_na, 0);
199 m_imagelabel->setPixmap(QPixmap::
200 fromImage(image, Qt::ColorOnly));