Observe volume keys on Symbian. Add minimal documentation to headers.
[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 #ifdef Q_OS_SYMBIAN
14 const char *DEFAULT_ORIENTATION = "portrait";
15 #else
16 const char *DEFAULT_ORIENTATION = "landscape";
17 #endif
18
19 const int ZOOM_MIN = 75;
20 const int ZOOM_MAX = 250;
21 const int ZOOM_STEP = 25;
22
23 SettingsWindow::SettingsWindow(QWidget *parent):  AdopterWindow(parent)
24 {
25 #ifdef Q_WS_MAEMO_5
26     setAttribute(Qt::WA_Maemo5StackedWindow, true);
27     setAttribute(Qt::WA_Maemo5AutoOrientation, true);
28 #endif
29     setWindowTitle("Settings");
30
31     Settings *settings = Settings::instance();
32     QScrollArea *scroller = new QScrollArea(this);
33 #if defined(Q_WS_MAEMO_5)
34     scroller->setProperty("FingerScrollable", true);
35     scroller->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
36 #elif defined(Q_OS_SYMBIAN)
37     FlickCharm *charm = new FlickCharm(this);
38     charm->activateOn(scroller);
39 #else
40     scroller->setVerticalScrollBarPolicy(Qt::ScrollBarAsNeeded);
41 #endif
42     scroller->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
43     scroller->setFrameStyle(QFrame::NoFrame);
44
45     QWidget *contents = new QWidget(scroller);
46     QVBoxLayout *layout = new QVBoxLayout(contents);
47     contents->setLayout(layout);
48
49 #ifndef Q_OS_SYMBIAN
50     QCheckBox *backlight = new QCheckBox(tr("Keep backlight on"), contents);
51     layout->addWidget(backlight);
52     backlight->setChecked(settings->value("lightson", false).toBool());
53 #endif
54
55     QCheckBox *grabVolume =
56             new QCheckBox(tr("Navigate with volume keys"), contents);
57     layout->addWidget(grabVolume);
58     grabVolume->setChecked(settings->value("usevolumekeys", false).toBool());
59
60     int zoom = Settings::instance()->value("zoom").toInt();
61     if (zoom < ZOOM_MIN) {
62         zoom = ZOOM_MIN;
63     } else if (zoom > ZOOM_MAX) {
64         zoom = ZOOM_MAX;
65     }
66     zoomLabel = new QLabel(tr("Zoom level: %1%").arg(zoom), contents);
67     layout->addWidget(zoomLabel);
68     zoomSlider = new QSlider(Qt::Horizontal, contents);
69     zoomSlider->setMinimum(ZOOM_MIN);
70     zoomSlider->setMaximum(ZOOM_MAX);
71     zoomSlider->setSingleStep(ZOOM_STEP);
72     zoomSlider->setPageStep(ZOOM_STEP);
73     zoomSlider->setValue(zoom);
74     layout->addWidget(zoomSlider);
75
76     QLabel *fontLabel = new QLabel(tr("Font:"), contents);
77     layout->addWidget(fontLabel);
78     QString defaultFamily = fontLabel->fontInfo().family();
79     QString family =
80             Settings::instance()->value("font", defaultFamily).toString();
81     fontButton = new QFontComboBox(contents);
82     fontButton->setCurrentFont(QFont(family));
83     fontButton->setEditable(false);
84     layout->addWidget(fontButton);
85
86     QLabel *colorLabel = new QLabel(tr("Color scheme:"), contents);
87     layout->addWidget(colorLabel);
88     ToolButtonBox *box = new ToolButtonBox(this);
89     layout->addWidget(box);
90     box->addButton(SchemeDefault, tr("Default"),
91                    Platform::icon("style-default"));
92     box->addButton(SchemeNight, tr("Night"), Platform::icon("style-night"));
93     box->addButton(SchemeDay, tr("Day"), Platform::icon("style-day"));
94     box->addButton(SchemeSand, tr("Sand"), Platform::icon("style-sand"));
95     box->addStretch();
96     QString scheme = settings->value("scheme", "default").toString();
97     if (scheme == "night") {
98         box->toggle(SchemeNight);
99     } else if (scheme == "day") {
100         box->toggle(SchemeDay);
101     } else if (scheme == "sand") {
102         box->toggle(SchemeSand);
103     } else {
104         box->toggle(SchemeDefault);
105     }
106
107 #ifndef Q_OS_SYMBIAN
108     QLabel *orientationLabel = new QLabel(tr("Orientation:"), contents);
109     layout->addWidget(orientationLabel);
110     orientationBox = new ToolButtonBox(this);
111     layout->addWidget(orientationBox);
112     orientationBox->addButton(OrientationPortrait, tr("Portrait"),
113                               ":/icons/settings-portrait.png");
114     orientationBox->addButton(OrientationLandscape, tr("Landscape"),
115                               ":/icons/settings-landscape.png");
116     orientationBox->addStretch();
117     QString orientation =
118         settings->value("orientation", DEFAULT_ORIENTATION).toString();
119     if (orientation == "portrait") {
120         orientationBox->toggle(OrientationPortrait);
121     } else {
122         orientationBox->toggle(OrientationLandscape);
123     }
124 #endif // !Q_OS_SYMBIAN
125
126     layout->addStretch();
127     scroller->setWidget(contents);
128     contents->show();
129     scroller->setWidgetResizable(true);
130
131     setCentralWidget(scroller);
132
133 #ifndef Q_OS_SYMBIAN
134     connect(backlight, SIGNAL(toggled(bool)),
135             this, SLOT(onLightsToggled(bool)));
136 #endif
137     connect(grabVolume, SIGNAL(toggled(bool)),
138             this, SLOT(onGrabVolumeToggled(bool)));
139     connect(zoomSlider, SIGNAL(valueChanged(int)),
140             this, SLOT(onSliderValueChanged(int)));
141     connect(fontButton, SIGNAL(currentFontChanged(const QFont &)),
142             this, SLOT(onCurrentFontChanged(const QFont &)));
143     connect(box, SIGNAL(buttonClicked(int)),
144             this, SLOT(onSchemeButtonClicked(int)));
145 #ifndef Q_OS_SYMBIAN
146     connect(orientationBox, SIGNAL(buttonClicked(int)),
147             this, SLOT(onOrientationButtonClicked(int)));
148 #endif
149
150 #ifdef Q_OS_SYMBIAN
151     QAction *closeAction = new QAction(parent? tr("Back"): tr("Exit"), this);
152     closeAction->setSoftKeyRole(QAction::NegativeSoftKey);
153     connect(closeAction, SIGNAL(triggered()), this, SLOT(close()));
154     QMainWindow::addAction(closeAction);
155 #endif
156 }
157
158 void SettingsWindow::onSliderValueChanged(int value)
159 {
160     int step = zoomSlider->singleStep();
161     if (value % step) {
162         zoomSlider->setValue((value + step / 2) / step * step);
163         return;
164     }
165     zoomLabel->setText(tr("Zoom level: %1%").arg(value));
166 #if defined(Q_WS_MAEMO_5) || defined(Q_OS_SYMBIAN)
167     // Constant re-scaling of the book view is too much for mobiles
168 #else
169     Settings::instance()->setValue("zoom", value);
170 #endif // Q_WS_MAEMO_5
171 }
172
173 void SettingsWindow::onCurrentFontChanged(const QFont &font)
174 {
175 #ifdef Q_WS_MAEMO_5
176     Q_UNUSED(font);
177 #else
178     Settings::instance()->setValue("font", font.family());
179 #endif // Q_WS_MAEMO_5
180 }
181
182 void SettingsWindow::onSchemeButtonClicked(int id)
183 {
184     QString scheme;
185     switch (id) {
186     case SchemeDay: scheme = "day"; break;
187     case SchemeNight: scheme = "night"; break;
188     case SchemeSand: scheme = "sand"; break;
189     default: scheme = "default"; break;
190     }
191     Settings::instance()->setValue("scheme", scheme);
192 }
193
194 void SettingsWindow::onOrientationButtonClicked(int id)
195 {
196 #ifdef Q_WS_MAEMO_5
197     Q_UNUSED(id);
198 #else
199     QString orientation;
200     switch (id) {
201     case OrientationLandscape:
202         orientation = "landscape";
203         break;
204     default:
205         orientation = "portrait";
206         break;
207     }
208     Settings::instance()->setValue("orientation", orientation);
209 #endif // Q_WS_MAEMO_5
210 }
211
212 #if defined(Q_WS_MAEMO_5) || defined(Q_OS_SYMBIAN)
213
214 void SettingsWindow::closeEvent(QCloseEvent *e)
215 {
216     TRACE;
217     Settings *settings = Settings::instance();
218     settings->setValue("zoom", zoomSlider->value());
219     settings->setValue("font", fontButton->currentFont().family());
220 #ifndef Q_OS_SYMBIAN
221     settings->setValue("orientation",
222         (orientationBox->checkedId() == OrientationLandscape)?
223         "landscape": "portrait");
224 #endif // Q_OS_SYMBIAN
225     e->accept();
226 }
227
228 #endif // Q_WS_MAEMO_5 || Q_OS_SYMBIAN
229
230 void SettingsWindow::onLightsToggled(bool value)
231 {
232     Settings::instance()->setValue("lightson", value);
233 }
234
235 void SettingsWindow::onGrabVolumeToggled(bool enable)
236 {
237     Settings::instance()->setValue("usevolumekeys", enable);
238 }