xdxfDialog in qml
[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) : QDialog(parent) {
31
32 guiInterface = parent;
33
34 #ifndef Q_WS_MAEMO_5
35     view= new QDeclarativeView();
36     view->setSource(QUrl::fromLocalFile("/usr/share/mdictionary/qml/SettingsWidget.qml"));
37     view->setResizeMode(QDeclarativeView::SizeRootObjectToView);
38     view->setAlignment(Qt::AlignCenter);
39     view->show();
40
41     view->setMinimumWidth(250);
42     view->setMaximumWidth(250);
43     view->setMinimumHeight(view->sizeHint().height());
44     view->setMaximumHeight(view->sizeHint().height());
45
46     mainLayout = new QVBoxLayout;
47     mainLayout->addWidget(view);
48     setLayout(mainLayout);
49     view->setWindowTitle(tr("Settings"));
50
51     QGraphicsObject *rootObject = view->rootObject();
52
53     connect(this, SIGNAL(setMinHistory(QVariant)),
54            rootObject, SLOT(setMinHistory(QVariant)));
55     connect(this, SIGNAL(setValueHistory(QVariant)),
56            rootObject, SLOT(setValueHistory(QVariant)));
57     connect(this, SIGNAL(setMaxHistory(QVariant)),
58            rootObject, SLOT(setMaxHistory(QVariant)));
59     connect(this, SIGNAL(setMinSearchResult(QVariant)),
60            rootObject, SLOT(setMinSearchResult(QVariant)));
61     connect(this, SIGNAL(setValueSearchResult(QVariant)),
62            rootObject, SLOT(setValueSearchResult(QVariant)));
63     connect(this, SIGNAL(setMaxSearchResult(QVariant)),
64            rootObject, SLOT(setMaxSearchResult(QVariant)));
65     connect(this, SIGNAL(setCheckedBookmarks(QVariant)),
66            rootObject, SLOT(setCheckedBookmarks(QVariant)));
67     connect(this, SIGNAL(setCheckedDictionaries(QVariant)),
68            rootObject, SLOT(setCheckedDictionaries(QVariant)));
69
70     connect(rootObject, SIGNAL(historySizeValueChanged(int)),
71            this, SLOT(historySizeValueChanged(int)));
72     connect(rootObject, SIGNAL(searchResulValueChanged(int)),
73            this, SLOT(searchResulValueChanged(int)));
74     connect(rootObject, SIGNAL(dictionariesCheckBoxChanged(bool)),
75            this, SLOT(dictionariesCheckBoxChanged(bool)));
76     connect(rootObject, SIGNAL(bookmarksCheckBoxChanged(bool)),
77            this, SLOT(bookmarksCheckBoxChanged(bool)));
78     connect(rootObject, SIGNAL(saveButtonClicked()),
79            this, SLOT(save()));
80
81 #else
82     setWindowTitle(tr("Settings"));
83     setModal(true);
84 #endif
85
86     initalizeUI();
87 }
88
89 void SettingsWidget::historySizeValueChanged(int value){
90     historyValue = value;
91     changed();
92 }
93
94 void SettingsWidget::searchResulValueChanged(int value){
95     searchResultValue=value;
96     changed();
97 }
98
99 void SettingsWidget::dictionariesCheckBoxChanged(bool boolean){
100     dictionariesCheckBox=boolean;
101     changed();
102 }
103
104 void SettingsWidget::bookmarksCheckBoxChanged(bool boolean){
105     bookmarksCheckBox=boolean;
106     changed();
107 }
108
109 void SettingsWidget::initalizeUI() {
110 #ifndef Q_WS_MAEMO_5
111     emit setMinHistory(1);
112     emit setMaxHistory(50);
113     emit setMinHistory(0);
114     emit setMaxHistory(500);
115     settings = 0;
116 #else
117     verticalLayout = new QVBoxLayout;
118     historySizeLayout = new QHBoxLayout;
119     historySizeSpinBox = new SpinBox;
120     searchResultLayout = new QHBoxLayout;
121     searchResultSizeSpinBox = new SpinBox;
122     searchInBookmarksCheckBox = new QCheckBox(tr("Bookmarks"));
123     searchInDictionariesCheckBox = new QCheckBox(tr("Dictionaries"));
124
125     historySizeSpinBox->setMinimum(1);
126     historySizeSpinBox->setMaximum(50);
127     historySizeToolTip = tr("Limits maximum number of words saved in history");
128     historySizeLayout->addWidget(historySizeSpinBox);
129
130     #ifdef Q_WS_MAEMO_5
131         historySizeInfoToolButton = new QToolButton;
132         historySizeInfoToolButton->setIcon(QIcon::fromTheme("general_information"));
133         historySizeLayout->addWidget(historySizeInfoToolButton);
134         searchResultSizeInfoToolButton = new QToolButton;
135         searchResultSizeInfoToolButton->setIcon(QIcon::fromTheme("general_information"));
136     #endif
137
138     searchResultSizeSpinBox->setMinimum(0);
139     searchResultSizeSpinBox->setMaximum(500);
140     searchResultSizeSpinBox->setSpecialValueText(tr("Unlimited"));
141     searchResultSizeToolTip = tr("Limits maximum number of found words, affects"
142                                  " only when searching in file.");
143     searchResultLayout->addWidget(searchResultSizeSpinBox);
144
145     #ifdef Q_WS_MAEMO_5
146         searchResultLayout->addWidget(searchResultSizeInfoToolButton);
147     #endif
148
149     spinBoxesFormLayout = new QFormLayout;
150     spinBoxesFormLayout->addRow(tr("Search result size"), searchResultLayout);
151     spinBoxesFormLayout->addRow(tr("History size"), historySizeLayout);
152
153     verticalLayout->addLayout(spinBoxesFormLayout);
154     checkBoxesLabel = new QLabel(tr("Search in:"),this);
155
156     verticalLayout->addSpacing(20);
157     verticalLayout->addWidget(checkBoxesLabel);
158     verticalLayout->addWidget(searchInDictionariesCheckBox);
159     verticalLayout->addWidget(searchInBookmarksCheckBox);
160
161     setLayout(verticalLayout);
162
163     connect(historySizeSpinBox, SIGNAL(valueChanged(int)),
164             this,SLOT(changed()));
165     connect(searchResultSizeSpinBox, SIGNAL(valueChanged(int)),
166             this,SLOT(changed()));
167     connect(searchInDictionariesCheckBox, SIGNAL(toggled(bool)),
168             this,SLOT(changed()));
169     connect(searchInBookmarksCheckBox, SIGNAL(toggled(bool)),
170             this,SLOT(changed()));
171
172     #ifdef Q_WS_MAEMO_5
173         connect(searchResultSizeInfoToolButton, SIGNAL(clicked()),
174                 this, SLOT(showSearchResultSizeInfo()));
175         connect(historySizeInfoToolButton, SIGNAL(clicked()),
176                 this, SLOT(showHistorySizeInfo()));
177     #else
178         historySizeSpinBox->setToolTip(historySizeToolTip);
179         searchResultSizeSpinBox->setToolTip(searchResultSizeToolTip);
180     #endif
181
182
183     settings = 0;
184
185     #ifndef Q_WS_MAEMO_5
186         setMinimumWidth(250);
187         setMaximumWidth(250);
188         footerLayout = new QHBoxLayout;
189         closeButton = new QPushButton(tr("Save"));
190         footerLayout->addStretch(0);
191         footerLayout->addWidget(closeButton);
192         verticalLayout->addLayout(footerLayout);
193
194         setMinimumHeight(sizeHint().height());
195         setMaximumHeight(sizeHint().height());
196         connect(closeButton, SIGNAL(clicked()), this, SLOT(save()));
197     #endif
198 #endif
199 }
200
201 void SettingsWidget::showEvent(QShowEvent *e) {
202
203 #ifndef Q_WS_MAEMO_5
204     settings = guiInterface->settings();
205
206     emit setValueHistory(settings->value("history_size").toInt());
207     emit setValueSearchResult(settings->value("search_limit").toInt());
208
209     if(settings->value("search_bookmarks") == "true")
210         emit setCheckedBookmarks(true);
211     else
212         emit setCheckedBookmarks(false);
213
214     if(settings->value("search_dictionaries") == "true")
215         emit setCheckedDictionaries(true);
216     else
217         emit setCheckedDictionaries(false);
218
219     QDialog::showEvent(e);
220 #else
221    #ifndef Q_WS_MAEMO_5
222        _save = false;
223    #endif
224
225    settings = guiInterface->settings();
226
227    historySizeSpinBox->setValue(
228             settings->value("history_size").toInt());
229
230     searchResultSizeSpinBox->setValue(
231             settings->value("search_limit").toInt());
232
233     if(settings->value("search_bookmarks") == "true")
234         searchInBookmarksCheckBox->setChecked(true);
235     else
236         searchInBookmarksCheckBox->setChecked(false);
237
238     if(settings->value("search_dictionaries") == "true")
239         searchInDictionariesCheckBox->setChecked(true);
240     else
241         searchInDictionariesCheckBox->setChecked(false);
242     QDialog::showEvent(e);
243 #endif
244     _changed = false;
245 }
246
247 void SettingsWidget::hideEvent(QHideEvent *e) {
248     QDialog::hideEvent(e);
249
250 #ifndef Q_WS_MAEMO_5
251     if(settings && _save) {
252         Settings* newSettings = new Settings;
253         newSettings->setValue("history_size",QString::number(historyValue));
254         newSettings->setValue("search_limit",QString::number(searchResultValue));
255         if(dictionariesCheckBox==true)
256             newSettings->setValue("search_dictionaries", "true");
257         else
258             newSettings->setValue("search_dictionaries", "false");
259
260         if(bookmarksCheckBox==true)
261             newSettings->setValue("search_bookmarks", "true");
262         else
263             newSettings->setValue("search_bookmarks", "false");
264         foreach(QString key, newSettings->keys()) {
265             if(settings->value(key) != newSettings->value(key)) {
266                 guiInterface->setSettings(newSettings);
267                 break;
268             }
269         }
270         _changed = false;
271         if(settings) {
272             delete settings;
273             settings = 0;
274         }
275     }
276 #else
277     if(settings && _changed &&
278        QMessageBox::question(this,
279                              tr("Save"),
280                              tr("Do you want to save changes?"),
281         QMessageBox::Save, QMessageBox::Cancel) == QMessageBox::Save) {
282
283         Settings* newSettings = new Settings;
284         newSettings->setValue("history_size",
285                               QString::number(historySizeSpinBox->value()));
286         newSettings->setValue("search_limit",
287                               QString::number(
288                                       searchResultSizeSpinBox->value()));
289
290         if(searchInDictionariesCheckBox->isChecked())
291             newSettings->setValue("search_dictionaries", "true");
292         else
293             newSettings->setValue("search_dictionaries", "false");
294
295         if(searchInBookmarksCheckBox->isChecked())
296             newSettings->setValue("search_bookmarks", "true");
297         else
298             newSettings->setValue("search_bookmarks", "false");
299
300         //setting new settings only if they are different than old ones
301         foreach(QString key, newSettings->keys()) {
302             if(settings->value(key) != newSettings->value(key)) {
303                 guiInterface->setSettings(newSettings);
304                 break;
305             }
306         }
307     }
308     if(settings) {
309         delete settings;
310         settings = 0;
311     }
312     _changed = false;
313 #endif
314 }
315
316
317 void SettingsWidget::changed() {
318     _changed = true;
319 }
320
321 #ifndef Q_WS_MAEMO_5
322     void SettingsWidget::save() {
323         _save = true;
324         hide();
325     }
326 #endif
327
328
329 #ifdef Q_WS_MAEMO_5
330     void SettingsWidget::showHistorySizeInfo() {
331         Q_EMIT notify(Notify::Warning, historySizeToolTip);
332     }
333
334     void SettingsWidget::showSearchResultSizeInfo() {
335         Q_EMIT notify(Notify::Warning, searchResultSizeToolTip);
336     }
337 #endif