delete unnecessary variable
[mdictionary] / trunk / src / plugins / google / src / GoogleSettingsDialog.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 /*! \file GoogleSettingsDialog.cpp
23     \author Jakub Jaszczynski <j.j.jaszczynski@gmail.com>
24 */
25
26 #include "GoogleSettingsDialog.h"
27 #include <QDebug>
28
29 GoogleSettingsDialog::GoogleSettingsDialog(QWidget *parent,Settings *pluginSettings) :
30     QDialog(parent)
31 {
32     QMap<QString, QString> languages;
33     languages=GooglePlugin::initLanguages();
34
35     int actualLangTo=1;
36     int actualLangFrom=1;
37
38     if(pluginSettings==0) {
39         _langTo=languages.key("pl");
40         _langFrom=languages.key("en");
41     }
42     else {
43         _langTo=languages.key(pluginSettings->value("lang_to"));
44         _langFrom=languages.key(pluginSettings->value("lang_from"));
45     }
46
47     #ifdef Q_WS_MAEMO_5
48         changeLangButton=new QPushButton(QIcon::fromTheme("general_refresh"),tr(""));
49     #else
50         changeLangButton=new QPushButton(QIcon::fromTheme("object-flip-vertical"),tr(""));
51     #endif
52
53     langFromLabel = new QLabel("From:");
54     langToLabel = new QLabel(" To: ");
55     connectInfoLabel = new QLabel(tr("Google plugin make use of internet "
56                                      "conection, so it may cost You."));
57     infoLabel = new QLabel(tr("Plugin:GoogleTranslator \n From: %1 \n To: %2").arg(
58                             _langFrom,_langTo));
59     verticalLayout = new QVBoxLayout(this);
60     langLayout = new QVBoxLayout(this);
61     langFromLayout = new QHBoxLayout(this);
62     langToLayout = new QHBoxLayout(this);
63     changelangLayout = new QHBoxLayout(this);
64     langFromComboBox = new QComboBox(this);
65     langToComboBox = new QComboBox(this);
66     saveButton = new QPushButton(tr("Save settings"),this);
67
68     connectInfoLabel->setWordWrap(true);
69
70     int i=0;
71     foreach(QString langs,languages.keys()){
72         if(langs==_langTo)
73             actualLangTo=i;
74         if(langs==_langFrom)
75             actualLangFrom=i;
76         langToComboBox->addItem(langs);
77         langFromComboBox->addItem(langs);
78         i++;
79     }
80     langToComboBox->setCurrentIndex(actualLangTo);
81     langFromComboBox->setCurrentIndex(actualLangFrom);
82
83     setWindowTitle(tr("Google Settings"));
84     setLayout(verticalLayout);
85     verticalLayout->addWidget(infoLabel);
86     verticalLayout->addWidget(connectInfoLabel);
87     langFromLayout->addWidget(langFromLabel);
88     langFromLayout->addWidget(langFromComboBox);
89     langToLayout->addWidget(langToLabel);
90     langToLayout->addWidget(langToComboBox);
91     langLayout->addLayout(langFromLayout);
92     langLayout->addLayout(langToLayout);
93     changelangLayout->addLayout(langLayout);
94     changelangLayout->addWidget(changeLangButton);
95     verticalLayout->addLayout(changelangLayout);
96     verticalLayout->addWidget(saveButton);
97
98     setModal(true);
99
100     connect(saveButton, SIGNAL(clicked()),this, SLOT(accept()));
101     connect(langFromComboBox, SIGNAL(activated(int)),this, SLOT(activatedFrom(int)));
102     connect(langToComboBox, SIGNAL(activated(int)),this, SLOT(activatedTo(int)));
103     connect(changeLangButton, SIGNAL(clicked()),this, SLOT(changeLangButtonClicked()));
104 }
105
106 void GoogleSettingsDialog::activatedFrom(int index) {
107     _langFrom=langFromComboBox->itemText(index);
108 }
109
110 void GoogleSettingsDialog::activatedTo(int index) {
111      _langTo=langToComboBox->itemText(index);
112 }
113
114 void GoogleSettingsDialog::changeLangButtonClicked() {
115     int tempIndexTo=langToComboBox->currentIndex();
116     QString tempLangTo=_langTo;
117     langToComboBox->setCurrentIndex(langFromComboBox->currentIndex());
118     langFromComboBox->setCurrentIndex(tempIndexTo);
119     _langTo=_langFrom;
120     _langFrom=tempLangTo;
121 }
122
123 QString GoogleSettingsDialog::langFrom() {
124     return _langFrom;
125 }
126
127 QString GoogleSettingsDialog::langTo() {
128     return _langTo;
129 }
130
131 Settings* GoogleSettingsDialog::getSettings(QWidget *parent,Settings *pluginSettings) {
132     GoogleSettingsDialog settingsDialog(parent,pluginSettings);
133     QMap<QString, QString> languages;
134     languages=GooglePlugin::initLanguages();
135     if(settingsDialog.exec()==QDialog::Accepted) {
136         Settings *settings = new Settings();
137         settings->setValue("lang_to",languages.value(settingsDialog.langTo()));
138         settings->setValue("lang_from",languages.value(settingsDialog.langFrom()));
139         settings->setValue("connection_accept","true");
140         settings->setValue("type","google");
141         return settings;
142     }
143     return 0;
144 }
145
146 void GoogleSettingsDialog::changeSettings(GooglePlugin* plugin, QWidget *parent) {
147     Settings *settings = new Settings();
148     settings=getSettings(parent,plugin->settings());
149     if(settings)
150         plugin->setSettings(settings);
151 }