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>
32 #include <QWebElement>
36 StationView::StationView(QWidget *parent) :
39 showArrivalsAction(new QAction(tr("Arrivals"), this)),
40 showDeparturesAction(new QAction(tr("Departures"), this)),
41 showSettingsAction(new QAction(tr("Settings"), this)),
42 showStationListSelectAction(new QAction(tr("Change Station"), this)),
43 showAboutAction(new QAction(tr("About"), this)),
44 viewSelectionGroup(new QActionGroup(this)),
45 menuBar(new QMenuBar(this)),
46 menu(new QMenu(menuBar)),
47 view(new QWebView(this))
49 showArrivalsAction->setCheckable(true);
50 showDeparturesAction->setCheckable(true);
51 showDeparturesAction->setChecked(true);
53 Settings *settings = Settings::instance();
54 if (settings->showArrivalsPreferred()) {
55 showArrivalsAction->setChecked(true);
57 showDeparturesAction->setChecked(true);
60 viewSelectionGroup->addAction(showArrivalsAction);
61 viewSelectionGroup->addAction(showDeparturesAction);
62 menu->addAction(showDeparturesAction);
63 menu->addAction(showArrivalsAction);
64 menu->addAction(showStationListSelectAction);
65 menu->addAction(showSettingsAction);
66 menu->addAction(showAboutAction);
67 menuBar->addAction(menu->menuAction());
69 view->setTextSizeMultiplier(2.0);
70 view->setBackgroundRole(QPalette::Window);
71 connect(showAboutAction, SIGNAL(triggered()), this, SIGNAL(aboutTriggered()));
72 connect(showSettingsAction, SIGNAL(triggered()), this, SIGNAL(settingsChangeRequested()));
73 connect(showStationListSelectAction, SIGNAL(triggered()), this, SIGNAL(stationListSelectTriggered()));
74 connect(viewSelectionGroup, SIGNAL(triggered(QAction *)), this, SLOT(viewSelectionGroupTriggered(QAction *)));
75 setCentralWidget(view);
77 setAttribute(Qt::WA_Maemo5StackedWindow);
78 setAttribute(Qt::WA_Maemo5AutoOrientation);
81 setWindowState(Qt::WindowMaximized);
85 void StationView::setStation(const QString &station)
87 setWindowTitle(station);
91 void StationView::changeView(void)
93 qDebug() << "View Changed";
94 if (showArrivalsAction->isChecked()) {
95 qDebug() << "Showing Arrivals";
97 qDebug() << "Showing Departures";
101 void StationView::setBaseUrl(const QUrl &baseUrl)
103 theBaseUrl = baseUrl;
106 void StationView::updateView(const QByteArray &page)
110 view->setContent(page, "text/html", theBaseUrl);
111 QWebElement doc = view->page()->mainFrame()->documentElement();
113 // Find the first div
114 QWebElement current = doc.findFirst("div");
116 qDebug() << "skipping to the departures";
117 // Skip to the first div of class corpocentrale, which contains the first
118 // departure-related contents
119 while (!current.classes().contains("corpocentrale")) {
120 current = current.nextSibling();
121 qDebug() << "skipping to the next element";
122 if (current.isNull())
125 // Mark every div as a departure class element; the next corpocentrale
126 // marks the start of the arrivals section
127 qDebug() << "marking departures";
129 current.addClass("departures");
130 current = current.nextSibling();
131 qDebug() << "marking as departures";
132 if (current.isNull())
134 } while (!current.classes().contains("corpocentrale"));
135 // Mark everything as an arrival, until reaching the footer
136 while (!current.classes().contains("footer")) {
137 current.addClass("arrivals");
138 current = current.nextSibling();
139 qDebug() << "marking as arrival";
140 if (current.isNull())
145 void StationView::viewSelectionGroupTriggered(QAction *action)
147 Settings *settings = Settings::instance();
148 settings->setShowArrivalsPreferred(
149 action == showArrivalsAction ? true : false);
153 void StationView::updateCss(void)
156 Settings *settings = Settings::instance();
157 QStringList paths = QDir::searchPaths("css");
159 if (settings->showArrivalsPreferred()) {
160 fileInfo = QFileInfo("css:arrivals.css");
162 fileInfo = QFileInfo("css:departures.css");
164 cssUrl = QUrl::fromLocalFile(fileInfo.absoluteFilePath());
165 qDebug() << "Css url:" << cssUrl;
166 QWebSettings::globalSettings()->setUserStyleSheetUrl(cssUrl);