e4ca1bf027aa029da15657130c633239d4ef016e
[mdictionary] / src / plugins / xdxf / XdxfDialog.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 //Created by Mateusz Półrola
23
24 #include "XdxfDialog.h"
25 #include <QDebug>
26
27 XdxfDialog::XdxfDialog(XdxfPlugin *plugin,
28                        XdxfDialogType type,
29                        QWidget *parent) :
30     QDialog(parent) {
31     this->plugin = plugin;
32     this->type = type;
33
34
35     cacheToolTip = tr("Optimize for quicker searches (may take some time)");
36     accentsToolTip = tr("Strip accents (searching takes more time, but spelling doesn't have to be exact)");
37
38     initializeUI();
39
40     connect(cacheCheckBox, SIGNAL(toggled(bool)),
41             this, SLOT(setGenerateCache(bool)));
42
43     connect(accentsCheckBox, SIGNAL(toggled(bool)),
44             this, SLOT(setAccents(bool)));
45
46     #ifdef Q_WS_MAEMO_5
47         connect(accentsInfoToolButton, SIGNAL(clicked()),
48                  this, SLOT(showAccentsInfo()));
49
50         connect(cacheInfoToolButton, SIGNAL(clicked()),
51                  this, SLOT(showCacheInfo()));
52     #endif
53
54     if(type == New) {
55         connect(browseButton, SIGNAL(clicked()),
56                 this, SLOT(selectFile()));
57     }
58
59     connect(confirmButton, SIGNAL(clicked()),
60             this, SLOT(accept()));
61
62 }
63
64
65 void XdxfDialog::initializeUI() {
66     mainVerticalLayout = new QVBoxLayout;
67     widget = new QWidget;
68     widget->setLayout(mainVerticalLayout);
69
70     infoLabel = new QLabel;
71     infoLabel->setWordWrap(true);
72
73     if(type == New) {
74         setWindowTitle(tr("Add new XDXF dictionary"));
75
76         browseLayout = new QHBoxLayout;
77         browseButton = new QPushButton(tr("Browse"));
78         infoLabel->setText(tr("Dictionary file: not selected"));
79
80         browseLayout->addWidget(infoLabel, 0, Qt::AlignLeft);
81         browseLayout->addWidget(browseButton, 0, Qt::AlignRight);
82
83         mainVerticalLayout->addLayout(browseLayout);
84     }
85     else {
86         setWindowTitle(tr("XDXF Settings"));
87
88         infoLabel->setText(tr("Plugin: ") + plugin->type().toUpper() +"\n" +
89                        tr("From: ") + plugin->langFrom() + "\n" +
90                        tr("To: ") + plugin->langTo() + "\n" +
91                        tr("Description: ") + plugin->name() + "\n" +
92                        plugin->infoNote());
93         mainVerticalLayout->addWidget(infoLabel);
94     }
95
96     accentsLayout = new QHBoxLayout;
97     accentsCheckBox = new QCheckBox(tr("Strip accents"));
98     accentsCheckBox->setToolTip(accentsToolTip);
99     accentsLayout->addWidget(accentsCheckBox); 
100     #ifdef Q_WS_MAEMO_5
101         accentsInfoToolButton = new QToolButton;
102         accentsInfoToolButton->setIcon(QIcon::fromTheme("general_information"));
103         accentsLayout->addWidget(accentsInfoToolButton);
104     #endif
105
106     cacheLayout = new QHBoxLayout;
107     cacheCheckBox = new QCheckBox(tr("Optimize"));
108     cacheCheckBox->setToolTip(cacheToolTip);
109     cacheLayout->addWidget(cacheCheckBox);
110     #ifdef Q_WS_MAEMO_5
111         cacheInfoToolButton = new QToolButton;
112         cacheInfoToolButton->setIcon(QIcon::fromTheme("general_information"));
113         cacheLayout->addWidget(cacheInfoToolButton);
114     #endif
115
116     mainVerticalLayout->addLayout(cacheLayout);
117     mainVerticalLayout->addLayout(accentsLayout);
118
119
120     //load old setting if exists
121     if(!plugin) {
122         cacheCheckBox->setChecked(true);
123         accentsCheckBox->setChecked(true);
124         accentsCheckBox->setEnabled(false);
125         _generateCache = true;
126         _accents = true;
127         _dictionaryFilePath = "";
128     }
129     else if(plugin && plugin->settings()->value("cached") == "true") {
130         cacheCheckBox->setChecked(true);
131         accentsCheckBox->setChecked(true);
132         accentsCheckBox->setEnabled(false);
133         _generateCache = true;
134         _accents = true;
135     }
136     else {
137         cacheCheckBox->setChecked(false);
138         _generateCache = false;
139
140         if(plugin->settings()->value("strip_accents") == "true") {
141             accentsCheckBox->setChecked(true);
142             _accents = true;
143         }
144         else {
145             accentsCheckBox->setChecked(false);
146             _accents = false;
147         }
148     }
149
150     confirmButton = new QPushButton;
151     mainVerticalLayout->addWidget(confirmButton);
152     if(type == New) {
153         confirmButton->setText(tr("Add"));
154     }
155     else {
156         confirmButton->setText(tr("Save settings"));
157     }
158
159     scrollArea = new QScrollArea;
160     scrollArea->setWidget(widget);
161     scrollArea->setWidgetResizable(true);
162     #ifdef Q_WS_MAEMO_5
163         scrollArea->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
164     #else
165         if(type==New) {
166             infoLabel->setMinimumWidth(200);
167             setMinimumSize(sizeHint().width()*1.5, sizeHint().height()*1.2);
168             setMaximumSize(sizeHint().width()*1.7, sizeHint().height()*1.5);
169             scrollArea->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
170         }
171     #endif
172
173     layout = new QHBoxLayout;
174     layout->addWidget(scrollArea);
175     setLayout(layout);
176
177     #ifndef Q_WS_MAEMO_5
178         setMinimumSize(400,200);
179     #else
180         setMinimumHeight(350);
181     #endif
182
183     scrollArea->setLineWidth(0);
184     scrollArea->setMidLineWidth(0);
185     scrollArea->setFrameStyle(QFrame::NoFrame);
186
187
188 }
189
190
191 void XdxfDialog::setAccents(bool accents) {
192     _accents = accents;
193 }
194
195 void XdxfDialog::setGenerateCache(bool generate) {
196     _generateCache = generate;
197
198     if(generate) {
199         _lastAccents = _accents;
200         accentsCheckBox->setChecked(true);
201     }
202     else
203         accentsCheckBox->setChecked(_lastAccents);
204
205     accentsCheckBox->setEnabled(!generate);
206 }
207
208 void XdxfDialog::selectFile() {
209     QString fileName = QFileDialog::getOpenFileName(this,
210                                      tr("Select dictionary file"),
211                                      _dictionaryFilePath,
212                                      tr("XDXF Files (*.xdxf)"),
213                                      NULL,
214                                      NULL);
215
216     if (!fileName.isEmpty()) {
217         infoLabel->setText(tr("Dictionary file: %1").arg(fileName));
218         _dictionaryFilePath = fileName;
219         updateGeometry();
220     }
221 }
222
223 void XdxfDialog::saveSettings() {
224     _settings = new Settings;
225     if(plugin) {
226         foreach(QString key, plugin->settings()->keys())
227             _settings->setValue(key, plugin->settings()->value(key));
228     }
229     else {
230         _settings->setValue("path", _dictionaryFilePath);
231     }
232
233     if(_generateCache)
234         _settings->setValue("generateCache", "true");
235     else
236         _settings->setValue("generateCache", "false");
237
238     if(_accents)
239         _settings->setValue("strip_accents", "true");
240     else
241         _settings->setValue("strip_accents", "false");
242 }
243
244 void XdxfDialog::accept() {
245     if(type == New && _dictionaryFilePath.isEmpty()) {
246         Q_EMIT notify(Notify::Warning, tr("File path is not set"));
247
248         return;
249     }
250
251     saveSettings();
252     QDialog::accept();
253 }
254
255 Settings* XdxfDialog::getSettings() {
256     return _settings;
257 }
258
259 #ifdef Q_WS_MAEMO_5
260     void XdxfDialog::showCacheInfo() {
261         Q_EMIT notify(Notify::Warning, cacheToolTip);
262     }
263
264     void XdxfDialog::showAccentsInfo() {
265         Q_EMIT notify(Notify::Warning, accentsToolTip);
266     }
267 #endif
268