Refactored settings relative to StationView
[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
24 #include <QAction>
25 #include <QActionGroup>
26 #include <QDebug>
27 #include <QMenu>
28 #include <QMenuBar>
29 #include <QSettings>
30 #include <QWebElement>
31 #include <QWebFrame>
32 #include <QWebView>
33
34 StationView::StationView(QWidget *parent) :
35     QMainWindow(parent),
36     theStation(""),
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))
46 {
47     showArrivalsAction->setCheckable(true);
48     showDeparturesAction->setCheckable(true);
49     showDeparturesAction->setChecked(true);
50
51     QSettings settings;
52     if (settings.value("StationView/ShowArrivals", false).toBool() == true) {
53         showArrivalsAction->setChecked(true);
54     } else {
55         showDeparturesAction->setChecked(true);
56     }
57
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());
66     setMenuBar(menuBar);
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(settingsTriggered()));
71     connect(showStationListSelectAction, SIGNAL(triggered()), this, SIGNAL(stationListSelectTriggered()));
72     connect(viewSelectionGroup, SIGNAL(triggered(QAction *)), this, SLOT(viewSelectionGroupTriggered(QAction *)));
73     setCentralWidget(view);
74 #ifdef Q_WS_MAEMO_5
75     setAttribute(Qt::WA_Maemo5StackedWindow);
76     setAttribute(Qt::WA_Maemo5AutoOrientation);
77 #endif
78 }
79
80 void StationView::setStation(const QString &station)
81 {
82     setWindowTitle(station);
83     theStation = station;
84 }
85
86 void StationView::changeView(void)
87 {
88     qDebug() << "View Changed";
89     if (showArrivalsAction->isChecked()) {
90         qDebug() << "Showing Arrivals";
91     } else {
92         qDebug() << "Showing Departures";
93     }
94 }
95
96 void StationView::setBaseUrl(const QUrl &baseUrl)
97 {
98     theBaseUrl = baseUrl;
99 }
100
101 void StationView::updateView(const QByteArray &page)
102 {
103     qDebug() << page;
104     updateCss();
105     view->setContent(page, "text/html", theBaseUrl);
106     QWebElement doc = view->page()->mainFrame()->documentElement();
107
108     // Find the first div
109     QWebElement current = doc.findFirst("div");
110
111     qDebug() << "skipping to the departures";
112     // Skip to the first div of class corpocentrale, which contains the first
113     // departure-related contents
114     while (!current.classes().contains("corpocentrale")) {
115         current = current.nextSibling();
116         qDebug() << "skipping to the next element";
117         if (current.isNull())
118             break;
119     }
120     // Mark every div as a departure class element; the next corpocentrale
121     // marks the start of the arrivals section
122     qDebug() << "marking departures";
123     do {
124         current.addClass("departures");
125         current = current.nextSibling();
126         qDebug() << "marking as departures";
127         if (current.isNull())
128             break;
129     } while (!current.classes().contains("corpocentrale"));
130     // Mark everything as an arrival, until reaching the footer
131     while (!current.classes().contains("footer")) {
132         current.addClass("arrivals");
133         current = current.nextSibling();
134         qDebug() << "marking as arrival";
135         if (current.isNull())
136             break;
137     }
138 }
139
140 void StationView::viewSelectionGroupTriggered(QAction *action)
141 {
142     QSettings settings;
143     if (action == showArrivalsAction) {
144         settings.setValue("StationView/ShowArrivals", true);
145     } else {
146         settings.setValue("StationView/ShowArrivals", false);
147     }
148     updateCss();
149 }
150
151 void StationView::updateCss(void)
152 {
153     QUrl cssUrl;
154     QSettings settings;
155     if (settings.value("StationView/ShowArrivals", true).toBool()) {
156         cssUrl.setEncodedUrl("file:///opt/usr/share/apps/quandoparte/css/arrivals.css");
157     } else {
158         cssUrl.setEncodedUrl("file:///opt/usr/share/apps/quandoparte/css/departures.css");
159     }
160     QWebSettings::globalSettings()->setUserStyleSheetUrl(cssUrl);
161 }