06226a93c728bb9a470bf6e265427066c0a079b7
[mdictionary] / src / mdictionary / 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 /*! \file DictManagerWidget.cpp
23     \brief Implements applications settings widget
24     \author Mateusz Półrola <mateusz.polrola@comarch.pl>
25 */
26
27 #include "SettingsWidget.h"
28 #include <QDebug>
29
30 SettingsWidget::SettingsWidget(GUIInterface *parent) :
31         QDialog(parent)
32 {
33     guiInterface = parent;
34
35     setWindowTitle(tr("Settings"));
36
37     initalizeUI();
38
39     setModal(true);
40 }
41
42 void SettingsWidget::initalizeUI() {
43     verticalLayout = new QVBoxLayout;
44     setLayout(verticalLayout);
45
46     historySizeLayout = new QHBoxLayout;
47
48     historySizeSpinBox = new SpinBox;
49     historySizeSpinBox->setMinimum(1);
50     historySizeSpinBox->setMaximum(50);
51
52     historySizeToolTip = tr("Limits maximum number of words saved in history");
53
54     #ifdef Q_WS_MAEMO_5
55         historySizeInfoToolButton = new QToolButton;
56         historySizeInfoToolButton->setIcon(QIcon::fromTheme("general_information"));
57     #endif
58
59     historySizeLayout->addWidget(historySizeSpinBox);
60     #ifdef Q_WS_MAEMO_5
61         historySizeLayout->addWidget(historySizeInfoToolButton);
62     #endif
63
64
65
66
67     searchResultLayout = new QHBoxLayout;
68
69     searchResultSizeSpinBox = new SpinBox;
70     searchResultSizeSpinBox->setMinimum(0);
71     searchResultSizeSpinBox->setMaximum(500);
72     searchResultSizeSpinBox->setSpecialValueText(tr("Unlimited"));
73
74     searchResultSizeToolTip = tr("Limits maximum number of found words, affects"
75                                  " only when searching in file.");
76
77     #ifdef Q_WS_MAEMO_5
78         searchResultSizeInfoToolButton = new QToolButton;
79         searchResultSizeInfoToolButton->setIcon(QIcon::fromTheme("general_information"));
80     #endif
81
82     searchResultLayout->addWidget(searchResultSizeSpinBox);
83     #ifdef Q_WS_MAEMO_5
84         searchResultLayout->addWidget(searchResultSizeInfoToolButton);
85     #endif
86
87
88
89
90
91     spinBoxesFormLayout = new QFormLayout;
92     spinBoxesFormLayout->addRow(tr("Search result size"),
93                                 searchResultLayout);
94     spinBoxesFormLayout->addRow(tr("History size"),
95                                 historySizeLayout);
96
97
98
99
100     verticalLayout->addLayout(spinBoxesFormLayout);
101
102
103     checkBoxesLabel = new QLabel(tr("Search in:"),this);
104
105     searchInBookmarksCheckBox = new QCheckBox(tr("Bookmarks"));
106     searchInDictionariesCheckBox = new QCheckBox(tr("Dictionaries"));
107
108     verticalLayout->addSpacing(20);
109     verticalLayout->addWidget(checkBoxesLabel);
110     verticalLayout->addWidget(searchInDictionariesCheckBox);
111     verticalLayout->addWidget(searchInBookmarksCheckBox);
112
113
114     connect(historySizeSpinBox, SIGNAL(valueChanged(int)), this,
115             SLOT(changed()));
116     connect(searchResultSizeSpinBox, SIGNAL(valueChanged(int)), this,
117             SLOT(changed()));
118     connect(searchInDictionariesCheckBox, SIGNAL(toggled(bool)), this,
119             SLOT(changed()));
120     connect(searchInBookmarksCheckBox, SIGNAL(toggled(bool)), this,
121             SLOT(changed()));
122
123
124 #ifdef Q_WS_MAEMO_5
125         connect(searchResultSizeInfoToolButton, SIGNAL(clicked()),
126                 this, SLOT(showSearchResultSizeInfo()));
127
128         connect(historySizeInfoToolButton, SIGNAL(clicked()),
129                 this, SLOT(showHistorySizeInfo()));
130 #else
131         historySizeSpinBox->setToolTip(historySizeToolTip);
132         searchResultSizeSpinBox->setToolTip(searchResultSizeToolTip);
133 #endif
134
135
136     settings = 0;
137
138     #ifndef Q_WS_MAEMO_5
139         setMinimumWidth(250);
140         setMaximumWidth(250);
141         footerLayout = new QHBoxLayout;
142         closeButton = new QPushButton(tr("Save"));
143         footerLayout->addStretch(0);
144         footerLayout->addWidget(closeButton);
145         verticalLayout->addLayout(footerLayout);
146
147         setMinimumHeight(sizeHint().height());
148         setMaximumHeight(sizeHint().height());
149         connect(closeButton, SIGNAL(clicked()), this, SLOT(save()));
150     #endif
151 }
152
153 void SettingsWidget::showEvent(QShowEvent *e) {
154
155    #ifndef Q_WS_MAEMO_5
156        _save = false;
157    #endif
158
159    settings = guiInterface->settings();
160
161    historySizeSpinBox->setValue(
162             settings->value("history_size").toInt());
163
164     searchResultSizeSpinBox->setValue(
165             settings->value("search_limit").toInt());
166
167     if(settings->value("search_bookmarks") == "true")
168         searchInBookmarksCheckBox->setChecked(true);
169     else
170         searchInBookmarksCheckBox->setChecked(false);
171
172     if(settings->value("search_dictionaries") == "true")
173         searchInDictionariesCheckBox->setChecked(true);
174     else
175         searchInDictionariesCheckBox->setChecked(false);
176
177     _changed = false;
178     QDialog::showEvent(e);
179 }
180
181 void SettingsWidget::hideEvent(QHideEvent *e) {
182     QDialog::hideEvent(e);
183
184     #ifndef Q_WS_MAEMO_5
185         if(settings && _save) {
186     #else
187         if(settings && _changed &&
188            QMessageBox::question(this,
189                                  tr("Save"),
190                                  tr("Do you want to save changes?"),
191              QMessageBox::Save, QMessageBox::Cancel) == QMessageBox::Save) {
192
193     #endif
194         Settings* newSettings = new Settings;
195         newSettings->setValue("history_size",
196                               QString::number(historySizeSpinBox->value()));
197         newSettings->setValue("search_limit",
198                               QString::number(
199                                       searchResultSizeSpinBox->value()));
200
201         if(searchInDictionariesCheckBox->isChecked())
202             newSettings->setValue("search_dictionaries", "true");
203         else
204             newSettings->setValue("search_dictionaries", "false");
205
206         if(searchInBookmarksCheckBox->isChecked())
207             newSettings->setValue("search_bookmarks", "true");
208         else
209             newSettings->setValue("search_bookmarks", "false");
210
211         //setting new settings only if they are different than old ones
212         QString key;
213         foreach(key, newSettings->keys()) {
214             if(settings->value(key) != newSettings->value(key)) {
215                 guiInterface->setSettings(newSettings);
216                 break;
217             }
218         }
219
220     }
221     if(settings) {
222         delete settings;
223         settings = 0;
224     }
225     _changed = false;
226 }
227
228
229 void SettingsWidget::changed() {
230     _changed = true;
231 }
232
233 #ifndef Q_WS_MAEMO_5
234     void SettingsWidget::save() {
235         _save = true;
236         hide();
237     }
238 #endif
239
240
241 #ifdef Q_WS_MAEMO_5
242     void SettingsWidget::showHistorySizeInfo() {
243         Q_EMIT notify(Notify::Warning, historySizeToolTip);
244     }
245
246     void SettingsWidget::showSearchResultSizeInfo() {
247         Q_EMIT notify(Notify::Warning, searchResultSizeToolTip);
248     }
249 #endif