Merge branch 'cache'
[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(this);
35     setLayout(verticalLayout);
36
37     historySizeSpinBox = new QSpinBox(this);
38     searchResultSizeSpinBox = new QSpinBox(this);
39
40     spinBoxesFormLayout = new QFormLayout(this);
41
42     spinBoxesFormLayout->addRow(tr("Search result size"),
43                                 searchResultSizeSpinBox);
44
45     spinBoxesFormLayout->addRow(tr("History size"),
46                                 historySizeSpinBox);
47
48     searchResultSizeSpinBox->setMinimum(1);
49     historySizeSpinBox->setMinimum(1);
50
51     #ifdef Q_WS_MAEMO_5
52         verticalLayout->addSpacing(20);
53     #endif
54     verticalLayout->addLayout(spinBoxesFormLayout);
55
56
57     checkBoxesLabel = new QLabel(tr("Search in:"),this);
58
59     searchInBookmarksCheckBox = new QCheckBox(tr("Bookmarks"),this);
60     searchInDictionariesCheckBox = new QCheckBox(tr("Dictionaries"),this);
61
62     verticalLayout->addSpacing(20);
63     verticalLayout->addWidget(checkBoxesLabel);
64     verticalLayout->addWidget(searchInDictionariesCheckBox);
65     verticalLayout->addWidget(searchInBookmarksCheckBox);
66
67
68     connect(historySizeSpinBox, SIGNAL(valueChanged(int)), this,
69             SLOT(changed()));
70     connect(searchResultSizeSpinBox, SIGNAL(valueChanged(int)), this,
71             SLOT(changed()));
72     connect(searchInDictionariesCheckBox, SIGNAL(toggled(bool)), this,
73             SLOT(changed()));
74     connect(searchInBookmarksCheckBox, SIGNAL(toggled(bool)), this,
75             SLOT(changed()));
76
77
78     settings = 0;
79
80     #ifndef Q_WS_MAEMO_5
81         setMinimumWidth(250);
82         setMaximumWidth(250);
83         footerLayout = new QHBoxLayout(this);
84         closeButton = new QPushButton(tr("Save"));
85         footerLayout->addStretch(0);
86         footerLayout->addWidget(closeButton);
87         verticalLayout->addLayout(footerLayout);
88         connect(closeButton, SIGNAL(clicked()), this, SLOT(save()));
89     #endif
90 }
91
92 void SettingsWidget::showEvent(QShowEvent *e) {
93
94    #ifndef Q_WS_MAEMO_5
95        _save = false;
96    #endif
97    settings = guiInterface->settings();
98
99    historySizeSpinBox->setValue(
100             settings->value("history_size").toInt());
101
102     searchResultSizeSpinBox->setValue(
103             settings->value("search_limit").toInt());
104
105     if(settings->value("search_bookmarks") == "true")
106         searchInBookmarksCheckBox->setChecked(true);
107     else
108         searchInBookmarksCheckBox->setChecked(false);
109
110     if(settings->value("search_dictionaries") == "true")
111         searchInDictionariesCheckBox->setChecked(true);
112     else
113         searchInDictionariesCheckBox->setChecked(false);
114
115     _changed = false;
116     QDialog::showEvent(e);
117 }
118
119 void SettingsWidget::hideEvent(QHideEvent *e) {
120     QDialog::hideEvent(e);
121
122     #ifndef Q_WS_MAEMO_5
123     if(settings && _save) {
124     #else
125     if(settings && _changed &&
126             QMessageBox::question(this, "Save", "Do you want to save changes?",
127              QMessageBox::Save, QMessageBox::Cancel) == QMessageBox::Save) {
128
129     #endif
130         Settings* newSettings = new Settings;
131         newSettings->setValue("history_size",
132                               QString::number(historySizeSpinBox->value()));
133         newSettings->setValue("search_limit",
134                               QString::number(
135                                       searchResultSizeSpinBox->value()));
136
137         if(searchInDictionariesCheckBox->isChecked())
138             newSettings->setValue("search_dictionaries", "true");
139         else
140             newSettings->setValue("search_dictionaries", "false");
141
142         if(searchInBookmarksCheckBox->isChecked())
143             newSettings->setValue("search_bookmarks", "true");
144         else
145             newSettings->setValue("search_bookmarks", "false");
146
147         //setting new settings only if their are different that old ones
148         QString key;
149         foreach(key, newSettings->keys()) {
150             if(settings->value(key) != newSettings->value(key)) {
151                 guiInterface->setSettings(newSettings);
152                 break;
153             }
154         }
155
156     }
157     if(settings) {
158         delete settings;
159         settings = 0;
160     }
161     _changed = false;
162 }
163
164
165 void SettingsWidget::changed() {
166     _changed = true;
167 }
168
169 #ifndef Q_WS_MAEMO_5
170     void SettingsWidget::save() {
171         _save = true;
172         hide();
173     }
174 #endif