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