New xdxf dialog, one which provides methods to add new or change setting of existing...
[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     initializeUI();
35
36     connect(cacheCheckBox, SIGNAL(toggled(bool)),
37             this, SLOT(setGenerateCache(bool)));
38
39     connect(accentsCheckBox, SIGNAL(toggled(bool)),
40             this, SLOT(setAccents(bool)));
41
42     connect(accentsInfoToolButton, SIGNAL(clicked()),
43              this, SLOT(showAccentsInfo()));
44
45     connect(cacheInfoToolButton, SIGNAL(clicked()),
46              this, SLOT(showCacheInfo()));
47
48     if(type == New) {
49         connect(browseButton, SIGNAL(clicked()),
50                 this, SLOT(selectFile()));
51
52         connect(addButton, SIGNAL(clicked()),
53                 this, SLOT(accept()));
54     }
55     else {
56         connect(saveButton, SIGNAL(clicked()),
57                 this, SLOT(accept()));
58     }
59 }
60
61
62 void XdxfDialog::initializeUI() {
63     mainVerticalLayout = new QVBoxLayout;
64     widget = new QWidget;
65     widget->setLayout(mainVerticalLayout);
66
67     if(type == New) {
68         setWindowTitle(tr("Add new XDXF dictionary"));
69
70
71         browseLayout = new QHBoxLayout;
72         browseButton = new QPushButton(tr("Browse"));
73         browseLabel = new QLabel(tr("Dictionary file: not selected"));
74
75         browseLayout->addWidget(browseLabel, 0, Qt::AlignLeft);
76         browseLayout->addWidget(browseButton, 0, Qt::AlignRight);
77
78         mainVerticalLayout->addLayout(browseLayout);
79     }
80     else {
81         setWindowTitle(tr("XDXF Settings"));
82
83
84         infoLabel = new QLabel;
85
86         infoLabel->setText(tr("Plugin: ") + plugin->type().toUpper() +"\n" +
87                        tr("From: ") + plugin->langFrom() + "\n" +
88                        tr("To: ") + plugin->langTo() + "\n" +
89                        tr("Description: ") + plugin->name() + "\n" +
90                        tr("License: ") + plugin->infoNote());
91         infoLabel->setWordWrap(true);
92         mainVerticalLayout->addWidget(infoLabel);
93     }
94
95     accentsCheckBox = new QCheckBox(tr("Strip accents"));
96     accentsInfoToolButton = new QToolButton;
97     accentsInfoToolButton->setIcon(QIcon::fromTheme("general_information"));
98
99     accentsLayout = new QHBoxLayout;
100     accentsLayout->addWidget(accentsCheckBox);
101     accentsLayout->addWidget(accentsInfoToolButton);
102
103
104     cacheCheckBox = new QCheckBox(tr("Optimize for quicker searches"));
105     cacheInfoToolButton = new QToolButton;
106     cacheInfoToolButton->setIcon(QIcon::fromTheme("general_information"));
107
108     cacheLayout = new QHBoxLayout;
109     cacheLayout->addWidget(cacheCheckBox);
110     cacheLayout->addWidget(cacheInfoToolButton);
111
112     mainVerticalLayout->addLayout(cacheLayout);
113     mainVerticalLayout->addLayout(accentsLayout);
114
115
116     //load old setting if exists
117     if(!plugin) {
118         cacheCheckBox->setChecked(true);
119         accentsCheckBox->setChecked(true);
120         accentsCheckBox->setEnabled(false);
121         _generateCache = true;
122         _accents = true;
123         _dictionaryFilePath = "";
124     }
125     else if(plugin && plugin->settings()->value("cached") == "true") {
126         cacheCheckBox->setChecked(true);
127         accentsCheckBox->setChecked(true);
128         accentsCheckBox->setEnabled(false);
129         _generateCache = true;
130         _accents = true;
131     }
132     else {
133         cacheCheckBox->setChecked(false);
134         _generateCache = false;
135
136         if(plugin->settings()->value("strip_accents") == "true") {
137             accentsCheckBox->setChecked(true);
138             _accents = true;
139         }
140         else {
141             accentsCheckBox->setChecked(false);
142             _accents = false;
143         }
144     }
145
146     if(type == New) {
147         addButton = new QPushButton(tr("Add"));
148         mainVerticalLayout->addWidget(addButton);
149     }
150     else {
151         saveButton = new QPushButton(tr("Save settings"));
152         mainVerticalLayout->addWidget(saveButton);
153     }
154
155     scrollArea = new QScrollArea;
156     scrollArea->setWidget(widget);
157     scrollArea->setWidgetResizable(true);
158     scrollArea->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
159
160     layout = new QHBoxLayout;
161     layout->addWidget(scrollArea);
162     setLayout(layout);
163 }
164
165
166 void XdxfDialog::setAccents(bool accents) {
167     _accents = accents;
168 }
169
170 void XdxfDialog::setGenerateCache(bool generate) {
171     _generateCache = generate;
172
173     if(generate) {
174         _lastAccents = _accents;
175         accentsCheckBox->setChecked(true);
176     }
177     else
178         accentsCheckBox->setChecked(_lastAccents);
179
180     accentsCheckBox->setEnabled(!generate);
181 }
182
183 void XdxfDialog::selectFile() {
184     QString fileName = QFileDialog::getOpenFileName(this,
185                                      tr("Select dictionary file"),
186                                      _dictionaryFilePath,
187                                      tr("XDXF Files (*.xdxf)"),
188                                      NULL,
189                                      NULL);
190
191     if (!fileName.isEmpty()) {
192         browseLabel->setText(tr("Dictionary file: %1").arg(fileName));
193         _dictionaryFilePath = fileName;
194     }
195 }
196
197 void XdxfDialog::saveSettings() {
198     _settings = new Settings;
199     if(plugin) {
200         foreach(QString key, plugin->settings()->keys())
201             _settings->setValue(key, plugin->settings()->value(key));
202     }
203     else {
204         _settings->setValue("path", _dictionaryFilePath);
205     }
206
207     if(_generateCache)
208         _settings->setValue("generateCache", "true");
209     else
210         _settings->setValue("generateCache", "false");
211
212     if(_accents)
213         _settings->setValue("strip_accents", "true");
214     else
215         _settings->setValue("strip_accents", "false");
216 }
217
218 void XdxfDialog::accept() {
219     if(type == New && _dictionaryFilePath.isEmpty()) {
220         Q_EMIT notify(Notify::Warning, tr("File path is not set"), this);
221
222         return;
223     }
224
225     saveSettings();
226     QDialog::accept();
227 }
228
229 Settings* XdxfDialog::getSettings() {
230     return _settings;
231 }
232
233 void XdxfDialog::showCacheInfo() {
234     Q_EMIT notify(Notify::Info, tr("Optimize for quicker searches (may take some time)"), this);
235 }
236
237 void XdxfDialog::showAccentsInfo() {
238     Q_EMIT notify(Notify::Info, tr("Strip accents (searching takes more "
239                                    "time, but spelling don't have to be exact)"), this);
240 }
241