Fixed toolbar and menu items.
[medard] / src / mainwindow.cpp
index d76a17c..c82f413 100644 (file)
  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  */
 
+#ifdef Q_WS_MAEMO_6
+#include <MLayout>
+#include <MAction>
+#include <MWidgetAction>
+#include <MComboBox>
+#endif
+
 #include <QtGui>
 #include <QSettings>
 
+#include "aboutdialog.h"
 #include "mainwindow.h"
 
+#ifdef Q_WS_MAEMO_6
+MainWindow::MainWindow(QGraphicsItem *parent) : MApplicationPage(parent)
+{
+    m_downloader = new MedardDownloader();
+
+    connect(m_downloader, SIGNAL(downloadFinished(const QString &, const QDateTime &)), this,
+            SLOT(downloadedFinished(const QString &, const QDateTime &)));
+    connect(m_downloader, SIGNAL(downloadFailed()), this, SLOT(downloadFailed()));
+
+    m_forecast = new ForecastWidget();
+    m_forecast->setPreferredSize(m_downloader->imageSize());
+
+    m_forecastTypeLabel = new MLabel();
+    m_forecastTypeLabel->setAlignment(Qt::AlignCenter);
+    m_forecastTypeLabel->setPreferredSize(220, 80);
+
+    m_forecastInitialDateLabel = new MLabel();
+    m_forecastInitialDateLabel->setAlignment(Qt::AlignCenter);
+    m_forecastInitialDateLabel->setWordWrap(true);
+//    m_forecastInitialDateLabel->setDisabled(true);
+
+    m_forecastDateLabel = new MLabel();
+    m_forecastDateLabel->setAlignment(Qt::AlignCenter);
+    m_forecastDateLabel->setWordWrap(true);
+
+    m_downloadRetryButton = new MButton(tr("Download again"));
+    m_downloadRetryButton->setPreferredWidth(220);
+
+    connect(m_downloadRetryButton, SIGNAL(clicked()), this, SLOT(downloadAgainClicked()));
+
+    m_minusDayButton = new MButton(tr("-1 d"));
+    m_minusDayButton->setPreferredWidth(50);
+    m_plusDayButton = new MButton(tr("+1 d"));
+    m_plusDayButton->setPreferredWidth(50);
+    m_minusHourButton = new MButton(tr("-1 h"));
+    m_minusHourButton->setPreferredWidth(50);
+    m_plusHourButton = new MButton(tr("+1 h"));
+    m_plusHourButton->setPreferredWidth(50);
+
+    connect(m_minusDayButton, SIGNAL(clicked()), this, SLOT(minusDayClicked()));
+    connect(m_plusDayButton, SIGNAL(clicked()), this, SLOT(plusDayClicked()));
+    connect(m_minusHourButton, SIGNAL(clicked()), this, SLOT(minusHourClicked()));
+    connect(m_plusHourButton, SIGNAL(clicked()), this, SLOT(plusHourClicked()));
+
+    setupUi();
+    setupMenu();
+
+    loadSettings();
+}
+#else
 MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent)
 {
     m_downloader = new MedardDownloader();
@@ -31,7 +89,7 @@ MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent)
     connect(m_downloader, SIGNAL(downloadFailed()), this, SLOT(downloadFailed()));
 
     m_forecast = new ForecastWidget();
-    m_forecast->setFixedSize(m_downloader->getImageSize());
+    m_forecast->setFixedSize(m_downloader->imageSize());
 
     m_forecastTypeLabel = new QLabel();
     m_forecastTypeLabel->setAlignment(Qt::AlignCenter);
@@ -63,12 +121,46 @@ MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent)
 
     loadSettings();
 }
+#endif
 
 MainWindow::~MainWindow()
 {
     delete m_downloader;
 }
 
