New version. Update home page link in About box.
[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 = settings->value("font", platform->defaultFont()).toString();
75     fontButton = new QFontComboBox(contents);
76     fontButton->setCurrentFont(QFont(family));
77     fontButton->setEditable(false);
78     layout->addWidget(fontButton);
79
80     QLabel *colorLabel = new QLabel(tr("Color scheme:"), contents);
81     layout->addWidget(colorLabel);
82     ToolButtonBox *box = new ToolButtonBox(this);
83     layout->addWidget(box);
84     box->addButton(SchemeDefault, tr("Default"),
85                    Platform::instance()->icon("style-default"));
86     box->addButton(SchemeNight, tr("Night"),
87                    Platform::instance()->icon("style-night"));
88     box->addButton(SchemeDay, tr("Day"), Platform::instance()->icon("style-day"));
89     box->addButton(SchemeSand, tr("Sand"),
90                    Platform::instance()->icon("style-sand"));
91     box->addStretch();
92     QString scheme = settings->value("scheme", "default").toString();
93     if (scheme == "night") {
94         box->toggle(SchemeNight);
95     } else if (scheme == "day") {
96         box->toggle(SchemeDay);
97     } else if (scheme == "sand") {
98         box->toggle(SchemeSand);
99     } else {
100         box->toggle(SchemeDefault);
101     }
102
103 #ifndef Q_OS_SYMBIAN
104     QLabel *orientationLabel = new QLabel(tr("Orientation:"), contents);
105     layout->addWidget(orientationLabel);
106     orientationBox = new ToolButtonBox(this);
107     layout->addWidget(orientationBox);
108     orientationBox->addButton(OrientationPortrait, tr("Portrait"),
109                               ":/icons/settings-portrait.png");
110     orientationBox->addButton(OrientationLandscape, tr("Landscape"),
111                               ":/icons/settings-landscape.png");
112     orientationBox->addStretch();
113     QString orientation =
114         settings->value("orientation", platform->defaultOrientation()).toString();
115     if (orientation == "portrait") {
116         orientationBox->toggle(OrientationPortrait);
117     } else {
118         orientationBox->toggle(OrientationLandscape);
119     }
120 #endif // !Q_OS_SYMBIAN
121
122     layout->addStretch();
123     scroller->setWidget(contents);
124     contents->show();
125     scroller->setWidgetResizable(true);
126
127     setCentralWidget(scroller);
128
129 #ifndef Q_OS_SYMBIAN
130     connect(backlight, SIGNAL(toggled(bool)),
131             this, SLOT(onLightsToggled(bool)));
132 #endif
133     connect(grabVolume, SIGNAL(toggled(bool)),
134             this, SLOT(onGrabVolumeToggled(bool)));
135     connect(zoomSlider, SIGNAL(valueChanged(int)),
136             this, SLOT(onSliderValueChanged(int)));
137     connect(fontButton, SIGNAL(currentFontChanged(const QFont &)),
138             this, SLOT(onCurrentFontChanged(const QFont &)));
139     connect(box, SIGNAL(buttonClicked(int)),
140             this, SLOT(onSchemeButtonClicked(int)));
141 #ifndef Q_OS_SYMBIAN
142     connect(orientationBox, SIGNAL(buttonClicked(int)),
143             this, SLOT(onOrientationButtonClicked(int)));
144 #endif
145
146 #ifdef Q_OS_SYMBIAN
147     QAction *closeAction = new QAction(parent? tr("Back"): tr("Exit"), this);
148     closeAction->setSoftKeyRole(QAction::NegativeSoftKey);
149     connect(closeAction, SIGNAL(triggered()), this, SLOT(close()));
150     QMainWindow::addAction(closeAction);
151 #endif
152 }
153
154 void SettingsWindow::onSliderValueChanged(int value)
155 {
156     int step = zoomSlider->singleStep();
157     if (value % step) {
158         zoomSlider->setValue((value + step / 2) / step * step);
159         return;
160     }
161     zoomLabel->setText(tr("Zoom level: %1%").arg(value));
162 }
163
164 void SettingsWindow::onCurrentFontChanged(const QFont &font)
165 {
166     Q_UNUSED(font);
167 }
168
169 void SettingsWindow::onSchemeButtonClicked(int id)
170 {
171     QString scheme;
172     switch (id) {
173     case SchemeDay: scheme = "day"; break;
174     case SchemeNight: scheme = "night"; break;
175     case SchemeSand: scheme = "sand"; break;
176     default: scheme = "default"; break;
177     }
178     Settings::instance()->setValue("scheme", scheme);
179 }
180
181 void SettingsWindow::onOrientationButtonClicked(int id)
182 {
183 #ifdef Q_WS_MAEMO_5
184     Q_UNUSED(id);
185 #else
186     QString orientation;
187     switch (id) {
188     case OrientationLandscape:
189         orientation = "landscape";
190         break;
191     default:
192         orientation = "portrait";
193         break;
194     }
195     Settings::instance()->setValue("orientation", orientation);
196 #endif // Q_WS_MAEMO_5
197 }
198
199 void SettingsWindow::closeEvent(QCloseEvent *e)
200 {
201     TRACE;
202     Settings *settings = Settings::instance();
203     settings->setValue("zoom", zoomSlider->value());
204     settings->setValue("font", fontButton->currentFont().family());
205 #ifndef Q_OS_SYMBIAN
206     settings->setValue("orientation",
207         (orientationBox->checkedId() == OrientationLandscape)?
208         "landscape": "portrait");
209 #endif // Q_OS_SYMBIAN
210     e->accept();
211 }
212
213 void SettingsWindow::onLightsToggled(bool value)
214 {
215     Settings::instance()->setValue("lightson", value);
216 }
217
218 void SettingsWindow::onGrabVolumeToggled(bool enable)
219 {
220     Settings::instance()->setValue("usevolumekeys", enable);
221 }