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