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>
29 #include <QWebElement>
33 StationView::StationView(QWidget *parent) :
36 showArrivalsAction(new QAction(tr("Arrivals"), this)),
37 showDeparturesAction(new QAction(tr("Departures"), this)),
38 showSettingsAction(new QAction(tr("Settings"), this)),
39 showStationListSelectAction(new QAction(tr("Change Station"), this)),
40 showAboutAction(new QAction(tr("About"), this)),
41 viewSelectionGroup(new QActionGroup(this)),
42 menuBar(new QMenuBar(this)),
43 menu(new QMenu(menuBar)),
44 view(new QWebView(this))
46 showArrivalsAction->setCheckable(true);
47 showDeparturesAction->setCheckable(true);
48 showDeparturesAction->setChecked(true);
49 viewSelectionGroup->addAction(showArrivalsAction);
50 viewSelectionGroup->addAction(showDeparturesAction);
51 menu->addAction(showDeparturesAction);
52 menu->addAction(showArrivalsAction);
53 menu->addAction(showStationListSelectAction);
54 menu->addAction(showSettingsAction);
55 menu->addAction(showAboutAction);
56 menuBar->addAction(menu->menuAction());
58 view->setTextSizeMultiplier(2.0);
59 view->setBackgroundRole(QPalette::Window);
60 connect(showAboutAction, SIGNAL(triggered()), this, SIGNAL(aboutTriggered()));
61 connect(showSettingsAction, SIGNAL(triggered()), this, SIGNAL(settingsTriggered()));
62 connect(showStationListSelectAction, SIGNAL(triggered()), this, SIGNAL(stationListSelectTriggered()));
63 connect(viewSelectionGroup, SIGNAL(triggered(QAction *)), this, SLOT(viewSelectionGroupTriggered(QAction *)));
64 setCentralWidget(view);
66 setAttribute(Qt::WA_Maemo5StackedWindow);
67 setAttribute(Qt::WA_Maemo5AutoOrientation);
71 void StationView::setStation(const QString &station)
73 setWindowTitle(station);
77 void StationView::changeView(void)
79 qDebug() << "View Changed";
80 if (showArrivalsAction->isChecked()) {
81 qDebug() << "Showing Arrivals";
83 qDebug() << "Showing Departures";
87 void StationView::setBaseUrl(const QUrl &baseUrl)
92 void StationView::updateView(const QByteArray &page)
96 view->setContent(page, "text/html", theBaseUrl);
97 QWebElement doc = view->page()->mainFrame()->documentElement();
100 QWebElement current = doc.findFirst("div");
102 qDebug() << "skipping to the departures";
103 // Skip to the first div of class corpocentrale, which contains the first
104 // departure-related contents
105 while (!current.classes().contains("corpocentrale")) {
106 current = current.nextSibling();
107 qDebug() << "skipping to the next element";
108 if (current.isNull())
111 // Mark every div as a departure class element; the next corpocentrale
112 // marks the start of the arrivals section
113 qDebug() << "marking departures";
115 current.addClass("departures");
116 current = current.nextSibling();
117 qDebug() << "marking as departures";
118 if (current.isNull())
120 } while (!current.classes().contains("corpocentrale"));
121 // Mark everything as an arrival, until reaching the footer
122 while (!current.classes().contains("footer")) {
123 current.addClass("arrivals");
124 current = current.nextSibling();
125 qDebug() << "marking as arrival";
126 if (current.isNull())
131 void StationView::viewSelectionGroupTriggered(QAction *action)
133 if (action == showArrivalsAction) {
134 emit showingArrivalsChanged(true);
136 emit showingArrivalsChanged(false);
141 void StationView::updateCss(void)
143 QByteArray styleSheet("data:text/css;charset=utf-8,base64,");
144 QByteArray styleSheetText = QByteArray(
146 "-webkit-user-select: none ! important;\n"
147 "background-color: black ! important;\n"
148 "color: white ! important;}\n"
149 ".testata_red {visibility: hidden ! important;}\n"
151 "visibility: hidden ! important;}\n");
152 styleSheet += styleSheetText.toBase64();
153 qDebug() << styleSheet;
154 //QUrl cssUrl = QUrl::fromEncoded(styleSheet);
157 // XXX Maemo5 specific
158 if (showArrivalsAction->isChecked()) {
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);