Fixed Settings Dialog
[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(settingsChangeRequested()));
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 #if defined(Q_WS_S60)
79     setWindowState(Qt::WindowMaximized);
80 #endif
81 }
82
83 void StationView::setStation(const QString &station)
84 {
85     setWindowTitle(station);
86     theStation = station;
87 }
88
89 void StationView::changeView(void)
90 {
91     qDebug() << "View Changed";
92     if (showArrivalsAction->isChecked()) {
93         qDebug() << "Showing Arrivals";
94     } else {
95         qDebug() << "Showing Departures";
96     }
97 }
98
99 void StationView::setBaseUrl(const QUrl &baseUrl)
100 {
101     theBaseUrl = baseUrl;
102 }
103
104 void StationView::updateView(const QByteArray &page)
105 {
106     qDebug() << page;
107     updateCss();
108     view->setContent(page, "text/html", theBaseUrl);
109     QWebElement doc = view->page()->mainFrame()->documentElement();
110
111     // Find the first div
112     QWebElement current = doc.findFirst("div");
113
114     qDebug() << "skipping to the departures";
115     // Skip to the first div of class corpocentrale, which contains the first
116     // departure-related contents
117     while (!current.classes().contains("corpocentrale")) {
118         current = current.nextSibling();
119         qDebug() << "skipping to the next element";
120         if (current.isNull())
121             break;
122     }
123     // Mark every div as a departure class element; the next corpocentrale
124     // marks the start of the arrivals section
125     qDebug() << "marking departures";
126     do {
127         current.addClass("departures");
128         current = current.nextSibling();
129         qDebug() << "marking as departures";
130         if (current.isNull())
131             break;
132     } while (!current.classes().contains("corpocentrale"));
133     // Mark everything as an arrival, until reaching the footer
134     while (!current.classes().contains("footer")) {
135         current.addClass("arrivals");
136         current = current.nextSibling();
137         qDebug() << "marking as arrival";
138         if (current.isNull())
139             break;
140     }
141 }
142
143 void StationView::viewSelectionGroupTriggered(QAction *action)
144 {
145     QSettings settings;
146     if (action == showArrivalsAction) {
147         settings.setValue("StationView/ShowArrivals", true);
148     } else {
149         settings.setValue("StationView/ShowArrivals", false);
150     }
151     updateCss();
152 }
153
154 void StationView::updateCss(void)
155 {
156     QUrl cssUrl;
157     QSettings settings;
158     if (settings.value("StationView/ShowArrivals", true).toBool()) {
159         cssUrl.setEncodedUrl("file:///opt/usr/share/apps/quandoparte/css/arrivals.css");
160     } else {
161         cssUrl.setEncodedUrl("file:///opt/usr/share/apps/quandoparte/css/departures.css");
162     }
163     QWebSettings::globalSettings()->setUserStyleSheetUrl(cssUrl);
164 }