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