Update from old repository.
[dorian] / settingswindow.cpp
1 #include <QtGui>
2
3 #include "settingswindow.h"
4 #include "settings.h"
5
6 #ifdef Q_OS_SYMBIAN
7 #define DEFAULT_ORIENTATION "portrait"
8 #else
9 #define DEFAULT_ORIENTATION "landscape"
10 #endif
11
12 SettingsWindow::SettingsWindow(QWidget *parent):  QMainWindow(parent)
13 {
14 #ifdef Q_WS_MAEMO_5
15     setAttribute(Qt::WA_Maemo5StackedWindow, true);
16     setAttribute(Qt::WA_Maemo5AutoOrientation, true);
17 #endif
18     setWindowTitle("Settings");
19
20     QScrollArea *scroller = new QScrollArea(this);
21 #ifdef Q_WS_MAEMO_5
22     scroller->setProperty("FingerScrollable", true);
23     scroller->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
24 #else
25     scroller->setVerticalScrollBarPolicy(Qt::ScrollBarAsNeeded);
26 #endif
27     scroller->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
28     scroller->setFrameStyle(QFrame::NoFrame);
29
30     QWidget *contents = new QWidget(scroller);
31     QVBoxLayout *layout = new QVBoxLayout(contents);
32     contents->setLayout(layout);
33
34     QLabel *zoomLabel = new QLabel(tr("Zoom level:"), contents);
35     layout->addWidget(zoomLabel);
36     zoomSlider = new QSlider(Qt::Horizontal, contents);
37     zoomSlider->setMinimum(50);
38     zoomSlider->setMaximum(300);
39     zoomSlider->setValue(Settings::instance()->value("zoom").toInt());
40     layout->addWidget(zoomSlider);
41
42     QLabel *fontLabel = new QLabel(tr("Font:"), contents);
43     layout->addWidget(fontLabel);
44     QString defaultFamily = fontLabel->fontInfo().family();
45     QString family = Settings::instance()->value("font", defaultFamily).toString();
46     fontButton = new QFontComboBox(contents);
47     fontButton->setCurrentFont(QFont(family));
48     fontButton->setEditable(false);
49     layout->addWidget(fontButton);
50
51     QLabel *colorLabel = new QLabel(tr("Color scheme:"), contents);
52     layout->addWidget(colorLabel);
53     QFrame *box = new QFrame(this);
54     layout->addWidget(box);
55     QHBoxLayout *boxLayout = new QHBoxLayout(box);
56     boxLayout->setMargin(0);
57     box->setLayout(boxLayout);
58     QButtonGroup *group = new QButtonGroup(this);
59
60     QToolButton *defaultSchemeButton = new QToolButton(box);
61     defaultSchemeButton->setToolButtonStyle(Qt::ToolButtonTextUnderIcon);
62     defaultSchemeButton->setText(tr("Default"));
63     defaultSchemeButton->setIconSize(QSize(81, 81));
64     defaultSchemeButton->setIcon(QIcon(":/icons/style-default.png"));
65     defaultSchemeButton->setCheckable(true);
66     boxLayout->addWidget(defaultSchemeButton);
67     group->addButton(defaultSchemeButton);
68     group->setId(defaultSchemeButton, SchemeDefault);
69
70     QToolButton *nightSchemeButton = new QToolButton(box);
71     nightSchemeButton->setToolButtonStyle(Qt::ToolButtonTextUnderIcon);
72     nightSchemeButton->setText(tr("Night"));
73     nightSchemeButton->setCheckable(true);
74     nightSchemeButton->setIconSize(QSize(81, 81));
75     nightSchemeButton->setIcon(QIcon(":/icons/style-night.png"));
76     boxLayout->addWidget(nightSchemeButton);
77     group->addButton(nightSchemeButton);
78     group->setId(nightSchemeButton, SchemeNight);
79
80     QToolButton *daySchemeButton = new QToolButton(box);
81     daySchemeButton->setToolButtonStyle(Qt::ToolButtonTextUnderIcon);
82     daySchemeButton->setText(tr("Day"));
83     daySchemeButton->setCheckable(true);
84     daySchemeButton->setIconSize(QSize(81, 81));
85     daySchemeButton->setIcon(QIcon(":/icons/style-day.png"));
86     boxLayout->addWidget(daySchemeButton);
87     group->addButton(daySchemeButton);
88     group->setId(daySchemeButton, SchemeDay);
89
90     QToolButton *sandSchemeButton = new QToolButton(box);
91     sandSchemeButton->setToolButtonStyle(Qt::ToolButtonTextUnderIcon);
92     sandSchemeButton->setText(tr("Sand"));
93     sandSchemeButton->setCheckable(true);
94     sandSchemeButton->setIconSize(QSize(81, 81));
95     sandSchemeButton->setIcon(QIcon(":/icons/style-sand.png"));
96     boxLayout->addWidget(sandSchemeButton);
97     group->addButton(sandSchemeButton);
98     group->setId(sandSchemeButton, SchemeSand);
99
100     QString scheme = Settings::instance()->value("scheme", "default").toString();
101     if (scheme == "night") {
102         nightSchemeButton->toggle();
103     }
104     else if (scheme == "day") {
105         daySchemeButton->toggle();
106     }
107     else if (scheme == "sand") {
108         sandSchemeButton->toggle();
109     }
110     else {
111         defaultSchemeButton->toggle();
112     }
113
114     QLabel *orientationLabel = new QLabel(tr("Orientation:"), contents);
115     layout->addWidget(orientationLabel);
116
117     QFrame *orientationBox = new QFrame(this);
118     layout->addWidget(orientationBox);
119     QHBoxLayout *orientationLayout = new QHBoxLayout(orientationBox);
120     orientationLayout->setMargin(0);
121     orientationBox->setLayout(orientationLayout);
122     QButtonGroup *orientationGroup = new QButtonGroup(this);
123
124     QToolButton *portraitButton = new QToolButton(box);
125     portraitButton->setToolButtonStyle(Qt::ToolButtonTextUnderIcon);
126     portraitButton->setText(tr("Portrait"));
127     portraitButton->setIconSize(QSize(81, 81));
128     portraitButton->setIcon(QIcon(":/icons/settings-portrait.png"));
129     portraitButton->setCheckable(true);
130     orientationLayout->addWidget(portraitButton);
131     orientationGroup->addButton(portraitButton);
132     orientationGroup->setId(portraitButton, OrientationPortrait);
133
134     QToolButton *landscapeButton = new QToolButton(box);
135     landscapeButton->setToolButtonStyle(Qt::ToolButtonTextUnderIcon);
136     landscapeButton->setText(tr("Landscape"));
137     landscapeButton->setIconSize(QSize(81, 81));
138     landscapeButton->setIcon(QIcon(":/icons/settings-landscape.png"));
139     landscapeButton->setCheckable(true);
140     orientationLayout->addWidget(landscapeButton);
141     orientationGroup->addButton(landscapeButton);
142     orientationGroup->setId(landscapeButton, OrientationLandscape);
143
144     orientationLayout->addStretch();
145
146     QString orientation =
147         Settings::instance()->value("orientation", DEFAULT_ORIENTATION).toString();
148     if (orientation == "portrait") {
149         portraitButton->toggle();
150     }
151     else {
152         landscapeButton->toggle();
153     }
154
155     boxLayout->addStretch();
156     layout->addStretch();
157     scroller->setWidget(contents);
158     contents->show();
159     scroller->setWidgetResizable(true);
160
161     setCentralWidget(scroller);
162
163     connect(zoomSlider, SIGNAL(valueChanged(int)),
164             this, SLOT(onSliderValueChanged(int)));
165     connect(fontButton, SIGNAL(currentFontChanged(const QFont &)),
166             this, SLOT(onCurrentFontChanged(const QFont &)));
167     connect(group, SIGNAL(buttonClicked(int)),
168             this, SLOT(onSchemeButtonClicked(int)));
169     connect(orientationGroup, SIGNAL(buttonClicked(int)),
170             this, SLOT(onOrientationButtonClicked(int)));
171 }
172
173 void SettingsWindow::onSliderValueChanged(int value)
174 {
175 #ifdef Q_WS_MAEMO_5 // Re-scaling the book view is too much for the N900
176     Q_UNUSED(value);
177 #else
178     Settings::instance()->setValue("zoom", value);
179 #endif // Q_WS_MAEMO_5
180 }
181
182 void SettingsWindow::onCurrentFontChanged(const QFont &font)
183 {
184 #ifdef Q_WS_MAEMO_5
185     Q_UNUSED(font);
186 #else
187     Settings::instance()->setValue("font", font.family());
188 #endif // Q_WS_MAEMO_5
189 }
190
191 void SettingsWindow::onSchemeButtonClicked(int id)
192 {
193     QString scheme;
194     switch (id) {
195     case SchemeDay: scheme = "day"; break;
196     case SchemeNight: scheme = "night"; break;
197     case SchemeSand: scheme = "sand"; break;
198     default: scheme = "default"; break;
199     }
200     Settings::instance()->setValue("scheme", scheme);
201 }
202
203 void SettingsWindow::onOrientationButtonClicked(int id)
204 {
205     QString orientation;
206     switch (id) {
207     case OrientationLandscape: orientation = "landscape"; break;
208     default: orientation = "portrait"; break;
209     }
210     Settings::instance()->setValue("orientation", orientation);
211 }
212
213 #ifdef Q_WS_MAEMO_5
214
215 void SettingsWindow::closeEvent(QCloseEvent *e)
216 {
217     Settings::instance()->setValue("zoom", zoomSlider->value());
218     Settings::instance()->setValue("font", fontButton->currentFont().family());
219     e->accept();
220 }
221
222 #endif // Q_WS_MAEMO_5