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