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