Brush up.
[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 const int ZOOM_MIN = 75;
14 const int ZOOM_MAX = 250;
15 const int ZOOM_STEP = 25;
16
17 SettingsWindow::SettingsWindow(QWidget *parent):  AdopterWindow(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     Platform *platform = Platform::instance();
27
28     QScrollArea *scroller = new QScrollArea(this);
29 #if defined(Q_WS_MAEMO_5)
30     scroller->setProperty("FingerScrollable", true);
31     scroller->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
32 #elif defined(Q_OS_SYMBIAN)
33     FlickCharm *charm = new FlickCharm(this);
34     charm->activateOn(scroller);
35 #else
36     scroller->setVerticalScrollBarPolicy(Qt::ScrollBarAsNeeded);
37 #endif
38     scroller->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
39     scroller->setFrameStyle(QFrame::NoFrame);
40
41     QWidget *contents = new QWidget(scroller);
42     QVBoxLayout *layout = new QVBoxLayout(contents);
43     contents->setLayout(layout);
44
45 #ifndef Q_OS_SYMBIAN
46     QCheckBox *backlight = new QCheckBox(tr("Keep backlight on"), contents);
47     layout->addWidget(backlight);
48     backlight->setChecked(settings->value("lightson", false).toBool());
49 #endif
50
51     QCheckBox *grabVolume =
52             new QCheckBox(tr("Navigate with volume keys"), contents);
53     layout->addWidget(grabVolume);
54     grabVolume->setChecked(settings->value("usevolumekeys", false).toBool());
55
56     int zoom = settings->value("zoom", platform->defaultZoom()).toInt();
57     if (zoom < ZOOM_MIN) {
58         zoom = ZOOM_MIN;
59     } else if (zoom > ZOOM_MAX) {
60         zoom = ZOOM_MAX;
61     }
62     zoomLabel = new QLabel(tr("Zoom level: %1%").arg(zoom), contents);
63     layout->addWidget(zoomLabel);
64     zoomSlider = new QSlider(Qt::Horizontal, contents);
65     zoomSlider->setMinimum(ZOOM_MIN);
66     zoomSlider->setMaximum(ZOOM_MAX);
67     zoomSlider->setSingleStep(ZOOM_STEP);
68     zoomSlider->setPageStep(ZOOM_STEP);
69     zoomSlider->setValue(zoom);
70     layout->addWidget(zoomSlider);
71
72     QLabel *fontLabel = new QLabel(tr("Font:"), contents);
73     layout->addWidget(fontLabel);
74     QString family =
75             settings->value("font", platform->defaultFont()).toString();
76     fontButton = new QFontComboBox(contents);
77     fontButton->setCurrentFont(QFont(family));
78     fontButton->setEditable(false);
79     layout->addWidget(fontButton);
80
81     QLabel *colorLabel = new QLabel(tr("Color scheme:"), contents);
82     layout->addWidget(colorLabel);
83     ToolButtonBox *box = new ToolButtonBox(this);
84     layout->addWidget(box);
85     box->addButton(SchemeDefault, tr("Default"),
86                    Platform::instance()->icon("style-default"));
87     box->addButton(SchemeNight, tr("Night"),
88                    Platform::instance()->icon("style-night"));
89     box->addButton(SchemeDay, tr("Day"),
90                    Platform::instance()->icon("style-day"));
91     box->addButton(SchemeSand, tr("Sand"),
92                    Platform::instance()->icon("style-sand"));
93     box->addStretch();
94     QString scheme = settings->value("scheme", "default").toString();
95     if (scheme == "night") {
96         box->toggle(SchemeNight);
97     } else if (scheme == "day") {
98         box->toggle(SchemeDay);
99     } else if (scheme == "sand") {
100         box->toggle(SchemeSand);
101     } else {
102         box->toggle(SchemeDefault);
103     }
104
105     layout->addStretch();
106     scroller->setWidget(contents);
107     contents->show();
108     scroller->setWidgetResizable(true);
109
110     setCentralWidget(scroller);
111
112 #ifndef Q_OS_SYMBIAN
113     connect(backlight, SIGNAL(toggled(bool)),
114             this, SLOT(onLightsToggled(bool)));
115 #endif
116     connect(grabVolume, SIGNAL(toggled(bool)),
117             this, SLOT(onGrabVolumeToggled(bool)));
118     connect(zoomSlider, SIGNAL(valueChanged(int)),
119             this, SLOT(onSliderValueChanged(int)));
120     connect(fontButton, SIGNAL(currentFontChanged(const QFont &)),
121             this, SLOT(onCurrentFontChanged(const QFont &)));
122     connect(box, SIGNAL(buttonClicked(int)),
123             this, SLOT(onSchemeButtonClicked(int)));
124
125 #ifdef Q_OS_SYMBIAN
126     QAction *closeAction = new QAction(parent? tr("Back"): tr("Exit"), this);
127     closeAction->setSoftKeyRole(QAction::NegativeSoftKey);
128     connect(closeAction, SIGNAL(triggered()), this, SLOT(close()));
129     QMainWindow::addAction(closeAction);
130 #endif
131 }
132
133 void SettingsWindow::onSliderValueChanged(int value)
134 {
135     int step = zoomSlider->singleStep();
136     if (value % step) {
137         zoomSlider->setValue((value + step / 2) / step * step);
138         return;
139     }
140     zoomLabel->setText(tr("Zoom level: %1%").arg(value));
141 }
142
143 void SettingsWindow::onCurrentFontChanged(const QFont &font)
144 {
145     Q_UNUSED(font);
146 }
147
148 void SettingsWindow::onSchemeButtonClicked(int id)
149 {
150     QString scheme;
151     switch (id) {
152     case SchemeDay: scheme = "day"; break;
153     case SchemeNight: scheme = "night"; break;
154     case SchemeSand: scheme = "sand"; break;
155     default: scheme = "default"; break;
156     }
157     Settings::instance()->setValue("scheme", scheme);
158 }
159
160 void SettingsWindow::onOrientationButtonClicked(int id)
161 {
162     QString orientation;
163     switch (id) {
164     case OrientationLandscape:
165         orientation = "landscape";
166         break;
167     default:
168         orientation = "portrait";
169         break;
170     }
171     Settings::instance()->setValue("orientation", orientation);
172 }
173
174 void SettingsWindow::closeEvent(QCloseEvent *e)
175 {
176     TRACE;
177     Settings *settings = Settings::instance();
178     settings->setValue("zoom", zoomSlider->value());
179     settings->setValue("font", fontButton->currentFont().family());
180     e->accept();
181 }
182
183 void SettingsWindow::onLightsToggled(bool value)
184 {
185     Settings::instance()->setValue("lightson", value);
186 }
187
188 void SettingsWindow::onGrabVolumeToggled(bool enable)
189 {
190     Settings::instance()->setValue("usevolumekeys", enable);
191 }