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()));
119 void XdxfDialog::initializeUI() {
124 emit setPlugin(plugin->type().toUpper());
125 emit setFrom(plugin->langFrom());
126 emit setTo(plugin->langTo());
127 emit setDescription(plugin->name());
128 emit setInfo(plugin->infoNote());
129 emit setButtonText("Save settings");
132 emit setButtonText("Add");
134 connect(&XdxfPlugin::dictDownloader, SIGNAL(fileDownloaded(QString)),
135 this, SLOT(fileDownloaded(QString)));
139 emit setCheckedOptimalize(true);
140 emit setCheckedStrip(true);
141 // accentsCheckBox->setEnabled(false);
142 _generateCache = true;
144 _dictionaryFilePath = "";
146 else if(plugin && plugin->settings()->value("cached") == "true") {
147 emit setCheckedOptimalize(true);
148 emit setCheckedStrip(true);
149 // accentsCheckBox->setEnabled(false);
150 _generateCache = true;
154 emit setCheckedOptimalize(false);
155 _generateCache = false;
156 if(plugin->settings()->value("strip_accents") == "true") {
157 emit setCheckedStrip(true);
161 emit setCheckedStrip(false);
167 mainVerticalLayout = new QVBoxLayout;
168 widget = new QWidget;
169 widget->setLayout(mainVerticalLayout);
171 infoLabel = new QLabel;
172 infoLabel->setWordWrap(true);
175 setWindowTitle(tr("Add new XDXF dictionary"));
177 browseLayout = new QHBoxLayout;
179 QVBoxLayout* buttonLayout = new QVBoxLayout;
180 browseButton = new QPushButton(tr("Browse"));
181 browseButton->setMaximumWidth(150);
183 downloadButton = new QPushButton(tr("Download"));
184 downloadButton->setMaximumWidth(150);
186 infoLayout = new QHBoxLayout;
187 infoLabel->setText(tr("Dictionary file: not selected"));
189 browseLayout->addWidget(infoLabel,0,Qt::AlignLeft);
190 browseLayout->addLayout(buttonLayout);
191 buttonLayout->addWidget(browseButton,0,Qt::AlignLeft);
192 buttonLayout->addWidget(downloadButton,0,Qt::AlignLeft);
194 mainVerticalLayout->addLayout(browseLayout);
197 setWindowTitle(tr("XDXF Settings"));
199 infoLabel->setText(tr("Plugin: ") + plugin->type().toUpper() +"\n" +
200 tr("From: ") + plugin->langFrom() + "\n" +
201 tr("To: ") + plugin->langTo() + "\n" +
202 tr("Description: ") + plugin->name() + "\n" +
204 mainVerticalLayout->addWidget(infoLabel);
207 accentsLayout = new QHBoxLayout;
208 accentsCheckBox = new QCheckBox(tr("Strip accents"));
209 accentsCheckBox->setToolTip(accentsToolTip);
210 accentsLayout->addWidget(accentsCheckBox);
212 accentsInfoToolButton = new QToolButton;
213 accentsInfoToolButton->setIcon(QIcon::fromTheme("general_information"));
214 accentsLayout->addWidget(accentsInfoToolButton);
217 cacheLayout = new QHBoxLayout;
218 cacheCheckBox = new QCheckBox(tr("Optimize"));
219 cacheCheckBox->setToolTip(cacheToolTip);
220 cacheLayout->addWidget(cacheCheckBox);
223 cacheInfoToolButton = new QToolButton;
224 cacheInfoToolButton->setIcon(QIcon::fromTheme("general_information"));
225 cacheLayout->addWidget(cacheInfoToolButton);
228 mainVerticalLayout->addLayout(cacheLayout);
229 mainVerticalLayout->addLayout(accentsLayout);
231 //load old setting if exists
233 cacheCheckBox->setChecked(true);
234 accentsCheckBox->setChecked(true);
235 accentsCheckBox->setEnabled(false);
236 _generateCache = true;
238 _dictionaryFilePath = "";
240 else if(plugin && plugin->settings()->value("cached") == "true") {
241 cacheCheckBox->setChecked(true);
242 accentsCheckBox->setChecked(true);
243 accentsCheckBox->setEnabled(false);
244 _generateCache = true;
248 cacheCheckBox->setChecked(false);
249 _generateCache = false;
251 if(plugin->settings()->value("strip_accents") == "true") {
252 accentsCheckBox->setChecked(true);
256 accentsCheckBox->setChecked(false);
261 confirmButton = new QPushButton;
262 mainVerticalLayout->addWidget(confirmButton);
264 confirmButton->setText(tr("Add"));
266 confirmButton->setText(tr("Save settings"));
268 scrollArea = new QScrollArea;
269 scrollArea->setWidget(widget);
270 scrollArea->setWidgetResizable(true);
273 scrollArea->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
276 infoLabel->setMinimumWidth(250);
277 scrollArea->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
281 layout = new QHBoxLayout;
282 layout->addWidget(scrollArea);
286 setMinimumSize(385,200);
288 setMinimumHeight(350);
291 scrollArea->setLineWidth(0);
292 scrollArea->setMidLineWidth(0);
293 scrollArea->setFrameStyle(QFrame::NoFrame);
298 void XdxfDialog::setAccents(bool accents) {
303 void XdxfDialog::setGenerateCache(bool generate) {
304 _generateCache = generate;
307 _lastAccents = _accents;
308 emit setCheckedOptimalize(true);
309 emit setCheckedStrip(true);
312 emit setCheckedStrip(_lastAccents);
314 // accentsCheckBox->setEnabled(!generate);
317 _lastAccents = _accents;
318 accentsCheckBox->setChecked(true);
321 accentsCheckBox->setChecked(_lastAccents);
322 accentsCheckBox->setEnabled(!generate);
327 void XdxfDialog::selectFile() {
328 QString fileName = QFileDialog::getOpenFileName(this,
329 tr("Select dictionary file"),
331 tr("XDXF Files (*.xdxf)"),
334 if (!fileName.isEmpty()) {
335 _dictionaryFilePath = fileName;
337 emit setPath(tr("Dictionary file: %1").arg(fileName));
339 infoLabel->setText(tr("Dictionary file: %1").arg(fileName));
346 void XdxfDialog::fileDownloaded(QString) {
348 emit setPath(tr("Dictionary file: %1").arg(XdxfPlugin::dictDownloader.downloadedFile()));
349 _dictionaryFilePath = XdxfPlugin::dictDownloader.downloadedFile();
351 infoLabel->setText(tr("Dictionary file: %1").arg(XdxfPlugin::dictDownloader.downloadedFile()));
352 _dictionaryFilePath = XdxfPlugin::dictDownloader.downloadedFile();
358 void XdxfDialog::downloadFile() {
359 XdxfPlugin::dictDownloader.download(this);
363 void XdxfDialog::saveSettings() {
364 _settings = new Settings;
366 foreach(QString key, plugin->settings()->keys())
367 _settings->setValue(key, plugin->settings()->value(key));
370 _settings->setValue("path", _dictionaryFilePath);
372 _settings->setValue("generateCache", "true");
374 _settings->setValue("generateCache", "false");
377 _settings->setValue("strip_accents", "true");
379 _settings->setValue("strip_accents", "false");
383 void XdxfDialog::accept() {
384 if(type == New && _dictionaryFilePath.isEmpty()) {
385 Q_EMIT notify(Notify::Warning, tr("File path is not set"));
394 Settings* XdxfDialog::getSettings() {
400 void XdxfDialog::showCacheInfo() {
401 Q_EMIT notify(Notify::Warning, cacheToolTip);
405 void XdxfDialog::showAccentsInfo() {
406 Q_EMIT notify(Notify::Warning, accentsToolTip);