Added settings widget
[mdictionary] / trunk / src / base / gui / SettingsWidget.cpp
1 /*******************************************************************************
2
3     This file is part of mDictionary.
4
5     mDictionary is free software: you can redistribute it and/or modify
6     it under the terms of the GNU General Public License as published by
7     the Free Software Foundation, either version 3 of the License, or
8     (at your option) any later version.
9
10     mDictionary is distributed in the hope that it will be useful,
11     but WITHOUT ANY WARRANTY; without even the implied warranty of
12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13     GNU General Public License for more details.
14
15     You should have received a copy of the GNU General Public License
16     along with mDictionary.  If not, see <http://www.gnu.org/licenses/>.
17
18     Copyright 2010 Comarch S.A.
19
20 *******************************************************************************/
21
22 //Created by Mateusz Półrola
23
24 #include "SettingsWidget.h"
25
26 SettingsWidget::SettingsWidget(GUIInterface *parent) :
27     QDialog(parent)
28 {
29     setWindowTitle(tr("Settings"));
30
31     verticalLayout = new QVBoxLayout;
32     setLayout(verticalLayout);
33
34     historySizeSpinBox = new QSpinBox;
35     searchResultSizeSpinBox = new QSpinBox;
36
37     spinBoxesFormLayout = new QFormLayout;
38
39     spinBoxesFormLayout->addRow(tr("Search result size"),
40                                 searchResultSizeSpinBox);
41
42     spinBoxesFormLayout->addRow(tr("History size"),
43                                 historySizeSpinBox);
44
45     #ifdef Q_WS_MAEMO_5
46         verticalLayout->addSpacing(20);
47     #endif
48     verticalLayout->addLayout(spinBoxesFormLayout);
49
50
51     checkBoxesLabel = new QLabel(tr("Search in:"));
52
53     searchInBookmarksCheckBox = new QCheckBox(tr("Bookmarks"));
54     searchInDictionariesCheckBox = new QCheckBox(tr("Dictionaries"));
55
56     verticalLayout->addSpacing(20);
57     verticalLayout->addWidget(checkBoxesLabel);
58     verticalLayout->addWidget(searchInDictionariesCheckBox);
59     verticalLayout->addWidget(searchInBookmarksCheckBox);
60
61     #ifndef Q_WS_MAEMO_5
62         setMinimumWidth(250);
63         setMaximumWidth(250);
64     #endif
65 }
66
67 void SettingsWidget::showEvent(QShowEvent *e) {
68     settings = guiInterface->getSettings();
69
70     historySizeSpinBox->setValue(
71             settings->value("history_size").toInt());
72
73     searchResultSizeSpinBox->setValue(
74             settings->value("search_limit").toInt());
75
76     if(settings->value("search_bookmarks") == "true")
77         searchInBookmarksCheckBox->setChecked(true);
78     else
79         searchInBookmarksCheckBox->setChecked(false);
80
81     if(settings->value("search_dictionaries") == "true")
82         searchInDictionariesCheckBox->setChecked(true);
83     else
84         searchInDictionariesCheckBox->setChecked(false);
85
86     QDialog::showEvent(e);
87 }
88
89 void SettingsWidget::hideEvent(QHideEvent *e) {
90     Settings* newSettings;
91     newSettings->setValue("history_size",
92                           QString::number(historySizeSpinBox->value()));
93     newSettings->setValue("search_limit",
94                           QString::number(searchResultSizeSpinBox->value()));
95
96     if(searchInDictionariesCheckBox->isChecked())
97         newSettings->setValue("search_dictionaries", "true");
98     else
99         newSettings->setValue("search_dictionaries", "false");
100
101     if(searchInBookmarksCheckBox->isChecked())
102         newSettings->setValue("search_bookmarks", "true");
103     else
104         newSettings->setValue("search_bookmarks", "false");
105
106     QString key;
107     foreach(key, newSettings->keys()) {
108         if(settings->value(key) != newSettings->value(key)) {
109             guiInterface->setSettings(newSettings);
110             break;
111         }
112     }
113
114     QDialog::hideEvent(e);
115 }