Avoid complex types as statics.
[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::instance()->icon("style-default"));
92     box->addButton(SchemeNight, tr("Night"),
93                    Platform::instance()->icon("style-night"));
94     box->addButton(SchemeDay, tr("Day"), Platform::instance()->icon("style-day"));
95     box->addButton(SchemeSand, tr("Sand"),
96                    Platform::instance()->icon("style-sand"));
97     box->addStretch();
98     QString scheme = settings->value("scheme", "default").toString();
99     if (scheme == "night") {
100         box->toggle(SchemeNight);
101     } else if (scheme == "day") {
102         box->toggle(SchemeDay);
103     } else if (scheme == "sand") {
104         box->toggle(SchemeSand);
105     } else {
106         box->toggle(SchemeDefault);
107     }
108
109 #ifndef Q_OS_SYMBIAN
110     QLabel *orientationLabel = new QLabel(tr("Orientation:"), contents);
111     layout->addWidget(orientationLabel);
112     orientationBox = new ToolButtonBox(this);
113     layout->addWidget(orientationBox);
114     orientationBox->addButton(OrientationPortrait, tr("Portrait"),
115                               ":/icons/settings-portrait.png");
116     orientationBox->addButton(OrientationLandscape, tr("Landscape"),
117                               ":/icons/settings-landscape.png");
118     orientationBox->addStretch();
119     QString orientation =
120         settings->value("orientation", DEFAULT_ORIENTATION).toString();
121     if (orientation == "portrait") {
122         orientationBox->toggle(OrientationPortrait);
123     } else {
124         orientationBox->toggle(OrientationLandscape);
125     }
126 #endif // !Q_OS_SYMBIAN
127
128     layout->addStretch();
129     scroller->setWidget(contents);
130     contents->show();
131     scroller->setWidgetResizable(true);
132
133     setCentralWidget(scroller);
134
135 #ifndef Q_OS_SYMBIAN
136     connect(backlight, SIGNAL(toggled(bool)),
137             this, SLOT(onLightsToggled(bool)));
138 #endif
139     connect(grabVolume, SIGNAL(toggled(bool)),
140             this, SLOT(onGrabVolumeToggled(bool)));
141     connect(zoomSlider, SIGNAL(valueChanged(int)),
142             this, SLOT(onSliderValueChanged(int)));
143     connect(fontButton, SIGNAL(currentFontChanged(const QFont &)),
144             this, SLOT(onCurrentFontChanged(const QFont &)));
145     connect(box, SIGNAL(buttonClicked(int)),
146             this, SLOT(onSchemeButtonClicked(int)));
147 #ifndef Q_OS_SYMBIAN
148     connect(orientationBox, SIGNAL(buttonClicked(int)),
149             this, SLOT(onOrientationButtonClicked(int)));
150 #endif
151
152 #ifdef Q_OS_SYMBIAN
153     QAction *closeAction = new QAction(parent? tr("Back"): tr("Exit"), this);
154     closeAction->setSoftKeyRole(QAction::NegativeSoftKey);
155     connect(closeAction, SIGNAL(triggered()), this, SLOT(close()));
156     QMainWindow::addAction(closeAction);
157 #endif
158 }
159
160 void SettingsWindow::onSliderValueChanged(int value)
161 {
162     int step = zoomSlider->singleStep();
163     if (value % step) {
164         zoomSlider->setValue((value + step / 2) / step * step);
165         return;
166     }
167     zoomLabel->setText(tr("Zoom level: %1%").arg(value));
168 #if defined(Q_WS_MAEMO_5) || defined(Q_OS_SYMBIAN)
169     // Constant re-scaling of the book view is too much for mobiles
170 #else
171     Settings::instance()->setValue("zoom", value);
172 #endif // Q_WS_MAEMO_5
173 }
174
175 void SettingsWindow::onCurrentFontChanged(const QFont &font)
176 {
177 #ifdef Q_WS_MAEMO_5
178     Q_UNUSED(font);
179 #else
180     Settings::instance()->setValue("font", font.family());
181 #endif // Q_WS_MAEMO_5
182 }
183
184 void SettingsWindow::onSchemeButtonClicked(int id)
185 {
186     QString scheme;
187     switch (id) {
188     case SchemeDay: scheme = "day"; break;
189     case SchemeNight: scheme = "night"; break;
190     case SchemeSand: scheme = "sand"; break;
191     default: scheme = "default"; break;
192     }
193     Settings::instance()->setValue("scheme", scheme);
194 }
195
196 void SettingsWindow::onOrientationButtonClicked(int id)
197 {
198 #ifdef Q_WS_MAEMO_5
199     Q_UNUSED(id);
200 #else
201     QString orientation;
202     switch (id) {
203     case OrientationLandscape:
204         orientation = "landscape";
205         break;
206     default:
207         orientation = "portrait";
208         break;
209     }
210     Settings::instance()->setValue("orientation", orientation);
211 #endif // Q_WS_MAEMO_5
212 }
213
214 #if defined(Q_WS_MAEMO_5) || defined(Q_OS_SYMBIAN)
215
216 void SettingsWindow::closeEvent(QCloseEvent *e)
217 {
218     TRACE;
219     Settings *settings = Settings::instance();
220     settings->setValue("zoom", zoomSlider->value());
221     settings->setValue("font", fontButton->currentFont().family());
222 #ifndef Q_OS_SYMBIAN
223     settings->setValue("orientation",
224         (orientationBox->checkedId() == OrientationLandscape)?
225         "landscape": "portrait");
226 #endif // Q_OS_SYMBIAN
227     e->accept();
228 }
229
230 #endif // Q_WS_MAEMO_5 || Q_OS_SYMBIAN
231
232 void SettingsWindow::onLightsToggled(bool value)
233 {
234     Settings::instance()->setValue("lightson", value);
235 }
236
237 void SettingsWindow::onGrabVolumeToggled(bool enable)
238 {
239     Settings::instance()->setValue("usevolumekeys", enable);
240 }