Added tooltip for search limit
[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 //Created by Mateusz Półrola
23
24 #include "SettingsWidget.h"
25 #include <QDebug>
26
27 SettingsWidget::SettingsWidget(GUIInterface *parent) :
28     QDialog(parent)
29 {
30     guiInterface = parent;
31
32     setWindowTitle(tr("Settings"));
33
34     verticalLayout = new QVBoxLayout(this);
35     setLayout(verticalLayout);
36
37     historySizeSpinBox = new QSpinBox(this);
38     searchResultSizeSpinBox = new QSpinBox(this);
39     limitTip = "Limit maximal number of finded words, affect only when "
40                 "searching in file.";
41     searchResultSizeSpinBox->setToolTip(limitTip);
42
43     spinBoxesFormLayout = new QFormLayout(this);
44
45     spinBoxesFormLayout->addRow(tr("Search result size"),
46                                 searchResultSizeSpinBox);
47
48     spinBoxesFormLayout->addRow(tr("History size"),
49                                 historySizeSpinBox);
50
51     searchResultSizeSpinBox->setMinimum(0);
52     historySizeSpinBox->setMinimum(1);
53
54     #ifdef Q_WS_MAEMO_5
55         verticalLayout->addSpacing(20);
56     #endif
57     verticalLayout->addLayout(spinBoxesFormLayout);
58
59
60     checkBoxesLabel = new QLabel(tr("Search in:"),this);
61
62     searchInBookmarksCheckBox = new QCheckBox(tr("Bookmarks"),this);
63     searchInDictionariesCheckBox = new QCheckBox(tr("Dictionaries"),this);
64
65     verticalLayout->addSpacing(20);
66     verticalLayout->addWidget(checkBoxesLabel);
67     verticalLayout->addWidget(searchInDictionariesCheckBox);
68     verticalLayout->addWidget(searchInBookmarksCheckBox);
69
70
71     connect(historySizeSpinBox, SIGNAL(valueChanged(int)), this,
72             SLOT(changed()));
73     connect(searchResultSizeSpinBox, SIGNAL(valueChanged(int)), this,
74             SLOT(changed()));
75     connect(searchInDictionariesCheckBox, SIGNAL(toggled(bool)), this,
76             SLOT(changed()));
77     connect(searchInBookmarksCheckBox, SIGNAL(toggled(bool)), this,
78             SLOT(changed()));
79
80
81     settings = 0;
82
83     #ifndef Q_WS_MAEMO_5
84         setMinimumWidth(250);
85         setMaximumWidth(250);
86         footerLayout = new QHBoxLayout(this);
87         closeButton = new QPushButton(tr("Save"));
88         footerLayout->addStretch(0);
89         footerLayout->addWidget(closeButton);
90         verticalLayout->addLayout(footerLayout);
91         connect(closeButton, SIGNAL(clicked()), this, SLOT(save()));
92     #endif
93 }
94
95 void SettingsWidget::showEvent(QShowEvent *e) {
96
97    #ifndef Q_WS_MAEMO_5
98        _save = false;
99    #endif
100    settings = guiInterface->settings();
101
102    historySizeSpinBox->setValue(
103             settings->value("history_size").toInt());
104
105     searchResultSizeSpinBox->setValue(
106             settings->value("search_limit").toInt());
107
108     if(settings->value("search_bookmarks") == "true")
109         searchInBookmarksCheckBox->setChecked(true);
110     else
111         searchInBookmarksCheckBox->setChecked(false);
112
113     if(settings->value("search_dictionaries") == "true")
114         searchInDictionariesCheckBox->setChecked(true);
115     else
116         searchInDictionariesCheckBox->setChecked(false);
117
118     _changed = false;
119     QDialog::showEvent(e);
120 }
121
122 void SettingsWidget::hideEvent(QHideEvent *e) {
123     QDialog::hideEvent(e);
124
125     #ifndef Q_WS_MAEMO_5
126     if(settings && _save) {
127     #else
128     if(settings && _changed &&
129             QMessageBox::question(this, "Save", "Do you want to save changes?",
130              QMessageBox::Save, QMessageBox::Cancel) == QMessageBox::Save) {
131
132     #endif
133         Settings* newSettings = new Settings;
134         newSettings->setValue("history_size",
135                               QString::number(historySizeSpinBox->value()));
136         newSettings->setValue("search_limit",
137                               QString::number(
138                                       searchResultSizeSpinBox->value()));
139
140         if(searchInDictionariesCheckBox->isChecked())
141             newSettings->setValue("search_dictionaries", "true");
142         else
143             newSettings->setValue("search_dictionaries", "false");
144
145         if(searchInBookmarksCheckBox->isChecked())
146             newSettings->setValue("search_bookmarks", "true");
147         else
148             newSettings->setValue("search_bookmarks", "false");
149
150         //setting new settings only if their are different that old ones
151         QString key;
152         foreach(key, newSettings->keys()) {
153             if(settings->value(key) != newSettings->value(key)) {
154                 guiInterface->setSettings(newSettings);
155                 break;
156             }
157         }
158
159     }
160     if(settings) {
161         delete settings;
162         settings = 0;
163     }
164     _changed = false;
165 }
166
167
168 void SettingsWidget::changed() {
169     _changed = true;
170 }
171
172 #ifndef Q_WS_MAEMO_5
173     void SettingsWidget::save() {
174         _save = true;
175         hide();
176     }
177 #endif