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
23 #include "mainwindow.h"
25 MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent)
27 m_downloader = new MedardDownloader();
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()));
33 m_forecast = new ForecastWidget();
34 m_forecast->setFixedSize(m_downloader->getImageSize());
36 m_forecastTypeLabel = new QLabel();
37 m_forecastTypeLabel->setAlignment(Qt::AlignCenter);
38 m_forecastTypeLabel->setFixedSize(220, 80);
40 m_forecastInitialDateLabel = new QLabel();
41 m_forecastInitialDateLabel->setAlignment(Qt::AlignCenter);
42 m_forecastInitialDateLabel->setDisabled(true);
44 m_forecastDateLabel = new QLabel();
45 m_forecastDateLabel->setAlignment(Qt::AlignCenter);
47 m_downloadRetryButton = new QPushButton(tr("Download again"));
49 connect(m_downloadRetryButton, SIGNAL(clicked()), this, SLOT(downloadAgainClicked()));
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"));
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()));
67 MainWindow::~MainWindow()
72 void MainWindow::setupUi()
75 setAttribute(Qt::WA_Maemo5LandscapeOrientation, true);
77 setWindowTitle(tr("Medard"));
79 QWidget *widget = new QWidget;
80 setCentralWidget(widget);
82 QHBoxLayout *mainLayout = new QHBoxLayout();
83 mainLayout->setMargin(8);
84 mainLayout->setSpacing(4);
85 widget->setLayout(mainLayout);
87 mainLayout->addWidget(m_forecast);
88 mainLayout->addSpacing(4);
90 QVBoxLayout *layout = new QVBoxLayout();
91 mainLayout->addLayout(layout);
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);
100 QHBoxLayout *dayNavigationBox = new QHBoxLayout();
101 dayNavigationBox->addWidget(m_minusDayButton);
102 dayNavigationBox->addWidget(m_plusDayButton);
103 layout->addLayout(dayNavigationBox);
105 QHBoxLayout *hourNavigationBox = new QHBoxLayout();
106 hourNavigationBox->addWidget(m_minusHourButton);
107 hourNavigationBox->addWidget(m_plusHourButton);
108 layout->addLayout(hourNavigationBox);
110 hideNavigationButtons(false);
113 void MainWindow::setupMenu()
115 QMenuBar *menu = new QMenuBar();
118 m_domainActionGroup = new QActionGroup(this);
119 m_domainActionGroup->setExclusive(true);
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 *)));
129 QAction *seaLevelPreasureAction = new QAction(tr("Sea Level Pressure"), this);
130 menu->addAction(seaLevelPreasureAction);
131 connect(seaLevelPreasureAction, SIGNAL(triggered()), this, SLOT(seaLevelPreasureMenuClicked()));
133 QAction *precipitationAction = new QAction(tr("Precipitation"), this);
134 menu->addAction(precipitationAction);
135 connect(precipitationAction, SIGNAL(triggered()), this, SLOT(precipitationMenuClicked()));
137 QAction *windVelocityAction = new QAction(tr("Wind Velocity"), this);
138 menu->addAction(windVelocityAction);
139 connect(windVelocityAction, SIGNAL(triggered()), this, SLOT(windVelocityMenuClicked()));
141 QAction *cloudinessAction = new QAction(tr("Cloudiness"), this);
142 menu->addAction(cloudinessAction);
143 connect(cloudinessAction, SIGNAL(triggered()), this, SLOT(cloudinessMenuClicked()));
145 QAction *temperatureAction = new QAction(tr("Temperature"), this);
146 menu->addAction(temperatureAction);
147 connect(temperatureAction, SIGNAL(triggered()), this, SLOT(temperatureMenuClicked()));
150 void MainWindow::loadSettings()
154 int forecastDomain = settings.value("ForecastDomain").toInt();
155 int forecastType = settings.value("ForecastType").toInt();
157 m_downloader->setForecastDomain((MedardDownloader::ForecastDomain) forecastDomain);
158 m_domainActionGroup->actions().at(forecastDomain)->setChecked(true);
160 switch ((MedardDownloader::ForecastType) forecastType) {
161 case MedardDownloader::SeaLevelPressure:
162 seaLevelPreasureMenuClicked();
165 case MedardDownloader::Precipitation:
166 precipitationMenuClicked();
169 case MedardDownloader::WindVelocity:
170 windVelocityMenuClicked();
173 case MedardDownloader::Cloudiness:
174 cloudinessMenuClicked();
177 case MedardDownloader::Temperature:
178 temperatureMenuClicked();
183 void MainWindow::showNavigationButtons()
185 m_downloadRetryButton->hide();
187 m_minusDayButton->show();
188 m_plusDayButton->show();
189 m_minusHourButton->show();
190 m_plusHourButton->show();
193 void MainWindow::hideNavigationButtons(bool showRetryButton)
196 m_downloadRetryButton->show();
198 m_downloadRetryButton->hide();
200 m_minusDayButton->hide();
201 m_plusDayButton->hide();
202 m_minusHourButton->hide();
203 m_plusHourButton->hide();
206 void MainWindow::updateNavigationButtons()
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);
215 m_minusDayButton->setDisabled(false);
216 m_plusDayButton->setDisabled(false);
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);
226 m_minusHourButton->setDisabled(false);
227 m_plusHourButton->setDisabled(false);
231 void MainWindow::seaLevelPreasureMenuClicked()
233 forecastTypeChanged(tr("Sea Level Pressure"), MedardDownloader::SeaLevelPressure);
236 void MainWindow::precipitationMenuClicked()
238 forecastTypeChanged(tr("Precipitation"), MedardDownloader::Precipitation);
241 void MainWindow::windVelocityMenuClicked()
243 forecastTypeChanged(tr("Wind Velocity"), MedardDownloader::WindVelocity);
246 void MainWindow::cloudinessMenuClicked()
248 forecastTypeChanged(tr("Cloudiness"), MedardDownloader::Cloudiness);
251 void MainWindow::temperatureMenuClicked()
253 forecastTypeChanged(tr("Temperature"), MedardDownloader::Temperature);
256 void MainWindow::downloadAgainClicked()
258 m_forecast->clearImage(false);
259 m_downloader->downloadImage();
261 hideNavigationButtons(false);
264 void MainWindow::plusDayClicked()
266 forecastDateOffsetChanged(+24);
269 void MainWindow::minusDayClicked()
271 forecastDateOffsetChanged(-24);
274 void MainWindow::plusHourClicked()
276 forecastDateOffsetChanged(+1);
279 void MainWindow::minusHourClicked()
281 forecastDateOffsetChanged(-1);
284 void MainWindow::forecastTypeChanged(const QString label, MedardDownloader::ForecastType type)
286 m_forecastTypeLabel->setText(label);
287 m_forecast->clearImage(false);
288 m_downloader->setForecastType(type);
289 m_downloader->downloadImage();
292 settings.setValue("ForecastType", type);
295 void MainWindow::forecastDateOffsetChanged(int offset)
297 m_forecast->clearImage(false);
298 m_downloader->setForecastDateOffset(m_downloader->getForecastDateOffset() + offset);
299 m_downloader->downloadImage();
302 void MainWindow::forecastDomainChanged(QAction *action)
304 int forecastDomain = m_domainActionGroup->actions().indexOf(action);
306 m_forecast->clearImage(false);
308 if (forecastDomain == 0)
309 m_downloader->setForecastDomain(MedardDownloader::Europe);
311 m_downloader->setForecastDomain(MedardDownloader::CzechRepublic);
313 m_downloader->downloadImage();
316 settings.setValue("ForecastDomain", forecastDomain);
319 void MainWindow::downloadedFinished(const QString &filename, const QDateTime &date)
321 m_forecast->setImage(filename);
322 m_forecastInitialDateLabel->setText(tr("Forecast from:\n") +
323 m_downloader->getForecastInitialDate().toString("dd.MM.yyyy hh:mm"));
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()));
329 showNavigationButtons();
330 updateNavigationButtons();
333 void MainWindow::downloadFailed()
335 m_forecast->clearImage(true);
336 m_forecastInitialDateLabel->setText("");
337 m_forecastDateLabel->setText("");
339 hideNavigationButtons(true);