3 Copyright (C) 2011 Luciano Montanaro <mikelima@cirulla.net>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; see the file COPYING. If not, write to
17 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, USA.
22 #include "stationview.h"
25 #include <QActionGroup>
30 #include <QWebElement>
34 StationView::StationView(QWidget *parent) :
37 showArrivalsAction(new QAction(tr("Arrivals"), this)),
38 showDeparturesAction(new QAction(tr("Departures"), this)),
39 showSettingsAction(new QAction(tr("Settings"), this)),
40 showStationListSelectAction(new QAction(tr("Change Station"), this)),
41 showAboutAction(new QAction(tr("About"), this)),
42 viewSelectionGroup(new QActionGroup(this)),
43 menuBar(new QMenuBar(this)),
44 menu(new QMenu(menuBar)),
45 view(new QWebView(this))
47 showArrivalsAction->setCheckable(true);
48 showDeparturesAction->setCheckable(true);
49 showDeparturesAction->setChecked(true);
52 if (settings.value("StationView/ShowArrivals", false).toBool() == true) {
53 showArrivalsAction->setChecked(true);
55 showDeparturesAction->setChecked(true);
58 viewSelectionGroup->addAction(showArrivalsAction);
59 viewSelectionGroup->addAction(showDeparturesAction);
60 menu->addAction(showDeparturesAction);
61 menu->addAction(showArrivalsAction);
62 menu->addAction(showStationListSelectAction);
63 menu->addAction(showSettingsAction);
64 menu->addAction(showAboutAction);
65 menuBar->addAction(menu->menuAction());
67 view->setTextSizeMultiplier(2.0);
68 view->setBackgroundRole(QPalette::Window);
69 connect(showAboutAction, SIGNAL(triggered()), this, SIGNAL(aboutTriggered()));
70 connect(showSettingsAction, SIGNAL(triggered()), this, SIGNAL(settingsTriggered()));
71 connect(showStationListSelectAction, SIGNAL(triggered()), this, SIGNAL(stationListSelectTriggered()));
72 connect(viewSelectionGroup, SIGNAL(triggered(QAction *)), this, SLOT(viewSelectionGroupTriggered(QAction *)));
73 setCentralWidget(view);
75 setAttribute(Qt::WA_Maemo5StackedWindow);
76 setAttribute(Qt::WA_Maemo5AutoOrientation);
80 void StationView::setStation(const QString &station)
82 setWindowTitle(station);
86 void StationView::changeView(void)
88 qDebug() << "View Changed";
89 if (showArrivalsAction->isChecked()) {
90 qDebug() << "Showing Arrivals";
92 qDebug() << "Showing Departures";
96 void StationView::setBaseUrl(const QUrl &baseUrl)
101 void StationView::updateView(const QByteArray &page)
105 view->setContent(page, "text/html", theBaseUrl);
106 QWebElement doc = view->page()->mainFrame()->documentElement();
108 // Find the first div
109 QWebElement current = doc.findFirst("div");
111 qDebug() << "skipping to the departures";
112 // Skip to the first div of class corpocentrale, which contains the first
113 // departure-related contents
114 while (!current.classes().contains("corpocentrale")) {
115 current = current.nextSibling();
116 qDebug() << "skipping to the next element";
117 if (current.isNull())
120 // Mark every div as a departure class element; the next corpocentrale
121 // marks the start of the arrivals section
122 qDebug() << "marking departures";
124 current.addClass("departures");
125 current = current.nextSibling();
126 qDebug() << "marking as departures";
127 if (current.isNull())
129 } while (!current.classes().contains("corpocentrale"));
130 // Mark everything as an arrival, until reaching the footer
131 while (!current.classes().contains("footer")) {
132 current.addClass("arrivals");
133 current = current.nextSibling();
134 qDebug() << "marking as arrival";
135 if (current.isNull())
140 void StationView::viewSelectionGroupTriggered(QAction *action)
143 if (action == showArrivalsAction) {
144 settings.setValue("StationView/ShowArrivals", true);
146 settings.setValue("StationView/ShowArrivals", false);
151 void StationView::updateCss(void)
155 if (settings.value("StationView/ShowArrivals", true).toBool()) {
156 cssUrl.setEncodedUrl("file:///opt/usr/share/apps/quandoparte/css/arrivals.css");
158 cssUrl.setEncodedUrl("file:///opt/usr/share/apps/quandoparte/css/departures.css");
160 QWebSettings::globalSettings()->setUserStyleSheetUrl(cssUrl);