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