Initial commit
[iamhere] / src / window.cpp
diff --git a/src/window.cpp b/src/window.cpp
new file mode 100644 (file)
index 0000000..ae6b5c3
--- /dev/null
@@ -0,0 +1,203 @@
+/*
+ * I Am Here(iamhere) - an application to find user's location
+ * address.
+ * Copyright (C) 2010 Vytarani Mathane <vytarani.mathane@gmail.com>.
+ *
+ * This file is part of I Am Here.
+ *
+ * I Am Here 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 3 of the License, or
+ * (at your option) any later version.
+ *
+ * I Am Here 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 I Am Here.  If not, see <http://www.gnu.org/licenses/>.
+ */
+#include <QtGui>
+#include <QWidget>
+#include <QVBoxLayout>
+#include <QPushButton>
+#include <QGeoPositionInfoSource>
+#include <QGeoSearchManager>
+#include <QGeoServiceProvider>
+#include <QNetworkProxyFactory>
+#include <QTimer>
+
+#include "window.h"
+using namespace QtMobility;
+Window::Window(QWidget * parent)
+ : QMainWindow(parent)
+{
+       m_flag = 1;
+       m_source = QGeoPositionInfoSource::createDefaultSource(this);
+       if (m_source == 0)
+               QMessageBox::information(this, tr("Info"),
+                                        tr("Not a valid geo source!!"));
+
+       if (m_source) {
+               connect(m_source, SIGNAL(positionUpdated(QGeoPositionInfo)),
+                       this, SLOT(positionUpdated(QGeoPositionInfo)));
+               connect(m_source, SIGNAL(updateTimeout()), this,
+                       SLOT(updateTimeout()));
+               m_source->startUpdates();
+       }
+       m_serviceProvider = new QGeoServiceProvider("nokia");
+       if (m_serviceProvider->error() == QGeoServiceProvider::NoError) {
+               m_searchManager = m_serviceProvider->searchManager();
+               if (m_searchManager) {
+                       QObject::connect(m_searchManager,
+                                        SIGNAL(finished(QGeoSearchReply *)),
+                                        this,
+                                        SLOT(replyFinished
+                                             (QGeoSearchReply *)));
+                       QObject::connect(m_searchManager,
+                                        SIGNAL(error
+                                               (QGeoSearchReply *,
+                                                QGeoSearchReply::Error,
+                                                QString)), this,
+                                        SLOT(resultsError
+                                             (QGeoSearchReply *,
+                                              QGeoSearchReply::Error,
+                                              QString)));
+               }
+       }
+
+       setWindowTitle(tr("I am Here"));
+       //m_source->startUpdates();
+       QWidget *layout_widget = new QWidget(this);
+       //layout_widget->setMinimumSize(550,380);
+       //layout_widget->setMaximumSize(550,380);
+       QString image_name = ":/man1.png";
+       QImage image(image_name, 0);
+       m_imagelabel = new QLabel(layout_widget);
+       m_imagelabel->setPixmap(QPixmap::fromImage(image, Qt::ColorOnly));
+       QWidget *hlayout_widget = new QWidget(this);
+       QHBoxLayout *hlayout = new QHBoxLayout(hlayout_widget);
+       QPushButton *button1 = new QPushButton(tr("Try Again"), hlayout_widget);
+       QPushButton *button2 = new QPushButton(tr("Close"), hlayout_widget);
+       button1->setFixedSize(200, 50);
+       //QPalette palette = button1->palette();
+       //palette.setColor(QPalette::Button,QColor(Qt::darkGreen));
+       //button1->setPalette(palette);
+       //button1->setPalette(QPalette(QPalette::Button,QColor(Qt::darkGreen)));
+       //button1->setAutoFillBackground(true);
+       button1->setStyleSheet("background-color: darkGreen");
+       button2->setFixedSize(200, 50);
+       //button2->setAutoFillBackground(true);
+       button2->setStyleSheet("background-color: darkGreen");
+       hlayout->addWidget(button1);
+       hlayout->addWidget(button2);
+       m_textlabel = new QLabel(layout_widget);
+       QVBoxLayout *layout = new QVBoxLayout(layout_widget);
+       layout->addWidget(m_textlabel);
+       layout->addWidget(m_imagelabel);
+       layout->addWidget(hlayout_widget);
+       setCentralWidget(layout_widget);
+       setPalette(QPalette(Qt::white));
+       m_textlabel->setPalette(QPalette(Qt::yellow));
+       m_textlabel->setAutoFillBackground(true);
+       connect(button1, SIGNAL(pressed()), this, SLOT(handlePressed()));
+       connect(button2, SIGNAL(pressed()), this, SLOT(handlePressedClose()));
+       m_timer = new QTimer(this);
+       connect(m_timer, SIGNAL(timeout()), this, SLOT(advance()));
+       m_timer->start(1000);
+}
+
+void Window::handlePressed()
+{
+
+       if (m_source) {
+               m_textlabel->clear();
+               m_source->stopUpdates();
+               m_source->startUpdates();
+               m_timer->start(1000);
+       } else
+               QMessageBox::information(this, tr("Info"),
+                                        tr("Source not present!!"));
+}
+
+void Window::handlePressedClose()
+{
+       exit(0);
+}
+
+void Window::replyFinished(QGeoSearchReply * reply)
+{
+       QList < QGeoPlace > places = reply->places();
+       QString s;
+       if (!places[0].address().isEmpty()) {
+               s = places[0].address().postCode();
+               s.append(",");
+               s.append(places[0].address().thoroughfareName());
+               s.append(",");
+               s.append(places[0].address().thoroughfareNumber());
+               s.append(",");
+               s.append(places[0].address().county());
+               s.append(",");
+               s.append(places[0].address().city());
+               s.append(",");
+               s.append(places[0].address().district());
+               s.append(",");
+               s.append(places[0].address().state());
+               s.append(",");
+               s.append(places[0].address().country());
+       } else {
+               s.sprintf("places length is %d", places.length());
+       }
+       m_textlabel->setText(s);
+       m_timer->stop();
+       QString image_n = ":/bulb.png";
+       QImage image(image_n, 0);
+       m_imagelabel->setPixmap(QPixmap::fromImage(image, Qt::ColorOnly));
+       this->activateWindow();
+}
+
+void Window::resultsError(QGeoSearchReply * reply,
+                         QGeoSearchReply::Error errorCode, QString errorString)
+{
+       m_source->stopUpdates();
+       QString s = errorString;
+       QMessageBox msgBox;
+       msgBox.setText(s);
+       msgBox.exec();
+       QMessageBox::information(this, tr("Info"),
+                                tr("Try again after conneting to internet!"));
+       exit(1);
+}
+
+void Window::positionUpdated(const QGeoPositionInfo & info)
+{
+       QString s;
+       m_coord = info.coordinate();
+       if (m_searchManager) {
+               if (m_searchManager->supportsGeocoding())
+
+                       QGeoSearchReply *reply =
+                           m_searchManager->geocode(m_coord);
+       } else
+               QMessageBox::information(this, tr("Info"),
+                                        tr("search manager not present!!"));
+}
+
+void Window::advance()
+{
+       this->activateWindow();
+       if (m_flag == 1) {
+               QString image_n = ":/man2.png";
+               QImage image(image_n, 0);
+               m_imagelabel->setPixmap(QPixmap::
+                                       fromImage(image, Qt::ColorOnly));
+               m_flag = 0;
+       } else {
+               QString image_na = ":/man1.png";
+               QImage image(image_na, 0);
+               m_imagelabel->setPixmap(QPixmap::
+                                       fromImage(image, Qt::ColorOnly));
+               m_flag = 1;
+       }
+}