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