+#ifdef Q_WS_MAEMO_6
+void MainWindow::setupUi()
+{
+    setAttribute(Qt::WA_LockPortraitOrientation, true);
+    setWindowTitle(tr("Medard"));
+    setPannable(false);
+
+    QGraphicsLinearLayout *mainLayout = new QGraphicsLinearLayout(Qt::Horizontal);
+    centralWidget()->setLayout(mainLayout);
+
+    mainLayout->addItem(m_forecast);
+
+    QGraphicsLinearLayout *layout = new QGraphicsLinearLayout(Qt::Vertical);
+    mainLayout->addItem(layout);
+
+    layout->addItem(m_forecastTypeLabel);
+    layout->addItem(m_forecastDateLabel);
+    layout->addItem(m_forecastInitialDateLabel);
+    layout->addItem(m_downloadRetryButton);
+
+    QGraphicsLinearLayout *dayNavigationBox = new QGraphicsLinearLayout(Qt::Horizontal);
+    dayNavigationBox->addItem(m_minusDayButton);
+    dayNavigationBox->addItem(m_plusDayButton);
+    layout->addItem(dayNavigationBox);
+
+    QGraphicsLinearLayout *hourNavigationBox = new QGraphicsLinearLayout(Qt::Horizontal);
+    hourNavigationBox->addItem(m_minusHourButton);
+    hourNavigationBox->addItem(m_plusHourButton);
+    layout->addItem(hourNavigationBox);
+
+    hideNavigationButtons(false);
+}
+#else
 void MainWindow::setupUi()
 {
 #ifdef Q_WS_MAEMO_5
@@ -109,7 +201,73 @@ void MainWindow::setupUi()
 
     hideNavigationButtons(false);
 }
+#endif
 
+#ifdef Q_WS_MAEMO_6
+void MainWindow::setupMenu()
+{
+    QStringList forecastDomainList;
+    forecastDomainList << tr("Europe") 
+                       << tr("Czech Republic");
+
+    MWidgetAction *forecastDomainAction = new MWidgetAction(centralWidget());
+    forecastDomainAction->setLocation(MAction::ApplicationMenuLocation);
+
+    m_forecastDomainComboBox = new MComboBox;
+    m_forecastDomainComboBox->setTitle(tr("Domain"));
+    m_forecastDomainComboBox->setStyleName ("CommonComboBox");
+    m_forecastDomainComboBox->setIconVisible(false);
+    m_forecastDomainComboBox->addItems(forecastDomainList);
+    forecastDomainAction->setWidget(m_forecastDomainComboBox);
+    addAction(forecastDomainAction);
+    connect(m_forecastDomainComboBox, SIGNAL(activated(int)), this, SLOT(forecastDomainChanged(int)));
+
+    QStringList forecastTypeList;
+    forecastTypeList << tr("Sea Level Pressure")
+                     << tr("Precipitation")
+                     << tr("Wind Velocity")
+                     << tr("Cloudiness")
+                     << tr("Temperature");
+
+    MWidgetAction *forecastTypeAction = new MWidgetAction(centralWidget());
+    forecastTypeAction->setLocation(MAction::ApplicationMenuLocation);
+
+    m_forecastTypeComboBox = new MComboBox;
+    m_forecastTypeComboBox->setTitle(tr("Forecast"));
+    m_forecastTypeComboBox->setStyleName ("CommonComboBox");
+    m_forecastTypeComboBox->setIconVisible(false);
+    m_forecastTypeComboBox->addItems(forecastTypeList);
+    forecastTypeAction->setWidget(m_forecastTypeComboBox);
+    addAction(forecastTypeAction);
+    connect(m_forecastTypeComboBox, SIGNAL(activated(int)), this, SLOT(forecastTypeChanged(int)));
+
+    MAction *seaLevelPreasureAction = new MAction("icon-m-weather-cloudy", "", this);
+    seaLevelPreasureAction->setLocation(MAction::ToolBarLocation);
+    addAction(seaLevelPreasureAction);
+    connect(seaLevelPreasureAction, SIGNAL(triggered()), this, SLOT(seaLevelPreasureMenuClicked()));
+
+    MAction *precipitationAction = new MAction("icon-m-weather-rain", "", this);
+    precipitationAction->setLocation(MAction::ToolBarLocation);
+    addAction(precipitationAction);
+    connect(precipitationAction, SIGNAL(triggered()), this, SLOT(precipitationMenuClicked()));
+
+    MAction *cloudinessAction = new MAction("icon-m-weather-partly-sunny", "", this);
+    cloudinessAction->setLocation(MAction::ToolBarLocation);
+    addAction(cloudinessAction);
+    connect(cloudinessAction, SIGNAL(triggered()), this, SLOT(cloudinessMenuClicked()));
+
+    MAction *temperatureAction = new MAction("icon-m-weather-hot", "", this);
+    temperatureAction->setLocation(MAction::ToolBarLocation);
+    addAction(temperatureAction);
+    connect(temperatureAction, SIGNAL(triggered()), this, SLOT(temperatureMenuClicked()));
+
+    MAction *aboutAction = new MAction(this);
+    aboutAction->setText(tr("About"));
+    aboutAction->setLocation(MAction::ApplicationMenuLocation);
+    addAction(aboutAction);
+    connect(aboutAction, SIGNAL(triggered()), this, SLOT(aboutMenuClicked()));
+}
+#else
 void MainWindow::setupMenu()
 {
     QMenuBar *menu = new QMenuBar();
@@ -145,7 +303,12 @@ void MainWindow::setupMenu()
     QAction *temperatureAction = new QAction(tr("Temperature"), this);
     menu->addAction(temperatureAction);
     connect(temperatureAction, SIGNAL(triggered()), this, SLOT(temperatureMenuClicked()));
+
+    QAction *aboutAction = new QAction(tr("About"), this);
+    menu->addAction(aboutAction);
+    connect(aboutAction, SIGNAL(triggered()), this, SLOT(aboutMenuClicked()));
 }
