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 setAttribute(Qt::WA_LockPortraitOrientation, true);
135 setWindowTitle(tr("Medard"));
138 QGraphicsLinearLayout *mainLayout = new QGraphicsLinearLayout(Qt::Horizontal);
139 centralWidget()->setLayout(mainLayout);
141 mainLayout->addItem(m_forecast);
143 QGraphicsLinearLayout *layout = new QGraphicsLinearLayout(Qt::Vertical);
144 mainLayout->addItem(layout);
146 layout->addItem(m_forecastTypeLabel);
147 layout->addItem(m_forecastDateLabel);
148 layout->addItem(m_forecastInitialDateLabel);
149 layout->addItem(m_downloadRetryButton);
151 QGraphicsLinearLayout *dayNavigationBox = new QGraphicsLinearLayout(Qt::Horizontal);
152 dayNavigationBox->addItem(m_minusDayButton);
153 dayNavigationBox->addItem(m_plusDayButton);
154 layout->addItem(dayNavigationBox);
156 QGraphicsLinearLayout *hourNavigationBox = new QGraphicsLinearLayout(Qt::Horizontal);
157 hourNavigationBox->addItem(m_minusHourButton);
158 hourNavigationBox->addItem(m_plusHourButton);
159 layout->addItem(hourNavigationBox);
161 hideNavigationButtons(false);
164 void MainWindow::setupUi()
167 setAttribute(Qt::WA_Maemo5LandscapeOrientation, true);
169 setWindowTitle(tr("Medard"));
171 QWidget *widget = new QWidget;
172 setCentralWidget(widget);
174 QHBoxLayout *mainLayout = new QHBoxLayout();
175 mainLayout->setMargin(8);
176 mainLayout->setSpacing(4);
177 widget->setLayout(mainLayout);
179 mainLayout->addWidget(m_forecast);
180 mainLayout->addSpacing(4);
182 QVBoxLayout *layout = new QVBoxLayout();
183 mainLayout->addLayout(layout);
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);
192 QHBoxLayout *dayNavigationBox = new QHBoxLayout();
193 dayNavigationBox->addWidget(m_minusDayButton);
194 dayNavigationBox->addWidget(m_plusDayButton);
195 layout->addLayout(dayNavigationBox);
197 QHBoxLayout *hourNavigationBox = new QHBoxLayout();
198 hourNavigationBox->addWidget(m_minusHourButton);
199 hourNavigationBox->addWidget(m_plusHourButton);
200 layout->addLayout(hourNavigationBox);
202 hideNavigationButtons(false);
206 #ifdef MEEGO_EDITION_HARMATTAN
207 void MainWindow::setupMenu()
209 QStringList forecastDomainList;
210 forecastDomainList << tr("Europe")
211 << tr("Czech Republic");
213 MWidgetAction *forecastDomainAction = new MWidgetAction(centralWidget());
214 forecastDomainAction->setLocation(MAction::ApplicationMenuLocation);
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)));
225 QStringList forecastTypeList;
226 forecastTypeList << tr("Sea Level Pressure")
227 << tr("Precipitation")
228 << tr("Wind Velocity")
230 << tr("Temperature");
232 MWidgetAction *forecastTypeAction = new MWidgetAction(centralWidget());
233 forecastTypeAction->setLocation(MAction::ApplicationMenuLocation);
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)));
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()));
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()));
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()));
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()));
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()));
271 void MainWindow::setupMenu()
273 QMenuBar *menu = new QMenuBar();
276 m_domainActionGroup = new QActionGroup(this);
277 m_domainActionGroup->setExclusive(true);
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 *)));
287 QAction *seaLevelPreasureAction = new QAction(tr("Sea Level Pressure"), this);
288 menu->addAction(seaLevelPreasureAction);
289 connect(seaLevelPreasureAction, SIGNAL(triggered()), this, SLOT(seaLevelPreasureMenuClicked()));
291 QAction *precipitationAction = new QAction(tr("Precipitation"), this);
292 menu->addAction(precipitationAction);
293 connect(precipitationAction, SIGNAL(triggered()), this, SLOT(precipitationMenuClicked()));
295 QAction *windVelocityAction = new QAction(tr("Wind Velocity"), this);
296 menu->addAction(windVelocityAction);
297 connect(windVelocityAction, SIGNAL(triggered()), this, SLOT(windVelocityMenuClicked()));
299 QAction *cloudinessAction = new QAction(tr("Cloudiness"), this);
300 menu->addAction(cloudinessAction);
301 connect(cloudinessAction, SIGNAL(triggered()), this, SLOT(cloudinessMenuClicked()));
303 QAction *temperatureAction = new QAction(tr("Temperature"), this);
304 menu->addAction(temperatureAction);
305 connect(temperatureAction, SIGNAL(triggered()), this, SLOT(temperatureMenuClicked()));
307 QAction *aboutAction = new QAction(tr("About"), this);
308 menu->addAction(aboutAction);
309 connect(aboutAction, SIGNAL(triggered()), this, SLOT(aboutMenuClicked()));
313 void MainWindow::loadSettings()
317 int forecastDomain = settings.value("ForecastDomain").toInt();
318 int forecastType = settings.value("ForecastType").toInt();
320 m_downloader->setForecastDomain((MedardDownloader::ForecastDomain) forecastDomain);
321 #ifdef MEEGO_EDITION_HARMATTAN
322 m_forecastDomainComboBox->setCurrentIndex(forecastDomain);
324 m_domainActionGroup->actions().at(forecastDomain)->setChecked(true);
327 switch ((MedardDownloader::ForecastType) forecastType) {
328 case MedardDownloader::SeaLevelPressure:
329 seaLevelPreasureMenuClicked();
332 case MedardDownloader::Precipitation:
333 precipitationMenuClicked();
336 case MedardDownloader::WindVelocity:
337 windVelocityMenuClicked();
340 case MedardDownloader::Cloudiness:
341 cloudinessMenuClicked();
344 case MedardDownloader::Temperature:
345 temperatureMenuClicked();
350 void MainWindow::showNavigationButtons()
352 m_downloadRetryButton->hide();
354 m_minusDayButton->show();
355 m_plusDayButton->show();
356 m_minusHourButton->show();
357 m_plusHourButton->show();
360 void MainWindow::hideNavigationButtons(bool showRetryButton)
363 m_downloadRetryButton->show();
365 m_downloadRetryButton->hide();
367 m_minusDayButton->hide();
368 m_plusDayButton->hide();
369 m_minusHourButton->hide();
370 m_plusHourButton->hide();
373 void MainWindow::updateNavigationButtons()
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);
382 // m_minusDayButton->setDisabled(false);
383 // m_plusDayButton->setDisabled(false);
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);
393 // m_minusHourButton->setDisabled(false);
394 // m_plusHourButton->setDisabled(false);
398 void MainWindow::setForecastType(const QString label, MedardDownloader::ForecastType type)
400 m_forecastTypeLabel->setText(label);
401 m_forecast->clearImage(false);
402 m_downloader->setForecastType(type);
403 m_downloader->downloadImage();
406 settings.setValue("ForecastType", type);
409 void MainWindow::setForecastDateOffset(int offset)
411 m_forecast->clearImage(false);
412 m_downloader->setForecastDateOffset(m_downloader->forecastDateOffset() + offset);
413 m_downloader->downloadImage();
416 void MainWindow::seaLevelPreasureMenuClicked()
418 #ifdef MEEGO_EDITION_HARMATTAN
419 m_forecastTypeComboBox->setCurrentIndex(MedardDownloader::SeaLevelPressure);
421 setForecastType(tr("Sea Level Pressure"), MedardDownloader::SeaLevelPressure);
424 void MainWindow::precipitationMenuClicked()
426 #ifdef MEEGO_EDITION_HARMATTAN
427 m_forecastTypeComboBox->setCurrentIndex(MedardDownloader::Precipitation);
429 setForecastType(tr("Precipitation"), MedardDownloader::Precipitation);
432 void MainWindow::windVelocityMenuClicked()
434 #ifdef MEEGO_EDITION_HARMATTAN
435 m_forecastTypeComboBox->setCurrentIndex(MedardDownloader::WindVelocity);
437 setForecastType(tr("Wind Velocity"), MedardDownloader::WindVelocity);
440 void MainWindow::cloudinessMenuClicked()
442 #ifdef MEEGO_EDITION_HARMATTAN
443 m_forecastTypeComboBox->setCurrentIndex(MedardDownloader::Cloudiness);
445 setForecastType(tr("Cloudiness"), MedardDownloader::Cloudiness);
448 void MainWindow::temperatureMenuClicked()
450 #ifdef MEEGO_EDITION_HARMATTAN
451 m_forecastTypeComboBox->setCurrentIndex(MedardDownloader::Temperature);
453 setForecastType(tr("Temperature"), MedardDownloader::Temperature);
456 void MainWindow::aboutMenuClicked()
458 AboutDialog *dialog = new AboutDialog();
459 #ifdef MEEGO_EDITION_HARMATTAN
460 dialog->appear(MSceneWindow::DestroyWhenDismissed);
466 void MainWindow::downloadAgainClicked()
468 m_forecast->clearImage(false);
469 m_downloader->downloadImage();
471 hideNavigationButtons(false);
474 void MainWindow::plusDayClicked()
476 setForecastDateOffset(+24);
479 void MainWindow::minusDayClicked()
481 setForecastDateOffset(-24);
484 void MainWindow::plusHourClicked()
486 setForecastDateOffset(+1);
489 void MainWindow::minusHourClicked()
491 setForecastDateOffset(-1);
494 #ifdef MEEGO_EDITION_HARMATTAN
495 void MainWindow::forecastDomainChanged(int index)
497 m_forecast->clearImage(false);
500 m_downloader->setForecastDomain(MedardDownloader::Europe);
502 m_downloader->setForecastDomain(MedardDownloader::CzechRepublic);
504 m_downloader->downloadImage();
507 settings.setValue("ForecastDomain", index);
510 void MainWindow::forecastTypeChanged(int index)
512 switch ((MedardDownloader::ForecastType) index) {
513 case MedardDownloader::SeaLevelPressure:
514 setForecastType(tr("Sea Level Pressure"), MedardDownloader::SeaLevelPressure);
517 case MedardDownloader::Precipitation:
518 setForecastType(tr("Precipitation"), MedardDownloader::Precipitation);
521 case MedardDownloader::WindVelocity:
522 setForecastType(tr("Wind Velocity"), MedardDownloader::WindVelocity);
525 case MedardDownloader::Cloudiness:
526 setForecastType(tr("Cloudiness"), MedardDownloader::Cloudiness);
529 case MedardDownloader::Temperature:
530 setForecastType(tr("Temperature"), MedardDownloader::Temperature);
536 void MainWindow::forecastDomainChanged(QAction *action)
538 int forecastDomain = m_domainActionGroup->actions().indexOf(action);
540 m_forecast->clearImage(false);
542 if (forecastDomain == 0)
543 m_downloader->setForecastDomain(MedardDownloader::Europe);
545 m_downloader->setForecastDomain(MedardDownloader::CzechRepublic);
547 m_downloader->downloadImage();
550 settings.setValue("ForecastDomain", forecastDomain);
554 void MainWindow::downloadedFinished(const QString &filename, const QDateTime &date)
556 m_forecast->setImage(filename);
557 m_forecastInitialDateLabel->setText(tr("Forecast from:\n") +
558 m_downloader->forecastInitialDate().toString("dd.MM.yyyy hh:mm"));
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()));
564 showNavigationButtons();
565 updateNavigationButtons();
568 void MainWindow::downloadFailed()
570 m_forecast->clearImage(true);
571 m_forecastInitialDateLabel->setText("");
572 m_forecastDateLabel->setText("");
574 hideNavigationButtons(true);