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