qml ComboBox component and GoogleDialog complete
[mdictionary] / src / plugins / google / GoogleDialog.cpp
index f9aac4c..4ca07eb 100644 (file)
     Copyright 2010 Comarch S.A.
 
 *******************************************************************************/
+/*!
+    \file GoogleDialog.cpp
+    \brief Implementation of google plugin's dialogs.
 
-/*! \file GoogleSettingsDialog.cpp
     \author Jakub Jaszczynski <j.j.jaszczynski@gmail.com>
 */
 
@@ -32,6 +34,8 @@ GoogleDialog::GoogleDialog(GooglePlugin *plugin,
     this->plugin = plugin;
     this->type = type;
     _settings = 0;
+    _actualLangFrom = 0;
+    _actualLangTo = 0;
 
     if(plugin) {
         _langTo=GooglePlugin::languages.key(
@@ -45,10 +49,7 @@ GoogleDialog::GoogleDialog(GooglePlugin *plugin,
         _langFrom=GooglePlugin::languages.key("en");
     }
 
-
-
-    initializeUI();
-
+#ifdef Q_WS_MAEMO_5
     connect(confirmButton, SIGNAL(clicked()),
             this, SLOT(accept()));
 
@@ -60,10 +61,85 @@ GoogleDialog::GoogleDialog(GooglePlugin *plugin,
 
     connect(changeLangButton, SIGNAL(clicked()),
             this, SLOT(changeLangButtonClicked()));
+#else
+
+    int i=0,j=0;
+    int actualLangTo=0;
+    int actualLangFrom=0;
+
+    QList<QString> langList;
+    foreach(QString langs, GooglePlugin::languages.keys()){
+        if(langs==_langTo)
+            actualLangTo=j;
+        if(langs==_langFrom)
+            actualLangFrom=i;
+        if(langs!="Detect langlage"){
+            langList.append(langs);
+            j++;
+        }
+        i++;
+    }
+
+    _actualLangFrom = actualLangFrom;
+    _actualLangTo = actualLangTo;
+    model = new ComboBoxModel(langList);
+
+    view= new QDeclarativeView();
+    ctxt = view->rootContext();
+    ctxt->setContextProperty("comboBoxModel", &(*model));
+    view->setSource(QUrl::fromLocalFile("/usr/share/mdictionary/qml/GoogleDialog.qml"));
+    view->setResizeMode(QDeclarativeView::SizeRootObjectToView);
+    //view->setAlignment(Qt::AlignCenter);
+    view->show();
+
+    mainLayout = new QVBoxLayout;
+    mainLayout->addWidget(view);
+    setLayout(mainLayout);
+    view->setWindowTitle(tr("Google Settings"));
+
+    QGraphicsObject *rootObject = view->rootObject();
+
+    connect(rootObject, SIGNAL(saveButtonClicked(QString, QString)),
+            this, SLOT(saveButtonClicked(QString,QString)));
+
+    connect(this, SIGNAL(setInfo(QVariant)),
+           rootObject, SLOT(setInfo(QVariant)));
+    connect(this,SIGNAL(setNew(QVariant)),
+            rootObject, SLOT(setNew(QVariant)));
+    connect(this,SIGNAL(setStartValues(QVariant,QVariant,QVariant,QVariant)),
+            rootObject, SLOT(setStartValues(QVariant, QVariant, QVariant, QVariant)));
+
+#endif
+
+    initializeUI();
 }
 
 
 void GoogleDialog::initializeUI() {
+#ifndef Q_WS_MAEMO_5
+
+    setWindowTitle(tr("Google Plugin Settings"));
+    if (type != New){
+        emit setNew(false);
+        QString info=tr("Plugin: ") + plugin->type().toUpper() +"\n" +
+            tr("From: ") + _langFrom + "\n" +
+            tr("To: ") + _langTo;
+        emit setInfo(info);
+    }
+    else{
+        emit setNew(true);
+    }
+    emit setStartValues(_langFrom, _langTo, _actualLangFrom, _actualLangTo);
+
+//    setMinimumSize(sizeHint());
+
+
+#else
+
+    int i=0,j=0;
+    int actualLangTo=0;
+    int actualLangFrom=0;
+
     setWindowTitle(tr("Google Plugin Settings"));
 
     langFromLabel = new QLabel(tr("From:"));
@@ -72,16 +148,14 @@ void GoogleDialog::initializeUI() {
     verticalLayout = new QVBoxLayout;
     setLayout(verticalLayout);
 
-
     langLayout = new QVBoxLayout;
     langsFormLayout = new QFormLayout;
     changeLangLayout = new QHBoxLayout;
 
-
     #ifdef Q_WS_MAEMO_5
         setMinimumHeight(370);
         changeLangButton=new QPushButton(
-                                 QIcon::fromTheme("general_refresh"), "");
+                QIcon::fromTheme("general_refresh"), "");
     #else
         changeLangButton=new QPushButton(
                 QIcon::fromTheme("object-flip-vertical"),"");
@@ -93,23 +167,16 @@ void GoogleDialog::initializeUI() {
                    tr("To: ") + _langTo);
     verticalLayout->addWidget(infoLabel);
 
-
     #ifdef Q_WS_MAEMO_5
         connectInfoLabel = new QLabel(tr("Google plugin makes use of Internet "
                                          "connection, so it may cost You."));
         connectInfoLabel->setWordWrap(true);
-
         verticalLayout->addWidget(connectInfoLabel);
     #endif
 
     langFromComboBox = new QComboBox;
     langToComboBox = new QComboBox;
 
-    int i=0;
-    int j=0;
-    int actualLangTo=0;
-    int actualLangFrom=0;
-
     foreach(QString langs, GooglePlugin::languages.keys()){
         if(langs==_langTo)
             actualLangTo=j;
@@ -129,7 +196,6 @@ void GoogleDialog::initializeUI() {
     langLayout->addLayout(langsFormLayout);
 
     langsFormLayout->addRow(langFromLabel, langFromComboBox);
-
     langsFormLayout->addRow(langToLabel, langToComboBox);
 
     changeLangLayout->addLayout(langLayout);
@@ -148,22 +214,23 @@ void GoogleDialog::initializeUI() {
     verticalLayout->addWidget(confirmButton);
 
     setModal(true);
-
     setMinimumSize(sizeHint());
     setMaximumSize(sizeHint());
+#endif
 }
 
+
 void GoogleDialog::langFromChanged(int index) {
     _langFrom=langFromComboBox->itemText(index);
 }
 
+
 void GoogleDialog::langToChanged(int index) {
      _langTo=langToComboBox->itemText(index);
 }
 
-void GoogleDialog::changeLangButtonClicked() {
-    //int tempIndexTo=langToComboBox->currentIndex();
 
+void GoogleDialog::changeLangButtonClicked() {
     int indexTo=langToComboBox->findText(langFromComboBox->currentText());
     int indexFrom=langFromComboBox->findText(langToComboBox->currentText());
 
@@ -171,17 +238,15 @@ void GoogleDialog::changeLangButtonClicked() {
         langToComboBox->setCurrentIndex(indexTo);
         langFromComboBox->setCurrentIndex(indexFrom);
     }
-
-   // langToComboBox->setCurrentIndex(langFromComboBox->currentIndex());
-    //langFromComboBox->setCurrentIndex(tempIndexTo);
 }
 
+#ifdef Q_WS_MAEMO_5
 void GoogleDialog::accept() {
     saveSettings();
-
     QDialog::accept();
 }
 
+
 void GoogleDialog::saveSettings() {
     _settings = new Settings;
     if(plugin) {
@@ -195,6 +260,28 @@ void GoogleDialog::saveSettings() {
                         GooglePlugin::languages.value(_langFrom));
 }
 
+#else
+void GoogleDialog::saveButtonClicked(QString langFrom, QString langTo){
+    saveSettings(langFrom, langTo);
+    QDialog::accept();
+}
+
+void GoogleDialog::saveSettings(QString langFrom, QString langTo){
+    _settings = new Settings;
+    _langFrom = langFrom;
+    _langTo = langTo;
+    if(plugin) {
+        foreach(QString key, plugin->settings()->keys())
+            _settings->setValue(key, plugin->settings()->value(key));
+    }
+
+    _settings->setValue("lang_to",
+                        GooglePlugin::languages.value(langTo));
+    _settings->setValue("lang_from",
+                        GooglePlugin::languages.value(langFrom));
+}
+#endif
+
 Settings* GoogleDialog::getSettings() {
     return _settings;
 }