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