Implemented Arrival/Departures selection
authorLuciano Montanaro <mikelima@cirulla.net>
Sat, 14 May 2011 16:46:01 +0000 (18:46 +0200)
committerLuciano Montanaro <mikelima@zaphod>
Sat, 14 May 2011 16:46:01 +0000 (18:46 +0200)
Many changes, the StationView class is now mostly useful.
It is possible to select arrivals/departures from the menu to change the
displayed section.

Implemented using the QWebElement dom functions to annotate the elements
as departures or arrivals, as the structure of the viaggiatreno site is
unsuitable for a css only solution.

application/app.cpp
application/app.h
application/application.pro
application/resources/arrivals.css [new file with mode: 0644]
application/resources/departures.css [new file with mode: 0644]
application/resources/quandoparte.css [new file with mode: 0644]
application/stationlistview.cpp
application/stationview.cpp
application/stationview.h

index 97ab0d9..a4a084b 100644 (file)
@@ -46,6 +46,8 @@ App::App(QObject *parent) :
     connect(stationView, SIGNAL(stationListSelectTriggered()),
             SLOT(showStationSelectView()));
 
+    connect(stationView, SIGNAL(showingArrivalsChanged(bool)),
+            SLOT(setShowingArrivals(bool)));
     readSettings();
 
 #if defined(Q_WS_S60)
@@ -63,6 +65,11 @@ App::App(QObject *parent) :
     }
 }
 
