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