Removed prefix get from all methods.
[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 #include <QtGui>
21 #include <QSettings>
22
23 #include "aboutdialog.h"
24 #include "mainwindow.h"
25
26 MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent)
27 {
28     m_downloader = new MedardDownloader();
29
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()));
33
34     m_forecast = new ForecastWidget();
35     m_forecast->setFixedSize(m_downloader->imageSize());
36
37     m_forecastTypeLabel = new QLabel();
38     m_forecastTypeLabel->setAlignment(Qt::AlignCenter);
39     m_forecastTypeLabel->setFixedSize(220, 80);
40
41     m_forecastInitialDateLabel = new QLabel();
42     m_forecastInitialDateLabel->setAlignment(Qt::AlignCenter);
43     m_forecastInitialDateLabel->setDisabled(true);
44
45     m_forecastDateLabel = new QLabel();
46     m_forecastDateLabel->setAlignment(Qt::AlignCenter);
47
48     m_downloadRetryButton = new QPushButton(tr("Download again"));
49
50     connect(m_downloadRetryButton, SIGNAL(clicked()), this, SLOT(downloadAgainClicked()));
51
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"));
56
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()));
61
62     setupUi();
63     setupMenu();
64
65     loadSettings();
66 }
67
68 MainWindow::~MainWindow()
69 {
70     delete m_downloader;
71 }
72
73 void MainWindow::setupUi()
74 {
75 #ifdef Q_WS_MAEMO_5
76     setAttribute(Qt::WA_Maemo5LandscapeOrientation, true);
77 #endif
78     setWindowTitle(tr("Medard"));
79
80     QWidget *widget = new QWidget;
81     setCentralWidget(widget);
82
83     QHBoxLayout *mainLayout = new QHBoxLayout();
84     mainLayout->setMargin(8);
85     mainLayout->setSpacing(4);
86     widget->setLayout(mainLayout);
87
88     mainLayout->addWidget(m_forecast);
89     mainLayout->addSpacing(4);
90
91     QVBoxLayout *layout = new QVBoxLayout();
92     mainLayout->addLayout(layout);
93
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);
100
101     QHBoxLayout *dayNavigationBox = new QHBoxLayout();
102     dayNavigationBox->addWidget(m_minusDayButton);
103     dayNavigationBox->addWidget(m_plusDayButton);
104     layout->addLayout(dayNavigationBox);
105
106     QHBoxLayout *hourNavigationBox = new QHBoxLayout();
107     hourNavigationBox->addWidget(m_minusHourButton);
108     hourNavigationBox->addWidget(m_plusHourButton);
109     layout->addLayout(hourNavigationBox);
110
111     hideNavigationButtons(false);
112 }
113
114 void MainWindow::setupMenu()
115 {
116     QMenuBar *menu = new QMenuBar();
117     setMenuBar(menu);
118
119     m_domainActionGroup = new QActionGroup(this);
120     m_domainActionGroup->setExclusive(true);
121
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 *)));
129
130     QAction *seaLevelPreasureAction = new QAction(tr("Sea Level Pressure"), this);
131     menu->addAction(seaLevelPreasureAction);
132     connect(seaLevelPreasureAction, SIGNAL(triggered()), this, SLOT(seaLevelPreasureMenuClicked()));
133
134     QAction *precipitationAction = new QAction(tr("Precipitation"), this);
135     menu->addAction(precipitationAction);
136     connect(precipitationAction, SIGNAL(triggered()), this, SLOT(precipitationMenuClicked()));
137
138     QAction *windVelocityAction = new QAction(tr("Wind Velocity"), this);
139     menu->addAction(windVelocityAction);
140     connect(windVelocityAction, SIGNAL(triggered()), this, SLOT(windVelocityMenuClicked()));
141
142     QAction *cloudinessAction = new QAction(tr("Cloudiness"), this);
143     menu->addAction(cloudinessAction);
144     connect(cloudinessAction, SIGNAL(triggered()), this, SLOT(cloudinessMenuClicked()));
145
146     QAction *temperatureAction = new QAction(tr("Temperature"), this);
147     menu->addAction(temperatureAction);
148     connect(temperatureAction, SIGNAL(triggered()), this, SLOT(temperatureMenuClicked()));
149
150     QAction *aboutAction = new QAction(tr("About"), this);
151     menu->addAction(aboutAction);
152     connect(aboutAction, SIGNAL(triggered()), this, SLOT(aboutMenuClicked()));
153 }
154
155 void MainWindow::loadSettings()
156 {
157     QSettings settings;
158
159     int forecastDomain = settings.value("ForecastDomain").toInt();
160     int forecastType = settings.value("ForecastType").toInt();
161
162     m_downloader->setForecastDomain((MedardDownloader::ForecastDomain) forecastDomain);
163     m_domainActionGroup->actions().at(forecastDomain)->setChecked(true);
164
165     switch ((MedardDownloader::ForecastType) forecastType) {
166         case MedardDownloader::SeaLevelPressure:
167             seaLevelPreasureMenuClicked();
168             break;
169
170         case MedardDownloader::Precipitation:
171             precipitationMenuClicked();
172             break;
173
174         case MedardDownloader::WindVelocity:
175             windVelocityMenuClicked();
176             break;
177
178         case MedardDownloader::Cloudiness:
179             cloudinessMenuClicked();
180             break;
181
182         case MedardDownloader::Temperature:
183             temperatureMenuClicked();
184             break;
185     }
186 }
187
188 void MainWindow::showNavigationButtons()
189 {
190     m_downloadRetryButton->hide();
191
192     m_minusDayButton->show();
193     m_plusDayButton->show();
194     m_minusHourButton->show();
195     m_plusHourButton->show();
196 }
197
198 void MainWindow::hideNavigationButtons(bool showRetryButton)
199 {
200     if (showRetryButton)
201         m_downloadRetryButton->show();
202     else
203         m_downloadRetryButton->hide();
204
205     m_minusDayButton->hide();
206     m_plusDayButton->hide();
207     m_minusHourButton->hide();
208     m_plusHourButton->hide();
209 }
210
211 void MainWindow::updateNavigationButtons()
212 {
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);
219     } else {
220         m_minusDayButton->setDisabled(false);
221         m_plusDayButton->setDisabled(false);
222     }
223
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);
230     } else {
231         m_minusHourButton->setDisabled(false);
232         m_plusHourButton->setDisabled(false);
233     }
234 }
235
236 void MainWindow::seaLevelPreasureMenuClicked()
237 {
238     forecastTypeChanged(tr("Sea Level Pressure"), MedardDownloader::SeaLevelPressure);
239 }
240
241 void MainWindow::precipitationMenuClicked()
242 {
243     forecastTypeChanged(tr("Precipitation"), MedardDownloader::Precipitation);
244 }
245
246 void MainWindow::windVelocityMenuClicked()
247 {
248     forecastTypeChanged(tr("Wind Velocity"), MedardDownloader::WindVelocity);
249 }
250
251 void MainWindow::cloudinessMenuClicked()
252 {
253     forecastTypeChanged(tr("Cloudiness"), MedardDownloader::Cloudiness);
254 }
255
256 void MainWindow::temperatureMenuClicked()
257 {
258     forecastTypeChanged(tr("Temperature"), MedardDownloader::Temperature);
259 }
260
261 void MainWindow::aboutMenuClicked()
262 {
263     AboutDialog *dialog = new AboutDialog();
264     dialog->show();
265 }
266
267 void MainWindow::downloadAgainClicked()
268 {
269     m_forecast->clearImage(false);
270     m_downloader->downloadImage();
271
272     hideNavigationButtons(false);
273 }
274
275 void MainWindow::plusDayClicked()
276 {
277     forecastDateOffsetChanged(+24);
278 }
279
280 void MainWindow::minusDayClicked()
281 {
282     forecastDateOffsetChanged(-24);
283 }
284
285 void MainWindow::plusHourClicked()
286 {
287     forecastDateOffsetChanged(+1);
288 }
289
290 void MainWindow::minusHourClicked()
291 {
292     forecastDateOffsetChanged(-1);
293 }
294
295 void MainWindow::forecastTypeChanged(const QString label, MedardDownloader::ForecastType type)
296 {
297     m_forecastTypeLabel->setText(label);
298     m_forecast->clearImage(false);
299     m_downloader->setForecastType(type);
300     m_downloader->downloadImage();
301
302     QSettings settings;
303     settings.setValue("ForecastType", type);
304 }
305
306 void MainWindow::forecastDateOffsetChanged(int offset)
307 {
308     m_forecast->clearImage(false);
309     m_downloader->setForecastDateOffset(m_downloader->forecastDateOffset() + offset);
310     m_downloader->downloadImage();
311 }
312
313 void MainWindow::forecastDomainChanged(QAction *action)
314 {
315     int forecastDomain = m_domainActionGroup->actions().indexOf(action);
316
317     m_forecast->clearImage(false);
318
319     if (forecastDomain == 0)
320         m_downloader->setForecastDomain(MedardDownloader::Europe);
321     else
322         m_downloader->setForecastDomain(MedardDownloader::CzechRepublic);
323
324     m_downloader->downloadImage();
325
326     QSettings settings;
327     settings.setValue("ForecastDomain", forecastDomain);
328 }
329
330 void MainWindow::downloadedFinished(const QString &filename, const QDateTime &date)
331 {
332     m_forecast->setImage(filename);
333     m_forecastInitialDateLabel->setText(tr("Forecast from:\n") +
334                                           m_downloader->forecastInitialDate().toString("dd.MM.yyyy hh:mm"));
335
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()));
339
340     showNavigationButtons();
341     updateNavigationButtons();
342 }
343
344 void MainWindow::downloadFailed()
345 {
346     m_forecast->clearImage(true);
347     m_forecastInitialDateLabel->setText("");
348     m_forecastDateLabel->setText("");
349
350     hideNavigationButtons(true);
351 }