add setingWidget Qml
[mdictionary] / src / mdictionary / gui / SettingsWidget.cpp
index 6bd634e..b8b3c4d 100644 (file)
 
 *******************************************************************************/
 
-//! \file SettingsWidget.cpp
-//! \author Mateusz Półrola <mateusz.polrola@comarch.pl>
+/*! \file DictManagerWidget.cpp
+    \brief Implements applications settings widget
+    \author Mateusz Półrola <mateusz.polrola@comarch.pl>
+*/
 
 #include "SettingsWidget.h"
 #include <QDebug>
 
-SettingsWidget::SettingsWidget(GUIInterface *parent) :
-    QDialog(parent)
-{
-    guiInterface = parent;
+SettingsWidget::SettingsWidget(GUIInterface *parent) : QDialog(parent) {
 
+guiInterface = parent;
+
+#ifndef Q_WS_MAEMO_5
+
+    view= new QDeclarativeView();
+    view->setSource(QUrl::fromLocalFile("/usr/share/mdictionary/qml/SettingsWidget.qml"));
+    view->setResizeMode(QDeclarativeView::SizeRootObjectToView);
+    view->setAlignment(Qt::AlignCenter);
+    view->show();
+
+    view->setMinimumWidth(250);
+    view->setMaximumWidth(250);
+    view->setMinimumHeight(view->sizeHint().height());
+    view->setMaximumHeight(view->sizeHint().height());
+
+    mainLayout = new QVBoxLayout;
+    mainLayout->addWidget(view);
+    setLayout(mainLayout);
+    view->setWindowTitle(tr("Settings"));
+
+    QGraphicsObject *rootObject = view->rootObject();
+
+    connect(this, SIGNAL(setMinHistory(QVariant)),
+           rootObject, SLOT(setMinHistory(QVariant)));
+    connect(this, SIGNAL(setValueHistory(QVariant)),
+           rootObject, SLOT(setValueHistory(QVariant)));
+    connect(this, SIGNAL(setMaxHistory(QVariant)),
+           rootObject, SLOT(setMaxHistory(QVariant)));
+    connect(this, SIGNAL(setMinSearchResult(QVariant)),
+           rootObject, SLOT(setMinSearchResult(QVariant)));
+    connect(this, SIGNAL(setValueSearchResult(QVariant)),
+           rootObject, SLOT(setValueSearchResult(QVariant)));
+    connect(this, SIGNAL(setMaxSearchResult(QVariant)),
+           rootObject, SLOT(setMaxSearchResult(QVariant)));
+    connect(this, SIGNAL(setCheckedBookmarks(QVariant)),
+           rootObject, SLOT(setCheckedBookmarks(QVariant)));
+    connect(this, SIGNAL(setCheckedDictionaries(QVariant)),
+           rootObject, SLOT(setCheckedDictionaries(QVariant)));
+
+    connect(rootObject, SIGNAL(historySizeValueChanged(int)),
+           this, SLOT(historySizeValueChanged(int)));
+    connect(rootObject, SIGNAL(searchResulValueChanged(int)),
+           this, SLOT(searchResulValueChanged(int)));
+    connect(rootObject, SIGNAL(dictionariesCheckBoxChanged(bool)),
+           this, SLOT(dictionariesCheckBoxChanged(bool)));
+    connect(rootObject, SIGNAL(bookmarksCheckBoxChanged(bool)),
+           this, SLOT(bookmarksCheckBoxChanged(bool)));
+    connect(rootObject, SIGNAL(saveButtonClicked()),
+           this, SLOT(save()));
+
+#else
     setWindowTitle(tr("Settings"));
+    setModal(true);
+#endif
 
     initalizeUI();
 }
 
+void SettingsWidget::historySizeValueChanged(int value){
+    historyValue = value;
+    changed();
+}
+
+void SettingsWidget::searchResulValueChanged(int value){
+    searchResultValue=value;
+    changed();
+}
+
+void SettingsWidget::dictionariesCheckBoxChanged(bool boolean){
+    dictionariesCheckBox=boolean;
+    changed();
+}
+
+void SettingsWidget::bookmarksCheckBoxChanged(bool boolean){
+    bookmarksCheckBox=boolean;
+    changed();
+}
+
 void SettingsWidget::initalizeUI() {
+#ifndef Q_WS_MAEMO_5
+    emit setMinHistory(1);
+    emit setMaxHistory(50);
+    emit setMinHistory(0);
+    emit setMaxHistory(500);
+    settings = 0;
+#else
     verticalLayout = new QVBoxLayout;
-    setLayout(verticalLayout);
-
     historySizeLayout = new QHBoxLayout;
-
     historySizeSpinBox = new SpinBox;
+    searchResultLayout = new QHBoxLayout;
+    searchResultSizeSpinBox = new SpinBox;
+    searchInBookmarksCheckBox = new QCheckBox(tr("Bookmarks"));
+    searchInDictionariesCheckBox = new QCheckBox(tr("Dictionaries"));
+
     historySizeSpinBox->setMinimum(1);
     historySizeSpinBox->setMaximum(50);
-
     historySizeToolTip = tr("Limits maximum number of words saved in history");
+    historySizeLayout->addWidget(historySizeSpinBox);
 
     #ifdef Q_WS_MAEMO_5
         historySizeInfoToolButton = new QToolButton;
         historySizeInfoToolButton->setIcon(QIcon::fromTheme("general_information"));
-    #endif
-
-    historySizeLayout->addWidget(historySizeSpinBox);
-    #ifdef Q_WS_MAEMO_5
         historySizeLayout->addWidget(historySizeInfoToolButton);
+        searchResultSizeInfoToolButton = new QToolButton;
+        searchResultSizeInfoToolButton->setIcon(QIcon::fromTheme("general_information"));
     #endif
 
-
-
-
-    searchResultLayout = new QHBoxLayout;
-
-    searchResultSizeSpinBox = new SpinBox;
     searchResultSizeSpinBox->setMinimum(0);
     searchResultSizeSpinBox->setMaximum(500);
     searchResultSizeSpinBox->setSpecialValueText(tr("Unlimited"));
-
     searchResultSizeToolTip = tr("Limits maximum number of found words, affects"
-                                 "only when searching in file.");
-
-    #ifdef Q_WS_MAEMO_5
-        searchResultSizeInfoToolButton = new QToolButton;
-        searchResultSizeInfoToolButton->setIcon(QIcon::fromTheme("general_information"));
-    #endif
-
+                                 " only when searching in file.");
     searchResultLayout->addWidget(searchResultSizeSpinBox);
+
     #ifdef Q_WS_MAEMO_5
         searchResultLayout->addWidget(searchResultSizeInfoToolButton);
     #endif
 
-
-
-
-
     spinBoxesFormLayout = new QFormLayout;
-    spinBoxesFormLayout->addRow(tr("Search result size"),
-                                searchResultLayout);
-    spinBoxesFormLayout->addRow(tr("History size"),
-                                historySizeLayout);
-
-
-
+    spinBoxesFormLayout->addRow(tr("Search result size"), searchResultLayout);
+    spinBoxesFormLayout->addRow(tr("History size"), historySizeLayout);
 
     verticalLayout->addLayout(spinBoxesFormLayout);
-
-
     checkBoxesLabel = new QLabel(tr("Search in:"),this);
 
-    searchInBookmarksCheckBox = new QCheckBox(tr("Bookmarks"),this);
-    searchInDictionariesCheckBox = new QCheckBox(tr("Dictionaries"),this);
-
     verticalLayout->addSpacing(20);
     verticalLayout->addWidget(checkBoxesLabel);
     verticalLayout->addWidget(searchInDictionariesCheckBox);
     verticalLayout->addWidget(searchInBookmarksCheckBox);
 
+    setLayout(verticalLayout);
 
-    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()));
-
+    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()));
 
