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