+App::~App()
+{
+    saveSettings();
+}
+
 void App::downloadFinished(void)
 {
     disconnect(stationQueryReply, SIGNAL(finished()),
@@ -125,3 +132,8 @@ void App::saveSettings(void)
     settings.value("ShowingArrivals", showingArrivals);
     settings.value("CheckInterval", checkingInterval);
 }
+
+void App::setShowingArrivals(bool showArrivals)
+{
+    showingArrivals = showArrivals;
+}
index b37abca..abad5a9 100644 (file)
@@ -35,6 +35,7 @@ class App : public QObject
     Q_OBJECT
 public:
     explicit App(QObject *parent = 0);
+    ~App();
 
 signals:
 
@@ -44,6 +45,7 @@ public slots:
     void showAboutDialog(void);
     void showSettingsDialog(void);
     void showStationSelectView(void);
+    void setShowingArrivals(bool);
 public:
     void saveSettings(void);
     void readSettings(void);
index 4356f16..c195d69 100644 (file)
@@ -43,7 +43,10 @@ OTHER_FILES += \
     debian/README \
     debian/rules \
     quandoparte.desktop \
-    icons/quandoparte.png
+    icons/quandoparte.png \
+    resources/quandoparte.css \
+    resources/arrivals.css \
+    resources/departures.css
 
 unix:!symbian {
     maemo5 {
@@ -61,14 +64,17 @@ unix:!symbian {
     } else {
         desktopfile.path = /usr/share/applications
     }
-    INSTALLS += desktopfile
+    INSTALLS += desktopfile cssfile
 }
 
 unix:!symbian {
+    css.files = resources/$${TARGET}.css resources/arrivals.css resources/departures.css
+    css.path = /usr/share/apps/$${TARGET}/css
     icon48.files = icons/48x48/$${TARGET}.png
     icon64.files = icons/64x64/$${TARGET}.png
     icon48.path = /usr/share/icons/hicolor/48x48/apps
     icon64.path = /usr/share/icons/hicolor/64x64/apps
     INSTALLS += icon48
     INSTALLS += icon64
+    INSTALLS += css
 }
diff --git a/application/resources/arrivals.css b/application/resources/arrivals.css
new file mode 100644 (file)
index 0000000..9109e81
--- /dev/null
@@ -0,0 +1,9 @@
+@import url("./quandoparte.css");
+
+.arrivals {
+display: block
+}
+
+.departures {
+display: none
+}
diff --git a/application/resources/departures.css b/application/resources/departures.css
new file mode 100644 (file)
index 0000000..bb8b822
--- /dev/null
@@ -0,0 +1,9 @@
+@import url("./quandoparte.css");
+
+.departures {
+display: block
+}
+
+.arrivals {
+display: none
+}
diff --git a/application/resources/quandoparte.css b/application/resources/quandoparte.css
new file mode 100644 (file)
index 0000000..7154e03
--- /dev/null
@@ -0,0 +1,26 @@
+body {
+-webkit-user-select: none ! important;
+background-color: black ! important;
+color: white ! important;
+}
+
+.testata_red {
+display:none ! important;
+}
+
+.bloccotreno, .bloccorisultato {
+background-color: black ! important;
+color: white ! important;
+}
+
+h1, h2 {
+color: white ! important
+}
+
+#footer {
+display: none ! important;
+}
+
+div.bloccotreno br:first-of-type {
+display: none
+}
index 05b0924..afd83bf 100644 (file)
@@ -39,6 +39,7 @@ StationListView::StationListView(QWidget *parent) :
 {
 #ifdef Q_WS_MAEMO_5
     setAttribute(Qt::WA_Maemo5StackedWindow);
+    setAttribute(Qt::WA_Maemo5AutoOrientation);
 #endif
     ui->setupUi(this);
     viewSelectionGroup->addAction(ui->sortByNameAction);
index 4a8e355..47025e0 100644 (file)
@@ -26,6 +26,8 @@ Boston, MA 02110-1301, USA.
 #include <QDebug>
 #include <QMenu>
 #include <QMenuBar>
+#include <QWebElement>
+#include <QWebFrame>
 #include <QWebView>
 
 StationView::StationView(QWidget *parent) :
@@ -54,12 +56,15 @@ StationView::StationView(QWidget *parent) :
     menuBar->addAction(menu->menuAction());
     setMenuBar(menuBar);
     view->setTextSizeMultiplier(2.0);
+    view->setBackgroundRole(QPalette::Window);
     connect(showAboutAction, SIGNAL(triggered()), this, SIGNAL(aboutTriggered()));
     connect(showSettingsAction, SIGNAL(triggered()), this, SIGNAL(settingsTriggered()));
     connect(showStationListSelectAction, SIGNAL(triggered()), this, SIGNAL(stationListSelectTriggered()));
+    connect(viewSelectionGroup, SIGNAL(triggered(QAction *)), this, SLOT(viewSelectionGroupTriggered(QAction *)));
     setCentralWidget(view);
 #ifdef Q_WS_MAEMO_5
     setAttribute(Qt::WA_Maemo5StackedWindow);
+    setAttribute(Qt::WA_Maemo5AutoOrientation);
 #endif
 }
 
@@ -87,5 +92,71 @@ void StationView::setBaseUrl(const QUrl &baseUrl)
 void StationView::updateView(const QByteArray &page)
 {
     qDebug() << page;
+    updateCss();
     view->setContent(page, "text/html", theBaseUrl);
+    QWebElement doc = view->page()->mainFrame()->documentElement();
+
+    // Find the first div
+    QWebElement current = doc.findFirst("div");
+
+    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 {
+        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")) {
+        current.addClass("arrivals");
+        current = current.nextSibling();
+        qDebug() << "marking as arrival";
+        if (current.isNull())
+            break;
+    }
+}
+
+void StationView::viewSelectionGroupTriggered(QAction *action)
+{
+    if (action == showArrivalsAction) {
+        emit showingArrivalsChanged(true);
+    } else {
+        emit showingArrivalsChanged(false);
+    }
+    updateCss();
+}
+
+void StationView::updateCss(void)
+{
+    QByteArray styleSheet("data:text/css;charset=utf-8,base64,");
+    QByteArray styleSheetText = QByteArray(
+                "body {\n"
+                "-webkit-user-select: none ! important;\n"
+                "background-color: black ! important;\n"
+                "color: white ! important;}\n"
+                ".testata_red {visibility: hidden ! important;}\n"
+                "#footer {\n"
+                "visibility: hidden ! important;}\n");
+    styleSheet += styleSheetText.toBase64();
+    qDebug() << styleSheet;
+    //QUrl cssUrl = QUrl::fromEncoded(styleSheet);
+    QUrl cssUrl;
+    if (showArrivalsAction->isChecked()) {
+        cssUrl.setEncodedUrl("file:///usr/share/apps/quandoparte/css/arrivals.css");
+    } else {
+        cssUrl.setEncodedUrl("file:///usr/share/apps/quandoparte/css/departures.css");
+    }
+    QWebSettings::globalSettings()->setUserStyleSheetUrl(cssUrl);
 }
index 9c1715e..75ce9aa 100644 (file)
@@ -21,12 +21,17 @@ signals:
     void aboutTriggered(void);
     void settingsTriggered(void);
     void stationListSelectTriggered(void);
+    void showingArrivalsChanged(bool);
 
 public slots:
     void updateView(const QByteArray &page);
 
 private slots:
     void changeView(void);
+    void viewSelectionGroupTriggered(QAction *);
+
+private:
+    void updateCss(void);
 
 private:
     QString theStation;