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