+#endif
 
 void MainWindow::loadSettings()
 {
@@ -155,7 +318,11 @@ void MainWindow::loadSettings()
     int forecastType = settings.value("ForecastType").toInt();
 
     m_downloader->setForecastDomain((MedardDownloader::ForecastDomain) forecastDomain);
+#ifdef Q_WS_MAEMO_6
+    m_forecastDomainComboBox->setCurrentIndex(forecastDomain);
+#else
     m_domainActionGroup->actions().at(forecastDomain)->setChecked(true);
+#endif
 
     switch ((MedardDownloader::ForecastType) forecastType) {
         case MedardDownloader::SeaLevelPressure:
@@ -205,52 +372,95 @@ void MainWindow::hideNavigationButtons(bool showRetryButton)
 
 void MainWindow::updateNavigationButtons()
 {
-    if ((m_downloader->getForecastDateOffset() - 24) < m_downloader->getMinForecastDateOffset()) {
-        m_minusDayButton->setDisabled(true);
-        m_plusDayButton->setDisabled(false);
-    } else if ((m_downloader->getForecastDateOffset() + 24) > m_downloader->getMaxForecastDateOffset()) {
-        m_minusDayButton->setDisabled(false);
-        m_plusDayButton->setDisabled(true);
+    if ((m_downloader->forecastDateOffset() - 24) < m_downloader->minForecastDateOffset()) {
+//        m_minusDayButton->setDisabled(true);
+//        m_plusDayButton->setDisabled(false);
+    } else if ((m_downloader->forecastDateOffset() + 24) > m_downloader->maxForecastDateOffset()) {
+//        m_minusDayButton->setDisabled(false);
+//        m_plusDayButton->setDisabled(true);
     } else {
-        m_minusDayButton->setDisabled(false);
-        m_plusDayButton->setDisabled(false);
+//        m_minusDayButton->setDisabled(false);
+//        m_plusDayButton->setDisabled(false);
     }
 
-    if ((m_downloader->getForecastDateOffset() - 1) < m_downloader->getMinForecastDateOffset()) {
-        m_minusHourButton->setDisabled(true);
-        m_plusHourButton->setDisabled(false);
-    } else if ((m_downloader->getForecastDateOffset() + 1) > m_downloader->getMaxForecastDateOffset()) {
-        m_minusHourButton->setDisabled(false);
-        m_plusHourButton->setDisabled(true);
+    if ((m_downloader->forecastDateOffset() - 1) < m_downloader->minForecastDateOffset()) {
+//        m_minusHourButton->setDisabled(true);
+//        m_plusHourButton->setDisabled(false);
+    } else if ((m_downloader->forecastDateOffset() + 1) > m_downloader->maxForecastDateOffset()) {
+//        m_minusHourButton->setDisabled(false);
+//        m_plusHourButton->setDisabled(true);
     } else {
-        m_minusHourButton->setDisabled(false);
-        m_plusHourButton->setDisabled(false);
+//        m_minusHourButton->setDisabled(false);
+//        m_plusHourButton->setDisabled(false);
     }
 }
 
+void MainWindow::setForecastType(const QString label, MedardDownloader::ForecastType type)
+{
+    m_forecastTypeLabel->setText(label);
+    m_forecast->clearImage(false);
+    m_downloader->setForecastType(type);
+    m_downloader->downloadImage();
+
+    QSettings settings;
+    settings.setValue("ForecastType", type);
+}
+
+void MainWindow::setForecastDateOffset(int offset)
+{
+    m_forecast->clearImage(false);
+    m_downloader->setForecastDateOffset(m_downloader->forecastDateOffset() + offset);
+    m_downloader->downloadImage();
+}
+
 void MainWindow::seaLevelPreasureMenuClicked()
 {
-    forecastTypeChanged(tr("Sea Level Pressure"), MedardDownloader::SeaLevelPressure);
+#ifdef Q_WS_MAEMO_6
+    m_forecastTypeComboBox->setCurrentIndex(MedardDownloader::SeaLevelPressure);
+#endif
+    setForecastType(tr("Sea Level Pressure"), MedardDownloader::SeaLevelPressure);
 }
 
 void MainWindow::precipitationMenuClicked()
 {
-    forecastTypeChanged(tr("Precipitation"), MedardDownloader::Precipitation);
+#ifdef Q_WS_MAEMO_6
+    m_forecastTypeComboBox->setCurrentIndex(MedardDownloader::Precipitation);
+#endif
+    setForecastType(tr("Precipitation"), MedardDownloader::Precipitation);
 }
 
 void MainWindow::windVelocityMenuClicked()
 {
-    forecastTypeChanged(tr("Wind Velocity"), MedardDownloader::WindVelocity);
+#ifdef Q_WS_MAEMO_6
+    m_forecastTypeComboBox->setCurrentIndex(MedardDownloader::WindVelocity);
+#endif
+    setForecastType(tr("Wind Velocity"), MedardDownloader::WindVelocity);
 }
 
 void MainWindow::cloudinessMenuClicked()
 {
-    forecastTypeChanged(tr("Cloudiness"), MedardDownloader::Cloudiness);
+#ifdef Q_WS_MAEMO_6
+    m_forecastTypeComboBox->setCurrentIndex(MedardDownloader::Cloudiness);
+#endif
+    setForecastType(tr("Cloudiness"), MedardDownloader::Cloudiness);
 }
 
 void MainWindow::temperatureMenuClicked()
 {
-    forecastTypeChanged(tr("Temperature"), MedardDownloader::Temperature);
+#ifdef Q_WS_MAEMO_6
+    m_forecastTypeComboBox->setCurrentIndex(MedardDownloader::Temperature);
+#endif
+    setForecastType(tr("Temperature"), MedardDownloader::Temperature);
+}
+
+void MainWindow::aboutMenuClicked()
+{
+    AboutDialog *dialog = new AboutDialog();
+#ifdef Q_WS_MAEMO_6
+    dialog->appear(MSceneWindow::DestroyWhenDismissed);
+#else
+    dialog->exec();
+#endif
 }
 
 void MainWindow::downloadAgainClicked()
@@ -263,42 +473,66 @@ void MainWindow::downloadAgainClicked()
 
 void MainWindow::plusDayClicked()
 {
-    forecastDateOffsetChanged(+24);
+    setForecastDateOffset(+24);
 }
 
 void MainWindow::minusDayClicked()
 {
-    forecastDateOffsetChanged(-24);
+    setForecastDateOffset(-24);
 }
 
 void MainWindow::plusHourClicked()
 {
-    forecastDateOffsetChanged(+1);
+    setForecastDateOffset(+1);
 }
 
 void MainWindow::minusHourClicked()
 {
-    forecastDateOffsetChanged(-1);
+    setForecastDateOffset(-1);
 }
 
-void MainWindow::forecastTypeChanged(const QString label, MedardDownloader::ForecastType type)
+#ifdef Q_WS_MAEMO_6
+void MainWindow::forecastDomainChanged(int index)
 {
-    m_forecastTypeLabel->setText(label);
     m_forecast->clearImage(false);
-    m_downloader->setForecastType(type);
+
+    if (index == 0)
+        m_downloader->setForecastDomain(MedardDownloader::Europe);
+    else
+        m_downloader->setForecastDomain(MedardDownloader::CzechRepublic);
+
     m_downloader->downloadImage();
 
     QSettings settings;
-    settings.setValue("ForecastType", type);
+    settings.setValue("ForecastDomain", index);
 }
 
-void MainWindow::forecastDateOffsetChanged(int offset)
+void MainWindow::forecastTypeChanged(int index)
 {
-    m_forecast->clearImage(false);
-    m_downloader->setForecastDateOffset(m_downloader->getForecastDateOffset() + offset);
-    m_downloader->downloadImage();
+    switch ((MedardDownloader::ForecastType) index) {
+        case MedardDownloader::SeaLevelPressure:
+            setForecastType(tr("Sea Level Pressure"), MedardDownloader::SeaLevelPressure);
+            break;
+
+        case MedardDownloader::Precipitation:
+            setForecastType(tr("Precipitation"), MedardDownloader::Precipitation);
+            break;
+
+        case MedardDownloader::WindVelocity:
+            setForecastType(tr("Wind Velocity"), MedardDownloader::WindVelocity);
+            break;
+
+        case MedardDownloader::Cloudiness:
+            setForecastType(tr("Cloudiness"), MedardDownloader::Cloudiness);
+            break;
+
+        case MedardDownloader::Temperature:
+            setForecastType(tr("Temperature"), MedardDownloader::Temperature);
+            break;
+    }
 }
 
+#else
 void MainWindow::forecastDomainChanged(QAction *action)
 {
     int forecastDomain = m_domainActionGroup->actions().indexOf(action);
@@ -315,12 +549,13 @@ void MainWindow::forecastDomainChanged(QAction *action)
     QSettings settings;
     settings.setValue("ForecastDomain", forecastDomain);
 }
+#endif
 
 void MainWindow::downloadedFinished(const QString &filename, const QDateTime &date)
 {
     m_forecast->setImage(filename);
     m_forecastInitialDateLabel->setText(tr("Forecast from:\n") +
-                                          m_downloader->getForecastInitialDate().toString("dd.MM.yyyy hh:mm"));
+                                          m_downloader->forecastInitialDate().toString("dd.MM.yyyy hh:mm"));
 
     /* upcase the first letter of name of day */
     QString temp = date.toString("dddd\ndd.MM.yyyy hh:mm");