Make program compilable
[mdictionary] / src / plugins / stardict / StarDialog.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 "StarDialog.h"
25 #include <QDebug>
26
27 StarDialog::StarDialog(StarDictPlugin *plugin,
28                        StarDialogType 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 StarDialog::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 StarDialog::setAccents(bool accents) {
192     _accents = accents;
193 }
194
195
196 void StarDialog::selectFile() {
197     QString fileName = QFileDialog::getOpenFileName(this,
198                                      tr("Select dictionary file"),
199                                      _dictionaryFilePath,
200                                      tr("XDXF Files (*.xdxf)"),
201                                      NULL,
202                                      NULL);
203
204     if (!fileName.isEmpty()) {
205         infoLabel->setText(tr("Dictionary file: %1").arg(fileName));
206         _dictionaryFilePath = fileName;
207         updateGeometry();
208     }
209 }
210
211 void StarDialog::saveSettings() {
212     _settings = new Settings;
213     if(plugin) {
214         foreach(QString key, plugin->settings()->keys())
215             _settings->setValue(key, plugin->settings()->value(key));
216     }
217     else {
218         _settings->setValue("path", _dictionaryFilePath);
219     }
220
221     if(_generateCache)
222         _settings->setValue("generateCache", "true");
223     else
224         _settings->setValue("generateCache", "false");
225
226     if(_accents)
227         _settings->setValue("strip_accents", "true");
228     else
229         _settings->setValue("strip_accents", "false");
230 }
231
232 void StarDialog::accept() {
233     if(type == New && _dictionaryFilePath.isEmpty()) {
234         Q_EMIT notify(Notify::Warning, tr("File path is not set"));
235
236         return;
237     }
238
239     saveSettings();
240     QDialog::accept();
241 }
242
243 Settings* StarDialog::getSettings() {
244     return _settings;
245 }
246
247 #ifdef Q_WS_MAEMO_5
248     void StarDialog::showAccentsInfo() {
249         Q_EMIT notify(Notify::Warning, accentsToolTip);
250     }
251 #endif
252