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(settingsChangeRequested()));
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);
79 setWindowState(Qt::WindowMaximized);
83 void StationView::setStation(const QString &station)
85 setWindowTitle(station);
89 void StationView::changeView(void)
91 qDebug() << "View Changed";
92 if (showArrivalsAction->isChecked()) {
93 qDebug() << "Showing Arrivals";
95 qDebug() << "Showing Departures";
99 void StationView::setBaseUrl(const QUrl &baseUrl)
101 theBaseUrl = baseUrl;
104 void StationView::updateView(const QByteArray &page)
108 view->setContent(page, "text/html", theBaseUrl);
109 QWebElement doc = view->page()->mainFrame()->documentElement();
111 // Find the first div
112 QWebElement current = doc.findFirst("div");
114 qDebug() << "skipping to the departures";
115 // Skip to the first div of class corpocentrale, which contains the first
116 // departure-related contents
117 while (!current.classes().contains("corpocentrale")) {
118 current = current.nextSibling();
119 qDebug() << "skipping to the next element";
120 if (current.isNull())
123 // Mark every div as a departure class element; the next corpocentrale
124 // marks the start of the arrivals section
125 qDebug() << "marking departures";
127 current.addClass("departures");
128 current = current.nextSibling();
129 qDebug() << "marking as departures";
130 if (current.isNull())
132 } while (!current.classes().contains("corpocentrale"));
133 // Mark everything as an arrival, until reaching the footer
134 while (!current.classes().contains("footer")) {
135 current.addClass("arrivals");
136 current = current.nextSibling();
137 qDebug() << "marking as arrival";
138 if (current.isNull())
143 void StationView::viewSelectionGroupTriggered(QAction *action)
146 if (action == showArrivalsAction) {
147 settings.setValue("StationView/ShowArrivals", true);
149 settings.setValue("StationView/ShowArrivals", false);
154 void StationView::updateCss(void)
158 if (settings.value("StationView/ShowArrivals", true).toBool()) {
159 cssUrl.setEncodedUrl("file:///opt/usr/share/apps/quandoparte/css/arrivals.css");
161 cssUrl.setEncodedUrl("file:///opt/usr/share/apps/quandoparte/css/departures.css");
163 QWebSettings::globalSettings()->setUserStyleSheetUrl(cssUrl);