change and add some comments, remove some warning
[mdictionary] / src / plugins / xdxf / XdxfDialog.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 XdxfDialog.cpp
23     \author Mateusz Półrola <mateusz.polrola@gmail.com>
24 */
25
26 #include "XdxfDialog.h"
27 #include <QDebug>
28
29 XdxfDialog::XdxfDialog(XdxfPlugin *plugin,
30                        XdxfDialogType type,
31                        QWidget *parent) :
32                 QDialog(parent) {
33     this->plugin = plugin;
34     this->type = type;
35
36     cacheToolTip = tr("Optimize for quicker searches (may take some time)");
37     accentsToolTip = tr("Strip accents (searching takes more time, but spelling doesn't have to be exact)");
38
39     initializeUI();
40
41     connect(cacheCheckBox, SIGNAL(toggled(bool)),
42             this, SLOT(setGenerateCache(bool)));
43     connect(accentsCheckBox, SIGNAL(toggled(bool)),
44             this, SLOT(setAccents(bool)));
45
46     #ifdef Q_WS_MAEMO_5
47         connect(accentsInfoToolButton, SIGNAL(clicked()),
48                  this, SLOT(showAccentsInfo()));
49         connect(cacheInfoToolButton, SIGNAL(clicked()),
50                  this, SLOT(showCacheInfo()));
51     #endif
52
53     if(type == New) {
54         connect(browseButton, SIGNAL(clicked()),
55                 this, SLOT(selectFile()));
56         connect(downloadButton, SIGNAL(clicked()),
57                 this, SLOT(downloadFile()));
58         connect(&XdxfPlugin::dictDownloader, SIGNAL(fileDownloaded(QString)),
59                 this, SLOT(fileDownloaded(QString)));
60     }
61
62     connect(confirmButton, SIGNAL(clicked()),
63             this, SLOT(accept()));
64 }
65
66
67 void XdxfDialog::fileDownloaded(QString) {
68     infoLabel->setText(tr("Dictionary file: %1").arg(XdxfPlugin::dictDownloader.downloadedFile()));
69     _dictionaryFilePath = XdxfPlugin::dictDownloader.downloadedFile();
70     updateGeometry();
71 }
72
73
74 void XdxfDialog::initializeUI() {
75     mainVerticalLayout = new QVBoxLayout;
76     widget = new QWidget;
77     widget->setLayout(mainVerticalLayout);
78
79     infoLabel = new QLabel;
80     infoLabel->setWordWrap(true);
81
82     if(type == New) {
83         setWindowTitle(tr("Add new XDXF dictionary"));
84
85         browseLayout = new QHBoxLayout;
86
87         QVBoxLayout* buttonLayout = new QVBoxLayout;
88         browseButton = new QPushButton(tr("Browse"));
89         browseButton->setMaximumWidth(150);
90
91         downloadButton = new QPushButton(tr("Download"));
92         downloadButton->setMaximumWidth(150);
93
94         infoLayout = new QHBoxLayout;
95         infoLabel->setText(tr("Dictionary file: not selected"));
96
97         browseLayout->addWidget(infoLabel,0,Qt::AlignLeft);
98         browseLayout->addLayout(buttonLayout);
99         buttonLayout->addWidget(browseButton,0,Qt::AlignLeft);
100         buttonLayout->addWidget(downloadButton,0,Qt::AlignLeft);
101
102         mainVerticalLayout->addLayout(browseLayout);
103     }
104     else {
105         setWindowTitle(tr("XDXF Settings"));
106
107         infoLabel->setText(tr("Plugin: ") + plugin->type().toUpper() +"\n" +
108                        tr("From: ") + plugin->langFrom() + "\n" +
109                        tr("To: ") + plugin->langTo() + "\n" +
110                        tr("Description: ") + plugin->name() + "\n" +
111                        plugin->infoNote());
112         mainVerticalLayout->addWidget(infoLabel);
113     }
114
115     accentsLayout = new QHBoxLayout;
116     accentsCheckBox = new QCheckBox(tr("Strip accents"));
117     accentsCheckBox->setToolTip(accentsToolTip);
118     accentsLayout->addWidget(accentsCheckBox); 
119     #ifdef Q_WS_MAEMO_5
120         accentsInfoToolButton = new QToolButton;
121         accentsInfoToolButton->setIcon(QIcon::fromTheme("general_information"));
122         accentsLayout->addWidget(accentsInfoToolButton);
123     #endif
124
125     cacheLayout = new QHBoxLayout;
126     cacheCheckBox = new QCheckBox(tr("Optimize"));
127     cacheCheckBox->setToolTip(cacheToolTip);
128     cacheLayout->addWidget(cacheCheckBox);
129
130     #ifdef Q_WS_MAEMO_5
131         cacheInfoToolButton = new QToolButton;
132         cacheInfoToolButton->setIcon(QIcon::fromTheme("general_information"));
133         cacheLayout->addWidget(cacheInfoToolButton);
134     #endif
135
136     mainVerticalLayout->addLayout(cacheLayout);
137     mainVerticalLayout->addLayout(accentsLayout);
138
139     //load old setting if exists
140     if(!plugin) {
141         cacheCheckBox->setChecked(true);
142         accentsCheckBox->setChecked(true);
143         accentsCheckBox->setEnabled(false);
144         _generateCache = true;
145         _accents = true;
146         _dictionaryFilePath = "";
147     }
148     else if(plugin && plugin->settings()->value("cached") == "true") {
149         cacheCheckBox->setChecked(true);
150         accentsCheckBox->setChecked(true);
151         accentsCheckBox->setEnabled(false);
152         _generateCache = true;
153         _accents = true;
154     }
155     else {
156         cacheCheckBox->setChecked(false);
157         _generateCache = false;
158
159         if(plugin->settings()->value("strip_accents") == "true") {
160             accentsCheckBox->setChecked(true);
161             _accents = true;
162         }
163         else {
164             accentsCheckBox->setChecked(false);
165             _accents = false;
166         }
167     }
168
169     confirmButton = new QPushButton;
170     mainVerticalLayout->addWidget(confirmButton);
171     if(type == New)
172         confirmButton->setText(tr("Add"));
173     else
174         confirmButton->setText(tr("Save settings"));
175
176     scrollArea = new QScrollArea;
177     scrollArea->setWidget(widget);
178     scrollArea->setWidgetResizable(true);
179
180     #ifdef Q_WS_MAEMO_5
181         scrollArea->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
182     #else
183         if(type==New) {
184             infoLabel->setMinimumWidth(250);
185             setMinimumSize(sizeHint().width()*1.5, sizeHint().height()*1.2);
186             setMaximumSize(sizeHint().width()*1.7, sizeHint().height()*1.5);
187             scrollArea->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
188         }
189     #endif
190
191     layout = new QHBoxLayout;
192     layout->addWidget(scrollArea);
193     setLayout(layout);
194
195     #ifndef Q_WS_MAEMO_5
196         setMinimumSize(385,200);
197     #else
198         setMinimumHeight(350);
199     #endif
200
201     scrollArea->setLineWidth(0);
202     scrollArea->setMidLineWidth(0);
203     scrollArea->setFrameStyle(QFrame::NoFrame);
204 }
205
206
207 void XdxfDialog::setAccents(bool accents) {
208     _accents = accents;
209 }
210
211
212 void XdxfDialog::setGenerateCache(bool generate) {
213     _generateCache = generate;
214
215     if(generate) {
216         _lastAccents = _accents;
217         accentsCheckBox->setChecked(true);
218     }
219     else
220         accentsCheckBox->setChecked(_lastAccents);
221     accentsCheckBox->setEnabled(!generate);
222 }
223
224
225 void XdxfDialog::selectFile() {
226     QString fileName = QFileDialog::getOpenFileName(this,
227                                      tr("Select dictionary file"),
228                                      _dictionaryFilePath,
229                                      tr("XDXF Files (*.xdxf)"),
230                                      NULL,
231                                      NULL);
232     if (!fileName.isEmpty()) {
233         infoLabel->setText(tr("Dictionary file: %1").arg(fileName));
234         _dictionaryFilePath = fileName;
235         updateGeometry();
236     }
237 }
238
239
240 void XdxfDialog::downloadFile() {
241    XdxfPlugin::dictDownloader.download(this);
242 }
243
244
245 void XdxfDialog::saveSettings() {
246     _settings = new Settings;
247     if(plugin) {
248         foreach(QString key, plugin->settings()->keys())
249             _settings->setValue(key, plugin->settings()->value(key));
250     }
251     else
252         _settings->setValue("path", _dictionaryFilePath);
253
254     if(_generateCache)
255         _settings->setValue("generateCache", "true");
256     else
257         _settings->setValue("generateCache", "false");
258
259     if(_accents)
260         _settings->setValue("strip_accents", "true");
261     else
262         _settings->setValue("strip_accents", "false");
263 }
264
265
266 void XdxfDialog::accept() {
267     if(type == New && _dictionaryFilePath.isEmpty()) {
268         Q_EMIT notify(Notify::Warning, tr("File path is not set"));
269         return;
270     }
271
272     saveSettings();
273     QDialog::accept();
274 }
275
276
277 Settings* XdxfDialog::getSettings() {
278     return _settings;
279 }
280
281
282 #ifdef Q_WS_MAEMO_5
283     void XdxfDialog::showCacheInfo() {
284         Q_EMIT notify(Notify::Warning, cacheToolTip);
285     }
286
287
288     void XdxfDialog::showAccentsInfo() {
289         Q_EMIT notify(Notify::Warning, accentsToolTip);
290     }
291 #endif
292