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