Added option for stripping accents on add dict dialog;
[mdictionary] / trunk / src / plugins / xdxf / src / XdxfSettingsDialog.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 XdxfSettingsDialog.cpp
23 */
24 //Created by Mateusz Półrola
25
26 #include "XdxfSettingsDialog.h"
27 #include <QDebug>
28
29 XdxfSettingsDialog::XdxfSettingsDialog(XdxfPlugin *plugin, QWidget *parent) :
30     QDialog(parent)
31 {
32     this->plugin = plugin;
33     verticalLayout = new QVBoxLayout(this);
34     setLayout(verticalLayout);
35
36     setWindowTitle(tr("XDXF Settings"));
37
38
39     infoLabel = new QLabel(this);
40
41     infoLabel->setText(tr("Plugin type: ") + plugin->type() +"\n" +
42                    tr("From: ") + plugin->langFrom() + "\n" +
43                    tr("To: ") + plugin->langTo() + "\n" +
44                    tr("Description: ") + plugin->name());
45
46     verticalLayout->addWidget(infoLabel);
47
48     browseLayout = new QHBoxLayout(this);
49     verticalLayout->addLayout(browseLayout);
50
51     browseButton =  new QPushButton(tr("Browse"),this);
52     browseLabel = new QLabel(tr("Dictionary file: ") +
53                              plugin->settings()->value("path"),this);
54
55     browseLayout->addWidget(browseLabel);
56     browseLayout->addWidget(browseButton,0, Qt::AlignRight);
57
58
59     cacheLayout = new QHBoxLayout(this);
60     verticalLayout->addLayout(cacheLayout);
61     accentsCheckBox = new QCheckBox(tr("Strip accents \n(searching takes more time, "
62                  "but spelling don't have to be exact)"));
63     verticalLayout->addWidget(accentsCheckBox);
64
65     if(plugin->settings()->value("strip_accents") == "true")
66         accentsCheckBox->setChecked(true);
67     else
68         accentsCheckBox->setChecked(false);
69
70     cacheCheckBox = new QCheckBox(tr("Optimize for quicker searches (may take some time)"),this);
71     if(plugin->settings()->value("cached") == "true") {
72         cacheCheckBox->setChecked(true);
73         _generateCache = true;
74     }
75     else {
76         cacheCheckBox->setChecked(false);
77         _generateCache = false;
78     }
79
80     cacheLayout->addWidget(cacheCheckBox);
81
82     saveButton = new QPushButton(tr("Save settings"),this);
83
84     verticalLayout->addWidget(saveButton);
85
86     setModal(true);
87
88     connect(browseButton, SIGNAL(clicked()),
89             this, SLOT(selectFile()));
90
91     connect(saveButton, SIGNAL(clicked()),
92             this, SLOT(accept()));
93
94     connect(cacheCheckBox, SIGNAL(toggled(bool)),
95             SLOT(setGenerateCache(bool)));
96
97     _dicitonaryFilePath = plugin->settings()->value("path");
98 }
99
100 void XdxfSettingsDialog::setGenerateCache(bool generate) {
101     _generateCache = generate;
102 }
103
104 bool XdxfSettingsDialog::generateCache() {
105     return _generateCache;
106 }
107
108 void XdxfSettingsDialog::selectFile() {
109     QString fileName = QFileDialog::getOpenFileName(this,
110                                      tr("Select dictionary file"),
111                                      "",
112                                      tr("XDXF Files (*.xdxf)"),
113                                      NULL,
114                                      NULL);
115     if (!fileName.isEmpty()) {
116         browseLabel->setText(tr("Dictionary file: ") + fileName);
117         _dicitonaryFilePath = fileName;
118     }
119 }
120
121 QString XdxfSettingsDialog::dicitonaryFilePath() {
122     return _dicitonaryFilePath;
123 }
124
125 Settings* XdxfSettingsDialog::getSettings(XdxfPlugin *plugin,
126                                           QWidget *parent) {
127     XdxfSettingsDialog settingsDialog(plugin, parent);
128
129
130     if(settingsDialog.exec()==QDialog::Accepted) {
131         Settings* settings = new Settings;
132         foreach(QString key, plugin->settings()->keys())
133             settings->setValue(key, plugin->settings()->value(key));
134         settings->setValue("path", settingsDialog.dicitonaryFilePath());
135
136         if(settingsDialog.generateCache()) {
137             settings->setValue("generateCache", "true");
138         }
139         else {
140             settings->setValue("generateCache", "false");
141         }
142
143         if(settingsDialog.accentsCheckBox->isChecked())
144             settings->setValue("strip_accents", "true");
145         else
146             settings->setValue("strip_accents", "false");
147
148         plugin->setSettings(settings);
149         return 0;
150     }
151
152     return 0;
153 }
154
155