Few small fixes for confirmation system
[mdictionary] / trunk / src / base / gui / SettingsWidget.cpp
index f0de440..e20ffdd 100644 (file)
 //Created by Mateusz Półrola
 
 #include "SettingsWidget.h"
+#include <QDebug>
 
 SettingsWidget::SettingsWidget(GUIInterface *parent) :
     QDialog(parent)
 {
+    guiInterface = parent;
+
     setWindowTitle(tr("Settings"));
 
     verticalLayout = new QVBoxLayout;
@@ -42,6 +45,9 @@ SettingsWidget::SettingsWidget(GUIInterface *parent) :
     spinBoxesFormLayout->addRow(tr("History size"),
                                 historySizeSpinBox);
 
+    searchResultSizeSpinBox->setMinimum(1);
+    historySizeSpinBox->setMinimum(1);
+
     #ifdef Q_WS_MAEMO_5
         verticalLayout->addSpacing(20);
     #endif
@@ -58,16 +64,39 @@ SettingsWidget::SettingsWidget(GUIInterface *parent) :
     verticalLayout->addWidget(searchInDictionariesCheckBox);
     verticalLayout->addWidget(searchInBookmarksCheckBox);
 
+
+    connect(historySizeSpinBox, SIGNAL(valueChanged(int)), this,
+            SLOT(changed()));
+    connect(searchResultSizeSpinBox, SIGNAL(valueChanged(int)), this,
+            SLOT(changed()));
+    connect(searchInDictionariesCheckBox, SIGNAL(toggled(bool)), this,
+            SLOT(changed()));
+    connect(searchInBookmarksCheckBox, SIGNAL(toggled(bool)), this,
+            SLOT(changed()));
+
+
+    settings = 0;
+
     #ifndef Q_WS_MAEMO_5
         setMinimumWidth(250);
         setMaximumWidth(250);
+        footerLayout = new QHBoxLayout(this);
+        closeButton = new QPushButton(tr("Save"));
+        footerLayout->addStretch(0);
+        footerLayout->addWidget(closeButton);
+        verticalLayout->addLayout(footerLayout);
+        connect(closeButton, SIGNAL(clicked()), this, SLOT(save()));
     #endif
 }
 
 void SettingsWidget::showEvent(QShowEvent *e) {
-    settings = guiInterface->getSettings();
 
-    historySizeSpinBox->setValue(
+   #ifndef Q_WS_MAEMO_5
+       _save = false;
+   #endif
+   settings = guiInterface->settings();
+
+   historySizeSpinBox->setValue(
             settings->value("history_size").toInt());
 
     searchResultSizeSpinBox->setValue(
@@ -83,33 +112,63 @@ void SettingsWidget::showEvent(QShowEvent *e) {
     else
         searchInDictionariesCheckBox->setChecked(false);
 
+    _changed = false;
     QDialog::showEvent(e);
 }
 
 void SettingsWidget::hideEvent(QHideEvent *e) {
-    Settings* newSettings;
-    newSettings->setValue("history_size",
-                          QString::number(historySizeSpinBox->value()));
-    newSettings->setValue("search_limit",
-                          QString::number(searchResultSizeSpinBox->value()));
-
-    if(searchInDictionariesCheckBox->isChecked())
-        newSettings->setValue("search_dictionaries", "true");
-    else
-        newSettings->setValue("search_dictionaries", "false");
+    QDialog::hideEvent(e);
 
-    if(searchInBookmarksCheckBox->isChecked())
-        newSettings->setValue("search_bookmarks", "true");
-    else
-        newSettings->setValue("search_bookmarks", "false");
+    #ifndef Q_WS_MAEMO_5
+    if(settings && _save) {
+    #else
+    if(settings && _changed &&
+            QMessageBox::question(this, "Save", "Do you want to save changes?",
+             QMessageBox::Save, QMessageBox::Cancel) == QMessageBox::Save) {
 
-    QString key;
-    foreach(key, newSettings->keys()) {
-        if(settings->value(key) != newSettings->value(key)) {
-            guiInterface->setSettings(newSettings);
-            break;
+    #endif
+        Settings* newSettings = new Settings;
+        newSettings->setValue("history_size",
+                              QString::number(historySizeSpinBox->value()));
+        newSettings->setValue("search_limit",
+                              QString::number(
+                                      searchResultSizeSpinBox->value()));
+
+        if(searchInDictionariesCheckBox->isChecked())
+            newSettings->setValue("search_dictionaries", "true");
+        else
+            newSettings->setValue("search_dictionaries", "false");
+
+        if(searchInBookmarksCheckBox->isChecked())
+            newSettings->setValue("search_bookmarks", "true");
+        else
+            newSettings->setValue("search_bookmarks", "false");
+
+        //setting new settings only if their are different that old ones
+        QString key;
+        foreach(key, newSettings->keys()) {
+            if(settings->value(key) != newSettings->value(key)) {
+                guiInterface->setSettings(newSettings);
+                break;
+            }
         }
+
     }
+    if(settings) {
+        delete settings;
+        settings = 0;
+    }
+    _changed = false;
+}
 
-    QDialog::hideEvent(e);
+
+void SettingsWidget::changed() {
+    _changed = true;
 }
+
+#ifndef Q_WS_MAEMO_5
+    void SettingsWidget::save() {
+        _save = true;
+        hide();
+    }
+#endif