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