Initial commit
[iamhere] / src / window.cpp
1 /*
2  * I Am Here(iamhere) - an application to find user's location
3  * address.
4  * Copyright (C) 2010 Vytarani Mathane <vytarani.mathane@gmail.com>.
5  *
6  * This file is part of I Am Here.
7  *
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.
12  *
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.
17  *
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/>.
20  */
21 #include <QtGui>
22 #include <QWidget>
23 #include <QVBoxLayout>
24 #include <QPushButton>
25 #include <QGeoPositionInfoSource>
26 #include <QGeoSearchManager>
27 #include <QGeoServiceProvider>
28 #include <QNetworkProxyFactory>
29 #include <QTimer>
30
31 #include "window.h"
32 using namespace QtMobility;
33 Window::Window(QWidget * parent)
34  : QMainWindow(parent)
35 {
36         m_flag = 1;
37         m_source = QGeoPositionInfoSource::createDefaultSource(this);
38         if (m_source == 0)
39                 QMessageBox::information(this, tr("Info"),
40                                          tr("Not a valid geo source!!"));
41
42         if (m_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();
48         }
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 *)),
55                                          this,
56                                          SLOT(replyFinished
57                                               (QGeoSearchReply *)));
58                         QObject::connect(m_searchManager,
59                                          SIGNAL(error
60                                                 (QGeoSearchReply *,
61                                                  QGeoSearchReply::Error,
62                                                  QString)), this,
63                                          SLOT(resultsError
64                                               (QGeoSearchReply *,
65                                                QGeoSearchReply::Error,
66                                                QString)));
67                 }
68         }
69
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);
109 }
110
111 void Window::handlePressed()
112 {
113
114         if (m_source) {
115                 m_textlabel->clear();
116                 m_source->stopUpdates();
117                 m_source->startUpdates();
118                 m_timer->start(1000);
119         } else
120                 QMessageBox::information(this, tr("Info"),
121                                          tr("Source not present!!"));
122 }
123
124 void Window::handlePressedClose()
125 {
126         exit(0);
127 }
128
129 void Window::replyFinished(QGeoSearchReply * reply)
130 {
131         QList < QGeoPlace > places = reply->places();
132         QString s;
133         if (!places[0].address().isEmpty()) {
134                 s = places[0].address().postCode();
135                 s.append(",");
136                 s.append(places[0].address().thoroughfareName());
137                 s.append(",");
138                 s.append(places[0].address().thoroughfareNumber());
139                 s.append(",");
140                 s.append(places[0].address().county());
141                 s.append(",");
142                 s.append(places[0].address().city());
143                 s.append(",");
144                 s.append(places[0].address().district());
145                 s.append(",");
146                 s.append(places[0].address().state());
147                 s.append(",");
148                 s.append(places[0].address().country());
149         } else {
150                 s.sprintf("places length is %d", places.length());
151         }
152         m_textlabel->setText(s);
153         m_timer->stop();
154         QString image_n = ":/bulb.png";
155         QImage image(image_n, 0);
156         m_imagelabel->setPixmap(QPixmap::fromImage(image, Qt::ColorOnly));
157         this->activateWindow();
158 }
159
160 void Window::resultsError(QGeoSearchReply * reply,
161                           QGeoSearchReply::Error errorCode, QString errorString)
162 {
163         m_source->stopUpdates();
164         QString s = errorString;
165         QMessageBox msgBox;
166         msgBox.setText(s);
167         msgBox.exec();
168         QMessageBox::information(this, tr("Info"),
169                                  tr("Try again after conneting to internet!"));
170         exit(1);
171 }
172
173 void Window::positionUpdated(const QGeoPositionInfo & info)
174 {
175         QString s;
176         m_coord = info.coordinate();
177         if (m_searchManager) {
178                 if (m_searchManager->supportsGeocoding())
179
180                         QGeoSearchReply *reply =
181                             m_searchManager->geocode(m_coord);
182         } else
183                 QMessageBox::information(this, tr("Info"),
184                                          tr("search manager not present!!"));
185 }
186
187 void Window::advance()
188 {
189         this->activateWindow();
190         if (m_flag == 1) {
191                 QString image_n = ":/man2.png";
192                 QImage image(image_n, 0);
193                 m_imagelabel->setPixmap(QPixmap::
194                                         fromImage(image, Qt::ColorOnly));
195                 m_flag = 0;
196         } else {
197                 QString image_na = ":/man1.png";
198                 QImage image(image_na, 0);
199                 m_imagelabel->setPixmap(QPixmap::
200                                         fromImage(image, Qt::ColorOnly));
201                 m_flag = 1;
202         }
203 }