3 * Copyright (C) 2011 Roman Moravcik
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.
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.
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
20 #ifdef MEEGO_EDITION_HARMATTAN
23 #include <MWidgetAction>
30 #include "aboutdialog.h"
31 #include "mainwindow.h"
33 #ifdef MEEGO_EDITION_HARMATTAN
34 MainWindow::MainWindow(QGraphicsItem *parent) : MApplicationPage(parent)
36 m_downloader = new MedardDownloader();
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()));
42 m_forecast = new ForecastWidget();
43 m_forecast->setPreferredSize(m_downloader->imageSize());
45 m_forecastTypeLabel = new MLabel();
46 m_forecastTypeLabel->setAlignment(Qt::AlignCenter);
47 m_forecastTypeLabel->setPreferredSize(220, 80);
49 m_forecastInitialDateLabel = new MLabel();
50 m_forecastInitialDateLabel->setAlignment(Qt::AlignCenter);
51 m_forecastInitialDateLabel->setWordWrap(true);
52 // m_forecastInitialDateLabel->setDisabled(true);
54 m_forecastDateLabel = new MLabel();
55 m_forecastDateLabel->setAlignment(Qt::AlignCenter);
56 m_forecastDateLabel->setWordWrap(true);
58 m_downloadRetryButton = new MButton(tr("Download again"));
59 m_downloadRetryButton->setPreferredWidth(220);
61 connect(m_downloadRetryButton, SIGNAL(clicked()), this, SLOT(downloadAgainClicked()));
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);
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()));
83 MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent)
85 m_downloader = new MedardDownloader();
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()));
91 m_forecast = new ForecastWidget();
92 m_forecast->setFixedSize(m_downloader->imageSize());
94 m_forecastTypeLabel = new QLabel();
95 m_forecastTypeLabel->setAlignment(Qt::AlignCenter);
96 m_forecastTypeLabel->setFixedSize(220, 80);
98 m_forecastInitialDateLabel = new QLabel();
99 m_forecastInitialDateLabel->setAlignment(Qt::AlignCenter);
100 m_forecastInitialDateLabel->setDisabled(true);
102 m_forecastDateLabel = new QLabel();
103 m_forecastDateLabel->setAlignment(Qt::AlignCenter);
105 m_downloadRetryButton = new QPushButton(tr("Download again"));
107 connect(m_downloadRetryButton, SIGNAL(clicked()), this, SLOT(downloadAgainClicked()));
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"));
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()));
126 MainWindow::~MainWindow()
131 #ifdef MEEGO_EDITION_HARMATTAN
132 void MainWindow::setupUi()
134 setWindowTitle(tr("Medard"));
137 QGraphicsLinearLayout *mainLayout = new QGraphicsLinearLayout(Qt::Horizontal);
138 centralWidget()->setLayout(mainLayout);
140 mainLayout->addItem(m_forecast);
142 QGraphicsLinearLayout *layout = new QGraphicsLinearLayout(Qt::Vertical);
143 mainLayout->addItem(layout);
145 layout->addItem(m_forecastTypeLabel);
146 layout->addItem(m_forecastDateLabel);
147 layout->addItem(m_forecastInitialDateLabel);
148 layout->addItem(m_downloadRetryButton);
150 QGraphicsLinearLayout *dayNavigationBox = new QGraphicsLinearLayout(Qt::Horizontal);
151 dayNavigationBox->addItem(m_minusDayButton);
152 dayNavigationBox->addItem(m_plusDayButton);
153 layout->addItem(dayNavigationBox);
155 QGraphicsLinearLayout *hourNavigationBox = new QGraphicsLinearLayout(Qt::Horizontal);
156 hourNavigationBox->addItem(m_minusHourButton);
157 hourNavigationBox->addItem(m_plusHourButton);
158 layout->addItem(hourNavigationBox);
160 hideNavigationButtons(false);
163 void MainWindow::setupUi()
166 setAttribute(Qt::WA_Maemo5LandscapeOrientation, true);
168 setWindowTitle(tr("Medard"));
170 QWidget *widget = new QWidget;
171 setCentralWidget(widget);
173 QHBoxLayout *mainLayout = new QHBoxLayout();
174 mainLayout->setMargin(8);
175 mainLayout->setSpacing(4);
176 widget->setLayout(mainLayout);
178 mainLayout->addWidget(m_forecast);
179 mainLayout->addSpacing(4);
181 QVBoxLayout *layout = new QVBoxLayout();
182 mainLayout->addLayout(layout);
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);
191 QHBoxLayout *dayNavigationBox = new QHBoxLayout();
192 dayNavigationBox->addWidget(m_minusDayButton);
193 dayNavigationBox->addWidget(m_plusDayButton);
194 layout->addLayout(dayNavigationBox);
196 QHBoxLayout *hourNavigationBox = new QHBoxLayout();
197 hourNavigationBox->addWidget(m_minusHourButton);
198 hourNavigationBox->addWidget(m_plusHourButton);
199 layout->addLayout(hourNavigationBox);
201 hideNavigationButtons(false);
205 #ifdef MEEGO_EDITION_HARMATTAN
206 void MainWindow::setupMenu()
208 QStringList forecastDomainList;
209 forecastDomainList << tr("Europe")
210 << tr("Czech Republic");
212 MWidgetAction *forecastDomainAction = new MWidgetAction(centralWidget());
213 forecastDomainAction->setLocation(MAction::ApplicationMenuLocation);
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)));
224 QStringList forecastTypeList;
225 forecastTypeList << tr("Sea Level Pressure")
226 << tr("Precipitation")
227 << tr("Wind Velocity")
229 << tr("Temperature");
231 MWidgetAction *forecastTypeAction = new MWidgetAction(centralWidget());
232 forecastTypeAction->setLocation(MAction::ApplicationMenuLocation);
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)));
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()));
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()));
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()));
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()));
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()));
270 void MainWindow::setupMenu()
272 QMenuBar *menu = new QMenuBar();
275 m_domainActionGroup = new QActionGroup(this);
276 m_domainActionGroup->setExclusive(true);
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 *)));
286 QAction *seaLevelPreasureAction = new QAction(tr("Sea Level Pressure"), this);
287 menu->addAction(seaLevelPreasureAction);
288 connect(seaLevelPreasureAction, SIGNAL(triggered()), this, SLOT(seaLevelPreasureMenuClicked()));
290 QAction *precipitationAction = new QAction(tr("Precipitation"), this);
291 menu->addAction(precipitationAction);
292 connect(precipitationAction, SIGNAL(triggered()), this, SLOT(precipitationMenuClicked()));
294 QAction *windVelocityAction = new QAction(tr("Wind Velocity"), this);
295 menu->addAction(windVelocityAction);
296 connect(windVelocityAction, SIGNAL(triggered()), this, SLOT(windVelocityMenuClicked()));
298 QAction *cloudinessAction = new QAction(tr("Cloudiness"), this);
299 menu->addAction(cloudinessAction);
300 connect(cloudinessAction, SIGNAL(triggered()), this, SLOT(cloudinessMenuClicked()));
302 QAction *temperatureAction = new QAction(tr("Temperature"), this);
303 menu->addAction(temperatureAction);
304 connect(temperatureAction, SIGNAL(triggered()), this, SLOT(temperatureMenuClicked()));
306 QAction *aboutAction = new QAction(tr("About"), this);
307 menu->addAction(aboutAction);
308 connect(aboutAction, SIGNAL(triggered()), this, SLOT(aboutMenuClicked()));
312 void MainWindow::loadSettings()
316 int forecastDomain = settings.value("ForecastDomain").toInt();
317 int forecastType = settings.value("ForecastType").toInt();
319 m_downloader->setForecastDomain((MedardDownloader::ForecastDomain) forecastDomain);
320 #ifdef MEEGO_EDITION_HARMATTAN
321 m_forecastDomainComboBox->setCurrentIndex(forecastDomain);
323 m_domainActionGroup->actions().at(forecastDomain)->setChecked(true);
326 switch ((MedardDownloader::ForecastType) forecastType) {
327 case MedardDownloader::SeaLevelPressure:
328 seaLevelPreasureMenuClicked();
331 case MedardDownloader::Precipitation:
332 precipitationMenuClicked();
335 case MedardDownloader::WindVelocity:
336 windVelocityMenuClicked();
339 case MedardDownloader::Cloudiness:
340 cloudinessMenuClicked();
343 case MedardDownloader::Temperature:
344 temperatureMenuClicked();
349 void MainWindow::showNavigationButtons()
351 m_downloadRetryButton->hide();
353 m_minusDayButton->show();
354 m_plusDayButton->show();
355 m_minusHourButton->show();
356 m_plusHourButton->show();
359 void MainWindow::hideNavigationButtons(bool showRetryButton)
362 m_downloadRetryButton->show();
364 m_downloadRetryButton->hide();
366 m_minusDayButton->hide();
367 m_plusDayButton->hide();
368 m_minusHourButton->hide();
369 m_plusHourButton->hide();
372 void MainWindow::updateNavigationButtons()
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);
381 // m_minusDayButton->setDisabled(false);
382 // m_plusDayButton->setDisabled(false);
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);
392 // m_minusHourButton->setDisabled(false);
393 // m_plusHourButton->setDisabled(false);
397 void MainWindow::setForecastType(const QString label, MedardDownloader::ForecastType type)
399 m_forecastTypeLabel->setText(label);
400 m_forecast->clearImage(false);
401 m_downloader->setForecastType(type);
402 m_downloader->downloadImage();
405 settings.setValue("ForecastType", type);
408 void MainWindow::setForecastDateOffset(int offset)
410 m_forecast->clearImage(false);
411 m_downloader->setForecastDateOffset(m_downloader->forecastDateOffset() + offset);
412 m_downloader->downloadImage();
415 void MainWindow::seaLevelPreasureMenuClicked()
417 #ifdef MEEGO_EDITION_HARMATTAN
418 m_forecastTypeComboBox->setCurrentIndex(MedardDownloader::SeaLevelPressure);
420 setForecastType(tr("Sea Level Pressure"), MedardDownloader::SeaLevelPressure);
423 void MainWindow::precipitationMenuClicked()
425 #ifdef MEEGO_EDITION_HARMATTAN
426 m_forecastTypeComboBox->setCurrentIndex(MedardDownloader::Precipitation);
428 setForecastType(tr("Precipitation"), MedardDownloader::Precipitation);
431 void MainWindow::windVelocityMenuClicked()
433 #ifdef MEEGO_EDITION_HARMATTAN
434 m_forecastTypeComboBox->setCurrentIndex(MedardDownloader::WindVelocity);
436 setForecastType(tr("Wind Velocity"), MedardDownloader::WindVelocity);
439 void MainWindow::cloudinessMenuClicked()
441 #ifdef MEEGO_EDITION_HARMATTAN
442 m_forecastTypeComboBox->setCurrentIndex(MedardDownloader::Cloudiness);
444 setForecastType(tr("Cloudiness"), MedardDownloader::Cloudiness);
447 void MainWindow::temperatureMenuClicked()
449 #ifdef MEEGO_EDITION_HARMATTAN
450 m_forecastTypeComboBox->setCurrentIndex(MedardDownloader::Temperature);
452 setForecastType(tr("Temperature"), MedardDownloader::Temperature);
455 void MainWindow::aboutMenuClicked()
457 AboutDialog *dialog = new AboutDialog();
458 #ifdef MEEGO_EDITION_HARMATTAN
459 dialog->appear(MSceneWindow::DestroyWhenDismissed);
465 void MainWindow::downloadAgainClicked()
467 m_forecast->clearImage(false);
468 m_downloader->downloadImage();
470 hideNavigationButtons(false);
473 void MainWindow::plusDayClicked()
475 setForecastDateOffset(+24);
478 void MainWindow::minusDayClicked()
480 setForecastDateOffset(-24);
483 void MainWindow::plusHourClicked()
485 setForecastDateOffset(+1);
488 void MainWindow::minusHourClicked()
490 setForecastDateOffset(-1);
493 #ifdef MEEGO_EDITION_HARMATTAN
494 void MainWindow::forecastDomainChanged(int index)
496 m_forecast->clearImage(false);
499 m_downloader->setForecastDomain(MedardDownloader::Europe);
501 m_downloader->setForecastDomain(MedardDownloader::CzechRepublic);
503 m_downloader->downloadImage();
506 settings.setValue("ForecastDomain", index);
509 void MainWindow::forecastTypeChanged(int index)
511 switch ((MedardDownloader::ForecastType) index) {
512 case MedardDownloader::SeaLevelPressure:
513 setForecastType(tr("Sea Level Pressure"), MedardDownloader::SeaLevelPressure);
516 case MedardDownloader::Precipitation:
517 setForecastType(tr("Precipitation"), MedardDownloader::Precipitation);
520 case MedardDownloader::WindVelocity:
521 setForecastType(tr("Wind Velocity"), MedardDownloader::WindVelocity);
524 case MedardDownloader::Cloudiness:
525 setForecastType(tr("Cloudiness"), MedardDownloader::Cloudiness);
528 case MedardDownloader::Temperature:
529 setForecastType(tr("Temperature"), MedardDownloader::Temperature);
535 void MainWindow::forecastDomainChanged(QAction *action)
537 int forecastDomain = m_domainActionGroup->actions().indexOf(action);
539 m_forecast->clearImage(false);
541 if (forecastDomain == 0)
542 m_downloader->setForecastDomain(MedardDownloader::Europe);
544 m_downloader->setForecastDomain(MedardDownloader::CzechRepublic);
546 m_downloader->downloadImage();
549 settings.setValue("ForecastDomain", forecastDomain);
553 void MainWindow::downloadedFinished(const QString &filename, const QDateTime &date)
555 m_forecast->setImage(filename);
556 m_forecastInitialDateLabel->setText(tr("Forecast from:\n") +
557 m_downloader->forecastInitialDate().toString("dd.MM.yyyy hh:mm"));
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()));
563 showNavigationButtons();
564 updateNavigationButtons();
567 void MainWindow::downloadFailed()
569 m_forecast->clearImage(true);
570 m_forecastInitialDateLabel->setText("");
571 m_forecastDateLabel->setText("");
573 hideNavigationButtons(true);