e964f050a6dcc4469d7169e9dc23ce2bd15f1144
[mdictionary] / trunk / src / plugins / xdxf / src / XdxfSettingsDialog.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 XdxfSettingsDialog.cpp
23 */
24 //Created by Mateusz Półrola
25
26 #include "XdxfSettingsDialog.h"
27 #include <QDebug>
28
29 XdxfSettingsDialog::XdxfSettingsDialog(XdxfPlugin *plugin, QWidget *parent) :
30     QDialog(parent)
31 {
32     this->plugin = plugin;
33     verticalLayout = new QVBoxLayout(this);
34     setLayout(verticalLayout);
35
36     setWindowTitle(tr("XDXF Settings"));
37
38
39     infoLabel = new QLabel(this);
40
41     infoLabel->setText(tr("Plugin type: ") + plugin->type() +"\n" +
42                    tr("From: ") + plugin->langFrom() + "\n" +
43                    tr("To: ") + plugin->langTo() + "\n" +
44                    tr("Description: ") + plugin->name());
45
46     verticalLayout->addWidget(infoLabel);
47
48     browseLayout = new QHBoxLayout(this);
49     verticalLayout->addLayout(browseLayout);
50
51     browseButton =  new QPushButton(tr("Browse"),this);
52     browseLabel = new QLabel(tr("Dictionary file: ") +
53                              plugin->settings()->value("path"),this);
54
55     browseLayout->addWidget(browseLabel);
56     browseLayout->addWidget(browseButton,0, Qt::AlignRight);
57
58
59     cacheLayout = new QHBoxLayout(this);
60     verticalLayout->addLayout(cacheLayout);
61
62     cacheCheckBox = new QCheckBox(tr("Cached"),this);
63     if(plugin->settings()->value("cached") == "true") {
64         cacheCheckBox->setChecked(true);
65         _generateCache = true;
66     }
67     else {
68         cacheCheckBox->setChecked(false);
69         _generateCache = false;
70     }
71
72     cacheLayout->addWidget(cacheCheckBox);
73
74     saveButton = new QPushButton(tr("Save settings"),this);
75
76     verticalLayout->addWidget(saveButton);
77
78     setModal(true);
79
80     connect(browseButton, SIGNAL(clicked()),
81             this, SLOT(selectFile()));
82
83     connect(saveButton, SIGNAL(clicked()),
84             this, SLOT(accept()));
85
86     connect(cacheCheckBox, SIGNAL(toggled(bool)),
87             SLOT(setGenerateCache(bool)));
88
89     _dicitonaryFilePath = plugin->settings()->value("path");
90 }
91
92 void XdxfSettingsDialog::setGenerateCache(bool generate) {
93     _generateCache = generate;
94 }
95
96 bool XdxfSettingsDialog::generateCache() {
97     return _generateCache;
98 }
99
100 void XdxfSettingsDialog::selectFile() {
101     QString fileName = QFileDialog::getOpenFileName(this,
102                                      tr("Select dictionary file"),
103                                      "",
104                                      tr("XDXF Files (*.xdxf)"),
105                                      NULL,
106                                      NULL);
107     if (!fileName.isEmpty()) {
108         browseLabel->setText(tr("Dictionary file: ") + fileName);
109         _dicitonaryFilePath = fileName;
110     }
111 }
112
113 QString XdxfSettingsDialog::dicitonaryFilePath() {
114     return _dicitonaryFilePath;
115 }
116
117 Settings* XdxfSettingsDialog::getSettings(XdxfPlugin *plugin,
118                                           QWidget *parent) {
119     XdxfSettingsDialog settingsDialog(plugin, parent);
120
121
122     if(settingsDialog.exec()==QDialog::Accepted) {
123         Settings* settings = new Settings;
124         foreach(QString key, plugin->settings()->keys())
125             settings->setValue(key, plugin->settings()->value(key));
126         settings->setValue("path", settingsDialog.dicitonaryFilePath());
127
128         if(settingsDialog.generateCache()) {
129             settings->setValue("generateCache", "true");
130         }
131         else {
132             settings->setValue("generateCache", "false");
133         }
134         plugin->setSettings(settings);
135         delete settings;
136         return 0;
137     }
138
139     return 0;
140 }
141
142