Skelleton of StarDict
[mdictionary] / src / plugins / stardict / StarDialog.cpp
diff --git a/src/plugins/stardict/StarDialog.cpp b/src/plugins/stardict/StarDialog.cpp
new file mode 100644 (file)
index 0000000..e4ca1bf
--- /dev/null
@@ -0,0 +1,268 @@
+/*******************************************************************************
+
+    This file is part of mDictionary.
+
+    mDictionary is free software: you can redistribute it and/or modify
+    it under the terms of the GNU General Public License as published by
+    the Free Software Foundation, either version 3 of the License, or
+    (at your option) any later version.
+
+    mDictionary is distributed in the hope that it will be useful,
+    but WITHOUT ANY WARRANTY; without even the implied warranty of
+    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+    GNU General Public License for more details.
+
+    You should have received a copy of the GNU General Public License
+    along with mDictionary.  If not, see <http://www.gnu.org/licenses/>.
+
+    Copyright 2010 Comarch S.A.
+
+*******************************************************************************/
+
+//Created by Mateusz Półrola
+
+#include "XdxfDialog.h"
+#include <QDebug>
+
+XdxfDialog::XdxfDialog(XdxfPlugin *plugin,
+                       XdxfDialogType type,
+                       QWidget *parent) :
+    QDialog(parent) {
+    this->plugin = plugin;
+    this->type = type;
+
+
+    cacheToolTip = tr("Optimize for quicker searches (may take some time)");
+    accentsToolTip = tr("Strip accents (searching takes more time, but spelling doesn't have to be exact)");
+
+    initializeUI();
+
+    connect(cacheCheckBox, SIGNAL(toggled(bool)),
+            this, SLOT(setGenerateCache(bool)));
+
+    connect(accentsCheckBox, SIGNAL(toggled(bool)),
+            this, SLOT(setAccents(bool)));
+
+    #ifdef Q_WS_MAEMO_5
+        connect(accentsInfoToolButton, SIGNAL(clicked()),
+                 this, SLOT(showAccentsInfo()));
+
+        connect(cacheInfoToolButton, SIGNAL(clicked()),
+                 this, SLOT(showCacheInfo()));
+    #endif
+
+    if(type == New) {
+        connect(browseButton, SIGNAL(clicked()),
+                this, SLOT(selectFile()));
+    }
+
+    connect(confirmButton, SIGNAL(clicked()),
+            this, SLOT(accept()));
+
+}
+
+
+void XdxfDialog::initializeUI() {
+    mainVerticalLayout = new QVBoxLayout;
+    widget = new QWidget;
+    widget->setLayout(mainVerticalLayout);
+
+    infoLabel = new QLabel;
+    infoLabel->setWordWrap(true);
+
+    if(type == New) {
+        setWindowTitle(tr("Add new XDXF dictionary"));
+
+        browseLayout = new QHBoxLayout;
+        browseButton = new QPushButton(tr("Browse"));
+        infoLabel->setText(tr("Dictionary file: not selected"));
+
+        browseLayout->addWidget(infoLabel, 0, Qt::AlignLeft);
+        browseLayout->addWidget(browseButton, 0, Qt::AlignRight);
+
+        mainVerticalLayout->addLayout(browseLayout);
+    }
+    else {
+        setWindowTitle(tr("XDXF Settings"));
+
+        infoLabel->setText(tr("Plugin: ") + plugin->type().toUpper() +"\n" +
+                       tr("From: ") + plugin->langFrom() + "\n" +
+                       tr("To: ") + plugin->langTo() + "\n" +
+                       tr("Description: ") + plugin->name() + "\n" +
+                       plugin->infoNote());
+        mainVerticalLayout->addWidget(infoLabel);
+    }
+
+    accentsLayout = new QHBoxLayout;
+    accentsCheckBox = new QCheckBox(tr("Strip accents"));
+    accentsCheckBox->setToolTip(accentsToolTip);
+    accentsLayout->addWidget(accentsCheckBox); 
+    #ifdef Q_WS_MAEMO_5
+        accentsInfoToolButton = new QToolButton;
+        accentsInfoToolButton->setIcon(QIcon::fromTheme("general_information"));
+        accentsLayout->addWidget(accentsInfoToolButton);
+    #endif
+
+    cacheLayout = new QHBoxLayout;
+    cacheCheckBox = new QCheckBox(tr("Optimize"));
+    cacheCheckBox->setToolTip(cacheToolTip);
+    cacheLayout->addWidget(cacheCheckBox);
+    #ifdef Q_WS_MAEMO_5
+        cacheInfoToolButton = new QToolButton;
+        cacheInfoToolButton->setIcon(QIcon::fromTheme("general_information"));
+        cacheLayout->addWidget(cacheInfoToolButton);
+    #endif
+
+    mainVerticalLayout->addLayout(cacheLayout);
+    mainVerticalLayout->addLayout(accentsLayout);
+
+
+    //load old setting if exists
+    if(!plugin) {
+        cacheCheckBox->setChecked(true);
+        accentsCheckBox->setChecked(true);
+        accentsCheckBox->setEnabled(false);
+        _generateCache = true;
+        _accents = true;
+        _dictionaryFilePath = "";
+    }
+    else if(plugin && plugin->settings()->value("cached") == "true") {
+        cacheCheckBox->setChecked(true);
+        accentsCheckBox->setChecked(true);
+        accentsCheckBox->setEnabled(false);
+        _generateCache = true;
+        _accents = true;
+    }
+    else {
+        cacheCheckBox->setChecked(false);
+        _generateCache = false;
+
+        if(plugin->settings()->value("strip_accents") == "true") {
+            accentsCheckBox->setChecked(true);
+            _accents = true;
+        }
+        else {
+            accentsCheckBox->setChecked(false);
+            _accents = false;
+        }
+    }
+
+    confirmButton = new QPushButton;
+    mainVerticalLayout->addWidget(confirmButton);
+    if(type == New) {
+        confirmButton->setText(tr("Add"));
+    }
+    else {
+        confirmButton->setText(tr("Save settings"));
+    }
+
+    scrollArea = new QScrollArea;
+    scrollArea->setWidget(widget);
+    scrollArea->setWidgetResizable(true);
+    #ifdef Q_WS_MAEMO_5
+        scrollArea->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
+    #else
+        if(type==New) {
+            infoLabel->setMinimumWidth(200);
+            setMinimumSize(sizeHint().width()*1.5, sizeHint().height()*1.2);
+            setMaximumSize(sizeHint().width()*1.7, sizeHint().height()*1.5);
+            scrollArea->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
+        }
+    #endif
+
+    layout = new QHBoxLayout;
+    layout->addWidget(scrollArea);
+    setLayout(layout);
+
+    #ifndef Q_WS_MAEMO_5
+        setMinimumSize(400,200);
+    #else
+        setMinimumHeight(350);
+    #endif
+
+    scrollArea->setLineWidth(0);
+    scrollArea->setMidLineWidth(0);
+    scrollArea->setFrameStyle(QFrame::NoFrame);
+
+
+}
+
+
+void XdxfDialog::setAccents(bool accents) {
+    _accents = accents;
+}
+
+void XdxfDialog::setGenerateCache(bool generate) {
+    _generateCache = generate;
+
+    if(generate) {
+        _lastAccents = _accents;
+        accentsCheckBox->setChecked(true);
+    }
+    else
+        accentsCheckBox->setChecked(_lastAccents);
+
+    accentsCheckBox->setEnabled(!generate);
+}
+
+void XdxfDialog::selectFile() {
+    QString fileName = QFileDialog::getOpenFileName(this,
+                                     tr("Select dictionary file"),
+                                     _dictionaryFilePath,
+                                     tr("XDXF Files (*.xdxf)"),
+                                     NULL,
+                                     NULL);
+
+    if (!fileName.isEmpty()) {
+        infoLabel->setText(tr("Dictionary file: %1").arg(fileName));
+        _dictionaryFilePath = fileName;
+        updateGeometry();
+    }
+}
+
+void XdxfDialog::saveSettings() {
+    _settings = new Settings;
+    if(plugin) {
+        foreach(QString key, plugin->settings()->keys())
+            _settings->setValue(key, plugin->settings()->value(key));
+    }
+    else {
+        _settings->setValue("path", _dictionaryFilePath);
+    }
+
+    if(_generateCache)
+        _settings->setValue("generateCache", "true");
+    else
+        _settings->setValue("generateCache", "false");
+
+    if(_accents)
+        _settings->setValue("strip_accents", "true");
+    else
+        _settings->setValue("strip_accents", "false");
+}
+
+void XdxfDialog::accept() {
+    if(type == New && _dictionaryFilePath.isEmpty()) {
+        Q_EMIT notify(Notify::Warning, tr("File path is not set"));
+
+        return;
+    }
+
+    saveSettings();
+    QDialog::accept();
+}
+
+Settings* XdxfDialog::getSettings() {
+    return _settings;
+}
+
+#ifdef Q_WS_MAEMO_5
+    void XdxfDialog::showCacheInfo() {
+        Q_EMIT notify(Notify::Warning, cacheToolTip);
+    }
+
+    void XdxfDialog::showAccentsInfo() {
+        Q_EMIT notify(Notify::Warning, accentsToolTip);
+    }
+#endif
+