d76a17c18c2f2d6e663bf2e15c7b66fdc27d6f8c
[medard] / src / mainwindow.cpp
1 /*
2  *  Medard for Maemo.
3  *  Copyright (C) 2011 Roman Moravcik
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
13  *  GNU 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; if not, write to the Free Software
17  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18  */
19
20 #include <QtGui>
21 #include <QSettings>
22
23 #include "mainwindow.h"
24
25 MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent)
26 {
27     m_downloader = new MedardDownloader();
28
29     connect(m_downloader, SIGNAL(downloadFinished(const QString &, const QDateTime &)), this,
30             SLOT(downloadedFinished(const QString &, const QDateTime &)));
31     connect(m_downloader, SIGNAL(downloadFailed()), this, SLOT(downloadFailed()));
32
33     m_forecast = new ForecastWidget();
34     m_forecast->setFixedSize(m_downloader->getImageSize());
35
36     m_forecastTypeLabel = new QLabel();
37     m_forecastTypeLabel->setAlignment(Qt::AlignCenter);
38     m_forecastTypeLabel->setFixedSize(220, 80);
39
40     m_forecastInitialDateLabel = new QLabel();
41     m_forecastInitialDateLabel->setAlignment(Qt::AlignCenter);
42     m_forecastInitialDateLabel->setDisabled(true);
43
44     m_forecastDateLabel = new QLabel();
45     m_forecastDateLabel->setAlignment(Qt::AlignCenter);
46
47     m_downloadRetryButton = new QPushButton(tr("Download again"));
48
49     connect(m_downloadRetryButton, SIGNAL(clicked()), this, SLOT(downloadAgainClicked()));
50
51     m_minusDayButton = new QPushButton(tr("-1 d"));
52     m_plusDayButton = new QPushButton(tr("+1 d"));
53     m_minusHourButton = new QPushButton(tr("-1 h"));
54     m_plusHourButton = new QPushButton(tr("+1 h"));
55
56     connect(m_minusDayButton, SIGNAL(clicked()), this, SLOT(minusDayClicked()));
57     connect(m_plusDayButton, SIGNAL(clicked()), this, SLOT(plusDayClicked()));
58     connect(m_minusHourButton, SIGNAL(clicked()), this, SLOT(minusHourClicked()));
59     connect(m_plusHourButton, SIGNAL(clicked()), this, SLOT(plusHourClicked()));
60
61     setupUi();
62     setupMenu();
63
64     loadSettings();
65 }
66
67 MainWindow::~MainWindow()
68 {
69     delete m_downloader;
70 }
71
72 void MainWindow::setupUi()
73 {
74 #ifdef Q_WS_MAEMO_5
75     setAttribute(Qt::WA_Maemo5LandscapeOrientation, true);
76 #endif
77     setWindowTitle(tr("Medard"));
78
79     QWidget *widget = new QWidget;
80     setCentralWidget(widget);
81
82     QHBoxLayout *mainLayout = new QHBoxLayout();
83     mainLayout->setMargin(8);
84     mainLayout->setSpacing(4);
85     widget->setLayout(mainLayout);
86
87     mainLayout->addWidget(m_forecast);
88     mainLayout->addSpacing(4);
89
90     QVBoxLayout *layout = new QVBoxLayout();
91     mainLayout->addLayout(layout);
92
93     layout->addWidget(m_forecastTypeLabel);
94     layout->addWidget(m_forecastDateLabel);
95     layout->addSpacing(20);
96     layout->addWidget(m_forecastInitialDateLabel);
97     layout->addSpacing(20);
98     layout->addWidget(m_downloadRetryButton);
99
100     QHBoxLayout *dayNavigationBox = new QHBoxLayout();
101     dayNavigationBox->addWidget(m_minusDayButton);
102     dayNavigationBox->addWidget(m_plusDayButton);
103     layout->addLayout(dayNavigationBox);
104
105     QHBoxLayout *hourNavigationBox = new QHBoxLayout();
106     hourNavigationBox->addWidget(m_minusHourButton);
107     hourNavigationBox->addWidget(m_plusHourButton);
108     layout->addLayout(hourNavigationBox);
109
110     hideNavigationButtons(false);
111 }
112
113 void MainWindow::setupMenu()
114 {
115     QMenuBar *menu = new QMenuBar();
116     setMenuBar(menu);
117
118     m_domainActionGroup = new QActionGroup(this);
119     m_domainActionGroup->setExclusive(true);
120
121     QAction *domainAction;
122     domainAction = new QAction(tr("Europe"), m_domainActionGroup);
123     domainAction->setCheckable(true);
124     domainAction = new QAction(tr("Czech Republic"), m_domainActionGroup);
125     domainAction ->setCheckable(true);
126     menu->addActions(m_domainActionGroup->actions());
127     connect(m_domainActionGroup, SIGNAL(triggered(QAction *)), this, SLOT(forecastDomainChanged(QAction *)));
128
129     QAction *seaLevelPreasureAction = new QAction(tr("Sea Level Pressure"), this);
130     menu->addAction(seaLevelPreasureAction);
131     connect(seaLevelPreasureAction, SIGNAL(triggered()), this, SLOT(seaLevelPreasureMenuClicked()));
132
133     QAction *precipitationAction = new QAction(tr("Precipitation"), this);
134     menu->addAction(precipitationAction);
135     connect(precipitationAction, SIGNAL(triggered()), this, SLOT(precipitationMenuClicked()));
136
137     QAction *windVelocityAction = new QAction(tr("Wind Velocity"), this);
138     menu->addAction(windVelocityAction);
139     connect(windVelocityAction, SIGNAL(triggered()), this, SLOT(windVelocityMenuClicked()));
140
141     QAction *cloudinessAction = new QAction(tr("Cloudiness"), this);
142     menu->addAction(cloudinessAction);
143     connect(cloudinessAction, SIGNAL(triggered()), this, SLOT(cloudinessMenuClicked()));
144
145     QAction *temperatureAction = new QAction(tr("Temperature"), this);
146     menu->addAction(temperatureAction);
147     connect(temperatureAction, SIGNAL(triggered()), this, SLOT(temperatureMenuClicked()));
148 }
149
150 void MainWindow::loadSettings()
151 {
152     QSettings settings;
153
154     int forecastDomain = settings.value("ForecastDomain").toInt();
155     int forecastType = settings.value("ForecastType").toInt();
156
157     m_downloader->setForecastDomain((MedardDownloader::ForecastDomain) forecastDomain);
158     m_domainActionGroup->actions().at(forecastDomain)->setChecked(true);
159
160     switch ((MedardDownloader::ForecastType) forecastType) {
161         case MedardDownloader::SeaLevelPressure:
162             seaLevelPreasureMenuClicked();
163             break;
164
165         case MedardDownloader::Precipitation:
166             precipitationMenuClicked();
167             break;
168
169         case MedardDownloader::WindVelocity:
170             windVelocityMenuClicked();
171             break;
172
173         case MedardDownloader::Cloudiness:
174             cloudinessMenuClicked();
175             break;
176
177         case MedardDownloader::Temperature:
178             temperatureMenuClicked();
179             break;
180     }
181 }
182
183 void MainWindow::showNavigationButtons()
184 {
185     m_downloadRetryButton->hide();
186
187     m_minusDayButton->show();
188     m_plusDayButton->show();
189     m_minusHourButton->show();
190     m_plusHourButton->show();
191 }
192
193 void MainWindow::hideNavigationButtons(bool showRetryButton)
194 {
195     if (showRetryButton)
196         m_downloadRetryButton->show();
197     else
198         m_downloadRetryButton->hide();
199
200     m_minusDayButton->hide();
201     m_plusDayButton->hide();
202     m_minusHourButton->hide();
203     m_plusHourButton->hide();
204 }
205
206 void MainWindow::updateNavigationButtons()
207 {
208     if ((m_downloader->getForecastDateOffset() - 24) < m_downloader->getMinForecastDateOffset()) {
209         m_minusDayButton->setDisabled(true);
210         m_plusDayButton->setDisabled(false);
211     } else if ((m_downloader->getForecastDateOffset() + 24) > m_downloader->getMaxForecastDateOffset()) {
212         m_minusDayButton->setDisabled(false);
213         m_plusDayButton->setDisabled(true);
214     } else {
215         m_minusDayButton->setDisabled(false);
216         m_plusDayButton->setDisabled(false);
217     }
218
219     if ((m_downloader->getForecastDateOffset() - 1) < m_downloader->getMinForecastDateOffset()) {
220         m_minusHourButton->setDisabled(true);
221         m_plusHourButton->setDisabled(false);
222     } else if ((m_downloader->getForecastDateOffset() + 1) > m_downloader->getMaxForecastDateOffset()) {
223         m_minusHourButton->setDisabled(false);
224         m_plusHourButton->setDisabled(true);
225     } else {
226         m_minusHourButton->setDisabled(false);
227         m_plusHourButton->setDisabled(false);
228     }
229 }
230
231 void MainWindow::seaLevelPreasureMenuClicked()
232 {
233     forecastTypeChanged(tr("Sea Level Pressure"), MedardDownloader::SeaLevelPressure);
234 }
235
236 void MainWindow::precipitationMenuClicked()
237 {
238     forecastTypeChanged(tr("Precipitation"), MedardDownloader::Precipitation);
239 }
240
241 void MainWindow::windVelocityMenuClicked()
242 {
243     forecastTypeChanged(tr("Wind Velocity"), MedardDownloader::WindVelocity);
244 }
245
246 void MainWindow::cloudinessMenuClicked()
247 {
248     forecastTypeChanged(tr("Cloudiness"), MedardDownloader::Cloudiness);
249 }
250
251 void MainWindow::temperatureMenuClicked()
252 {
253     forecastTypeChanged(tr("Temperature"), MedardDownloader::Temperature);
254 }
255
256 void MainWindow::downloadAgainClicked()
257 {
258     m_forecast->clearImage(false);
259     m_downloader->downloadImage();
260
261     hideNavigationButtons(false);
262 }
263
264 void MainWindow::plusDayClicked()
265 {
266     forecastDateOffsetChanged(+24);
267 }
268
269 void MainWindow::minusDayClicked()
270 {
271     forecastDateOffsetChanged(-24);
272 }
273
274 void MainWindow::plusHourClicked()
275 {
276     forecastDateOffsetChanged(+1);
277 }
278
279 void MainWindow::minusHourClicked()
280 {
281     forecastDateOffsetChanged(-1);
282 }
283
284 void MainWindow::forecastTypeChanged(const QString label, MedardDownloader::ForecastType type)
285 {
286     m_forecastTypeLabel->setText(label);
287     m_forecast->clearImage(false);
288     m_downloader->setForecastType(type);
289     m_downloader->downloadImage();
290
291     QSettings settings;
292     settings.setValue("ForecastType", type);
293 }
294
295 void MainWindow::forecastDateOffsetChanged(int offset)
296 {
297     m_forecast->clearImage(false);
298     m_downloader->setForecastDateOffset(m_downloader->getForecastDateOffset() + offset);
299     m_downloader->downloadImage();
300 }
301
302 void MainWindow::forecastDomainChanged(QAction *action)
303 {
304     int forecastDomain = m_domainActionGroup->actions().indexOf(action);
305
306     m_forecast->clearImage(false);
307
308     if (forecastDomain == 0)
309         m_downloader->setForecastDomain(MedardDownloader::Europe);
310     else
311         m_downloader->setForecastDomain(MedardDownloader::CzechRepublic);
312
313     m_downloader->downloadImage();
314
315     QSettings settings;
316     settings.setValue("ForecastDomain", forecastDomain);
317 }
318
319 void MainWindow::downloadedFinished(const QString &filename, const QDateTime &date)
320 {
321     m_forecast->setImage(filename);
322     m_forecastInitialDateLabel->setText(tr("Forecast from:\n") +
323                                           m_downloader->getForecastInitialDate().toString("dd.MM.yyyy hh:mm"));
324
325     /* upcase the first letter of name of day */
326     QString temp = date.toString("dddd\ndd.MM.yyyy hh:mm");
327     m_forecastDateLabel->setText(temp.replace(0, 1, temp.at(0).toUpper()));
328
329     showNavigationButtons();
330     updateNavigationButtons();
331 }
332
333 void MainWindow::downloadFailed()
334 {
335     m_forecast->clearImage(true);
336     m_forecastInitialDateLabel->setText("");
337     m_forecastDateLabel->setText("");
338
339     hideNavigationButtons(true);
340 }