fa262a82ab14037115e6dde3891004818f4bad2b
[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
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
34     verticalLayout = new QVBoxLayout;
35
36     QWidget* w = new QWidget;
37     w->setLayout(verticalLayout);
38
39     setWindowTitle(tr("XDXF Settings"));
40
41
42     infoLabel = new QLabel(this);
43
44     infoLabel->setText(tr("Plugin: ") + plugin->type().toUpper() +"\n" +
45                    tr("From: ") + plugin->langFrom() + "\n" +
46                    tr("To: ") + plugin->langTo() + "\n" +
47                    tr("Description: ") + plugin->name() + "\n" +
48                    tr("License: ") + plugin->infoNote());
49     infoLabel->setWordWrap(true);
50
51
52     verticalLayout->addWidget(infoLabel);
53
54
55     accentsCheckBox = new QCheckBox(tr("Strip accents"));
56     /*(searching takes more time, "
57                      "but spelling don't have to be exact)*/
58     if(plugin->settings()->value("strip_accents") == "true")
59         accentsCheckBox->setChecked(true);
60     else
61         accentsCheckBox->setChecked(false);
62
63     cacheCheckBox = new QCheckBox(tr("Optimize for quicker searches"));
64 /*(may take some time)*/
65     if(plugin->settings()->value("cached") == "true") {
66         cacheCheckBox->setChecked(true);
67         accentsCheckBox->setChecked(true);
68         accentsCheckBox->setEnabled(false);
69         _generateCache = true;
70     }
71     else {
72         cacheCheckBox->setChecked(false);
73         _generateCache = false;
74     }
75
76     accentsToolButton = new QToolButton;
77     cacheToolButton = new QToolButton;
78
79     accentsToolButton->setIcon(QIcon::fromTheme("general_information"));
80     cacheToolButton->setIcon(QIcon::fromTheme("general_information"));
81
82     cacheLayout = new QHBoxLayout;
83     accentsLayout = new QHBoxLayout;
84
85     accentsLayout->addWidget(accentsCheckBox);
86     accentsLayout->addWidget(accentsToolButton);
87
88     cacheLayout->addWidget(cacheCheckBox);
89     cacheLayout->addWidget(cacheToolButton);
90
91     verticalLayout->addLayout(cacheLayout);
92     verticalLayout->addLayout(accentsLayout);
93
94
95
96     saveButton = new QPushButton(tr("Save settings"));
97
98     verticalLayout->addWidget(saveButton);
99
100     scrollArea = new QScrollArea;
101     scrollArea->setWidget(w);
102
103
104     QHBoxLayout* layout = new QHBoxLayout();
105
106     scrollArea->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
107     scrollArea->setWidgetResizable(true);
108     layout->addWidget(scrollArea);
109     setLayout(layout);
110
111
112
113     setModal(true);
114
115     connect(saveButton, SIGNAL(clicked()),
116             this, SLOT(accept()));
117
118     connect(cacheCheckBox, SIGNAL(toggled(bool)),
119             SLOT(setGenerateCache(bool)));
120
121     connect(accentsCheckBox, SIGNAL(clicked(bool)), SLOT(setAccents(bool)));
122
123
124     _dicitonaryFilePath = plugin->settings()->value("path");
125     lastAccents = accentsCheckBox->isChecked();
126 }
127
128
129 void XdxfSettingsDialog::setAccents(bool state) {
130     lastAccents = state;
131 }
132
133 void XdxfSettingsDialog::setGenerateCache(bool generate) {
134     _generateCache = generate;
135
136     if(generate)
137         accentsCheckBox->setChecked(true);
138     else
139         accentsCheckBox->setChecked(lastAccents);
140
141     accentsCheckBox->setEnabled(!generate);
142 }
143
144 bool XdxfSettingsDialog::generateCache() {
145     return _generateCache;
146 }
147
148 QString XdxfSettingsDialog::dicitonaryFilePath() {
149     return _dicitonaryFilePath;
150 }
151
152 Settings* XdxfSettingsDialog::getSettings(XdxfPlugin *plugin,
153                                           QWidget *parent) {
154     XdxfSettingsDialog settingsDialog(plugin, parent);
155
156
157     if(settingsDialog.exec()==QDialog::Accepted) {
158         Settings* settings = new Settings;
159         foreach(QString key, plugin->settings()->keys())
160             settings->setValue(key, plugin->settings()->value(key));
161         settings->setValue("path", settingsDialog.dicitonaryFilePath());
162
163         if(settingsDialog.generateCache()) {
164             settings->setValue("generateCache", "true");
165         }
166         else {
167             settings->setValue("generateCache", "false");
168         }
169
170         if(settingsDialog.accentsCheckBox->isChecked())
171             settings->setValue("strip_accents", "true");
172         else
173             settings->setValue("strip_accents", "false");
174
175         plugin->setSettings(settings);
176         delete settings;
177         return 0;
178     }
179
180     return 0;
181 }
182
183