Add new widget ToolButtonBox. Add setting to prevent display blanking.
[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     QLabel *zoomLabel = new QLabel(tr("Zoom level:"), contents);
41     layout->addWidget(zoomLabel);
42     zoomSlider = new QSlider(Qt::Horizontal, contents);
43     zoomSlider->setMinimum(50);
44     zoomSlider->setMaximum(300);
45     zoomSlider->setValue(Settings::instance()->value("zoom").toInt());
46     layout->addWidget(zoomSlider);
47
48     QLabel *fontLabel = new QLabel(tr("Font:"), contents);
49     layout->addWidget(fontLabel);
50     QString defaultFamily = fontLabel->fontInfo().family();
51     QString family = Settings::instance()->value("font", defaultFamily).toString();
52     fontButton = new QFontComboBox(contents);
53     fontButton->setCurrentFont(QFont(family));
54     fontButton->setEditable(false);
55     layout->addWidget(fontButton);
56
57     QLabel *colorLabel = new QLabel(tr("Color scheme:"), contents);
58     layout->addWidget(colorLabel);
59     ToolButtonBox *box = new ToolButtonBox(this);
60     layout->addWidget(box);
61     box->addButton(SchemeDefault, tr("Default"), ":/icons/style-default.png");
62     box->addButton(SchemeNight, tr("Night"), ":/icons/style-night.png");
63     box->addButton(SchemeDay, tr("Day"), ":/icons/style-day.png");
64     box->addButton(SchemeSand, tr("Sand"), ":/icons/style-sand.png");
65     box->addStretch();
66     QString scheme = settings->value("scheme", "default").toString();
67     if (scheme == "night") {
68         box->toggle(SchemeNight);
69     } else if (scheme == "day") {
70         box->toggle(SchemeDay);
71     } else if (scheme == "sand") {
72         box->toggle(SchemeSand);
73     } else {
74         box->toggle(SchemeDefault);
75     }
76
77     QLabel *orientationLabel = new QLabel(tr("Orientation:"), contents);
78     layout->addWidget(orientationLabel);
79     orientationBox = new ToolButtonBox(this);
80     layout->addWidget(orientationBox);
81     orientationBox->addButton(OrientationPortrait, tr("Portrait"),
82                               ":/icons/settings-portrait.png");
83     orientationBox->addButton(OrientationLandscape, tr("Landscape"),
84                               ":/icons/settings-landscape.png");
85     orientationBox->addStretch();
86     QString orientation =
87         settings->value("orientation", DEFAULT_ORIENTATION).toString();
88     if (orientation == "portrait") {
89         orientationBox->toggle(OrientationPortrait);
90     } else {
91         orientationBox->toggle(OrientationLandscape);
92     }
93
94     layout->addStretch();
95     scroller->setWidget(contents);
96     contents->show();
97     scroller->setWidgetResizable(true);
98
99     setCentralWidget(scroller);
100
101     connect(backlight, SIGNAL(toggled(bool)), this, SLOT(onLightsToggled(bool)));
102     connect(zoomSlider, SIGNAL(valueChanged(int)),
103             this, SLOT(onSliderValueChanged(int)));
104     connect(fontButton, SIGNAL(currentFontChanged(const QFont &)),
105             this, SLOT(onCurrentFontChanged(const QFont &)));
106     connect(box, SIGNAL(buttonClicked(int)),
107             this, SLOT(onSchemeButtonClicked(int)));
108     connect(orientationBox, SIGNAL(buttonClicked(int)),
109             this, SLOT(onOrientationButtonClicked(int)));
110 }
111
112 void SettingsWindow::onSliderValueChanged(int value)
113 {
114 #ifdef Q_WS_MAEMO_5 // Re-scaling the book view is too much for the N900
115     Q_UNUSED(value);
116 #else
117     Settings::instance()->setValue("zoom", value);
118 #endif // Q_WS_MAEMO_5
119 }
120
121 void SettingsWindow::onCurrentFontChanged(const QFont &font)
122 {
123 #ifdef Q_WS_MAEMO_5
124     Q_UNUSED(font);
125 #else
126     Settings::instance()->setValue("font", font.family());
127 #endif // Q_WS_MAEMO_5
128 }
129
130 void SettingsWindow::onSchemeButtonClicked(int id)
131 {
132     QString scheme;
133     switch (id) {
134     case SchemeDay: scheme = "day"; break;
135     case SchemeNight: scheme = "night"; break;
136     case SchemeSand: scheme = "sand"; break;
137     default: scheme = "default"; break;
138     }
139     Settings::instance()->setValue("scheme", scheme);
140 }
141
142 void SettingsWindow::onOrientationButtonClicked(int id)
143 {
144 #ifdef Q_WS_MAEMO_5
145     Q_UNUSED(id);
146 #else
147     QString orientation;
148     switch (id) {
149     case OrientationLandscape:
150         orientation = "landscape";
151         break;
152     default:
153         orientation = "portrait";
154         break;
155     }
156     Settings::instance()->setValue("orientation", orientation);
157 #endif // Q_WS_MAEMO_5
158 }
159
160 #ifdef Q_WS_MAEMO_5
161
162 void SettingsWindow::closeEvent(QCloseEvent *e)
163 {
164     Settings *settings = Settings::instance();
165     settings->setValue("zoom", zoomSlider->value());
166     settings->setValue("font", fontButton->currentFont().family());
167     settings->setValue("orientation",
168         (orientationBox->checkedId() == OrientationLandscape)?
169         "landscape": "portrait");
170     e->accept();
171 }
172
173 #endif // Q_WS_MAEMO_5
174
175 void SettingsWindow::onLightsToggled(bool value)
176 {
177     Settings::instance()->setValue("lightson", value);
178 }