Fix forward navigation control on Linux.
[dorian] / settingswindow.cpp
1 #include <QtGui>
2
3 #include "settingswindow.h"
4 #include "settings.h"
5 #include "toolbuttonbox.h"
6 #include "platform.h"
7 #include "trace.h"
8
9 #ifdef Q_OS_SYMBIAN
10 #   include "flickcharm.h"
11 #endif
12
13 const int ZOOM_MIN = 75;
14 const int ZOOM_MAX = 250;
15 const int ZOOM_STEP = 25;
16
17 SettingsWindow::SettingsWindow(QWidget *parent):  AdopterWindow(parent)
18 {
19 #ifdef Q_WS_MAEMO_5
20     setAttribute(Qt::WA_Maemo5StackedWindow, true);
21     setAttribute(Qt::WA_Maemo5AutoOrientation, true);
22 #endif
23     setWindowTitle("Settings");
24
25     Settings *settings = Settings::instance();
26     Platform *platform = Platform::instance();
27
28     QScrollArea *scroller = new QScrollArea(this);
29 #if defined(Q_WS_MAEMO_5)
30     scroller->setProperty("FingerScrollable", true);
31     scroller->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
32 #elif defined(Q_OS_SYMBIAN)
33     FlickCharm *charm = new FlickCharm(this);
34     charm->activateOn(scroller);
35 #else
36     scroller->setVerticalScrollBarPolicy(Qt::ScrollBarAsNeeded);
37 #endif
38     scroller->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
39     scroller->setFrameStyle(QFrame::NoFrame);
40
41     QWidget *contents = new QWidget(scroller);
42     QVBoxLayout *layout = new QVBoxLayout(contents);
43     contents->setLayout(layout);
44
45 #if defined(Q_WS_MAEMO_5)
46     QCheckBox *backlight = new QCheckBox(tr("Keep backlight on"), contents);
47     layout->addWidget(backlight);
48     backlight->setChecked(settings->value("lightson", false).toBool());
49 #endif
50
51 #if defined(Q_WS_MAEMO_5) || defined(Q_OS_SYMBIAN)
52     QCheckBox *grabVolume =
53             new QCheckBox(tr("Navigate with volume keys"), contents);
54     layout->addWidget(grabVolume);
55     grabVolume->setChecked(settings->value("usevolumekeys", false).toBool());
56 #endif
57
58     int zoom = settings->value("zoom", platform->defaultZoom()).toInt();
59     if (zoom < ZOOM_MIN) {
60         zoom = ZOOM_MIN;
61     } else if (zoom > ZOOM_MAX) {
62         zoom = ZOOM_MAX;
63     }
64     zoomLabel = new QLabel(tr("Zoom level: %1%").arg(zoom), contents);
65     layout->addWidget(zoomLabel);
66     zoomSlider = new QSlider(Qt::Horizontal, contents);
67     zoomSlider->setMinimum(ZOOM_MIN);
68     zoomSlider->setMaximum(ZOOM_MAX);
69     zoomSlider->setSingleStep(ZOOM_STEP);
70     zoomSlider->setPageStep(ZOOM_STEP);
71     zoomSlider->setValue(zoom);
72     layout->addWidget(zoomSlider);
73
74     QLabel *fontLabel = new QLabel(tr("Font:"), contents);
75     layout->addWidget(fontLabel);
76     QString family =
77             settings->value("font", platform->defaultFont()).toString();
78     fontButton = new QFontComboBox(contents);
79     fontButton->setCurrentFont(QFont(family));
80     fontButton->setEditable(false);
81     layout->addWidget(fontButton);
82
83     QLabel *colorLabel = new QLabel(tr("Color scheme:"), contents);
84     layout->addWidget(colorLabel);
85     ToolButtonBox *box = new ToolButtonBox(this);
86     layout->addWidget(box);
87     box->addButton(SchemeDay, tr("Day"),
88                    Platform::instance()->icon("style-day"));
89     box->addButton(SchemeNight, tr("Night"),
90                    Platform::instance()->icon("style-night"));
91     box->addButton(SchemeSand, tr("Sand"),
92                    Platform::instance()->icon("style-sand"));
93     box->addStretch();
94     QString scheme = settings->value("scheme", "day").toString();
95     if (scheme == "night") {
96         box->toggle(SchemeNight);
97     } else if (scheme == "sand") {
98         box->toggle(SchemeSand);
99     } else {
100         box->toggle(SchemeDay);
101     }
102
103     layout->addStretch();
104     scroller->setWidget(contents);
105     contents->show();
106     scroller->setWidgetResizable(true);
107
108     setCentralWidget(scroller);
109
110 #if defined(Q_WS_MAEMO_5)
111     connect(backlight, SIGNAL(toggled(bool)),
112             this, SLOT(onLightsToggled(bool)));
113 #endif
114 #if defined(Q_WS_MAEMO_5) || defined(Q_OS_SYMBIAN)
115     connect(grabVolume, SIGNAL(toggled(bool)),
116             this, SLOT(onGrabVolumeToggled(bool)));
117 #endif
118     connect(zoomSlider, SIGNAL(valueChanged(int)),
119             this, SLOT(onSliderValueChanged(int)));
120     connect(fontButton, SIGNAL(currentFontChanged(const QFont &)),
121             this, SLOT(onCurrentFontChanged(const QFont &)));
122     connect(box, SIGNAL(buttonClicked(int)),
123             this, SLOT(onSchemeButtonClicked(int)));
124
125 #ifdef Q_OS_SYMBIAN
126     QAction *closeAction = new QAction(parent? tr("Back"): tr("Exit"), this);
127     closeAction->setSoftKeyRole(QAction::NegativeSoftKey);
128     connect(closeAction, SIGNAL(triggered()), this, SLOT(close()));
129     QMainWindow::addAction(closeAction);
130 #endif
131 }
132
133 void SettingsWindow::onSliderValueChanged(int value)
134 {
135     int step = zoomSlider->singleStep();
136     if (value % step) {
137         zoomSlider->setValue((value + step / 2) / step * step);
138         return;
139     }
140     zoomLabel->setText(tr("Zoom level: %1%").arg(value));
141 #if !defined(Q_WS_MAEMO_5) && !defined(Q_OS_SYMBIAN)
142     Settings::instance()->setValue("zoom", zoomSlider->value());
143 #endif
144 }
145
146 void SettingsWindow::onCurrentFontChanged(const QFont &font)
147 {
148 #if !defined(Q_WS_MAEMO_5) && !defined(Q_OS_SYMBIAN)
149     Settings::instance()->setValue("font", font.family());
150 #else
151     Q_UNUSED(font);
152 #endif
153 }
154
155 void SettingsWindow::onSchemeButtonClicked(int id)
156 {
157     QString scheme;
158     switch (id) {
159     case SchemeNight: scheme = "night"; break;
160     case SchemeSand: scheme = "sand"; break;
161     default: scheme = "day"; break;
162     }
163     Settings::instance()->setValue("scheme", scheme);
164 }
165
166 void SettingsWindow::onOrientationButtonClicked(int id)
167 {
168     QString orientation;
169     switch (id) {
170     case OrientationLandscape:
171         orientation = "landscape";
172         break;
173     default:
174         orientation = "portrait";
175         break;
176     }
177     Settings::instance()->setValue("orientation", orientation);
178 }
179
180 void SettingsWindow::closeEvent(QCloseEvent *e)
181 {
182     TRACE;
183     Settings *settings = Settings::instance();
184     settings->setValue("zoom", zoomSlider->value());
185     settings->setValue("font", fontButton->currentFont().family());
186     e->accept();
187 }
188
189 void SettingsWindow::onLightsToggled(bool value)
190 {
191     Settings::instance()->setValue("lightson", value);
192 }
193
194 void SettingsWindow::onGrabVolumeToggled(bool enable)
195 {
196     Settings::instance()->setValue("usevolumekeys", enable);
197 }