Some changes in look of xdxf dialog on desktop
[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 don'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                        tr("License: ") + 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 for quicker searches"));
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     #endif
165
166     layout = new QHBoxLayout;
167     layout->addWidget(scrollArea);
168     setLayout(layout);
169
170     #ifndef Q_WS_MAEMO_5
171         setMinimumSize(400,200);
172     #endif
173 }
174
175
176 void XdxfDialog::setAccents(bool accents) {
177     _accents = accents;
178 }
179
180 void XdxfDialog::setGenerateCache(bool generate) {
181     _generateCache = generate;
182
183     if(generate) {
184         _lastAccents = _accents;
185         accentsCheckBox->setChecked(true);
186     }
187     else
188         accentsCheckBox->setChecked(_lastAccents);
189
190     accentsCheckBox->setEnabled(!generate);
191 }
192
193 void XdxfDialog::selectFile() {
194     QString fileName = QFileDialog::getOpenFileName(this,
195                                      tr("Select dictionary file"),
196                                      _dictionaryFilePath,
197                                      tr("XDXF Files (*.xdxf)"),
198                                      NULL,
199                                      NULL);
200
201     if (!fileName.isEmpty()) {
202         infoLabel->setText(tr("Dictionary file: %1").arg(fileName));
203         _dictionaryFilePath = fileName;
204     }
205 }
206
207 void XdxfDialog::saveSettings() {
208     _settings = new Settings;
209     if(plugin) {
210         foreach(QString key, plugin->settings()->keys())
211             _settings->setValue(key, plugin->settings()->value(key));
212     }
213     else {
214         _settings->setValue("path", _dictionaryFilePath);
215     }
216
217     if(_generateCache)
218         _settings->setValue("generateCache", "true");
219     else
220         _settings->setValue("generateCache", "false");
221
222     if(_accents)
223         _settings->setValue("strip_accents", "true");
224     else
225         _settings->setValue("strip_accents", "false");
226 }
227
228 void XdxfDialog::accept() {
229     if(type == New && _dictionaryFilePath.isEmpty()) {
230         Q_EMIT notify(Notify::Warning, tr("File path is not set"));
231
232         return;
233     }
234
235     saveSettings();
236     QDialog::accept();
237 }
238
239 Settings* XdxfDialog::getSettings() {
240     return _settings;
241 }
242
243 #ifdef Q_WS_MAEMO_5
244     void XdxfDialog::showCacheInfo() {
245         Q_EMIT notify(Notify::Warning, cacheToolTip);
246     }
247
248     void XdxfDialog::showAccentsInfo() {
249         Q_EMIT notify(Notify::Warning, accentsToolTip);
250     }
251 #endif
252