-#ifdef Q_WS_MAEMO_5
+    #ifdef Q_WS_MAEMO_5
         connect(searchResultSizeInfoToolButton, SIGNAL(clicked()),
                 this, SLOT(showSearchResultSizeInfo()));
-
         connect(historySizeInfoToolButton, SIGNAL(clicked()),
                 this, SLOT(showHistorySizeInfo()));
-#else
+    #else
         historySizeSpinBox->setToolTip(historySizeToolTip);
         searchResultSizeSpinBox->setToolTip(searchResultSizeToolTip);
-#endif
+    #endif
 
 
     settings = 0;
@@ -139,12 +191,34 @@ void SettingsWidget::initalizeUI() {
         footerLayout->addStretch(0);
         footerLayout->addWidget(closeButton);
         verticalLayout->addLayout(footerLayout);
+
+        setMinimumHeight(sizeHint().height());
+        setMaximumHeight(sizeHint().height());
         connect(closeButton, SIGNAL(clicked()), this, SLOT(save()));
     #endif
+#endif
 }
 
 void SettingsWidget::showEvent(QShowEvent *e) {
 
+#ifndef Q_WS_MAEMO_5
+    settings = guiInterface->settings();
+
+    emit setValueHistory(settings->value("history_size").toInt());
+    emit setValueSearchResult(settings->value("search_limit").toInt());
+
+    if(settings->value("search_bookmarks") == "true")
+        emit setCheckedBookmarks(true);
+    else
+        emit setCheckedBookmarks(false);
+
+    if(settings->value("search_dictionaries") == "true")
+        emit setCheckedDictionaries(true);
+    else
+        emit setCheckedDictionaries(false);
+
+    QDialog::showEvent(e);
+#else
    #ifndef Q_WS_MAEMO_5
        _save = false;
    #endif
@@ -166,24 +240,47 @@ void SettingsWidget::showEvent(QShowEvent *e) {
         searchInDictionariesCheckBox->setChecked(true);
     else
         searchInDictionariesCheckBox->setChecked(false);
-
-    _changed = false;
     QDialog::showEvent(e);
+#endif
+    _changed = false;
 }
 
 void SettingsWidget::hideEvent(QHideEvent *e) {
     QDialog::hideEvent(e);
 
-    #ifndef Q_WS_MAEMO_5
-        if(settings && _save) {
-    #else
-        if(settings && _changed &&
-           QMessageBox::question(this,
-                                 tr("Save"),
-                                 tr("Do you want to save changes?"),
-             QMessageBox::Save, QMessageBox::Cancel) == QMessageBox::Save) {
+#ifndef Q_WS_MAEMO_5
+    if(settings && _save) {
+        Settings* newSettings = new Settings;
+        newSettings->setValue("history_size",QString::number(historyValue));
+        newSettings->setValue("search_limit",QString::number(searchResultValue));
+        if(dictionariesCheckBox==true)
+            newSettings->setValue("search_dictionaries", "true");
+        else
+            newSettings->setValue("search_dictionaries", "false");
+
+        if(bookmarksCheckBox==true)
+            newSettings->setValue("search_bookmarks", "true");
+        else
+            newSettings->setValue("search_bookmarks", "false");
+        foreach(QString key, newSettings->keys()) {
+            if(settings->value(key) != newSettings->value(key)) {
+                guiInterface->setSettings(newSettings);
+                break;
+            }
+        }
+        _changed = false;
+        if(settings) {
+            delete settings;
+            settings = 0;
+        }
+    }
+#else
+    if(settings && _changed &&
+       QMessageBox::question(this,
+                             tr("Save"),
+                             tr("Do you want to save changes?"),
+        QMessageBox::Save, QMessageBox::Cancel) == QMessageBox::Save) {
 
-    #endif
         Settings* newSettings = new Settings;
         newSettings->setValue("history_size",
                               QString::number(historySizeSpinBox->value()));
@@ -202,20 +299,19 @@ void SettingsWidget::hideEvent(QHideEvent *e) {
             newSettings->setValue("search_bookmarks", "false");
 
         //setting new settings only if they are different than old ones
-        QString key;
-        foreach(key, newSettings->keys()) {
+        foreach(QString key, newSettings->keys()) {
             if(settings->value(key) != newSettings->value(key)) {
                 guiInterface->setSettings(newSettings);
                 break;
             }
         }
-
     }
     if(settings) {
         delete settings;
         settings = 0;
     }
     _changed = false;
+#endif
 }