8f3b218d4fe5d2f16dd74fdb5da0f5752de7b24f
[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 #include <QDebug>
26
27 SettingsWidget::SettingsWidget(GUIInterface *parent) :
28     QDialog(parent)
29 {
30     guiInterface = parent;
31
32     setWindowTitle(tr("Settings"));
33
34     verticalLayout = new QVBoxLayout;
35     setLayout(verticalLayout);
36
37     historySizeSpinBox = new QSpinBox;
38     searchResultSizeSpinBox = new QSpinBox;
39
40     spinBoxesFormLayout = new QFormLayout;
41
42     spinBoxesFormLayout->addRow(tr("Search result size"),
43                                 searchResultSizeSpinBox);
44
45     spinBoxesFormLayout->addRow(tr("History size"),
46                                 historySizeSpinBox);
47
48     #ifdef Q_WS_MAEMO_5
49         verticalLayout->addSpacing(20);
50     #endif
51     verticalLayout->addLayout(spinBoxesFormLayout);
52
53
54     checkBoxesLabel = new QLabel(tr("Search in:"));
55
56     searchInBookmarksCheckBox = new QCheckBox(tr("Bookmarks"));
57     searchInDictionariesCheckBox = new QCheckBox(tr("Dictionaries"));
58
59     verticalLayout->addSpacing(20);
60     verticalLayout->addWidget(checkBoxesLabel);
61     verticalLayout->addWidget(searchInDictionariesCheckBox);
62     verticalLayout->addWidget(searchInBookmarksCheckBox);
63
64     #ifndef Q_WS_MAEMO_5
65         setMinimumWidth(250);
66         setMaximumWidth(250);
67     #endif
68 }
69
70 void SettingsWidget::showEvent(QShowEvent *e) {
71
72     settings = guiInterface->settings();
73
74     historySizeSpinBox->setValue(
75             settings->value("history_size").toInt());
76
77     searchResultSizeSpinBox->setValue(
78             settings->value("search_limit").toInt());
79
80     if(settings->value("search_bookmarks") == "true")
81         searchInBookmarksCheckBox->setChecked(true);
82     else
83         searchInBookmarksCheckBox->setChecked(false);
84
85     if(settings->value("search_dictionaries") == "true")
86         searchInDictionariesCheckBox->setChecked(true);
87     else
88         searchInDictionariesCheckBox->setChecked(false);
89
90     QDialog::showEvent(e);
91 }
92
93 void SettingsWidget::hideEvent(QHideEvent *e) {
94     Settings* newSettings = new Settings;
95     newSettings->setValue("history_size",
96                           QString::number(historySizeSpinBox->value()));
97     newSettings->setValue("search_limit",
98                           QString::number(searchResultSizeSpinBox->value()));
99
100     if(searchInDictionariesCheckBox->isChecked())
101         newSettings->setValue("search_dictionaries", "true");
102     else
103         newSettings->setValue("search_dictionaries", "false");
104
105     if(searchInBookmarksCheckBox->isChecked())
106         newSettings->setValue("search_bookmarks", "true");
107     else
108         newSettings->setValue("search_bookmarks", "false");
109
110     QString key;
111     foreach(key, newSettings->keys()) {
112         if(settings->value(key) != newSettings->value(key)) {
113             guiInterface->setSettings(newSettings);
114             break;
115         }
116     }
117
118     QDialog::hideEvent(e);
119 }