f79476c992fc9629c589006f9d52f87fedbcb009
[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 #ifdef MEEGO_EDITION_HARMATTAN
21 #include <MLayout>
22 #include <MAction>
23 #include <MWidgetAction>
24 #include <MComboBox>
25 #endif
26
27 #include <QtGui>
28 #include <QSettings>
29
30 #include "aboutdialog.h"
31 #include "mainwindow.h"
32
33 #ifdef MEEGO_EDITION_HARMATTAN
34 MainWindow::MainWindow(QGraphicsItem *parent) : MApplicationPage(parent)
35 {
36     m_downloader = new MedardDownloader();
37
38     connect(m_downloader, SIGNAL(downloadFinished(const QString &, const QDateTime &)), this,
39             SLOT(downloadedFinished(const QString &, const QDateTime &)));
40     connect(m_downloader, SIGNAL(downloadFailed()), this, SLOT(downloadFailed()));
41
42     m_forecast = new ForecastWidget();
43     m_forecast->setPreferredSize(m_downloader->imageSize());
44
45     m_forecastTypeLabel = new MLabel();
46     m_forecastTypeLabel->setAlignment(Qt::AlignCenter);
47     m_forecastTypeLabel->setPreferredSize(220, 80);
48
49     m_forecastInitialDateLabel = new MLabel();
50     m_forecastInitialDateLabel->setAlignment(Qt::AlignCenter);
51     m_forecastInitialDateLabel->setWordWrap(true);
52 //    m_forecastInitialDateLabel->setDisabled(true);
53
54     m_forecastDateLabel = new MLabel();
55     m_forecastDateLabel->setAlignment(Qt::AlignCenter);
56     m_forecastDateLabel->setWordWrap(true);
57
58     m_downloadRetryButton = new MButton(tr("Download again"));
59     m_downloadRetryButton->setPreferredWidth(220);
60
61     connect(m_downloadRetryButton, SIGNAL(clicked()), this, SLOT(downloadAgainClicked()));
62
63     m_minusDayButton = new MButton(tr("-1 d"));
64     m_minusDayButton->setPreferredWidth(50);
65     m_plusDayButton = new MButton(tr("+1 d"));
66     m_plusDayButton->setPreferredWidth(50);
67     m_minusHourButton = new MButton(tr("-1 h"));
68     m_minusHourButton->setPreferredWidth(50);
69     m_plusHourButton = new MButton(tr("+1 h"));
70     m_plusHourButton->setPreferredWidth(50);
71
72     connect(m_minusDayButton, SIGNAL(clicked()), this, SLOT(minusDayClicked()));
73     connect(m_plusDayButton, SIGNAL(clicked()), this, SLOT(plusDayClicked()));
74     connect(m_minusHourButton, SIGNAL(clicked()), this, SLOT(minusHourClicked()));
75     connect(m_plusHourButton, SIGNAL(clicked()), this, SLOT(plusHourClicked()));
76
77     setupUi();
78     setupMenu();
79
80     loadSettings();
81 }
82 #else
83 MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent)
84 {
85     m_downloader = new MedardDownloader();
86
87     connect(m_downloader, SIGNAL(downloadFinished(const QString &, const QDateTime &)), this,
88             SLOT(downloadedFinished(const QString &, const QDateTime &)));
89     connect(m_downloader, SIGNAL(downloadFailed()), this, SLOT(downloadFailed()));
90
91     m_forecast = new ForecastWidget();
92     m_forecast->setFixedSize(m_downloader->imageSize());
93
94     m_forecastTypeLabel = new QLabel();
95     m_forecastTypeLabel->setAlignment(Qt::AlignCenter);
96     m_forecastTypeLabel->setFixedSize(220, 80);
97
98     m_forecastInitialDateLabel = new QLabel();
99     m_forecastInitialDateLabel->setAlignment(Qt::AlignCenter);
100     m_forecastInitialDateLabel->setDisabled(true);
101
102     m_forecastDateLabel = new QLabel();
103     m_forecastDateLabel->setAlignment(Qt::AlignCenter);
104
105     m_downloadRetryButton = new QPushButton(tr("Download again"));
106
107     connect(m_downloadRetryButton, SIGNAL(clicked()), this, SLOT(downloadAgainClicked()));
108
109     m_minusDayButton = new QPushButton(tr("-1 d"));
110     m_plusDayButton = new QPushButton(tr("+1 d"));
111     m_minusHourButton = new QPushButton(tr("-1 h"));
112     m_plusHourButton = new QPushButton(tr("+1 h"));
113
114     connect(m_minusDayButton, SIGNAL(clicked()), this, SLOT(minusDayClicked()));
115     connect(m_plusDayButton, SIGNAL(clicked()), this, SLOT(plusDayClicked()));
116     connect(m_minusHourButton, SIGNAL(clicked()), this, SLOT(minusHourClicked()));
117     connect(m_plusHourButton, SIGNAL(clicked()), this, SLOT(plusHourClicked()));
118
119     setupUi();
120     setupMenu();
121
122     loadSettings();
123 }
124 #endif
125
126 MainWindow::~MainWindow()
127 {
128     delete m_downloader;
129 }
130
131 #ifdef MEEGO_EDITION_HARMATTAN
132 void MainWindow::setupUi()
133 {
134     setAttribute(Qt::WA_LockPortraitOrientation, true);
135     setWindowTitle(tr("Medard"));
136     setPannable(false);
137
138     QGraphicsLinearLayout *mainLayout = new QGraphicsLinearLayout(Qt::Horizontal);
139     centralWidget()->setLayout(mainLayout);
140
141     mainLayout->addItem(m_forecast);
142
143     QGraphicsLinearLayout *layout = new QGraphicsLinearLayout(Qt::Vertical);
144     mainLayout->addItem(layout);
145
146     layout->addItem(m_forecastTypeLabel);
147     layout->addItem(m_forecastDateLabel);
148     layout->addItem(m_forecastInitialDateLabel);
149     layout->addItem(m_downloadRetryButton);
150
151     QGraphicsLinearLayout *dayNavigationBox = new QGraphicsLinearLayout(Qt::Horizontal);
152     dayNavigationBox->addItem(m_minusDayButton);
153     dayNavigationBox->addItem(m_plusDayButton);
154     layout->addItem(dayNavigationBox);
155
156     QGraphicsLinearLayout *hourNavigationBox = new QGraphicsLinearLayout(Qt::Horizontal);
157     hourNavigationBox->addItem(m_minusHourButton);
158     hourNavigationBox->addItem(m_plusHourButton);
159     layout->addItem(hourNavigationBox);
160
161     hideNavigationButtons(false);
162 }
163 #else
164 void MainWindow::setupUi()
165 {
166 #ifdef Q_WS_MAEMO_5
167     setAttribute(Qt::WA_Maemo5LandscapeOrientation, true);
168 #endif
169     setWindowTitle(tr("Medard"));
170
171     QWidget *widget = new QWidget;
172     setCentralWidget(widget);
173
174     QHBoxLayout *mainLayout = new QHBoxLayout();
175     mainLayout->setMargin(8);
176     mainLayout->setSpacing(4);
177     widget->setLayout(mainLayout);
178
179     mainLayout->addWidget(m_forecast);
180     mainLayout->addSpacing(4);
181
182     QVBoxLayout *layout = new QVBoxLayout();
183     mainLayout->addLayout(layout);
184
185     layout->addWidget(m_forecastTypeLabel);
186     layout->addWidget(m_forecastDateLabel);
187     layout->addSpacing(20);
188     layout->addWidget(m_forecastInitialDateLabel);
189     layout->addSpacing(20);
190     layout->addWidget(m_downloadRetryButton);
191
192     QHBoxLayout *dayNavigationBox = new QHBoxLayout();
193     dayNavigationBox->addWidget(m_minusDayButton);
194     dayNavigationBox->addWidget(m_plusDayButton);
195     layout->addLayout(dayNavigationBox);
196
197     QHBoxLayout *hourNavigationBox = new QHBoxLayout();
198     hourNavigationBox->addWidget(m_minusHourButton);
199     hourNavigationBox->addWidget(m_plusHourButton);
200     layout->addLayout(hourNavigationBox);
201
202     hideNavigationButtons(false);
203 }
204 #endif
205
206 #ifdef MEEGO_EDITION_HARMATTAN
207 void MainWindow::setupMenu()
208 {
209     QStringList forecastDomainList;
210     forecastDomainList << tr("Europe") 
211                        << tr("Czech Republic");
212
213     MWidgetAction *forecastDomainAction = new MWidgetAction(centralWidget());
214     forecastDomainAction->setLocation(MAction::ApplicationMenuLocation);
215
216     m_forecastDomainComboBox = new MComboBox;
217     m_forecastDomainComboBox->setTitle(tr("Domain"));
218     m_forecastDomainComboBox->setStyleName ("CommonComboBox");
219     m_forecastDomainComboBox->setIconVisible(false);
220     m_forecastDomainComboBox->addItems(forecastDomainList);
221     forecastDomainAction->setWidget(m_forecastDomainComboBox);
222     addAction(forecastDomainAction);
223     connect(m_forecastDomainComboBox, SIGNAL(activated(int)), this, SLOT(forecastDomainChanged(int)));
224
225     QStringList forecastTypeList;
226     forecastTypeList << tr("Sea Level Pressure")
227                      << tr("Precipitation")
228                      << tr("Wind Velocity")
229                      << tr("Cloudiness")
230                      << tr("Temperature");
231
232     MWidgetAction *forecastTypeAction = new MWidgetAction(centralWidget());
233     forecastTypeAction->setLocation(MAction::ApplicationMenuLocation);
234
235     m_forecastTypeComboBox = new MComboBox;
236     m_forecastTypeComboBox->setTitle(tr("Forecast"));
237     m_forecastTypeComboBox->setStyleName ("CommonComboBox");
238     m_forecastTypeComboBox->setIconVisible(false);
239     m_forecastTypeComboBox->addItems(forecastTypeList);
240     forecastTypeAction->setWidget(m_forecastTypeComboBox);
241     addAction(forecastTypeAction);
242     connect(m_forecastTypeComboBox, SIGNAL(activated(int)), this, SLOT(forecastTypeChanged(int)));
243
244     MAction *seaLevelPreasureAction = new MAction("icon-m-weather-cloudy", "", this);
245     seaLevelPreasureAction->setLocation(MAction::ToolBarLocation);
246     addAction(seaLevelPreasureAction);
247     connect(seaLevelPreasureAction, SIGNAL(triggered()), this, SLOT(seaLevelPreasureMenuClicked()));
248
249     MAction *precipitationAction = new MAction("icon-m-weather-rain", "", this);
250     precipitationAction->setLocation(MAction::ToolBarLocation);
251     addAction(precipitationAction);
252     connect(precipitationAction, SIGNAL(triggered()), this, SLOT(precipitationMenuClicked()));
253
254     MAction *cloudinessAction = new MAction("icon-m-weather-partly-sunny", "", this);
255     cloudinessAction->setLocation(MAction::ToolBarLocation);
256     addAction(cloudinessAction);
257     connect(cloudinessAction, SIGNAL(triggered()), this, SLOT(cloudinessMenuClicked()));
258
259     MAction *temperatureAction = new MAction("icon-m-weather-hot", "", this);
260     temperatureAction->setLocation(MAction::ToolBarLocation);
261     addAction(temperatureAction);
262     connect(temperatureAction, SIGNAL(triggered()), this, SLOT(temperatureMenuClicked()));
263
264     MAction *aboutAction = new MAction(this);
265     aboutAction->setText(tr("About"));
266     aboutAction->setLocation(MAction::ApplicationMenuLocation);
267     addAction(aboutAction);
268     connect(aboutAction, SIGNAL(triggered()), this, SLOT(aboutMenuClicked()));
269 }
270 #else
271 void MainWindow::setupMenu()
272 {
273     QMenuBar *menu = new QMenuBar();
274     setMenuBar(menu);
275
276     m_domainActionGroup = new QActionGroup(this);
277     m_domainActionGroup->setExclusive(true);
278
279     QAction *domainAction;
280     domainAction = new QAction(tr("Europe"), m_domainActionGroup);
281     domainAction->setCheckable(true);
282     domainAction = new QAction(tr("Czech Republic"), m_domainActionGroup);
283     domainAction ->setCheckable(true);
284     menu->addActions(m_domainActionGroup->actions());
285     connect(m_domainActionGroup, SIGNAL(triggered(QAction *)), this, SLOT(forecastDomainChanged(QAction *)));
286
287     QAction *seaLevelPreasureAction = new QAction(tr("Sea Level Pressure"), this);
288     menu->addAction(seaLevelPreasureAction);
289     connect(seaLevelPreasureAction, SIGNAL(triggered()), this, SLOT(seaLevelPreasureMenuClicked()));
290
291     QAction *precipitationAction = new QAction(tr("Precipitation"), this);
292     menu->addAction(precipitationAction);
293     connect(precipitationAction, SIGNAL(triggered()), this, SLOT(precipitationMenuClicked()));
294
295     QAction *windVelocityAction = new QAction(tr("Wind Velocity"), this);
296     menu->addAction(windVelocityAction);
297     connect(windVelocityAction, SIGNAL(triggered()), this, SLOT(windVelocityMenuClicked()));
298
299     QAction *cloudinessAction = new QAction(tr("Cloudiness"), this);
300     menu->addAction(cloudinessAction);
301     connect(cloudinessAction, SIGNAL(triggered()), this, SLOT(cloudinessMenuClicked()));
302
303     QAction *temperatureAction = new QAction(tr("Temperature"), this);
304     menu->addAction(temperatureAction);
305     connect(temperatureAction, SIGNAL(triggered()), this, SLOT(temperatureMenuClicked()));
306
307     QAction *aboutAction = new QAction(tr("About"), this);
308     menu->addAction(aboutAction);
309     connect(aboutAction, SIGNAL(triggered()), this, SLOT(aboutMenuClicked()));
310 }
311 #endif
312
313 void MainWindow::loadSettings()
314 {
315     QSettings settings;
316
317     int forecastDomain = settings.value("ForecastDomain").toInt();
318     int forecastType = settings.value("ForecastType").toInt();
319
320     m_downloader->setForecastDomain((MedardDownloader::ForecastDomain) forecastDomain);
321 #ifdef MEEGO_EDITION_HARMATTAN
322     m_forecastDomainComboBox->setCurrentIndex(forecastDomain);
323 #else
324     m_domainActionGroup->actions().at(forecastDomain)->setChecked(true);
325 #endif
326
327     switch ((MedardDownloader::ForecastType) forecastType) {
328         case MedardDownloader::SeaLevelPressure:
329             seaLevelPreasureMenuClicked();
330             break;
331
332         case MedardDownloader::Precipitation:
333             precipitationMenuClicked();
334             break;
335
336         case MedardDownloader::WindVelocity:
337             windVelocityMenuClicked();
338             break;
339
340         case MedardDownloader::Cloudiness:
341             cloudinessMenuClicked();
342             break;
343
344         case MedardDownloader::Temperature:
345             temperatureMenuClicked();
346             break;
347     }
348 }
349
350 void MainWindow::showNavigationButtons()
351 {
352     m_downloadRetryButton->hide();
353
354     m_minusDayButton->show();
355     m_plusDayButton->show();
356     m_minusHourButton->show();
357     m_plusHourButton->show();
358 }
359
360 void MainWindow::hideNavigationButtons(bool showRetryButton)
361 {
362     if (showRetryButton)
363         m_downloadRetryButton->show();
364     else
365         m_downloadRetryButton->hide();
366
367     m_minusDayButton->hide();
368     m_plusDayButton->hide();
369     m_minusHourButton->hide();
370     m_plusHourButton->hide();
371 }
372
373 void MainWindow::updateNavigationButtons()
374 {
375     if ((m_downloader->forecastDateOffset() - 24) < m_downloader->minForecastDateOffset()) {
376 //        m_minusDayButton->setDisabled(true);
377 //        m_plusDayButton->setDisabled(false);
378     } else if ((m_downloader->forecastDateOffset() + 24) > m_downloader->maxForecastDateOffset()) {
379 //        m_minusDayButton->setDisabled(false);
380 //        m_plusDayButton->setDisabled(true);
381     } else {
382 //        m_minusDayButton->setDisabled(false);
383 //        m_plusDayButton->setDisabled(false);
384     }
385
386     if ((m_downloader->forecastDateOffset() - 1) < m_downloader->minForecastDateOffset()) {
387 //        m_minusHourButton->setDisabled(true);
388 //        m_plusHourButton->setDisabled(false);
389     } else if ((m_downloader->forecastDateOffset() + 1) > m_downloader->maxForecastDateOffset()) {
390 //        m_minusHourButton->setDisabled(false);
391 //        m_plusHourButton->setDisabled(true);
392     } else {
393 //        m_minusHourButton->setDisabled(false);
394 //        m_plusHourButton->setDisabled(false);
395     }
396 }
397
398 void MainWindow::setForecastType(const QString label, MedardDownloader::ForecastType type)
399 {
400     m_forecastTypeLabel->setText(label);
401     m_forecast->clearImage(false);
402     m_downloader->setForecastType(type);
403     m_downloader->downloadImage();
404
405     QSettings settings;
406     settings.setValue("ForecastType", type);
407 }
408
409 void MainWindow::setForecastDateOffset(int offset)
410 {
411     m_forecast->clearImage(false);
412     m_downloader->setForecastDateOffset(m_downloader->forecastDateOffset() + offset);
413     m_downloader->downloadImage();
414 }
415
416 void MainWindow::seaLevelPreasureMenuClicked()
417 {
418 #ifdef MEEGO_EDITION_HARMATTAN
419     m_forecastTypeComboBox->setCurrentIndex(MedardDownloader::SeaLevelPressure);
420 #endif
421     setForecastType(tr("Sea Level Pressure"), MedardDownloader::SeaLevelPressure);
422 }
423
424 void MainWindow::precipitationMenuClicked()
425 {
426 #ifdef MEEGO_EDITION_HARMATTAN
427     m_forecastTypeComboBox->setCurrentIndex(MedardDownloader::Precipitation);
428 #endif
429     setForecastType(tr("Precipitation"), MedardDownloader::Precipitation);
430 }
431
432 void MainWindow::windVelocityMenuClicked()
433 {
434 #ifdef MEEGO_EDITION_HARMATTAN
435     m_forecastTypeComboBox->setCurrentIndex(MedardDownloader::WindVelocity);
436 #endif
437     setForecastType(tr("Wind Velocity"), MedardDownloader::WindVelocity);
438 }
439
440 void MainWindow::cloudinessMenuClicked()
441 {
442 #ifdef MEEGO_EDITION_HARMATTAN
443     m_forecastTypeComboBox->setCurrentIndex(MedardDownloader::Cloudiness);
444 #endif
445     setForecastType(tr("Cloudiness"), MedardDownloader::Cloudiness);
446 }
447
448 void MainWindow::temperatureMenuClicked()
449 {
450 #ifdef MEEGO_EDITION_HARMATTAN
451     m_forecastTypeComboBox->setCurrentIndex(MedardDownloader::Temperature);
452 #endif
453     setForecastType(tr("Temperature"), MedardDownloader::Temperature);
454 }
455
456 void MainWindow::aboutMenuClicked()
457 {
458     AboutDialog *dialog = new AboutDialog();
459 #ifdef MEEGO_EDITION_HARMATTAN
460     dialog->appear(MSceneWindow::DestroyWhenDismissed);
461 #else
462     dialog->exec();
463 #endif
464 }
465
466 void MainWindow::downloadAgainClicked()
467 {
468     m_forecast->clearImage(false);
469     m_downloader->downloadImage();
470
471     hideNavigationButtons(false);
472 }
473
474 void MainWindow::plusDayClicked()
475 {
476     setForecastDateOffset(+24);
477 }
478
479 void MainWindow::minusDayClicked()
480 {
481     setForecastDateOffset(-24);
482 }
483
484 void MainWindow::plusHourClicked()
485 {
486     setForecastDateOffset(+1);
487 }
488
489 void MainWindow::minusHourClicked()
490 {
491     setForecastDateOffset(-1);
492 }
493
494 #ifdef MEEGO_EDITION_HARMATTAN
495 void MainWindow::forecastDomainChanged(int index)
496 {
497     m_forecast->clearImage(false);
498
499     if (index == 0)
500         m_downloader->setForecastDomain(MedardDownloader::Europe);
501     else
502         m_downloader->setForecastDomain(MedardDownloader::CzechRepublic);
503
504     m_downloader->downloadImage();
505
506     QSettings settings;
507     settings.setValue("ForecastDomain", index);
508 }
509
510 void MainWindow::forecastTypeChanged(int index)
511 {
512     switch ((MedardDownloader::ForecastType) index) {
513         case MedardDownloader::SeaLevelPressure:
514             setForecastType(tr("Sea Level Pressure"), MedardDownloader::SeaLevelPressure);
515             break;
516
517         case MedardDownloader::Precipitation:
518             setForecastType(tr("Precipitation"), MedardDownloader::Precipitation);
519             break;
520
521         case MedardDownloader::WindVelocity:
522             setForecastType(tr("Wind Velocity"), MedardDownloader::WindVelocity);
523             break;
524
525         case MedardDownloader::Cloudiness:
526             setForecastType(tr("Cloudiness"), MedardDownloader::Cloudiness);
527             break;
528
529         case MedardDownloader::Temperature:
530             setForecastType(tr("Temperature"), MedardDownloader::Temperature);
531             break;
532     }
533 }
534
535 #else
536 void MainWindow::forecastDomainChanged(QAction *action)
537 {
538     int forecastDomain = m_domainActionGroup->actions().indexOf(action);
539
540     m_forecast->clearImage(false);
541
542     if (forecastDomain == 0)
543         m_downloader->setForecastDomain(MedardDownloader::Europe);
544     else
545         m_downloader->setForecastDomain(MedardDownloader::CzechRepublic);
546
547     m_downloader->downloadImage();
548
549     QSettings settings;
550     settings.setValue("ForecastDomain", forecastDomain);
551 }
552 #endif
553
554 void MainWindow::downloadedFinished(const QString &filename, const QDateTime &date)
555 {
556     m_forecast->setImage(filename);
557     m_forecastInitialDateLabel->setText(tr("Forecast from:\n") +
558                                           m_downloader->forecastInitialDate().toString("dd.MM.yyyy hh:mm"));
559
560     /* upcase the first letter of name of day */
561     QString temp = date.toString("dddd\ndd.MM.yyyy hh:mm");
562     m_forecastDateLabel->setText(temp.replace(0, 1, temp.at(0).toUpper()));
563
564     showNavigationButtons();
565     updateNavigationButtons();
566 }
567
568 void MainWindow::downloadFailed()
569 {
570     m_forecast->clearImage(true);
571     m_forecastInitialDateLabel->setText("");
572     m_forecastDateLabel->setText("");
573
574     hideNavigationButtons(true);
575 }