Fixed refresh icon
[mdictionary] / trunk / src / plugins / google / src / GoogleSettingsDialog.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 "GoogleSettingsDialog.h"
27 #include <QDebug>
28
29 GoogleSettingsDialog::GoogleSettingsDialog(QWidget *parent,
30                                            QString langTo,
31                                            QString langFrom,
32                                            QString acceptButtonLabel) :
33     QDialog(parent) {
34     QMap<QString, QString> languages;
35     languages=GooglePlugin::initLanguages();
36
37     int actualLangTo=1;
38     int actualLangFrom=1;
39
40     if(langTo=="")
41         langTo="pl";
42     if(langFrom=="")
43         langFrom="en";
44
45     _langTo=languages.key(langTo);
46     _langFrom=languages.key(langFrom);
47
48     #ifdef Q_WS_MAEMO_5
49
50         changeLangButton=new QPushButton(
51                                  QIcon::fromTheme("general_refresh"), "");
52     #else
53         changeLangButton=new QPushButton(
54                 QIcon::fromTheme("object-flip-vertical"),tr(""));
55     #endif
56     langFromLabel = new QLabel("From:");
57     langToLabel = new QLabel(" To: ");
58     connectInfoLabel = new QLabel(tr("Google plugin make use of internet "
59                                      "conection, so it may cost You."));
60     connectInfoLabel->setWordWrap(true);
61
62
63     setWindowTitle(tr("Google Settings"));
64
65     verticalLayout = new QVBoxLayout;
66     langLayout = new QVBoxLayout;
67     langFromLayout = new QHBoxLayout;
68     langToLayout = new QHBoxLayout;
69     changelangLayout = new QHBoxLayout;
70
71     setLayout(verticalLayout);
72
73     infoLabel = new QLabel;
74     infoLabel->setText(tr("Plugin:GoogleTranslator \n")+
75                    tr("From: ") + languages.key(langFrom) + "\n" +
76                    tr("To: ") + languages.key(langTo));
77     verticalLayout->addWidget(infoLabel);
78
79     langFromComboBox = new QComboBox;
80     langToComboBox = new QComboBox;
81     int i=0;
82     foreach(QString langs,languages.keys()){
83         if(langs==languages.key(langTo))
84             actualLangTo=i;
85         if(langs==languages.key(langFrom))
86             actualLangFrom=i;
87         langToComboBox->addItem(langs);
88         langFromComboBox->addItem(langs);
89         i++;
90     }
91
92     verticalLayout->addWidget(connectInfoLabel);
93     langFromLayout->addWidget(langFromLabel);
94     langFromLayout->addWidget(langFromComboBox);
95     langToLayout->addWidget(langToLabel);
96     langToLayout->addWidget(langToComboBox);
97     langLayout->addLayout(langFromLayout);
98     langLayout->addLayout(langToLayout);
99     changelangLayout->addLayout(langLayout);
100     changelangLayout->addWidget(changeLangButton);
101     verticalLayout->addLayout(changelangLayout);
102
103
104     langToComboBox->setCurrentIndex(actualLangTo);
105     langFromComboBox->setCurrentIndex(actualLangFrom);
106
107     saveButton = new QPushButton(acceptButtonLabel);
108     verticalLayout->addWidget(saveButton);
109
110     setModal(true);
111
112     connect(saveButton, SIGNAL(clicked()),
113             this, SLOT(accept()));
114     connect(langFromComboBox, SIGNAL(activated(int)),
115             this, SLOT(activatedFrom(int)));
116     connect(langToComboBox, SIGNAL(activated(int)),
117             this, SLOT(activatedTo(int)));
118     connect(changeLangButton, SIGNAL(clicked()),
119             this, SLOT(changeLangButtonClicked()));
120 }
121
122 void GoogleSettingsDialog::activatedFrom(int index) {
123     _langFrom=langFromComboBox->itemText(index);
124 }
125
126 void GoogleSettingsDialog::activatedTo(int index) {
127      _langTo=langToComboBox->itemText(index);
128 }
129
130 void GoogleSettingsDialog::changeLangButtonClicked() {
131     int tempIndexTo=langToComboBox->currentIndex();
132     QString tempLangTo=_langTo;
133
134     langToComboBox->setCurrentIndex(langFromComboBox->currentIndex());
135     langFromComboBox->setCurrentIndex(tempIndexTo);
136
137     _langTo=_langFrom;
138     _langFrom=tempLangTo;
139 }
140
141 QString GoogleSettingsDialog::langFrom() {
142     return _langFrom;
143 }
144
145 QString GoogleSettingsDialog::langTo() {
146     return _langTo;
147 }
148
149 Settings* GoogleSettingsDialog::getSettings(QWidget *parent,
150                                             QString langTo,
151                                             QString langFrom,
152                                             QString acceptButtonLabel) {
153     GoogleSettingsDialog settingsDialog(parent,
154                                         langTo,
155                                         langFrom,
156                                         acceptButtonLabel);
157     QMap<QString, QString> languages;
158     languages=GooglePlugin::initLanguages();
159     if(settingsDialog.exec()==QDialog::Accepted) {
160         Settings *settings = new Settings();
161         settings->setValue("langTo",languages.value(settingsDialog.langTo()));
162         settings->setValue("langFrom",
163                            languages.value(settingsDialog.langFrom()));
164         settings->setValue("connectionAccept","true");
165         return settings;
166     }
167     return 0;
168 }
169
170 void GoogleSettingsDialog::changeSettings(GooglePlugin* plugin,
171                                           QWidget *parent) {
172     Settings *settings = new Settings();
173     settings=getSettings(parent,
174                          plugin->settings()->value("langTo"),
175                          plugin->settings()->value("langFrom"),
176                          tr("Save changes"));
177     if(settings)
178         plugin->setSettings(settings);
179 }