10566441dd7ab4408d030ea41e3f6dda57446b05
[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     historySizeSpinBox = new QSpinBox;
43     searchResultSizeSpinBox = new QSpinBox;
44     limitTip = tr("Limits maximum number of found words, affects only when "
45                 "searching in file.");
46     searchResultSizeSpinBox->setToolTip(limitTip);
47
48
49     spinBoxesFormLayout = new QFormLayout;
50     spinBoxesFormLayout->addRow(tr("Search result size"),
51                                 searchResultSizeSpinBox);
52     spinBoxesFormLayout->addRow(tr("History size"),
53                                 historySizeSpinBox);
54
55     searchResultSizeSpinBox->setMinimum(0);
56     searchResultSizeSpinBox->setSpecialValueText(tr("Unlimited"));
57     historySizeSpinBox->setMinimum(1);
58
59     searchResultSizeSpinBox->setMaximum(500);
60     historySizeSpinBox->setMaximum(50);
61
62     #ifdef Q_WS_MAEMO_5
63         verticalLayout->addSpacing(20);
64     #endif
65     verticalLayout->addLayout(spinBoxesFormLayout);
66
67
68     checkBoxesLabel = new QLabel(tr("Search in:"),this);
69
70     searchInBookmarksCheckBox = new QCheckBox(tr("Bookmarks"),this);
71     searchInDictionariesCheckBox = new QCheckBox(tr("Dictionaries"),this);
72
73     verticalLayout->addSpacing(20);
74     verticalLayout->addWidget(checkBoxesLabel);
75     verticalLayout->addWidget(searchInDictionariesCheckBox);
76     verticalLayout->addWidget(searchInBookmarksCheckBox);
77
78
79     connect(historySizeSpinBox, SIGNAL(valueChanged(int)), this,
80             SLOT(changed()));
81     connect(searchResultSizeSpinBox, SIGNAL(valueChanged(int)), this,
82             SLOT(changed()));
83     connect(searchInDictionariesCheckBox, SIGNAL(toggled(bool)), this,
84             SLOT(changed()));
85     connect(searchInBookmarksCheckBox, SIGNAL(toggled(bool)), this,
86             SLOT(changed()));
87
88
89     settings = 0;
90
91     #ifndef Q_WS_MAEMO_5
92         setMinimumWidth(250);
93         setMaximumWidth(250);
94         footerLayout = new QHBoxLayout;
95         closeButton = new QPushButton(tr("Save"));
96         footerLayout->addStretch(0);
97         footerLayout->addWidget(closeButton);
98         verticalLayout->addLayout(footerLayout);
99         connect(closeButton, SIGNAL(clicked()), this, SLOT(save()));
100     #endif
101 }
102
103 void SettingsWidget::showEvent(QShowEvent *e) {
104
105    #ifndef Q_WS_MAEMO_5
106        _save = false;
107    #endif
108
109    settings = guiInterface->settings();
110
111    historySizeSpinBox->setValue(
112             settings->value("history_size").toInt());
113
114     searchResultSizeSpinBox->setValue(
115             settings->value("search_limit").toInt());
116
117     if(settings->value("search_bookmarks") == "true")
118         searchInBookmarksCheckBox->setChecked(true);
119     else
120         searchInBookmarksCheckBox->setChecked(false);
121
122     if(settings->value("search_dictionaries") == "true")
123         searchInDictionariesCheckBox->setChecked(true);
124     else
125         searchInDictionariesCheckBox->setChecked(false);
126
127     _changed = false;
128     QDialog::showEvent(e);
129 }
130
131 void SettingsWidget::hideEvent(QHideEvent *e) {
132     QDialog::hideEvent(e);
133
134     #ifndef Q_WS_MAEMO_5
135         if(settings && _save) {
136     #else
137         if(settings && _changed &&
138            QMessageBox::question(this,
139                                  tr("Save"),
140                                  tr("Do you want to save changes?"),
141              QMessageBox::Save, QMessageBox::Cancel) == QMessageBox::Save) {
142
143     #endif
144         Settings* newSettings = new Settings;
145         newSettings->setValue("history_size",
146                               QString::number(historySizeSpinBox->value()));
147         newSettings->setValue("search_limit",
148                               QString::number(
149                                       searchResultSizeSpinBox->value()));
150
151         if(searchInDictionariesCheckBox->isChecked())
152             newSettings->setValue("search_dictionaries", "true");
153         else
154             newSettings->setValue("search_dictionaries", "false");
155
156         if(searchInBookmarksCheckBox->isChecked())
157             newSettings->setValue("search_bookmarks", "true");
158         else
159             newSettings->setValue("search_bookmarks", "false");
160
161         //setting new settings only if they are different than old ones
162         QString key;
163         foreach(key, newSettings->keys()) {
164             if(settings->value(key) != newSettings->value(key)) {
165                 guiInterface->setSettings(newSettings);
166                 break;
167             }
168         }
169
170     }
171     if(settings) {
172         delete settings;
173         settings = 0;
174     }
175     _changed = false;
176 }
177
178
179 void SettingsWidget::changed() {
180     _changed = true;
181 }
182
183 #ifndef Q_WS_MAEMO_5
184     void SettingsWidget::save() {
185         _save = true;
186         hide();
187     }
188 #endif