Fixed bugs in pro file
[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     accentsToolTip = tr("Strip accents (searching takes more time, but spelling doesn't have to be exact)");
35
36     initializeUI();
37
38
39     connect(accentsCheckBox, SIGNAL(toggled(bool)),
40             this, SLOT(setAccents(bool)));
41
42     #ifdef Q_WS_MAEMO_5
43         connect(accentsInfoToolButton, SIGNAL(clicked()),
44
45     #endif
46
47     if(type == New) {
48         connect(browseButton, SIGNAL(clicked()),
49                 this, SLOT(selectFile()));
50     }
51
52     connect(confirmButton, SIGNAL(clicked()),
53             this, SLOT(accept()));
54
55 }
56
57
58 void StarDialog::initializeUI() {
59     mainVerticalLayout = new QVBoxLayout;
60     widget = new QWidget;
61     widget->setLayout(mainVerticalLayout);
62
63     infoLabel = new QLabel;
64     infoLabel->setWordWrap(true);
65
66     if(type == New) {
67         setWindowTitle(tr("Add new StarDict dictionary"));
68
69         browseLayout = new QHBoxLayout;
70         browseButton = new QPushButton(tr("Browse"));
71         infoLabel->setText(tr("Dictionary file: not selected"));
72
73         browseLayout->addWidget(infoLabel, 0, Qt::AlignLeft);
74         browseLayout->addWidget(browseButton, 0, Qt::AlignRight);
75
76         mainVerticalLayout->addLayout(browseLayout);
77     }
78     else {
79         setWindowTitle(tr("StarDict Settings"));
80
81         infoLabel->setText(tr("Plugin: ") + plugin->type().toUpper() +"\n" +
82                        tr("From: ") + plugin->langFrom() + "\n" +
83                        tr("To: ") + plugin->langTo() + "\n" +
84                        tr("Description: ") + plugin->name() + "\n" +
85                        plugin->infoNote());
86         mainVerticalLayout->addWidget(infoLabel);
87     }
88
89     accentsLayout = new QHBoxLayout;
90     accentsCheckBox = new QCheckBox(tr("Strip accents"));
91     accentsCheckBox->setToolTip(accentsToolTip);
92     accentsLayout->addWidget(accentsCheckBox); 
93     #ifdef Q_WS_MAEMO_5
94         accentsInfoToolButton = new QToolButton;
95         accentsInfoToolButton->setIcon(QIcon::fromTheme("general_information"));
96         accentsLayout->addWidget(accentsInfoToolButton);
97     #endif
98
99
100     mainVerticalLayout->addLayout(accentsLayout);
101
102
103     //load old setting if exists
104     if(!plugin) {
105         accentsCheckBox->setChecked(true);
106         accentsCheckBox->setEnabled(false);
107         _accents = true;
108         _dictionaryFilePath = "";
109     }
110     else if(plugin && plugin->settings()->value("cached") == "true") {
111         accentsCheckBox->setChecked(true);
112         accentsCheckBox->setEnabled(false);
113         _accents = true;
114     }
115     else {
116         if(plugin->settings()->value("strip_accents") == "true") {
117             accentsCheckBox->setChecked(true);
118             _accents = true;
119         }
120         else {
121             accentsCheckBox->setChecked(false);
122             _accents = false;
123         }
124     }
125
126     confirmButton = new QPushButton;
127     mainVerticalLayout->addWidget(confirmButton);
128     if(type == New) {
129         confirmButton->setText(tr("Add"));
130     }
131     else {
132         confirmButton->setText(tr("Save settings"));
133     }
134
135     scrollArea = new QScrollArea;
136     scrollArea->setWidget(widget);
137     scrollArea->setWidgetResizable(true);
138     #ifdef Q_WS_MAEMO_5
139         scrollArea->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
140     #else
141         if(type==New) {
142             infoLabel->setMinimumWidth(200);
143             setMinimumSize(sizeHint().width()*1.5, sizeHint().height()*1.2);
144             setMaximumSize(sizeHint().width()*1.7, sizeHint().height()*1.5);
145             scrollArea->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
146         }
147     #endif
148
149     layout = new QHBoxLayout;
150     layout->addWidget(scrollArea);
151     setLayout(layout);
152
153     #ifndef Q_WS_MAEMO_5
154         setMinimumSize(400,200);
155     #else
156         setMinimumHeight(350);
157     #endif
158
159     scrollArea->setLineWidth(0);
160     scrollArea->setMidLineWidth(0);
161     scrollArea->setFrameStyle(QFrame::NoFrame);
162
163
164 }
165
166
167 void StarDialog::setAccents(bool accents) {
168     _accents = accents;
169 }
170
171
172 void StarDialog::selectFile() {
173     QString fileName = QFileDialog::getOpenFileName(this,
174                                      tr("Select dictionary file"),
175                                      _dictionaryFilePath,
176                                      tr("StarDict Files (*.dict *dict.dz *.idx *idx.gz *.ifo)"),
177                                      NULL,
178                                      NULL);
179
180     if (!fileName.isEmpty()) {
181         infoLabel->setText(tr("Dictionary file: %1").arg(fileName));
182         _dictionaryFilePath = fileName;
183         updateGeometry();
184     }
185 }
186
187 void StarDialog::saveSettings() {
188     _settings = new Settings;
189     if(plugin) {
190         foreach(QString key, plugin->settings()->keys())
191             _settings->setValue(key, plugin->settings()->value(key));
192     }
193     else {
194         _settings->setValue("path", _dictionaryFilePath);
195     }
196     if(_accents)
197         _settings->setValue("strip_accents", "true");
198     else
199         _settings->setValue("strip_accents", "false");
200 }
201
202 void StarDialog::accept() {
203     if(type == New && _dictionaryFilePath.isEmpty()) {
204         Q_EMIT notify(Notify::Warning, tr("File path is not set"));
205
206         return;
207     }
208
209     saveSettings();
210     QDialog::accept();
211 }
212
213 Settings* StarDialog::getSettings() {
214     return _settings;
215 }
216
217 #ifdef Q_WS_MAEMO_5
218     void StarDialog::showAccentsInfo() {
219         Q_EMIT notify(Notify::Warning, accentsToolTip);
220     }
221 #endif
222