Merge branch 'master' of ssh://drop.maemo.org/git/mdictionary
[mdictionary] / src / plugins / xdxf / XdxfLoadDialog.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 /*! \file XdxfLoadDialog.cpp
23 */
24 //Created by Mateusz Półrola
25
26 #include "XdxfLoadDialog.h"
27
28 XdxfLoadDialog::XdxfLoadDialog(QWidget *parent) :
29     QDialog(parent) {
30     verticalLayout = new QVBoxLayout;
31     setLayout(verticalLayout);
32
33     setWindowTitle(tr("Add new XDXF dictionary"));
34
35
36
37     browseLayout = new QHBoxLayout;
38
39     browseButton =  new QPushButton(tr("Browse"));
40     browseLabel = new QLabel(tr("Dictionary file: not selected"));
41     //browseLabel->setMargin(5);
42
43     browseLayout->addWidget(browseLabel, 0, Qt::AlignLeft);
44     browseLayout->addWidget(browseButton, 0, Qt::AlignRight);
45
46     verticalLayout->addLayout(browseLayout);
47
48     cacheLayout = new QHBoxLayout;
49     verticalLayout->addLayout(cacheLayout);
50     accentsCheckBox = new QCheckBox(tr("Strip accents (searching takes more "
51                  "time, but spelling don't have to be exact)"));
52     verticalLayout->addWidget(accentsCheckBox);
53
54     cacheCheckBox = new QCheckBox(tr("Optimize for quicker searches (may take some time)"),this);
55     cacheCheckBox->setChecked(true);
56     cacheLayout->addWidget(cacheCheckBox);
57
58     addButton = new QPushButton(tr("Add"));
59
60     verticalLayout->addWidget(addButton);
61
62     setModal(true);
63
64     connect(browseButton, SIGNAL(clicked()),
65             this, SLOT(selectFile()));
66
67     connect(addButton, SIGNAL(clicked()),
68             this, SLOT(addDictionary()));
69
70     connect(cacheCheckBox, SIGNAL(toggled(bool)),
71             SLOT(setGenerateCache(bool)));
72
73     connect(accentsCheckBox, SIGNAL(clicked(bool)), SLOT(setAccents(bool)));
74     lastAccents = accentsCheckBox->isChecked();
75     _dicitonaryFilePath = QString();
76 }
77
78
79
80 void XdxfLoadDialog::setAccents(bool state) {
81     lastAccents = state;
82 }
83
84
85
86 void XdxfLoadDialog::selectFile() {
87     QString fileName = QFileDialog::getOpenFileName(this,
88                                      tr("Select dictionary file"),
89                                      _dicitonaryFilePath,
90                                      tr("XDXF Files (*.xdxf)"),
91                                      NULL,
92                                      NULL);
93
94     if (!fileName.isEmpty()) {
95         browseLabel->setText(tr("Dictionary file: %1").arg(fileName));
96         _dicitonaryFilePath = fileName;
97     }repaint(rect());
98     resize(size());
99 }
100
101 void XdxfLoadDialog::addDictionary() {
102     _generateCache = cacheCheckBox->isChecked();
103     if(!_dicitonaryFilePath.isEmpty()) {
104         accept();
105     }
106 }
107
108 QString XdxfLoadDialog::dicitonaryFilePath() {
109     return _dicitonaryFilePath;
110 }
111
112 bool XdxfLoadDialog::generateCache() {
113     return _generateCache;
114 }
115
116 void XdxfLoadDialog::setGenerateCache(bool generate) {
117     _generateCache = generate;
118
119     if(generate)
120         accentsCheckBox->setChecked(true);
121     else
122         accentsCheckBox->setChecked(lastAccents);
123
124     accentsCheckBox->setEnabled(!generate);
125 }
126
127 Settings* XdxfLoadDialog::getSettings(QWidget *parent) {
128     XdxfLoadDialog loadDialog(parent);
129     Settings* settings = new Settings;
130
131     if(loadDialog.exec()==QDialog::Accepted) {
132         settings->setValue("path", loadDialog.dicitonaryFilePath());
133         if(loadDialog.generateCache()) {
134             settings->setValue("generateCache", "true");
135         }
136         else {
137             settings->setValue("generateCache", "false");
138         }
139         if(loadDialog.accentsCheckBox->isChecked())
140             settings->setValue("strip_accents", "true");
141         else
142             settings->setValue("strip_accents", "false");
143
144
145         return settings;
146     }
147
148     return NULL;
149 }
150
151