mainwindow.cpp 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. #include "mainwindow.h"
  2. #include "ui_mainwindow.h"
  3. #include <QtGui>
  4. #include <QtNetwork/QtNetwork>
  5. #include <QtXml/QtXml>
  6. #include <QtWebKit>
  7. #include <QDebug>
  8. #include <QtGui/QX11Info>
  9. #include <X11/Xlib.h>
  10. #include <X11/Xatom.h>
  11. MainWindow::MainWindow(QWidget *parent) :
  12. QMainWindow(parent),
  13. ui(new Ui::MainWindow),
  14. m_latitude(48.1142),
  15. m_longitude(-1.664),
  16. once(true),
  17. m_currentZoom(7)
  18. {
  19. ui->setupUi(this);
  20. qRegisterMetaType<QObjectList>();
  21. /*
  22. METTRE TOUT LE JAVASCRIPT DANS LE FICHIER
  23. COMME CA MAPPY PEUT ETRE CHANGE PAR N'IMPORTE QUELLE API
  24. */
  25. QWebSettings* settings = QWebSettings::globalSettings();
  26. settings->setAttribute(QWebSettings::JavascriptEnabled, true);
  27. settings->setAttribute(QWebSettings::DeveloperExtrasEnabled, true);
  28. connect(ui->webView->page()->mainFrame(), SIGNAL(javaScriptWindowObjectCleared()), this, SLOT(injectJavascriptIntoWindowObject()));
  29. connect(ui->webView, SIGNAL(loadFinished(bool)), this, SLOT(loadFinished(bool)));
  30. //ui->webView->load(QString("http://www.google.com"));
  31. QGeoPositionInfoSource *source = QGeoPositionInfoSource::createDefaultSource(this);
  32. if (source) {
  33. connect(source, SIGNAL(positionUpdated(QGeoPositionInfo)),
  34. this, SLOT(positionUpdated(QGeoPositionInfo)));
  35. source->startUpdates();
  36. }
  37. grabZoomKeys(true);
  38. }
  39. void MainWindow::grabZoomKeys(bool grab)
  40. {
  41. if (!winId()) {
  42. qWarning("Can't grab keys unless we have a window id");
  43. return;
  44. }
  45. unsigned long val = (grab) ? 1 : 0;
  46. Atom atom = XInternAtom(QX11Info::display(), "_HILDON_ZOOM_KEY_ATOM", False);
  47. if (!atom) {
  48. qWarning("Unable to obtain _HILDON_ZOOM_KEY_ATOM. This example will only work "
  49. "on a Maemo 5 device!");
  50. return;
  51. }
  52. XChangeProperty (QX11Info::display(),
  53. winId(),
  54. atom,
  55. XA_INTEGER,
  56. 32,
  57. PropModeReplace,
  58. reinterpret_cast<unsigned char *>(&val),
  59. 1);
  60. }
  61. void MainWindow::keyPressEvent(QKeyEvent* event)
  62. {
  63. switch (event->key()) {
  64. case Qt::Key_F7: {
  65. qDebug() << "zoom in" << endl;
  66. //QMouseEvent fakeEvent(QEvent::MouseButtonDblClick, QPoint(10,10), Qt::LeftButton, 0, 0);
  67. //QApplication::sendEvent(ui->webView, &fakeEvent);
  68. QString javascript = QString("map.setZoomLevel(%1);").arg(++m_currentZoom);
  69. ui->webView->page()->mainFrame()->evaluateJavaScript(javascript);
  70. event->accept();
  71. break;
  72. }
  73. case Qt::Key_F8:
  74. qDebug() << "zoom out" << endl;
  75. QString javascript = QString("map.setZoomLevel(%1);").arg(--m_currentZoom);
  76. ui->webView->page()->mainFrame()->evaluateJavaScript(javascript);
  77. event->accept();
  78. break;
  79. }
  80. QWidget::keyPressEvent(event);
  81. }
  82. void MainWindow::positionUpdated(const QGeoPositionInfo &info)
  83. {
  84. // qDebug() << "Position updated:" << info;
  85. if (once) {
  86. // OU Mieux : source->stopUpdates()
  87. once = false;
  88. // for now use hardcoded value for rennes
  89. //m_latitude = info.coordinate().latitude();
  90. //m_longitude = info.coordinate().longitude();
  91. //ui->lineEdit->setText(QString::number(m_longitude));
  92. ui->webView->load(QString("qrc:/html/map.html"));
  93. }
  94. }
  95. MainWindow::~MainWindow()
  96. {
  97. delete ui;
  98. }
  99. void MainWindow::debug(const QString& value) {
  100. qDebug("%s", qPrintable(value));
  101. }
  102. void MainWindow::injectJavascriptIntoWindowObject() {
  103. qDebug() << "load qt object" << endl;
  104. ui->webView->page()->mainFrame()->addToJavaScriptWindowObject("monmanager", this);
  105. }
  106. void MainWindow::loadFinished(bool ok) {
  107. QFile file(QString(":/script/map.js"));
  108. if (file.open(QIODevice::ReadOnly)) {
  109. QString javascript = QString::fromUtf8(file.readAll());
  110. ui->webView->page()->mainFrame()->evaluateJavaScript(javascript);
  111. }
  112. emit displayUser(m_longitude, m_latitude);
  113. /*
  114. QString javascript = "var map = new Mappy.api.map.Map({ container:'#mymap'});";
  115. javascript += QString("map.setCenter(new Mappy.api.geo.Coordinates(%1, %2),5);").arg(m_longitude).arg(m_latitude);
  116. ui->webView->page()->mainFrame()->evaluateJavaScript(javascript);
  117. */
  118. QNetworkAccessManager * manager = new QNetworkAccessManager(this);
  119. connect(manager, SIGNAL(finished(QNetworkReply*)), SLOT(dataReceived(QNetworkReply*)));
  120. QUrl url;
  121. url.setScheme("http");
  122. url.setEncodedHost("data.keolis-rennes.com");
  123. url.setEncodedPath("/xml/");
  124. url.addEncodedQueryItem("version", "1.0");
  125. url.addEncodedQueryItem("key", QString("JW48LBH2QN0A5M9").toAscii());
  126. url.addEncodedQueryItem("cmd", QString("getstation").toAscii());
  127. url.addEncodedQueryItem("param[request]", "proximity");
  128. url.addEncodedQueryItem("param[mode]", "coord");
  129. url.addEncodedQueryItem("param[lat]", QString::number(m_latitude).toAscii());
  130. url.addEncodedQueryItem("param[long]", QString::number(m_longitude).toAscii());
  131. //QUrl url(QString("http://data.keolis-rennes.com/xml/?version=1.0&key=%1&cmd=%2").arg("JW48LBH2QN0A5M9").arg("getstation"));
  132. QNetworkRequest request(url);
  133. manager->get(request);
  134. }
  135. void MainWindow::dataReceived(QNetworkReply* reply) {
  136. QDomDocument doc;
  137. if(doc.setContent(reply)) {
  138. //qDebug() << doc.toString();
  139. #ifdef Q_WS_MAEMO_5
  140. setAttribute(Qt::WA_Maemo5AutoOrientation, true);
  141. #endif
  142. // <opendata>
  143. QDomElement docElem = doc.documentElement();
  144. QDomNode nodeAnswer = docElem.namedItem("answer");
  145. if (!nodeAnswer.isNull()) {
  146. QDomNode nodeStatus = nodeAnswer.namedItem("status");
  147. if (!nodeStatus.isNull()) {
  148. QDomElement statusElem = nodeStatus.toElement();
  149. if (!statusElem.isNull()) {
  150. qDebug() << "code erreur" << statusElem.attribute("code") << endl;
  151. qDebug() << "message" << statusElem.attribute("message") << endl;
  152. }
  153. }
  154. /*
  155. QDomNode dataNode = docElem.namedItem("data");
  156. if (!dataNode.isNull()) {
  157. dataNode.childNodes()
  158. }*/
  159. }
  160. QDomNodeList stations = docElem.elementsByTagName("station");
  161. QObjectList list;
  162. for(int i = 0; i < stations.size(); i++) {
  163. QDomElement station = stations.item(i).toElement();
  164. QString name = station.namedItem("name").toElement().text();
  165. float latitude = station.namedItem("latitude").toElement().text().toFloat();
  166. float longitude = station.namedItem("longitude").toElement().text().toFloat();
  167. int slotCount = station.namedItem("slotsavailable").toElement().text().toInt();
  168. int bikeCount = station.namedItem("bikesavailable").toElement().text().toInt();
  169. /*
  170. QString javascript;
  171. javascript += "var markerLayer = new Mappy.api.map.layer.MarkerLayer(40);";
  172. javascript += "map.addLayer(markerLayer);";
  173. javascript += "var icon = new Mappy.api.ui.Icon(Mappy.api.ui.Icon.DEFAULT);";
  174. javascript += QString("icon.label = '%1';").arg(name);
  175. javascript += QString("var marker = new Mappy.api.map.Marker(new Mappy.api.geo.Coordinates(%1, %2), icon);").arg(longitude).arg(latitude);
  176. javascript += "marker.addToolTip('This is marker A');";
  177. javascript += "marker.addToolTip('This is marker A');";
  178. javascript += "marker.addListener(\"click\", function () {";
  179. javascript += "marker.openPopUp('Informations about marker A.');});";
  180. javascript += "markerLayer.addMarker(marker);";
  181. ui->webView->page()->mainFrame()->evaluateJavaScript(javascript);
  182. */
  183. Station *s= new Station(name, longitude, latitude, slotCount, bikeCount);
  184. m_stations.append(s);
  185. list.append(s);
  186. //emit addMarker(longitude, latitude);
  187. //emit addStations(m_stations);
  188. }
  189. emit addStations(list);
  190. }
  191. }
  192. QObjectList MainWindow::stations() const {
  193. //QVariantList list;
  194. QObjectList list;
  195. foreach(Station* s, m_stations) {
  196. //list.append(QVariant::fromValue(s));
  197. list.append(s);
  198. }
  199. return list;
  200. }