test change
[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
111     #ifdef Q_WS_MAEMO_5
112         cacheInfoToolButton = new QToolButton;
113         cacheInfoToolButton->setIcon(QIcon::fromTheme("general_information"));
114         cacheLayout->addWidget(cacheInfoToolButton);
115     #endif
116
117     mainVerticalLayout->addLayout(cacheLayout);
118     mainVerticalLayout->addLayout(accentsLayout);
119
120
121     //load old setting if exists
122     if(!plugin) {
123         cacheCheckBox->setChecked(true);
124         accentsCheckBox->setChecked(true);
125         accentsCheckBox->setEnabled(false);
126         _generateCache = true;
127         _accents = true;
128         _dictionaryFilePath = "";
129     }
130     else if(plugin && plugin->settings()->value("cached") == "true") {
131         cacheCheckBox->setChecked(true);
132         accentsCheckBox->setChecked(true);
133         accentsCheckBox->setEnabled(false);
134         _generateCache = true;
135         _accents = true;
136     }
137     else {
138         cacheCheckBox->setChecked(false);
139         _generateCache = false;
140
141         if(plugin->settings()->value("strip_accents") == "true") {
142             accentsCheckBox->setChecked(true);
143             _accents = true;
144         }
145         else {
146             accentsCheckBox->setChecked(false);
147             _accents = false;
148         }
149     }
150
151     confirmButton = new QPushButton;
152     mainVerticalLayout->addWidget(confirmButton);
153     if(type == New) {
154         confirmButton->setText(tr("Add"));
155     }
156     else {
157         confirmButton->setText(tr("Save settings"));
158     }
159
160     scrollArea = new QScrollArea;
161     scrollArea->setWidget(widget);
162     scrollArea->setWidgetResizable(true);
163     #ifdef Q_WS_MAEMO_5
164         scrollArea->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
165     #else
166         if(type==New) {
167             infoLabel->setMinimumWidth(200);
168             setMinimumSize(sizeHint().width()*1.5, sizeHint().height()*1.2);
169             setMaximumSize(sizeHint().width()*1.7, sizeHint().height()*1.5);
170             scrollArea->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
171         }
172     #endif
173
174     layout = new QHBoxLayout;
175     layout->addWidget(scrollArea);
176     setLayout(layout);
177
178     #ifndef Q_WS_MAEMO_5
179         setMinimumSize(400,200);
180     #else
181         setMinimumHeight(350);
182     #endif
183
184     scrollArea->setLineWidth(0);
185     scrollArea->setMidLineWidth(0);
186     scrollArea->setFrameStyle(QFrame::NoFrame);
187
188
189 }
190
191
192 void XdxfDialog::setAccents(bool accents) {
193     _accents = accents;
194 }
195
196 void XdxfDialog::setGenerateCache(bool generate) {
197     _generateCache = generate;
198
199     if(generate) {
200         _lastAccents = _accents;
201         accentsCheckBox->setChecked(true);
202     }
203     else
204         accentsCheckBox->setChecked(_lastAccents);
205
206     accentsCheckBox->setEnabled(!generate);
207 }
208
209 void XdxfDialog::selectFile() {
210     QString fileName = QFileDialog::getOpenFileName(this,
211                                      tr("Select dictionary file"),
212                                      _dictionaryFilePath,
213                                      tr("XDXF Files (*.xdxf)"),
214                                      NULL,
215                                      NULL);
216
217     if (!fileName.isEmpty()) {
218         infoLabel->setText(tr("Dictionary file: %1").arg(fileName));
219         _dictionaryFilePath = fileName;
220         updateGeometry();
221     }
222 }
223
224 void XdxfDialog::saveSettings() {
225     _settings = new Settings;
226     if(plugin) {
227         foreach(QString key, plugin->settings()->keys())
228             _settings->setValue(key, plugin->settings()->value(key));
229     }
230     else {
231         _settings->setValue("path", _dictionaryFilePath);
232     }
233
234     if(_generateCache)
235         _settings->setValue("generateCache", "true");
236     else
237         _settings->setValue("generateCache", "false");
238
239     if(_accents)
240         _settings->setValue("strip_accents", "true");
241     else
242         _settings->setValue("strip_accents", "false");
243 }
244
245 void XdxfDialog::accept() {
246     if(type == New && _dictionaryFilePath.isEmpty()) {
247         Q_EMIT notify(Notify::Warning, tr("File path is not set"));
248
249         return;
250     }
251
252     saveSettings();
253     QDialog::accept();
254 }
255
256 Settings* XdxfDialog::getSettings() {
257     return _settings;
258 }
259
260 #ifdef Q_WS_MAEMO_5
261     void XdxfDialog::showCacheInfo() {
262         Q_EMIT notify(Notify::Warning, cacheToolTip);
263     }
264
265     void XdxfDialog::showAccentsInfo() {
266         Q_EMIT notify(Notify::Warning, accentsToolTip);
267     }
268 #endif
269