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