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