Add settings for navigating with volume keys.
[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 #define DEFAULT_ORIENTATION "portrait"
9 #else
10 #define DEFAULT_ORIENTATION "landscape"
11 #endif
12
13 SettingsWindow::SettingsWindow(QWidget *parent):  QMainWindow(parent)
14 {
15 #ifdef Q_WS_MAEMO_5
16     setAttribute(Qt::WA_Maemo5StackedWindow, true);
17     setAttribute(Qt::WA_Maemo5AutoOrientation, true);
18 #endif
19     setWindowTitle("Settings");
20
21     Settings *settings = Settings::instance();
22     QScrollArea *scroller = new QScrollArea(this);
23 #ifdef Q_WS_MAEMO_5
24     scroller->setProperty("FingerScrollable", true);
25     scroller->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
26 #else
27     scroller->setVerticalScrollBarPolicy(Qt::ScrollBarAsNeeded);
28 #endif
29     scroller->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
30     scroller->setFrameStyle(QFrame::NoFrame);
31
32     QWidget *contents = new QWidget(scroller);
33     QVBoxLayout *layout = new QVBoxLayout(contents);
34     contents->setLayout(layout);
35
36     QCheckBox *backlight = new QCheckBox(tr("Keep backlight on"), contents);
37     layout->addWidget(backlight);
38     backlight->setChecked(settings->value("lightson", false).toBool());
39
40     QCheckBox *grabVolume =
41             new QCheckBox(tr("Navigate with volume keys"), contents);
42     layout->addWidget(grabVolume);
43     grabVolume->setChecked(settings->value("usevolumekeys", false).toBool());
44
45     QLabel *zoomLabel = new QLabel(tr("Zoom level:"), contents);
46     layout->addWidget(zoomLabel);
47     zoomSlider = new QSlider(Qt::Horizontal, contents);
48     zoomSlider->setMinimum(50);
49     zoomSlider->setMaximum(300);
50     zoomSlider->setValue(Settings::instance()->value("zoom").toInt());
51     layout->addWidget(zoomSlider);
52
53     QLabel *fontLabel = new QLabel(tr("Font:"), contents);
54     layout->addWidget(fontLabel);
55     QString defaultFamily = fontLabel->fontInfo().family();
56     QString family = Settings::instance()->value("font", defaultFamily).toString();
57     fontButton = new QFontComboBox(contents);
58     fontButton->setCurrentFont(QFont(family));
59     fontButton->setEditable(false);
60     layout->addWidget(fontButton);
61
62     QLabel *colorLabel = new QLabel(tr("Color scheme:"), contents);
63     layout->addWidget(colorLabel);
64     ToolButtonBox *box = new ToolButtonBox(this);
65     layout->addWidget(box);
66     box->addButton(SchemeDefault, tr("Default"), ":/icons/style-default.png");
67     box->addButton(SchemeNight, tr("Night"), ":/icons/style-night.png");
68     box->addButton(SchemeDay, tr("Day"), ":/icons/style-day.png");
69     box->addButton(SchemeSand, tr("Sand"), ":/icons/style-sand.png");
70     box->addStretch();
71     QString scheme = settings->value("scheme", "default").toString();
72     if (scheme == "night") {
73         box->toggle(SchemeNight);
74     } else if (scheme == "day") {
75         box->toggle(SchemeDay);
76     } else if (scheme == "sand") {
77         box->toggle(SchemeSand);
78     } else {
79         box->toggle(SchemeDefault);
80     }
81
82     QLabel *orientationLabel = new QLabel(tr("Orientation:"), contents);
83     layout->addWidget(orientationLabel);
84     orientationBox = new ToolButtonBox(this);
85     layout->addWidget(orientationBox);
86     orientationBox->addButton(OrientationPortrait, tr("Portrait"),
87                               ":/icons/settings-portrait.png");
88     orientationBox->addButton(OrientationLandscape, tr("Landscape"),
89                               ":/icons/settings-landscape.png");
90     orientationBox->addStretch();
91     QString orientation =
92         settings->value("orientation", DEFAULT_ORIENTATION).toString();
93     if (orientation == "portrait") {
94         orientationBox->toggle(OrientationPortrait);
95     } else {
96         orientationBox->toggle(OrientationLandscape);
97     }
98
99     layout->addStretch();
100     scroller->setWidget(contents);
101     contents->show();
102     scroller->setWidgetResizable(true);
103
104     setCentralWidget(scroller);
105
106     connect(backlight, SIGNAL(toggled(bool)), this, SLOT(onLightsToggled(bool)));
107     connect(grabVolume, SIGNAL(toggled(bool)),
108             this, SLOT(onGrabVolumeToggled(bool)));
109     connect(zoomSlider, SIGNAL(valueChanged(int)),
110             this, SLOT(onSliderValueChanged(int)));
111     connect(fontButton, SIGNAL(currentFontChanged(const QFont &)),
112             this, SLOT(onCurrentFontChanged(const QFont &)));
113     connect(box, SIGNAL(buttonClicked(int)),
114             this, SLOT(onSchemeButtonClicked(int)));
115     connect(orientationBox, SIGNAL(buttonClicked(int)),
116             this, SLOT(onOrientationButtonClicked(int)));
117 }
118
119 void SettingsWindow::onSliderValueChanged(int value)
120 {
121 #ifdef Q_WS_MAEMO_5 // Re-scaling the book view is too much for the N900
122     Q_UNUSED(value);
123 #else
124     Settings::instance()->setValue("zoom", value);
125 #endif // Q_WS_MAEMO_5
126 }
127
128 void SettingsWindow::onCurrentFontChanged(const QFont &font)
129 {
130 #ifdef Q_WS_MAEMO_5
131     Q_UNUSED(font);
132 #else
133     Settings::instance()->setValue("font", font.family());
134 #endif // Q_WS_MAEMO_5
135 }
136
137 void SettingsWindow::onSchemeButtonClicked(int id)
138 {
139     QString scheme;
140     switch (id) {
141     case SchemeDay: scheme = "day"; break;
142     case SchemeNight: scheme = "night"; break;
143     case SchemeSand: scheme = "sand"; break;
144     default: scheme = "default"; break;
145     }
146     Settings::instance()->setValue("scheme", scheme);
147 }
148
149 void SettingsWindow::onOrientationButtonClicked(int id)
150 {
151 #ifdef Q_WS_MAEMO_5
152     Q_UNUSED(id);
153 #else
154     QString orientation;
155     switch (id) {
156     case OrientationLandscape:
157         orientation = "landscape";
158         break;
159     default:
160         orientation = "portrait";
161         break;
162     }
163     Settings::instance()->setValue("orientation", orientation);
164 #endif // Q_WS_MAEMO_5
165 }
166
167 #ifdef Q_WS_MAEMO_5
168
169 void SettingsWindow::closeEvent(QCloseEvent *e)
170 {
171     Settings *settings = Settings::instance();
172     settings->setValue("zoom", zoomSlider->value());
173     settings->setValue("font", fontButton->currentFont().family());
174     settings->setValue("orientation",
175         (orientationBox->checkedId() == OrientationLandscape)?
176         "landscape": "portrait");
177     e->accept();
178 }
179
180 #endif // Q_WS_MAEMO_5
181
182 void SettingsWindow::onLightsToggled(bool value)
183 {
184     Settings::instance()->setValue("lightson", value);
185 }
186
187 void SettingsWindow::onGrabVolumeToggled(bool enable)
188 {
189     Settings::instance()->setValue("usevolumekeys", enable);
190 }