changes in xsl and css, 0 instead of NULL in code
[mdictionary] / src / plugins / xdxf / 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();
34        setLayout(verticalLayout);
35
36     setWindowTitle(tr("XDXF Settings"));
37
38
39     infoLabel = new QLabel(this);
40
41     infoLabel->setText(tr("Plugin: ") + plugin->type().toUpper() +"\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();
49     verticalLayout->addLayout(browseLayout);
50
51     browseButton =  new QPushButton(tr("Browse"));
52     browseLabel = new QLabel(tr("Dictionary file: ") +
53                              plugin->settings()->value("path"));
54
55     browseLayout->addWidget(browseLabel);
56     browseLayout->addWidget(browseButton,0, Qt::AlignRight);
57
58
59     cacheLayout = new QHBoxLayout();
60     verticalLayout->insertLayout(-1,cacheLayout,0);
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         accentsCheckBox->setChecked(true);
74         accentsCheckBox->setEnabled(false);
75         _generateCache = true;
76     }
77     else {
78         cacheCheckBox->setChecked(false);
79         _generateCache = false;
80     }
81
82     cacheLayout->addWidget(cacheCheckBox);
83
84     saveButton = new QPushButton(tr("Save settings"));
85
86     verticalLayout->addWidget(saveButton);
87
88     setModal(true);
89
90     connect(browseButton, SIGNAL(clicked()),
91             this, SLOT(selectFile()));
92
93     connect(saveButton, SIGNAL(clicked()),
94             this, SLOT(accept()));
95
96     connect(cacheCheckBox, SIGNAL(toggled(bool)),
97             SLOT(setGenerateCache(bool)));
98
99     connect(accentsCheckBox, SIGNAL(clicked(bool)), SLOT(setAccents(bool)));
100
101
102     _dicitonaryFilePath = plugin->settings()->value("path");
103     lastAccents = accentsCheckBox->isChecked();
104 }
105
106
107 void XdxfSettingsDialog::setAccents(bool state) {
108     lastAccents = state;
109 }
110
111
112 void XdxfSettingsDialog::setGenerateCache(bool generate) {
113     _generateCache = generate;
114
115     if(generate)
116         accentsCheckBox->setChecked(true);
117     else
118         accentsCheckBox->setChecked(lastAccents);
119
120     accentsCheckBox->setEnabled(!generate);
121 }
122
123 bool XdxfSettingsDialog::generateCache() {
124     return _generateCache;
125 }
126
127 void XdxfSettingsDialog::selectFile() {
128     QString fileName = QFileDialog::getOpenFileName(this,
129                                      tr("Select dictionary file"),
130                                      "",
131                                      tr("XDXF Files (*.xdxf)"),
132                                      0,
133                                      0);
134     if (!fileName.isEmpty()) {
135         browseLabel->setText(tr("Dictionary file: ") + fileName);
136         _dicitonaryFilePath = fileName;
137     }    
138 }
139
140 QString XdxfSettingsDialog::dicitonaryFilePath() {
141     return _dicitonaryFilePath;
142 }
143
144 Settings* XdxfSettingsDialog::getSettings(XdxfPlugin *plugin,
145                                           QWidget *parent) {
146     XdxfSettingsDialog settingsDialog(plugin, parent);
147
148
149     if(settingsDialog.exec()==QDialog::Accepted) {
150         Settings* settings = new Settings;
151         foreach(QString key, plugin->settings()->keys())
152             settings->setValue(key, plugin->settings()->value(key));
153         settings->setValue("path", settingsDialog.dicitonaryFilePath());
154
155         if(settingsDialog.generateCache()) {
156             settings->setValue("generateCache", "true");
157         }
158         else {
159             settings->setValue("generateCache", "false");
160         }
161
162         if(settingsDialog.accentsCheckBox->isChecked())
163             settings->setValue("strip_accents", "true");
164         else
165             settings->setValue("strip_accents", "false");
166
167         plugin->setSettings(settings);
168         delete settings;
169         return 0;
170     }
171
172     return 0;
173 }
174
175