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