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"
26 #include <QActionGroup>
33 #include <QWebElement>
37 StationView::StationView(QWidget *parent) :
40 showArrivalsAction(new QAction(tr("Arrivals"), this)),
41 showDeparturesAction(new QAction(tr("Departures"), this)),
42 showSettingsAction(new QAction(tr("Settings"), this)),
43 showStationListSelectAction(new QAction(tr("Change Station"), this)),
44 showAboutAction(new QAction(tr("About"), this)),
45 viewSelectionGroup(new QActionGroup(this)),
46 menuBar(new QMenuBar(this)),
47 menu(new QMenu(menuBar)),
48 view(new QWebView(this))
50 showArrivalsAction->setCheckable(true);
51 showDeparturesAction->setCheckable(true);
52 showDeparturesAction->setChecked(true);
54 Settings *settings = Settings::instance();
55 if (settings->showArrivalsPreferred()) {
56 showArrivalsAction->setChecked(true);
58 showDeparturesAction->setChecked(true);
61 viewSelectionGroup->addAction(showArrivalsAction);
62 viewSelectionGroup->addAction(showDeparturesAction);
63 menu->addAction(showDeparturesAction);
64 menu->addAction(showArrivalsAction);
65 menu->addAction(showStationListSelectAction);
66 menu->addAction(showSettingsAction);
67 menu->addAction(showAboutAction);
68 menuBar->addAction(menu->menuAction());
70 view->setTextSizeMultiplier(2.0);
71 view->setBackgroundRole(QPalette::Window);
72 connect(showAboutAction, SIGNAL(triggered()), this, SIGNAL(aboutTriggered()));
73 connect(showSettingsAction, SIGNAL(triggered()), this, SIGNAL(settingsChangeRequested()));
74 connect(showStationListSelectAction, SIGNAL(triggered()), this, SIGNAL(stationListSelectTriggered()));
75 connect(viewSelectionGroup, SIGNAL(triggered(QAction *)), this, SLOT(viewSelectionGroupTriggered(QAction *)));
76 setCentralWidget(view);
78 setAttribute(Qt::WA_Maemo5StackedWindow);
79 setAttribute(Qt::WA_Maemo5AutoOrientation);
82 setWindowState(Qt::WindowMaximized);
86 void StationView::setStation(const QString &station)
88 setWindowTitle(station);
92 void StationView::changeView(void)
94 qDebug() << "View Changed";
95 if (showArrivalsAction->isChecked()) {
96 qDebug() << "Showing Arrivals";
98 qDebug() << "Showing Departures";
102 void StationView::setBaseUrl(const QUrl &baseUrl)
104 theBaseUrl = baseUrl;
107 void StationView::updateView(const QByteArray &page)
111 view->setContent(page, "text/html", theBaseUrl);
112 QWebElement doc = view->page()->mainFrame()->documentElement();
114 // Find the first div
115 QWebElement current = doc.findFirst("div");
117 qDebug() << "skipping to the departures";
118 // Skip to the first div of class corpocentrale, which contains the first
119 // departure-related contents
120 while (!current.classes().contains("corpocentrale")) {
121 current = current.nextSibling();
122 qDebug() << "skipping to the next element";
123 if (current.isNull())
126 // Mark every div as a departure class element; the next corpocentrale
127 // marks the start of the arrivals section
128 qDebug() << "marking departures";
130 current.addClass("departures");
131 current = current.nextSibling();
132 qDebug() << "marking as departures";
133 if (current.isNull())
135 } while (!current.classes().contains("corpocentrale"));
136 // Mark everything as an arrival, until reaching the footer
137 while (!current.classes().contains("footer")) {
138 current.addClass("arrivals");
139 current = current.nextSibling();
140 qDebug() << "marking as arrival";
141 if (current.isNull())
146 void StationView::viewSelectionGroupTriggered(QAction *action)
148 Settings *settings = Settings::instance();
149 settings->setShowArrivalsPreferred(
150 action == showArrivalsAction ? true : false);
154 void StationView::updateCss(void)
157 Settings *settings = Settings::instance();
158 QStringList paths = QDir::searchPaths("css");
160 if (settings->showArrivalsPreferred()) {
161 fileInfo = QFileInfo("css:arrivals.css");
163 fileInfo = QFileInfo("css:departures.css");
165 cssUrl = QUrl::fromLocalFile(fileInfo.absoluteFilePath());
166 qDebug() << "Css url:" << cssUrl;
167 QWebSettings::globalSettings()->setUserStyleSheetUrl(cssUrl);