Make orientation switch explicit on Symbian, too.
[dorian] / settingswindow.cpp
index 033ed67..f09f9b2 100644 (file)
@@ -2,14 +2,19 @@
 
 #include "settingswindow.h"
 #include "settings.h"
+#include "toolbuttonbox.h"
+#include "platform.h"
+#include "trace.h"
 
 #ifdef Q_OS_SYMBIAN
-#define DEFAULT_ORIENTATION "portrait"
-#else
-#define DEFAULT_ORIENTATION "landscape"
+#   include "flickcharm.h"
 #endif
 
-SettingsWindow::SettingsWindow(QWidget *parent):  QMainWindow(parent)
+const int ZOOM_MIN = 75;
+const int ZOOM_MAX = 250;
+const int ZOOM_STEP = 25;
+
+SettingsWindow::SettingsWindow(QWidget *parent):  AdopterWindow(parent)
 {
 #ifdef Q_WS_MAEMO_5
     setAttribute(Qt::WA_Maemo5StackedWindow, true);
@@ -17,10 +22,16 @@ SettingsWindow::SettingsWindow(QWidget *parent):  QMainWindow(parent)
 #endif
     setWindowTitle("Settings");
 
+    Settings *settings = Settings::instance();
+    Platform *platform = Platform::instance();
+
     QScrollArea *scroller = new QScrollArea(this);
-#ifdef Q_WS_MAEMO_5
+#if defined(Q_WS_MAEMO_5)
     scroller->setProperty("FingerScrollable", true);
     scroller->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
+#elif defined(Q_OS_SYMBIAN)
+    FlickCharm *charm = new FlickCharm(this);
+    charm->activateOn(scroller);
 #else
     scroller->setVerticalScrollBarPolicy(Qt::ScrollBarAsNeeded);
 #endif
@@ -31,128 +42,83 @@ SettingsWindow::SettingsWindow(QWidget *parent):  QMainWindow(parent)
     QVBoxLayout *layout = new QVBoxLayout(contents);
     contents->setLayout(layout);
 
-    QLabel *zoomLabel = new QLabel(tr("Zoom level:"), contents);
+#ifndef Q_OS_SYMBIAN
+    QCheckBox *backlight = new QCheckBox(tr("Keep backlight on"), contents);
+    layout->addWidget(backlight);
+    backlight->setChecked(settings->value("lightson", false).toBool());
+#endif
+
+    QCheckBox *grabVolume =
+            new QCheckBox(tr("Navigate with volume keys"), contents);
+    layout->addWidget(grabVolume);
+    grabVolume->setChecked(settings->value("usevolumekeys", false).toBool());
+
+    int zoom = settings->value("zoom", platform->defaultZoom()).toInt();
+    if (zoom < ZOOM_MIN) {
+        zoom = ZOOM_MIN;
+    } else if (zoom > ZOOM_MAX) {
+        zoom = ZOOM_MAX;
+    }
+    zoomLabel = new QLabel(tr("Zoom level: %1%").arg(zoom), contents);
     layout->addWidget(zoomLabel);
-    QSlider *zoomSlider = new QSlider(Qt::Horizontal, contents);
-    zoomSlider->setMinimum(50);
-    zoomSlider->setMaximum(300);
-    zoomSlider->setValue(Settings::instance()->value("zoom").toInt());
+    zoomSlider = new QSlider(Qt::Horizontal, contents);
+    zoomSlider->setMinimum(ZOOM_MIN);
+    zoomSlider->setMaximum(ZOOM_MAX);
+    zoomSlider->setSingleStep(ZOOM_STEP);
+    zoomSlider->setPageStep(ZOOM_STEP);
+    zoomSlider->setValue(zoom);
     layout->addWidget(zoomSlider);
 
     QLabel *fontLabel = new QLabel(tr("Font:"), contents);
     layout->addWidget(fontLabel);
-    QString defaultFamily = fontLabel->fontInfo().family();
-    QString family = Settings::instance()->value("font", defaultFamily).toString();
-    QFontComboBox *fontButton = new QFontComboBox(contents);
+    QString family = settings->value("font", platform->defaultFont()).toString();
+    fontButton = new QFontComboBox(contents);
     fontButton->setCurrentFont(QFont(family));
     fontButton->setEditable(false);
     layout->addWidget(fontButton);
 
     QLabel *colorLabel = new QLabel(tr("Color scheme:"), contents);
     layout->addWidget(colorLabel);
-    QFrame *box = new QFrame(this);
+    ToolButtonBox *box = new ToolButtonBox(this);
     layout->addWidget(box);
-    QHBoxLayout *boxLayout = new QHBoxLayout(box);
-    boxLayout->setMargin(0);
-    box->setLayout(boxLayout);
-    QButtonGroup *group = new QButtonGroup(this);
-
-    QToolButton *defaultSchemeButton = new QToolButton(box);
-    defaultSchemeButton->setToolButtonStyle(Qt::ToolButtonTextUnderIcon);
-    defaultSchemeButton->setText(tr("Default"));
-    defaultSchemeButton->setIconSize(QSize(81, 81));
-    defaultSchemeButton->setIcon(QIcon(":/icons/style-default.png"));
-    defaultSchemeButton->setCheckable(true);
-    boxLayout->addWidget(defaultSchemeButton);
-    group->addButton(defaultSchemeButton);
-    group->setId(defaultSchemeButton, SchemeDefault);
-
-    QToolButton *nightSchemeButton = new QToolButton(box);
-    nightSchemeButton->setToolButtonStyle(Qt::ToolButtonTextUnderIcon);
-    nightSchemeButton->setText(tr("Night"));
-    nightSchemeButton->setCheckable(true);
-    nightSchemeButton->setIconSize(QSize(81, 81));
-    nightSchemeButton->setIcon(QIcon(":/icons/style-night.png"));
-    boxLayout->addWidget(nightSchemeButton);
-    group->addButton(nightSchemeButton);
-    group->setId(nightSchemeButton, SchemeNight);
-
-    QToolButton *daySchemeButton = new QToolButton(box);
-    daySchemeButton->setToolButtonStyle(Qt::ToolButtonTextUnderIcon);
-    daySchemeButton->setText(tr("Day"));
-    daySchemeButton->setCheckable(true);
-    daySchemeButton->setIconSize(QSize(81, 81));
-    daySchemeButton->setIcon(QIcon(":/icons/style-day.png"));
-    boxLayout->addWidget(daySchemeButton);
-    group->addButton(daySchemeButton);
-    group->setId(daySchemeButton, SchemeDay);
-
-    QToolButton *sandSchemeButton = new QToolButton(box);
-    sandSchemeButton->setToolButtonStyle(Qt::ToolButtonTextUnderIcon);
-    sandSchemeButton->setText(tr("Sand"));
-    sandSchemeButton->setCheckable(true);
-    sandSchemeButton->setIconSize(QSize(81, 81));
-    sandSchemeButton->setIcon(QIcon(":/icons/style-sand.png"));
-    boxLayout->addWidget(sandSchemeButton);
-    group->addButton(sandSchemeButton);
-    group->setId(sandSchemeButton, SchemeSand);
-
-    QString scheme = Settings::instance()->value("scheme", "default").toString();
+    box->addButton(SchemeDefault, tr("Default"),
+                   Platform::instance()->icon("style-default"));
+    box->addButton(SchemeNight, tr("Night"),
+                   Platform::instance()->icon("style-night"));
+    box->addButton(SchemeDay, tr("Day"), Platform::instance()->icon("style-day"));
+    box->addButton(SchemeSand, tr("Sand"),
+                   Platform::instance()->icon("style-sand"));
+    box->addStretch();
+    QString scheme = settings->value("scheme", "default").toString();
     if (scheme == "night") {
-        nightSchemeButton->toggle();
-    }
-    else if (scheme == "day") {
-        daySchemeButton->toggle();
-    }
-    else if (scheme == "sand") {
-        sandSchemeButton->toggle();
-    }
-    else {
-        defaultSchemeButton->toggle();
+        box->toggle(SchemeNight);
+    } else if (scheme == "day") {
+        box->toggle(SchemeDay);
+    } else if (scheme == "sand") {
+        box->toggle(SchemeSand);
+    } else {
+        box->toggle(SchemeDefault);
     }
 
+#if defined(Q_WS_MAEMO_5) || defined(Q_OS_SYMBIAN)
     QLabel *orientationLabel = new QLabel(tr("Orientation:"), contents);
     layout->addWidget(orientationLabel);
-
-    QFrame *orientationBox = new QFrame(this);
+    orientationBox = new ToolButtonBox(this);
     layout->addWidget(orientationBox);
-    QHBoxLayout *orientationLayout = new QHBoxLayout(orientationBox);
-    orientationLayout->setMargin(0);
-    orientationBox->setLayout(orientationLayout);
-    QButtonGroup *orientationGroup = new QButtonGroup(this);
-
-    QToolButton *portraitButton = new QToolButton(box);
-    portraitButton->setToolButtonStyle(Qt::ToolButtonTextUnderIcon);
-    portraitButton->setText(tr("Portrait"));
-    portraitButton->setIconSize(QSize(81, 81));
-    portraitButton->setIcon(QIcon(":/icons/settings-portrait.png"));
-    portraitButton->setCheckable(true);
-    orientationLayout->addWidget(portraitButton);
-    orientationGroup->addButton(portraitButton);
-    orientationGroup->setId(portraitButton, OrientationPortrait);
-
-    QToolButton *landscapeButton = new QToolButton(box);
-    landscapeButton->setToolButtonStyle(Qt::ToolButtonTextUnderIcon);
-    landscapeButton->setText(tr("Landscape"));
-    landscapeButton->setIconSize(QSize(81, 81));
-    landscapeButton->setIcon(QIcon(":/icons/settings-landscape.png"));
-    landscapeButton->setCheckable(true);
-    orientationLayout->addWidget(landscapeButton);
-    orientationGroup->addButton(landscapeButton);
-    orientationGroup->setId(landscapeButton, OrientationLandscape);
-
-    orientationLayout->addStretch();
-
+    orientationBox->addButton(OrientationPortrait, tr("Portrait"),
+                              ":/icons/settings-portrait.png");
+    orientationBox->addButton(OrientationLandscape, tr("Landscape"),
+                              ":/icons/settings-landscape.png");
+    orientationBox->addStretch();
     QString orientation =
-        Settings::instance()->value("orientation", DEFAULT_ORIENTATION).toString();
+        settings->value("orientation", platform->defaultOrientation()).toString();
     if (orientation == "portrait") {
-        portraitButton->toggle();
-    }
-    else {
-        landscapeButton->toggle();
+        orientationBox->toggle(OrientationPortrait);
+    } else {
+        orientationBox->toggle(OrientationLandscape);
     }
+#endif // defined(Q_WS_MAEMO_5) || defined(Q_OS_SYMBIAN)
 
-    boxLayout->addStretch();
     layout->addStretch();
     scroller->setWidget(contents);
     contents->show();
@@ -160,24 +126,44 @@ SettingsWindow::SettingsWindow(QWidget *parent):  QMainWindow(parent)
 
     setCentralWidget(scroller);
 
+#ifndef Q_OS_SYMBIAN
+    connect(backlight, SIGNAL(toggled(bool)),
+            this, SLOT(onLightsToggled(bool)));
+#endif
+    connect(grabVolume, SIGNAL(toggled(bool)),
+            this, SLOT(onGrabVolumeToggled(bool)));
     connect(zoomSlider, SIGNAL(valueChanged(int)),
             this, SLOT(onSliderValueChanged(int)));
     connect(fontButton, SIGNAL(currentFontChanged(const QFont &)),
             this, SLOT(onCurrentFontChanged(const QFont &)));
-    connect(group, SIGNAL(buttonClicked(int)),
+    connect(box, SIGNAL(buttonClicked(int)),
             this, SLOT(onSchemeButtonClicked(int)));
-    connect(orientationGroup, SIGNAL(buttonClicked(int)),
+#ifndef Q_OS_SYMBIAN
+    connect(orientationBox, SIGNAL(buttonClicked(int)),
             this, SLOT(onOrientationButtonClicked(int)));
+#endif
+
+#ifdef Q_OS_SYMBIAN
+    QAction *closeAction = new QAction(parent? tr("Back"): tr("Exit"), this);
+    closeAction->setSoftKeyRole(QAction::NegativeSoftKey);
+    connect(closeAction, SIGNAL(triggered()), this, SLOT(close()));
+    QMainWindow::addAction(closeAction);
+#endif
 }
 
 void SettingsWindow::onSliderValueChanged(int value)
 {
-    Settings::instance()->setValue("zoom", value);
+    int step = zoomSlider->singleStep();
+    if (value % step) {
+        zoomSlider->setValue((value + step / 2) / step * step);
+        return;
+    }
+    zoomLabel->setText(tr("Zoom level: %1%").arg(value));
 }
 
 void SettingsWindow::onCurrentFontChanged(const QFont &font)
 {
-    Settings::instance()->setValue("font", font.family());
+    Q_UNUSED(font);
 }
 
 void SettingsWindow::onSchemeButtonClicked(int id)
@@ -196,8 +182,34 @@ void SettingsWindow::onOrientationButtonClicked(int id)
 {
     QString orientation;
     switch (id) {
-    case OrientationLandscape: orientation = "landscape"; break;
-    default: orientation = "portrait"; break;
+    case OrientationLandscape:
+        orientation = "landscape";
+        break;
+    default:
+        orientation = "portrait";
+        break;
     }
     Settings::instance()->setValue("orientation", orientation);
 }
+
+void SettingsWindow::closeEvent(QCloseEvent *e)
+{
+    TRACE;
+    Settings *settings = Settings::instance();
+    settings->setValue("zoom", zoomSlider->value());
+    settings->setValue("font", fontButton->currentFont().family());
+    settings->setValue("orientation",
+        (orientationBox->checkedId() == OrientationLandscape)?
+        "landscape": "portrait");
+    e->accept();
+}
+
+void SettingsWindow::onLightsToggled(bool value)
+{
+    Settings::instance()->setValue("lightson", value);
+}
+
+void SettingsWindow::onGrabVolumeToggled(bool enable)
+{
+    Settings::instance()->setValue("usevolumekeys", enable);
+}