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 "aboutdialog.h"
24 #include "mainwindow.h"
26 MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent)
28 m_downloader = new MedardDownloader();
30 connect(m_downloader, SIGNAL(downloadFinished(const QString &, const QDateTime &)), this,
31 SLOT(downloadedFinished(const QString &, const QDateTime &)));
32 connect(m_downloader, SIGNAL(downloadFailed()), this, SLOT(downloadFailed()));
34 m_forecast = new ForecastWidget();
35 m_forecast->setFixedSize(m_downloader->imageSize());
37 m_forecastTypeLabel = new QLabel();
38 m_forecastTypeLabel->setAlignment(Qt::AlignCenter);
39 m_forecastTypeLabel->setFixedSize(220, 80);
41 m_forecastInitialDateLabel = new QLabel();
42 m_forecastInitialDateLabel->setAlignment(Qt::AlignCenter);
43 m_forecastInitialDateLabel->setDisabled(true);
45 m_forecastDateLabel = new QLabel();
46 m_forecastDateLabel->setAlignment(Qt::AlignCenter);
48 m_downloadRetryButton = new QPushButton(tr("Download again"));
50 connect(m_downloadRetryButton, SIGNAL(clicked()), this, SLOT(downloadAgainClicked()));
52 m_minusDayButton = new QPushButton(tr("-1 d"));
53 m_plusDayButton = new QPushButton(tr("+1 d"));
54 m_minusHourButton = new QPushButton(tr("-1 h"));
55 m_plusHourButton = new QPushButton(tr("+1 h"));
57 connect(m_minusDayButton, SIGNAL(clicked()), this, SLOT(minusDayClicked()));
58 connect(m_plusDayButton, SIGNAL(clicked()), this, SLOT(plusDayClicked()));
59 connect(m_minusHourButton, SIGNAL(clicked()), this, SLOT(minusHourClicked()));
60 connect(m_plusHourButton, SIGNAL(clicked()), this, SLOT(plusHourClicked()));
68 MainWindow::~MainWindow()
73 void MainWindow::setupUi()
76 setAttribute(Qt::WA_Maemo5LandscapeOrientation, true);
78 setWindowTitle(tr("Medard"));
80 QWidget *widget = new QWidget;
81 setCentralWidget(widget);
83 QHBoxLayout *mainLayout = new QHBoxLayout();
84 mainLayout->setMargin(8);
85 mainLayout->setSpacing(4);
86 widget->setLayout(mainLayout);
88 mainLayout->addWidget(m_forecast);
89 mainLayout->addSpacing(4);
91 QVBoxLayout *layout = new QVBoxLayout();
92 mainLayout->addLayout(layout);
94 layout->addWidget(m_forecastTypeLabel);
95 layout->addWidget(m_forecastDateLabel);
96 layout->addSpacing(20);
97 layout->addWidget(m_forecastInitialDateLabel);
98 layout->addSpacing(20);
99 layout->addWidget(m_downloadRetryButton);
101 QHBoxLayout *dayNavigationBox = new QHBoxLayout();
102 dayNavigationBox->addWidget(m_minusDayButton);
103 dayNavigationBox->addWidget(m_plusDayButton);
104 layout->addLayout(dayNavigationBox);
106 QHBoxLayout *hourNavigationBox = new QHBoxLayout();
107 hourNavigationBox->addWidget(m_minusHourButton);
108 hourNavigationBox->addWidget(m_plusHourButton);
109 layout->addLayout(hourNavigationBox);
111 hideNavigationButtons(false);
114 void MainWindow::setupMenu()
116 QMenuBar *menu = new QMenuBar();
119 m_domainActionGroup = new QActionGroup(this);
120 m_domainActionGroup->setExclusive(true);
122 QAction *domainAction;
123 domainAction = new QAction(tr("Europe"), m_domainActionGroup);
124 domainAction->setCheckable(true);
125 domainAction = new QAction(tr("Czech Republic"), m_domainActionGroup);
126 domainAction ->setCheckable(true);
127 menu->addActions(m_domainActionGroup->actions());
128 connect(m_domainActionGroup, SIGNAL(triggered(QAction *)), this, SLOT(forecastDomainChanged(QAction *)));
130 QAction *seaLevelPreasureAction = new QAction(tr("Sea Level Pressure"), this);
131 menu->addAction(seaLevelPreasureAction);
132 connect(seaLevelPreasureAction, SIGNAL(triggered()), this, SLOT(seaLevelPreasureMenuClicked()));
134 QAction *precipitationAction = new QAction(tr("Precipitation"), this);
135 menu->addAction(precipitationAction);
136 connect(precipitationAction, SIGNAL(triggered()), this, SLOT(precipitationMenuClicked()));
138 QAction *windVelocityAction = new QAction(tr("Wind Velocity"), this);
139 menu->addAction(windVelocityAction);
140 connect(windVelocityAction, SIGNAL(triggered()), this, SLOT(windVelocityMenuClicked()));
142 QAction *cloudinessAction = new QAction(tr("Cloudiness"), this);
143 menu->addAction(cloudinessAction);
144 connect(cloudinessAction, SIGNAL(triggered()), this, SLOT(cloudinessMenuClicked()));
146 QAction *temperatureAction = new QAction(tr("Temperature"), this);
147 menu->addAction(temperatureAction);
148 connect(temperatureAction, SIGNAL(triggered()), this, SLOT(temperatureMenuClicked()));
150 QAction *aboutAction = new QAction(tr("About"), this);
151 menu->addAction(aboutAction);
152 connect(aboutAction, SIGNAL(triggered()), this, SLOT(aboutMenuClicked()));
155 void MainWindow::loadSettings()
159 int forecastDomain = settings.value("ForecastDomain").toInt();
160 int forecastType = settings.value("ForecastType").toInt();
162 m_downloader->setForecastDomain((MedardDownloader::ForecastDomain) forecastDomain);
163 m_domainActionGroup->actions().at(forecastDomain)->setChecked(true);
165 switch ((MedardDownloader::ForecastType) forecastType) {
166 case MedardDownloader::SeaLevelPressure:
167 seaLevelPreasureMenuClicked();
170 case MedardDownloader::Precipitation:
171 precipitationMenuClicked();
174 case MedardDownloader::WindVelocity:
175 windVelocityMenuClicked();
178 case MedardDownloader::Cloudiness:
179 cloudinessMenuClicked();
182 case MedardDownloader::Temperature:
183 temperatureMenuClicked();
188 void MainWindow::showNavigationButtons()
190 m_downloadRetryButton->hide();
192 m_minusDayButton->show();
193 m_plusDayButton->show();
194 m_minusHourButton->show();
195 m_plusHourButton->show();
198 void MainWindow::hideNavigationButtons(bool showRetryButton)
201 m_downloadRetryButton->show();
203 m_downloadRetryButton->hide();
205 m_minusDayButton->hide();
206 m_plusDayButton->hide();
207 m_minusHourButton->hide();
208 m_plusHourButton->hide();
211 void MainWindow::updateNavigationButtons()
213 if ((m_downloader->forecastDateOffset() - 24) < m_downloader->minForecastDateOffset()) {
214 m_minusDayButton->setDisabled(true);
215 m_plusDayButton->setDisabled(false);
216 } else if ((m_downloader->forecastDateOffset() + 24) > m_downloader->maxForecastDateOffset()) {
217 m_minusDayButton->setDisabled(false);
218 m_plusDayButton->setDisabled(true);
220 m_minusDayButton->setDisabled(false);
221 m_plusDayButton->setDisabled(false);
224 if ((m_downloader->forecastDateOffset() - 1) < m_downloader->minForecastDateOffset()) {
225 m_minusHourButton->setDisabled(true);
226 m_plusHourButton->setDisabled(false);
227 } else if ((m_downloader->forecastDateOffset() + 1) > m_downloader->maxForecastDateOffset()) {
228 m_minusHourButton->setDisabled(false);
229 m_plusHourButton->setDisabled(true);
231 m_minusHourButton->setDisabled(false);
232 m_plusHourButton->setDisabled(false);
236 void MainWindow::seaLevelPreasureMenuClicked()
238 forecastTypeChanged(tr("Sea Level Pressure"), MedardDownloader::SeaLevelPressure);
241 void MainWindow::precipitationMenuClicked()
243 forecastTypeChanged(tr("Precipitation"), MedardDownloader::Precipitation);
246 void MainWindow::windVelocityMenuClicked()
248 forecastTypeChanged(tr("Wind Velocity"), MedardDownloader::WindVelocity);
251 void MainWindow::cloudinessMenuClicked()
253 forecastTypeChanged(tr("Cloudiness"), MedardDownloader::Cloudiness);
256 void MainWindow::temperatureMenuClicked()
258 forecastTypeChanged(tr("Temperature"), MedardDownloader::Temperature);
261 void MainWindow::aboutMenuClicked()
263 AboutDialog *dialog = new AboutDialog();
267 void MainWindow::downloadAgainClicked()
269 m_forecast->clearImage(false);
270 m_downloader->downloadImage();
272 hideNavigationButtons(false);
275 void MainWindow::plusDayClicked()
277 forecastDateOffsetChanged(+24);
280 void MainWindow::minusDayClicked()
282 forecastDateOffsetChanged(-24);
285 void MainWindow::plusHourClicked()
287 forecastDateOffsetChanged(+1);
290 void MainWindow::minusHourClicked()
292 forecastDateOffsetChanged(-1);
295 void MainWindow::forecastTypeChanged(const QString label, MedardDownloader::ForecastType type)
297 m_forecastTypeLabel->setText(label);
298 m_forecast->clearImage(false);
299 m_downloader->setForecastType(type);
300 m_downloader->downloadImage();
303 settings.setValue("ForecastType", type);
306 void MainWindow::forecastDateOffsetChanged(int offset)
308 m_forecast->clearImage(false);
309 m_downloader->setForecastDateOffset(m_downloader->forecastDateOffset() + offset);
310 m_downloader->downloadImage();
313 void MainWindow::forecastDomainChanged(QAction *action)
315 int forecastDomain = m_domainActionGroup->actions().indexOf(action);
317 m_forecast->clearImage(false);
319 if (forecastDomain == 0)
320 m_downloader->setForecastDomain(MedardDownloader::Europe);
322 m_downloader->setForecastDomain(MedardDownloader::CzechRepublic);
324 m_downloader->downloadImage();
327 settings.setValue("ForecastDomain", forecastDomain);
330 void MainWindow::downloadedFinished(const QString &filename, const QDateTime &date)
332 m_forecast->setImage(filename);
333 m_forecastInitialDateLabel->setText(tr("Forecast from:\n") +
334 m_downloader->forecastInitialDate().toString("dd.MM.yyyy hh:mm"));
336 /* upcase the first letter of name of day */
337 QString temp = date.toString("dddd\ndd.MM.yyyy hh:mm");
338 m_forecastDateLabel->setText(temp.replace(0, 1, temp.at(0).toUpper()));
340 showNavigationButtons();
341 updateNavigationButtons();
344 void MainWindow::downloadFailed()
346 m_forecast->clearImage(true);
347 m_forecastInitialDateLabel->setText("");
348 m_forecastDateLabel->setText("");
350 hideNavigationButtons(true);