Start of a proper station schedule parsing.
authorLuciano Montanaro <mikelima@cirulla.net>
Wed, 9 Nov 2011 00:01:16 +0000 (01:01 +0100)
committerLuciano Montanaro <mikelima@cirulla.net>
Tue, 27 Dec 2011 22:19:11 +0000 (23:19 +0100)
application/resources/harmattan/qml/StationPage.qml
application/stationlistmodel.cpp
application/stationschedulemodel.cpp
application/stationschedulemodel.h

index 2916622..e84f11f 100644 (file)
@@ -52,11 +52,12 @@ Page {
     }
     Item {
         id: view
-        anchors {
-            top: info.bottom;
-            bottom: parent.bottom;
-        }
-        width: parent.width
+        anchors.top: info.bottom
+        anchors.bottom: tools.top
+        x: 16
+        y: 16
+        width: parent.width - 32
+        height: parent.height
 
         ListView {
             id: stationScheduleView
@@ -66,7 +67,7 @@ Page {
             model:  schedule
             delegate: Item {
                 id: listItem
-                height: 48
+                height: 192
                 width: parent.width
                 BorderImage {
                     id: background
@@ -83,7 +84,7 @@ Page {
 
                         Label {
                             id: mainText
-                            text: Private.highlightSearch(model.display, UiConstants.AccentColor)
+                            text: model.display
                             font.bold: true
                         }
                     }
index b1b3c52..d0d12a8 100644 (file)
@@ -43,6 +43,7 @@ bool StationListModel::load(const QString &filename)
 
     qDebug() << "loading file:" << fi.absoluteFilePath();
 
+    emit layoutAboutToBeChanged();
     if (!file.open(QFile::ReadOnly | QFile::Text)) {
         qDebug() << "cannot open file:" << filename;
         return false;
index b39ebf6..34021be 100644 (file)
@@ -24,6 +24,9 @@ Boston, MA 02110-1301, USA.
 #include "dataprovider.h"
 
 #include <QDebug>
+#include <QWebElement>
+#include <QWebFrame>
+#include <QWebPage>
 
 StationScheduleModel::StationScheduleModel(const QString &name, QObject *parent) :
     QStringListModel(parent),
@@ -55,6 +58,57 @@ void StationScheduleModel::parse(const QByteArray &htmlReply, const QUrl &baseUr
     qDebug() << "--- start of query result --- cut here ------";
     qDebug() << QString::fromUtf8(htmlReply.constData());
     qDebug() << "--- end of query result ----- cut here ------";
+
+    emit layoutAboutToBeChanged();
+    QWebPage page;
+    page.mainFrame()->setContent(htmlReply, "text/html", baseUrl);
+    QWebElement doc = page.mainFrame()->documentElement();
+
+    // Find the first div
+    QWebElement current = doc.findFirst("div");
+
+    QStringList departures, arrivals;
+    qDebug() << "skipping to the departures";
+    // Skip to the first div of class corpocentrale, which contains the first
+    // departure-related contents
+    while (!current.classes().contains("corpocentrale")) {
+        current = current.nextSibling();
+        qDebug() << "skipping to the next element";
+        if (current.isNull())
+            break;
+    }
+    // Mark every div as a departure class element; the next corpocentrale
+    // marks the start of the arrivals section
+    qDebug() << "marking departures";
+    do {
+        if (current.classes().contains("bloccorisultato")) {
+            departures << current.toPlainText();
+        }
+        current.addClass("departures");
+        current = current.nextSibling();
+        qDebug() << "marking as departures";
+        if (current.isNull())
+            break;
+    } while (!current.classes().contains("corpocentrale"));
+
+    // Mark everything as an arrival, until reaching the footer
+    while (!current.classes().contains("footer")) {
+        if (current.classes().contains("bloccorisultato")) {
+            arrivals << current.toPlainText();
+        }
+        current.addClass("arrivals");
+        current = current.nextSibling();
+        qDebug() << "marking as arrival";
+        if (current.isNull())
+            break;
+    }
+
+    setStringList(departures);
+    qDebug() << "departures list contain:";
+    qDebug() << departures;
+    qDebug() << "arrivals list contain:";
+    qDebug() << arrivals;
+    emit  layoutChanged();
 }
 
 void StationScheduleModel::fetch(const QString &name)
index 0758b2f..957364c 100644 (file)
@@ -48,6 +48,8 @@ private slots:
 
 private:
     QString m_name;
+    QStringList departures;
+    QStringList arrivals;
 };
 
 #endif // STATIONSCHEDULEMODEL_H