qml ComboBox component and GoogleDialog complete
[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 GoogleDialog.cpp
23     \brief Implementation of google plugin's dialogs.
24
25     \author Jakub Jaszczynski <j.j.jaszczynski@gmail.com>
26 */
27
28 #include "GoogleDialog.h"
29 #include <QDebug>
30
31 GoogleDialog::GoogleDialog(GooglePlugin *plugin,
32                            GoogleDialogType type,
33                            QWidget *parent) : QDialog(parent) {
34     this->plugin = plugin;
35     this->type = type;
36     _settings = 0;
37     _actualLangFrom = 0;
38     _actualLangTo = 0;
39
40     if(plugin) {
41         _langTo=GooglePlugin::languages.key(
42                 plugin->settings()->value("lang_to"));
43
44         _langFrom=GooglePlugin::languages.key(
45                 plugin->settings()->value("lang_from"));
46     }
47     else {
48         _langTo=GooglePlugin::languages.key("pl");
49         _langFrom=GooglePlugin::languages.key("en");
50     }
51
52 #ifdef Q_WS_MAEMO_5
53     connect(confirmButton, SIGNAL(clicked()),
54             this, SLOT(accept()));
55
56     connect(langFromComboBox, SIGNAL(currentIndexChanged(int)),
57             this, SLOT(langFromChanged(int)));
58
59     connect(langToComboBox, SIGNAL(currentIndexChanged(int)),
60             this, SLOT(langToChanged(int)));
61
62     connect(changeLangButton, SIGNAL(clicked()),
63             this, SLOT(changeLangButtonClicked()));
64 #else
65
66     int i=0,j=0;
67     int actualLangTo=0;
68     int actualLangFrom=0;
69
70     QList<QString> langList;
71     foreach(QString langs, GooglePlugin::languages.keys()){
72         if(langs==_langTo)
73             actualLangTo=j;
74         if(langs==_langFrom)
75             actualLangFrom=i;
76         if(langs!="Detect langlage"){
77             langList.append(langs);
78             j++;
79         }
80         i++;
81     }
82
83     _actualLangFrom = actualLangFrom;
84     _actualLangTo = actualLangTo;
85     model = new ComboBoxModel(langList);
86
87     view= new QDeclarativeView();
88     ctxt = view->rootContext();
89     ctxt->setContextProperty("comboBoxModel", &(*model));
90     view->setSource(QUrl::fromLocalFile("/usr/share/mdictionary/qml/GoogleDialog.qml"));
91     view->setResizeMode(QDeclarativeView::SizeRootObjectToView);
92     //view->setAlignment(Qt::AlignCenter);
93     view->show();
94
95     mainLayout = new QVBoxLayout;
96     mainLayout->addWidget(view);
97     setLayout(mainLayout);
98     view->setWindowTitle(tr("Google Settings"));
99
100     QGraphicsObject *rootObject = view->rootObject();
101
102     connect(rootObject, SIGNAL(saveButtonClicked(QString, QString)),
103             this, SLOT(saveButtonClicked(QString,QString)));
104
105     connect(this, SIGNAL(setInfo(QVariant)),
106            rootObject, SLOT(setInfo(QVariant)));
107     connect(this,SIGNAL(setNew(QVariant)),
108             rootObject, SLOT(setNew(QVariant)));
109     connect(this,SIGNAL(setStartValues(QVariant,QVariant,QVariant,QVariant)),
110             rootObject, SLOT(setStartValues(QVariant, QVariant, QVariant, QVariant)));
111
112 #endif
113
114     initializeUI();
115 }
116
117
118 void GoogleDialog::initializeUI() {
119 #ifndef Q_WS_MAEMO_5
120
121     setWindowTitle(tr("Google Plugin Settings"));
122     if (type != New){
123         emit setNew(false);
124         QString info=tr("Plugin: ") + plugin->type().toUpper() +"\n" +
125             tr("From: ") + _langFrom + "\n" +
126             tr("To: ") + _langTo;
127         emit setInfo(info);
128     }
129     else{
130         emit setNew(true);
131     }
132     emit setStartValues(_langFrom, _langTo, _actualLangFrom, _actualLangTo);
133
134 //    setMinimumSize(sizeHint());
135
136
137 #else
138
139     int i=0,j=0;
140     int actualLangTo=0;
141     int actualLangFrom=0;
142
143     setWindowTitle(tr("Google Plugin Settings"));
144
145     langFromLabel = new QLabel(tr("From:"));
146     langToLabel = new QLabel(tr("To: "));
147
148     verticalLayout = new QVBoxLayout;
149     setLayout(verticalLayout);
150
151     langLayout = new QVBoxLayout;
152     langsFormLayout = new QFormLayout;
153     changeLangLayout = new QHBoxLayout;
154
155     #ifdef Q_WS_MAEMO_5
156         setMinimumHeight(370);
157         changeLangButton=new QPushButton(
158                 QIcon::fromTheme("general_refresh"), "");
159     #else
160         changeLangButton=new QPushButton(
161                 QIcon::fromTheme("object-flip-vertical"),"");
162     #endif
163
164     infoLabel = new QLabel;
165     infoLabel->setText(tr("Plugin: GoogleTranslator \n")+
166                    tr("From: ") + _langFrom + "\n" +
167                    tr("To: ") + _langTo);
168     verticalLayout->addWidget(infoLabel);
169
170     #ifdef Q_WS_MAEMO_5
171         connectInfoLabel = new QLabel(tr("Google plugin makes use of Internet "
172                                          "connection, so it may cost You."));
173         connectInfoLabel->setWordWrap(true);
174         verticalLayout->addWidget(connectInfoLabel);
175     #endif
176
177     langFromComboBox = new QComboBox;
178     langToComboBox = new QComboBox;
179
180     foreach(QString langs, GooglePlugin::languages.keys()){
181         if(langs==_langTo)
182             actualLangTo=j;
183         if(langs==_langFrom)
184             actualLangFrom=i;
185         if(langs!="Detect langlage"){
186             langToComboBox->addItem(langs);
187             j++;
188         }
189         langFromComboBox->addItem(langs);
190         i++;
191     }
192
193     langToComboBox->setCurrentIndex(actualLangTo);
194     langFromComboBox->setCurrentIndex(actualLangFrom);
195
196     langLayout->addLayout(langsFormLayout);
197
198     langsFormLayout->addRow(langFromLabel, langFromComboBox);
199     langsFormLayout->addRow(langToLabel, langToComboBox);
200
201     changeLangLayout->addLayout(langLayout);
202     changeLangLayout->addWidget(changeLangButton);
203
204     verticalLayout->addLayout(changeLangLayout);
205
206     confirmButton = new QPushButton;
207     if(type == New) {
208         confirmButton->setText(tr("Add"));
209     }
210     else {
211         confirmButton->setText(tr("Save settings"));
212     }
213
214     verticalLayout->addWidget(confirmButton);
215
216     setModal(true);
217     setMinimumSize(sizeHint());
218     setMaximumSize(sizeHint());
219 #endif
220 }
221
222
223 void GoogleDialog::langFromChanged(int index) {
224     _langFrom=langFromComboBox->itemText(index);
225 }
226
227
228 void GoogleDialog::langToChanged(int index) {
229      _langTo=langToComboBox->itemText(index);
230 }
231
232
233 void GoogleDialog::changeLangButtonClicked() {
234     int indexTo=langToComboBox->findText(langFromComboBox->currentText());
235     int indexFrom=langFromComboBox->findText(langToComboBox->currentText());
236
237     if(indexTo!= -1 && indexFrom!= -1) {
238         langToComboBox->setCurrentIndex(indexTo);
239         langFromComboBox->setCurrentIndex(indexFrom);
240     }
241 }
242
243 #ifdef Q_WS_MAEMO_5
244 void GoogleDialog::accept() {
245     saveSettings();
246     QDialog::accept();
247 }
248
249
250 void GoogleDialog::saveSettings() {
251     _settings = new Settings;
252     if(plugin) {
253         foreach(QString key, plugin->settings()->keys())
254             _settings->setValue(key, plugin->settings()->value(key));
255     }
256
257     _settings->setValue("lang_to",
258                         GooglePlugin::languages.value(_langTo));
259     _settings->setValue("lang_from",
260                         GooglePlugin::languages.value(_langFrom));
261 }
262
263 #else
264 void GoogleDialog::saveButtonClicked(QString langFrom, QString langTo){
265     saveSettings(langFrom, langTo);
266     QDialog::accept();
267 }
268
269 void GoogleDialog::saveSettings(QString langFrom, QString langTo){
270     _settings = new Settings;
271     _langFrom = langFrom;
272     _langTo = langTo;
273     if(plugin) {
274         foreach(QString key, plugin->settings()->keys())
275             _settings->setValue(key, plugin->settings()->value(key));
276     }
277
278     _settings->setValue("lang_to",
279                         GooglePlugin::languages.value(langTo));
280     _settings->setValue("lang_from",
281                         GooglePlugin::languages.value(langFrom));
282 }
283 #endif
284
285 Settings* GoogleDialog::getSettings() {
286     return _settings;
287 }