cdadfe22aa690f404e641491d005dd3b67b7d7a7
[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,
30                                            Settings *pluginSettings,
31                                            QString acceptButtonLabel) :
32                                            QDialog(parent)
33 {
34     QMap<QString, QString> languages;
35     languages=GooglePlugin::initLanguages();
36
37     int actualLangTo=1;
38     int actualLangFrom=1;
39
40     if(pluginSettings==0) {
41         _langTo=languages.key("pl");
42         _langFrom=languages.key("en");
43     }
44     else {
45         _langTo=languages.key(pluginSettings->value("lang_to"));
46         _langFrom=languages.key(pluginSettings->value("lang_from"));
47     }
48
49     setMinimumHeight(370);
50     #ifdef Q_WS_MAEMO_5
51
52         changeLangButton=new QPushButton(
53                                  QIcon::fromTheme("general_refresh"), "");
54     #else
55         changeLangButton=new QPushButton(
56                 QIcon::fromTheme("object-flip-vertical"),tr(""));
57     #endif
58
59     langFromLabel = new QLabel(tr("From:"));
60     langToLabel = new QLabel(tr(" To: "));
61     connectInfoLabel = new QLabel(tr("Google plugin makes use of Internet "
62                                      "connection, so it may cost You."));
63
64     connectInfoLabel->setWordWrap(true);
65     setWindowTitle(tr("Google Settings"));
66
67     verticalLayout = new QVBoxLayout;
68     langLayout = new QVBoxLayout;
69     langFromLayout = new QHBoxLayout;
70     langToLayout = new QHBoxLayout;
71     changelangLayout = new QHBoxLayout;
72
73     setLayout(verticalLayout);
74
75     infoLabel = new QLabel;
76     infoLabel->setText(tr("Plugin: GoogleTranslator \n")+
77                    tr("From: ") + _langFrom + "\n" +
78                    tr("To: ") + _langTo);
79     verticalLayout->addWidget(infoLabel);
80
81
82     langFromComboBox = new QComboBox;
83     langToComboBox = new QComboBox;
84
85     int i=0;
86     foreach(QString langs,languages.keys()){
87         if(langs==_langTo)
88             actualLangTo=i;
89         if(langs==_langFrom)
90             actualLangFrom=i;
91         langToComboBox->addItem(langs);
92         langFromComboBox->addItem(langs);
93         i++;
94     }
95     langToComboBox->setCurrentIndex(actualLangTo);
96     langFromComboBox->setCurrentIndex(actualLangFrom);
97
98
99
100     setWindowTitle(tr("Google Settings"));
101     verticalLayout->addWidget(connectInfoLabel);
102
103     langFromLayout->addWidget(langFromLabel);
104     langFromLayout->addWidget(langFromComboBox);
105     langToLayout->addWidget(langToLabel);
106     langToLayout->addWidget(langToComboBox);
107
108
109     langLayout->addLayout(langFromLayout);
110     langLayout->addLayout(langToLayout);
111     changelangLayout->addLayout(langLayout);
112     changelangLayout->addWidget(changeLangButton);
113     verticalLayout->addLayout(changelangLayout);
114
115     saveButton = new QPushButton(acceptButtonLabel);
116     verticalLayout->addWidget(saveButton);
117
118     setModal(true);
119
120     connect(saveButton, SIGNAL(clicked()),
121             this, SLOT(accept()));
122
123
124     connect(langFromComboBox, SIGNAL(activated(int)),
125             this, SLOT(activatedFrom(int)));
126     connect(langToComboBox, SIGNAL(activated(int)),
127             this, SLOT(activatedTo(int)));
128
129
130     connect(changeLangButton, SIGNAL(clicked()),
131             this, SLOT(changeLangButtonClicked()));
132 }
133
134
135 void GoogleSettingsDialog::activatedFrom(int index) {
136         _langFrom=langFromComboBox->itemText(index);
137 }
138
139 void GoogleSettingsDialog::activatedTo(int index) {
140      _langTo=langToComboBox->itemText(index);
141 }
142
143
144 void GoogleSettingsDialog::changeLangButtonClicked() {
145
146     int tempIndexTo=langToComboBox->currentIndex();
147     QString tempLangTo=_langTo;
148     langToComboBox->setCurrentIndex(langFromComboBox->currentIndex());
149     langFromComboBox->setCurrentIndex(tempIndexTo);
150     _langTo=_langFrom;
151     _langFrom=tempLangTo;
152
153 }
154
155 QString GoogleSettingsDialog::langFrom() {
156     return _langFrom;
157 }
158
159 QString GoogleSettingsDialog::langTo() {
160     return _langTo;
161 }
162
163 Settings* GoogleSettingsDialog::getSettings(QWidget *parent,
164                                             Settings *pluginSettings,
165                                              QString acceptButtonLabel) {
166     GoogleSettingsDialog settingsDialog(parent,pluginSettings,acceptButtonLabel);
167
168     QMap<QString, QString> languages;
169     languages=GooglePlugin::initLanguages();
170     if(settingsDialog.exec()==QDialog::Accepted) {
171         Settings *settings = new Settings();
172         settings->setValue("lang_to",languages.value(settingsDialog.langTo()));
173         settings->setValue("lang_from",languages.value(settingsDialog.langFrom()));
174         settings->setValue("connection_accepted","true");
175         settings->setValue("type","google");
176         return settings;
177     }
178     return 0;
179 }
180
181 void GoogleSettingsDialog::changeSettings(GooglePlugin* plugin,
182                                           QWidget *parent) {
183     Settings *settings = new Settings();
184     settings=getSettings(parent,plugin->settings(),tr("Save changes"));
185     if(settings)
186         plugin->setSettings(settings);
187 }