Updated the index.html page
[quandoparte] / application / stationview.cpp
1 /*
2
3 Copyright (C) 2011 Luciano Montanaro <mikelima@cirulla.net>
4
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.
9
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.
14
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.
19
20 */
21
22 #include "stationview.h"
23 #include "settings.h"
24
25 #include <QAction>
26 #include <QActionGroup>
27 #include <QDir>
28 #include <QFileInfo>
29 #include <QDebug>
30 #include <QMenu>
31 #include <QMenuBar>
32 #include <QWebElement>
33 #include <QWebFrame>
34 #include <QWebView>
35
36 StationView::StationView(QWidget *parent) :
37     QMainWindow(parent),
38     theStation(""),
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))
48 {
49     showArrivalsAction->setCheckable(true);
50     showDeparturesAction->setCheckable(true);
51     showDeparturesAction->setChecked(true);
52
53     Settings *settings = Settings::instance();
54     if (settings->showArrivalsPreferred()) {
55         showArrivalsAction->setChecked(true);
56     } else {
57         showDeparturesAction->setChecked(true);
58     }
59
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());
68     setMenuBar(menuBar);
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);
76 #ifdef Q_WS_MAEMO_5
77     setAttribute(Qt::WA_Maemo5StackedWindow);
78     setAttribute(Qt::WA_Maemo5AutoOrientation);
79 #endif
80 #if defined(Q_WS_S60)
81     setWindowState(Qt::WindowMaximized);
82 #endif
83 }
84
85 void StationView::setStation(const QString &station)
86 {
87     setWindowTitle(station);
88     theStation = station;
89 }
90
91 void StationView::changeView(void)
92 {
93     qDebug() << "View Changed";
94     if (showArrivalsAction->isChecked()) {
95         qDebug() << "Showing Arrivals";
96     } else {
97         qDebug() << "Showing Departures";
98     }
99 }
100
101 void StationView::setBaseUrl(const QUrl &baseUrl)
102 {
103     theBaseUrl = baseUrl;
104 }
105
106 void StationView::updateView(const QByteArray &page)
107 {
108     qDebug() << page;
109     updateCss();
110     view->setContent(page, "text/html", theBaseUrl);
111     QWebElement doc = view->page()->mainFrame()->documentElement();
112
113     // Find the first div
114     QWebElement current = doc.findFirst("div");
115
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())
123             break;
124     }
125     // Mark every div as a departure class element; the next corpocentrale
126     // marks the start of the arrivals section
127     qDebug() << "marking departures";
128     do {
129         current.addClass("departures");
130         current = current.nextSibling();
131         qDebug() << "marking as departures";
132         if (current.isNull())
133             break;
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())
141             break;
142     }
143 }
144
145 void StationView::viewSelectionGroupTriggered(QAction *action)
146 {
147     Settings *settings = Settings::instance();
148     settings->setShowArrivalsPreferred(
149                 action == showArrivalsAction ? true : false);
150     updateCss();
151 }
152
153 void StationView::updateCss(void)
154 {
155     QUrl cssUrl;
156     Settings *settings = Settings::instance();
157     QStringList paths = QDir::searchPaths("css");
158     QFileInfo fileInfo;
159     if (settings->showArrivalsPreferred()) {
160         fileInfo = QFileInfo("css:arrivals.css");
161     } else {
162         fileInfo = QFileInfo("css:departures.css");
163     }
164     cssUrl = QUrl::fromLocalFile(fileInfo.absoluteFilePath());
165     qDebug() << "Css url:" << cssUrl;
166     QWebSettings::globalSettings()->setUserStyleSheetUrl(cssUrl);
167 }