1 /*******************************************************************************
3 This file is part of mDictionary.
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.
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.
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/>.
18 Copyright 2010 Comarch S.A.
20 *******************************************************************************/
23 \brief Implementation of xdxf plugin's dialogs.
25 \author Mateusz Półrola <mateusz.polrola@gmail.com>
28 #include "XdxfDialog.h"
31 XdxfDialog::XdxfDialog(XdxfPlugin *plugin,
36 this->plugin = plugin;
41 view= new QDeclarativeView();
42 view->setSource(QUrl::fromLocalFile("/usr/share/mdictionary/qml/XdxfDialog.qml"));
43 view->setResizeMode(QDeclarativeView::SizeRootObjectToView);
44 view->setAlignment(Qt::AlignCenter);
47 mainLayout = new QVBoxLayout;
48 mainLayout->addWidget(view);
49 setLayout(mainLayout);
50 view->setWindowTitle(tr("Xdxf Settings"));
52 QGraphicsObject *rootObject = view->rootObject();
54 connect(this, SIGNAL(setPlugin(QVariant)),
55 rootObject, SLOT(setPlugin(QVariant)));
56 connect(this, SIGNAL(setFrom(QVariant)),
57 rootObject, SLOT(setFrom(QVariant)));
58 connect(this, SIGNAL(setTo(QVariant)),
59 rootObject, SLOT(setTo(QVariant)));
60 connect(this, SIGNAL(setDescription(QVariant)),
61 rootObject, SLOT(setDescription(QVariant)));
62 connect(this, SIGNAL(setInfo(QVariant)),
63 rootObject, SLOT(setInfo(QVariant)));
64 connect(this, SIGNAL(setCheckedOptimalize(QVariant)),
65 rootObject, SLOT(setCheckedOptimalize(QVariant)));
66 connect(this, SIGNAL(setCheckedStrip(QVariant)),
67 rootObject, SLOT(setCheckedStrip(QVariant)));
68 connect(this,SIGNAL(setButtonText(QVariant)),
69 rootObject, SLOT(setButtonText(QVariant)));
70 connect(this,SIGNAL(setNew(QVariant)),
71 rootObject, SLOT(setNew(QVariant)));
72 connect(this,SIGNAL(setPath(QVariant)),
73 rootObject, SLOT(setPath(QVariant)));
75 connect(rootObject, SIGNAL(saveButtonClicked()),
76 this, SLOT(accept()));
77 connect(rootObject, SIGNAL(downloadButtonClicked()),
78 this, SLOT(downloadFile()));
79 connect(rootObject, SIGNAL(browseButtonClicked()),
80 this, SLOT(selectFile()));
82 connect(rootObject, SIGNAL(optimalizeCheckboxChanged(bool)),
83 this, SLOT(setGenerateCache(bool)));
84 connect(rootObject, SIGNAL(stripCheckboxChanged(bool)),
85 this, SLOT(setAccents(bool)));
89 cacheToolTip = tr("Optimize for quicker searches (may take some time)");
90 accentsToolTip = tr("Strip accents (searching takes more time, but spelling doesn't have to be exact)");
92 connect(cacheCheckBox, SIGNAL(toggled(bool)),
93 this, SLOT(setGenerateCache(bool)));
94 connect(accentsCheckBox, SIGNAL(toggled(bool)),
95 this, SLOT(setAccents(bool)));
98 connect(accentsInfoToolButton, SIGNAL(clicked()),
99 this, SLOT(showAccentsInfo()));
100 connect(cacheInfoToolButton, SIGNAL(clicked()),
101 this, SLOT(showCacheInfo()));
105 connect(browseButton, SIGNAL(clicked()),
106 this, SLOT(selectFile()));
107 connect(downloadButton, SIGNAL(clicked()),
108 this, SLOT(downloadFile()));
109 connect(&XdxfPlugin::dictDownloader, SIGNAL(fileDownloaded(QString)),
110 this, SLOT(fileDownloaded(QString)));
112 connect(confirmButton, SIGNAL(clicked()),
113 this, SLOT(accept()));
120 void XdxfDialog::initializeUI() {
125 emit setPlugin(plugin->type().toUpper());
126 emit setFrom(plugin->langFrom());
127 emit setTo(plugin->langTo());
128 emit setDescription(plugin->name());
129 emit setInfo(plugin->infoNote());
133 connect(&XdxfPlugin::dictDownloader, SIGNAL(fileDownloaded(QString)),
134 this, SLOT(fileDownloaded(QString)));
138 emit setCheckedOptimalize(true);
139 emit setCheckedStrip(true);
140 // accentsCheckBox->setEnabled(false);
141 _generateCache = true;
143 _dictionaryFilePath = "";
145 else if(plugin && plugin->settings()->value("cached") == "true") {
146 emit setCheckedOptimalize(true);
147 emit setCheckedStrip(true);
148 // accentsCheckBox->setEnabled(false);
149 _generateCache = true;
153 emit setCheckedOptimalize(false);
154 _generateCache = false;
155 if(plugin->settings()->value("strip_accents") == "true") {
156 emit setCheckedStrip(true);
160 emit setCheckedStrip(false);
165 emit setButtonText("Add");
167 emit setButtonText("Save settings");
170 mainVerticalLayout = new QVBoxLayout;
171 widget = new QWidget;
172 widget->setLayout(mainVerticalLayout);
174 infoLabel = new QLabel;
175 infoLabel->setWordWrap(true);
178 setWindowTitle(tr("Add new XDXF dictionary"));
180 browseLayout = new QHBoxLayout;
182 QVBoxLayout* buttonLayout = new QVBoxLayout;
183 browseButton = new QPushButton(tr("Browse"));
184 browseButton->setMaximumWidth(150);
186 downloadButton = new QPushButton(tr("Download"));
187 downloadButton->setMaximumWidth(150);
189 infoLayout = new QHBoxLayout;
190 infoLabel->setText(tr("Dictionary file: not selected"));
192 browseLayout->addWidget(infoLabel,0,Qt::AlignLeft);
193 browseLayout->addLayout(buttonLayout);
194 buttonLayout->addWidget(browseButton,0,Qt::AlignLeft);
195 buttonLayout->addWidget(downloadButton,0,Qt::AlignLeft);
197 mainVerticalLayout->addLayout(browseLayout);
200 setWindowTitle(tr("XDXF Settings"));
202 infoLabel->setText(tr("Plugin: ") + plugin->type().toUpper() +"\n" +
203 tr("From: ") + plugin->langFrom() + "\n" +
204 tr("To: ") + plugin->langTo() + "\n" +
205 tr("Description: ") + plugin->name() + "\n" +
207 mainVerticalLayout->addWidget(infoLabel);
210 accentsLayout = new QHBoxLayout;
211 accentsCheckBox = new QCheckBox(tr("Strip accents"));
212 accentsCheckBox->setToolTip(accentsToolTip);
213 accentsLayout->addWidget(accentsCheckBox);
215 accentsInfoToolButton = new QToolButton;
216 accentsInfoToolButton->setIcon(QIcon::fromTheme("general_information"));
217 accentsLayout->addWidget(accentsInfoToolButton);
220 cacheLayout = new QHBoxLayout;
221 cacheCheckBox = new QCheckBox(tr("Optimize"));
222 cacheCheckBox->setToolTip(cacheToolTip);
223 cacheLayout->addWidget(cacheCheckBox);
226 cacheInfoToolButton = new QToolButton;
227 cacheInfoToolButton->setIcon(QIcon::fromTheme("general_information"));
228 cacheLayout->addWidget(cacheInfoToolButton);
231 mainVerticalLayout->addLayout(cacheLayout);
232 mainVerticalLayout->addLayout(accentsLayout);
234 //load old setting if exists
236 cacheCheckBox->setChecked(true);
237 accentsCheckBox->setChecked(true);
238 accentsCheckBox->setEnabled(false);
239 _generateCache = true;
241 _dictionaryFilePath = "";
243 else if(plugin && plugin->settings()->value("cached") == "true") {
244 cacheCheckBox->setChecked(true);
245 accentsCheckBox->setChecked(true);
246 accentsCheckBox->setEnabled(false);
247 _generateCache = true;
251 cacheCheckBox->setChecked(false);
252 _generateCache = false;
254 if(plugin->settings()->value("strip_accents") == "true") {
255 accentsCheckBox->setChecked(true);
259 accentsCheckBox->setChecked(false);
264 confirmButton = new QPushButton;
265 mainVerticalLayout->addWidget(confirmButton);
267 confirmButton->setText(tr("Add"));
269 confirmButton->setText(tr("Save settings"));
271 scrollArea = new QScrollArea;
272 scrollArea->setWidget(widget);
273 scrollArea->setWidgetResizable(true);
276 scrollArea->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
279 infoLabel->setMinimumWidth(250);
280 scrollArea->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
284 layout = new QHBoxLayout;
285 layout->addWidget(scrollArea);
289 setMinimumSize(385,200);
291 setMinimumHeight(350);
294 scrollArea->setLineWidth(0);
295 scrollArea->setMidLineWidth(0);
296 scrollArea->setFrameStyle(QFrame::NoFrame);
301 void XdxfDialog::setAccents(bool accents) {
302 qDebug()<<"setAcents"<<accents;
307 void XdxfDialog::setGenerateCache(bool generate) {
308 _generateCache = generate;
311 _lastAccents = _accents;
312 emit setCheckedOptimalize(true);
313 emit setCheckedStrip(true);
316 emit setCheckedStrip(_lastAccents);
318 // accentsCheckBox->setEnabled(!generate);
321 _lastAccents = _accents;
322 accentsCheckBox->setChecked(true);
325 accentsCheckBox->setChecked(_lastAccents);
326 accentsCheckBox->setEnabled(!generate);
331 void XdxfDialog::selectFile() {
332 QString fileName = QFileDialog::getOpenFileName(this,
333 tr("Select dictionary file"),
335 tr("XDXF Files (*.xdxf)"),
338 if (!fileName.isEmpty()) {
339 _dictionaryFilePath = fileName;
341 emit setPath(tr("Dictionary file: %1").arg(fileName));
343 infoLabel->setText(tr("Dictionary file: %1").arg(fileName));
350 void XdxfDialog::fileDownloaded(QString) {
352 qDebug()<<"fileDownloaded";
353 emit setPath(tr("Dictionary file: %1").arg(XdxfPlugin::dictDownloader.downloadedFile()));
354 _dictionaryFilePath = XdxfPlugin::dictDownloader.downloadedFile();
356 infoLabel->setText(tr("Dictionary file: %1").arg(XdxfPlugin::dictDownloader.downloadedFile()));
357 _dictionaryFilePath = XdxfPlugin::dictDownloader.downloadedFile();
363 void XdxfDialog::downloadFile() {
364 XdxfPlugin::dictDownloader.download(this);
368 void XdxfDialog::saveSettings() {
369 _settings = new Settings;
371 foreach(QString key, plugin->settings()->keys())
372 _settings->setValue(key, plugin->settings()->value(key));
375 _settings->setValue("path", _dictionaryFilePath);
377 _settings->setValue("generateCache", "true");
379 _settings->setValue("generateCache", "false");
382 _settings->setValue("strip_accents", "true");
384 _settings->setValue("strip_accents", "false");
388 void XdxfDialog::accept() {
389 if(type == New && _dictionaryFilePath.isEmpty()) {
390 Q_EMIT notify(Notify::Warning, tr("File path is not set"));
399 Settings* XdxfDialog::getSettings() {
405 void XdxfDialog::showCacheInfo() {
406 Q_EMIT notify(Notify::Warning, cacheToolTip);
410 void XdxfDialog::showAccentsInfo() {
411 Q_EMIT notify(Notify::Warning, accentsToolTip);