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