change comment's and fix bug (xslt transform)
[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     int i=0,j=0;
66     int actualLangTo=0;
67     int actualLangFrom=0;
68
69     setWindowTitle(tr("Google Plugin Settings"));
70
71     langFromLabel = new QLabel(tr("From:"));
72     langToLabel = new QLabel(tr("To: "));
73
74     verticalLayout = new QVBoxLayout;
75     setLayout(verticalLayout);
76
77     langLayout = new QVBoxLayout;
78     langsFormLayout = new QFormLayout;
79     changeLangLayout = new QHBoxLayout;
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     #ifdef Q_WS_MAEMO_5
97         connectInfoLabel = new QLabel(tr("Google plugin makes use of Internet "
98                                          "connection, so it may cost You."));
99         connectInfoLabel->setWordWrap(true);
100         verticalLayout->addWidget(connectInfoLabel);
101     #endif
102
103     langFromComboBox = new QComboBox;
104     langToComboBox = new QComboBox;
105
106     foreach(QString langs, GooglePlugin::languages.keys()){
107         if(langs==_langTo)
108             actualLangTo=j;
109         if(langs==_langFrom)
110             actualLangFrom=i;
111         if(langs!="Detect langlage"){
112             langToComboBox->addItem(langs);
113             j++;
114         }
115         langFromComboBox->addItem(langs);
116         i++;
117     }
118
119     langToComboBox->setCurrentIndex(actualLangTo);
120     langFromComboBox->setCurrentIndex(actualLangFrom);
121
122     langLayout->addLayout(langsFormLayout);
123
124     langsFormLayout->addRow(langFromLabel, langFromComboBox);
125     langsFormLayout->addRow(langToLabel, langToComboBox);
126
127     changeLangLayout->addLayout(langLayout);
128     changeLangLayout->addWidget(changeLangButton);
129
130     verticalLayout->addLayout(changeLangLayout);
131
132     confirmButton = new QPushButton;
133     if(type == New) {
134         confirmButton->setText(tr("Add"));
135     }
136     else {
137         confirmButton->setText(tr("Save settings"));
138     }
139
140     verticalLayout->addWidget(confirmButton);
141
142     setModal(true);
143     setMinimumSize(sizeHint());
144     setMaximumSize(sizeHint());
145 }
146
147
148 void GoogleDialog::langFromChanged(int index) {
149     _langFrom=langFromComboBox->itemText(index);
150 }
151
152
153 void GoogleDialog::langToChanged(int index) {
154      _langTo=langToComboBox->itemText(index);
155 }
156
157
158 void GoogleDialog::changeLangButtonClicked() {
159     int indexTo=langToComboBox->findText(langFromComboBox->currentText());
160     int indexFrom=langFromComboBox->findText(langToComboBox->currentText());
161
162     if(indexTo!= -1 && indexFrom!= -1) {
163         langToComboBox->setCurrentIndex(indexTo);
164         langFromComboBox->setCurrentIndex(indexFrom);
165     }
166 }
167
168
169 void GoogleDialog::accept() {
170     saveSettings();
171     QDialog::accept();
172 }
173
174
175 void GoogleDialog::saveSettings() {
176     _settings = new Settings;
177     if(plugin) {
178         foreach(QString key, plugin->settings()->keys())
179             _settings->setValue(key, plugin->settings()->value(key));
180     }
181
182     _settings->setValue("lang_to",
183                         GooglePlugin::languages.value(_langTo));
184     _settings->setValue("lang_from",
185                         GooglePlugin::languages.value(_langFrom));
186 }
187
188
189 Settings* GoogleDialog::getSettings() {
190     return _settings;
191 }