Added save confirm for general settings
[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;
35     setLayout(verticalLayout);
36
37     historySizeSpinBox = new QSpinBox;
38     searchResultSizeSpinBox = new QSpinBox;
39
40     spinBoxesFormLayout = new QFormLayout;
41
42     spinBoxesFormLayout->addRow(tr("Search result size"),
43                                 searchResultSizeSpinBox);
44
45     spinBoxesFormLayout->addRow(tr("History size"),
46                                 historySizeSpinBox);
47
48     searchResultSizeSpinBox->setMinimum(1);
49     historySizeSpinBox->setMinimum(1);
50
51     #ifdef Q_WS_MAEMO_5
52         verticalLayout->addSpacing(20);
53     #endif
54     verticalLayout->addLayout(spinBoxesFormLayout);
55
56
57     checkBoxesLabel = new QLabel(tr("Search in:"));
58
59     searchInBookmarksCheckBox = new QCheckBox(tr("Bookmarks"));
60     searchInDictionariesCheckBox = new QCheckBox(tr("Dictionaries"));
61
62     verticalLayout->addSpacing(20);
63     verticalLayout->addWidget(checkBoxesLabel);
64     verticalLayout->addWidget(searchInDictionariesCheckBox);
65     verticalLayout->addWidget(searchInBookmarksCheckBox);
66
67
68     settings = 0;
69
70     #ifndef Q_WS_MAEMO_5
71         setMinimumWidth(250);
72         setMaximumWidth(250);
73         footerLayout = new QHBoxLayout(this);
74         closeButton = new QPushButton(tr("Ok"));
75         footerLayout->addStretch(0);
76         footerLayout->addWidget(closeButton);
77         verticalLayout->addLayout(footerLayout);
78         connect(closeButton, SIGNAL(clicked()), this, SLOT(save()));
79     #endif
80 }
81
82 void SettingsWidget::showEvent(QShowEvent *e) {
83
84    #ifndef Q_WS_MAEMO_5
85        _save = false;
86    #endif
87    settings = guiInterface->settings();
88
89    historySizeSpinBox->setValue(
90             settings->value("history_size").toInt());
91
92     searchResultSizeSpinBox->setValue(
93             settings->value("search_limit").toInt());
94
95     if(settings->value("search_bookmarks") == "true")
96         searchInBookmarksCheckBox->setChecked(true);
97     else
98         searchInBookmarksCheckBox->setChecked(false);
99
100     if(settings->value("search_dictionaries") == "true")
101         searchInDictionariesCheckBox->setChecked(true);
102     else
103         searchInDictionariesCheckBox->setChecked(false);
104
105     QDialog::showEvent(e);
106 }
107
108 void SettingsWidget::hideEvent(QHideEvent *e) {
109     QDialog::hideEvent(e);
110
111     #ifndef Q_WS_MAEMO_5
112     if(settings && _save) {
113     #else
114     if(settings &&
115             QMessageBox::question(this, "Save", "Do you want to save changes?",
116              QMessageBox::Save, QMessageBox::Cancel) == QMessageBox::Save) {
117
118     #endif
119         Settings* newSettings = new Settings;
120         newSettings->setValue("history_size",
121                               QString::number(historySizeSpinBox->value()));
122         newSettings->setValue("search_limit",
123                               QString::number(
124                                       searchResultSizeSpinBox->value()));
125
126         if(searchInDictionariesCheckBox->isChecked())
127             newSettings->setValue("search_dictionaries", "true");
128         else
129             newSettings->setValue("search_dictionaries", "false");
130
131         if(searchInBookmarksCheckBox->isChecked())
132             newSettings->setValue("search_bookmarks", "true");
133         else
134             newSettings->setValue("search_bookmarks", "false");
135
136         //setting new settings only if their are different that old ones
137         QString key;
138         foreach(key, newSettings->keys()) {
139             if(settings->value(key) != newSettings->value(key)) {
140                 guiInterface->setSettings(newSettings);
141                 break;
142             }
143         }
144
145     }
146     if(settings) {
147         delete settings;
148         settings = 0;
149     }
150 }
151
152
153 #ifndef Q_WS_MAEMO_5
154     void SettingsWidget::save() {
155         _save = true;
156         hide();
157     }
158 #endif