1 /*******************************************************************************
3 This file is part of mDictionary.
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.
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.
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/>.
18 Copyright 2010 Comarch S.A.
20 *******************************************************************************/
23 \brief Implementation of stardict plugin's dialogs.
25 \author Mateusz Półrola <mateusz.polrola@gmail.com>
28 #include "StarDialog.h"
32 StarDialog::StarDialog(StarDictPlugin *plugin, StarDialogType type,
33 QWidget *parent) : QDialog(parent) {
34 this->plugin = plugin;
38 view= new QDeclarativeView();
39 view->setSource(QUrl::fromLocalFile("/usr/share/mdictionary/qml/StarDictDialog.qml"));
40 view->setResizeMode(QDeclarativeView::SizeRootObjectToView);
41 view->setAlignment(Qt::AlignCenter);
44 mainLayout = new QVBoxLayout;
45 mainLayout->addWidget(view);
46 setLayout(mainLayout);
47 view->setWindowTitle(tr("StarDict Settings"));
49 QGraphicsObject *rootObject = view->rootObject();
51 connect(this, SIGNAL(setInfo(QVariant)),
52 rootObject, SLOT(setInfo(QVariant)));
53 connect(this,SIGNAL(setButtonText(QVariant)),
54 rootObject, SLOT(setButtonText(QVariant)));
55 connect(this,SIGNAL(setNew(QVariant)),
56 rootObject, SLOT(setNew(QVariant)));
57 connect(this,SIGNAL(setPath(QVariant)),
58 rootObject, SLOT(setPath(QVariant)));
60 connect(rootObject, SIGNAL(saveButtonClicked()),
61 this, SLOT(accept()));
62 connect(rootObject, SIGNAL(browseButtonClicked()),
63 this, SLOT(selectFile()));
64 connect(rootObject, SIGNAL(heightChange(int)),
65 this, SLOT(heightChange(int)));
69 connect(browseButton, SIGNAL(clicked()),this, SLOT(selectFile()));
70 connect(confirmButton, SIGNAL(clicked()),this, SLOT(accept()));
75 void StarDialog::heightChange(int height){
76 if(height>sizeHint().height())
77 resize(sizeHint().width(),height);
81 void StarDialog::resizeEvent(QResizeEvent *e){
82 QDialog::resizeEvent(e);
83 if(lastHeight>sizeHint().height()){
84 resize(sizeHint().width(),lastHeight);
88 void StarDialog::initializeUI() {
92 QString info=tr("Plugin: ") + plugin->type().toUpper() +"\n" +
93 tr("Book name: ") + plugin->settings()->value("bookname") + "\n" +
94 tr("Version: ") + plugin->settings()->value("version") + "\n" +
95 tr("Word count: ") + plugin->settings()->value("wordcount") + "\n" +
96 tr("Author: ") + plugin->settings()->value("author") + "\n" +
97 tr("E-mail: ") + plugin->settings()->value("email") + "\n" +
98 tr("Website: ") + plugin->settings()->value("website") + "\n" +
99 tr("Description: ") + plugin->settings()->value("description")+"\n"+
100 tr("Date: ") + plugin->settings()->value("date");
102 emit setButtonText("Save settings");
106 emit setButtonText("Add");
110 _dictionaryFilePath = "";
113 mainVerticalLayout = new QVBoxLayout;
114 widget = new QWidget;
115 widget->setLayout(mainVerticalLayout);
116 infoLabel = new QLabel;
117 infoLabel->setWordWrap(true);
118 QVBoxLayout* buttonLayout = new QVBoxLayout;
121 browseLayout = new QHBoxLayout;
122 browseButton = new QPushButton(tr("Browse"));
123 browseButton->setMaximumWidth(150);
124 infoLabel->setText(tr("Dictionary file: not selected"));
125 setWindowTitle(tr("Add new StarDict dictionary"));
126 browseLayout->addWidget(infoLabel, 0, Qt::AlignLeft);
127 browseLayout->addLayout(buttonLayout);
128 browseLayout->addWidget(browseButton, 0, Qt::AlignRight);
129 mainVerticalLayout->addLayout(browseLayout);
132 setWindowTitle(tr("StarDict Settings"));
133 infoLabel->setText(tr("Plugin: ") + plugin->type().toUpper() +"\n" +
134 tr("Book name: ") + plugin->settings()->value("bookname")
136 tr("Version: ") + plugin->settings()->value("version") + "\n" +
137 tr("Word count: ") + plugin->settings()->value("wordcount")
139 tr("Author: ") + plugin->settings()->value("author") + "\n" +
140 tr("E-mail: ") + plugin->settings()->value("email") + "\n" +
141 tr("Website: ") + plugin->settings()->value("website") + "\n" +
142 tr("Description: ") + plugin->settings()->value("description")
144 tr("Date: ") + plugin->settings()->value("date"));
145 mainVerticalLayout->addWidget(infoLabel);
149 _dictionaryFilePath = "";
151 confirmButton = new QPushButton;
152 mainVerticalLayout->addWidget(confirmButton);
154 confirmButton->setText(tr("Add"));
156 confirmButton->setText(tr("Save settings"));
158 scrollArea = new QScrollArea;
159 scrollArea->setWidget(widget);
160 scrollArea->setWidgetResizable(true);
162 scrollArea->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
165 infoLabel->setMinimumWidth(200);
166 // setMinimumSize(sizeHint().width()*1.5, sizeHint().height()*1.2);
167 // setMaximumSize(sizeHint().width()*1.7, sizeHint().height()*1.5);
168 scrollArea->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
172 layout = new QHBoxLayout;
173 layout->addWidget(scrollArea);
177 setMinimumSize(400,200);
179 setMinimumHeight(350);
182 scrollArea->setLineWidth(0);
183 scrollArea->setMidLineWidth(0);
184 scrollArea->setFrameStyle(QFrame::NoFrame);
189 void StarDialog::selectFile() {
190 QString fileName = QFileDialog::getOpenFileName(this,
191 tr("Select dictionary file"),
193 tr("StarDict Files (*.dict *dict.dz *.idx *idx.gz *.ifo)"),
196 if (!fileName.isEmpty()) {
198 emit setPath(tr("Dictionary file: %1").arg(fileName));
200 infoLabel->setText(tr("Dictionary file: %1").arg(fileName));
202 _dictionaryFilePath = fileName;
203 if (_dictionaryFilePath.endsWith(".tar.bz2"))
204 _isCompressed = true;
206 _isCompressed = false;
208 resize(view->sizeHint());
217 void StarDialog::saveSettings() {
219 _settings = new Settings;
221 foreach(QString key, plugin->settings()->keys())
222 _settings->setValue(key, plugin->settings()->value(key));
224 if(_settings->value("path")=="")
225 _settings->setValue("path", _dictionaryFilePath);
227 if(_settings->value("ifoFileName")=="")
228 _settings->setValue("ifoFileName", _dictName + ".ifo");
230 if(_settings->value("idxFileName")=="")
231 _settings->setValue("idxFileName", _dictName + ".idx");
233 if(_settings->value("dictFileName")==""){
234 if (QFile::exists(_dictName + ".dict.dz") == true)
235 _settings->setValue("dictFileName", _dictName + ".dict.dz");
237 _settings->setValue("dictFileName", _dictName + ".dict");
239 if(_settings->value("synFileName")=="")
240 if (QFile::exists(_dictName + ".syn") == true)
241 _settings->setValue("synFileName", _dictName + ".syn");
245 void StarDialog::accept() {
246 if(type == New && _dictionaryFilePath.isEmpty()) {
247 Q_EMIT notify(Notify::Warning, tr("File path is not set"));
251 if(type == New && !_dictionaryFilePath.isEmpty() && !checkFiles()) {
252 Q_EMIT notify(Notify::Warning, tr("Dictionary files are not complete"));
261 bool StarDialog::checkFiles() {
262 if (!_isCompressed) {
263 if (_dictionaryFilePath.right(2) == "dz") {
264 _dictName = _dictionaryFilePath.left(_dictionaryFilePath.lastIndexOf("."));
265 _dictName = _dictName.left(_dictName.lastIndexOf("."));
268 _dictName = _dictionaryFilePath.left(_dictionaryFilePath.lastIndexOf("."));
271 if (QFile::exists(_dictName + ".idx") == false
272 && QFile::exists(_dictName + ".idx.gz") == false) {
275 if (QFile::exists(_dictName + ".dict") == false
276 && QFile::exists(_dictName + ".dict.dz") == false) {
286 Settings* StarDialog::getSettings() {