check xdxf file
[mdictionary] / src / mdictionary / gui / DictManagerWidget.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 DictManagerWidget.cpp
23 //! \brief Dictionaries management widget
24 //! \author Mateusz Półrola <mateusz.polrola@comarch.pl>
25
26 #include "DictManagerWidget.h"
27 #include "DictTypeSelectDialog.h"
28 #include <QDebug>
29 #include "../../common/DictDialog.h"
30
31 DictManagerWidget::DictManagerWidget(GUIInterface *parent) :
32     QDialog(parent) {
33     setWindowTitle(tr("Dictionaries"));
34     this->guiInterface = parent;
35
36     initalizeUI();
37 }
38
39 void DictManagerWidget::initalizeUI() {
40     verticalLayout = new QVBoxLayout;
41     setLayout(verticalLayout);
42
43     dictListWidget = new QListWidget;
44     verticalLayout->addWidget(dictListWidget);
45
46     dictListWidget->setSelectionMode(QAbstractItemView::SingleSelection);
47     dictListWidget->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
48
49     addNewDictButton = new QPushButton(tr("Add"));
50     removeDictButton = new QPushButton(tr("Remove"));
51     settingsButton = new QPushButton(tr("Settings"));
52
53     removeDictButton->setEnabled(false);
54     settingsButton->setEnabled(false);
55
56     buttonGroup = new QHBoxLayout;
57
58     buttonGroup->addWidget(addNewDictButton);
59     buttonGroup->addWidget(removeDictButton);
60     buttonGroup->addWidget(settingsButton);
61
62     verticalLayout->addLayout(buttonGroup, Qt::AlignBottom);
63
64
65     connect(addNewDictButton, SIGNAL(clicked()),
66             this, SLOT(saveChanges()));
67     connect(addNewDictButton, SIGNAL(clicked()),
68             this, SLOT(addNewDictButtonClicked()));
69
70     connect(removeDictButton, SIGNAL(clicked()),
71             this, SLOT(saveChanges()));
72     connect(removeDictButton, SIGNAL(clicked()),
73             this, SLOT(removeButtonClicked()));
74
75     connect(settingsButton, SIGNAL(clicked()),
76             this, SLOT(saveChanges()));
77     connect(settingsButton, SIGNAL(clicked()),
78             this, SLOT(settingsButtonClicked()));
79
80     connect(dictListWidget, SIGNAL(itemClicked(QListWidgetItem*)),
81             this, SLOT(itemSelected(QListWidgetItem*)));
82
83     connect(dictListWidget, SIGNAL(itemChanged(QListWidgetItem*)),
84             this, SLOT(changed()));
85
86     refreshDictsList();
87
88     #ifndef Q_WS_MAEMO_5
89         setMinimumSize(500,300);
90         closeButton = new QPushButton(tr("Save"));
91         buttonGroup->addWidget(closeButton);
92         connect(closeButton, SIGNAL(clicked()), this, SLOT(save()));
93     #endif
94 }
95
96
97 void DictManagerWidget::refreshDictsList() {
98
99     dictListWidget->clear();
100     dictsHash.clear();
101     removeDictButton->setEnabled(false);
102     settingsButton->setEnabled(false);
103
104     QHash<CommonDictInterface*, bool> dicts = guiInterface->getDictionaries();
105
106     QHashIterator<CommonDictInterface*, bool> i(dicts);
107
108     while(i.hasNext()) {
109         i.next();
110         QListWidgetItem* item = new QListWidgetItem();
111         QString name = i.key()->langFrom() + " - " + i.key()->langTo() + " (" +
112                        i.key()->type() + " " + i.key()->name() + ")";
113         item->setText(name);
114         item->setFlags(item->flags() | Qt::ItemIsUserCheckable);
115         if(i.value()) {
116             item->setCheckState(Qt::Checked);
117         }
118         else {
119             item->setCheckState(Qt::Unchecked);
120         }
121         item->setIcon(*i.key()->icon());
122
123         dictListWidget->addItem(item);
124         dictsHash.insert(item, i.key());
125     }
126 }
127
128 void DictManagerWidget::showEvent(QShowEvent *e) {
129     _changed = false;
130     #ifndef Q_WS_MAEMO_5
131       _save = false;
132     #endif
133     refreshDictsList();
134     QWidget::showEvent(e);
135 }
136
137 void DictManagerWidget::saveChanges() {
138
139     #ifndef Q_WS_MAEMO_5
140         if(_save) {
141     #else
142         if(_changed &&
143                 QMessageBox::question(this, tr("Save"),
144                                       tr("Do you want to save changes?"),
145                 QMessageBox::Save, QMessageBox::Cancel) == QMessageBox::Save) {
146     #endif
147         QList<CommonDictInterface*> checkedDicts;
148
149         for(int i=0; i<dictListWidget->count(); i++) {
150             QListWidgetItem* item = dictListWidget->item(i);
151             if(item->checkState() == Qt::Checked) {
152                 checkedDicts.push_back(dictsHash[item]);
153             }
154         }
155         _changed = false;
156         emit selectedDictionaries(checkedDicts);
157     }
158 }
159
160 void DictManagerWidget::hideEvent(QHideEvent *e) {
161     saveChanges();
162     QWidget::hideEvent(e);
163 }
164
165
166 void DictManagerWidget::addNewDictButtonClicked() {
167     #ifndef Q_WS_MAEMO_5
168     if(!_changed || QMessageBox::question(this,
169             tr("Save"), tr("Do you want to save changes?"),
170             QMessageBox::Save, QMessageBox::Cancel) == QMessageBox::Save) {
171         _save = true;
172         saveChanges();
173         _save = false;
174     }
175     #endif
176
177    CommonDictInterface* selectedPlugin =
178            DictTypeSelectDialog::addNewDict(guiInterface->getPlugins(),this);
179    if(selectedPlugin != NULL) {
180        Settings* settings =
181                selectedPlugin->dictDialog()->addNewDictionary(this);
182
183        if(settings != NULL) {
184            CommonDictInterface* newDict = selectedPlugin->getNew(settings);
185            if(newDict) {
186                delete settings;
187                Q_EMIT addDictionary(newDict);
188            }
189        }
190    }
191    refreshDictsList();
192 }
193
194 void DictManagerWidget::itemSelected(QListWidgetItem *) {
195     removeDictButton->setEnabled(true);
196     settingsButton->setEnabled(true);
197 }
198
199 void DictManagerWidget::removeButtonClicked() {
200     if(QMessageBox::question(this, tr("Remove dictionary"),
201             tr("Do you want to remove selected dictionary?"),
202             QMessageBox::Yes, QMessageBox::No) == QMessageBox::Yes) {
203
204         QList<QListWidgetItem*> selected = dictListWidget->selectedItems();
205         if(selected.count() > 0) {
206             emit removeDictionary(dictsHash[selected[0]]);
207             refreshDictsList();
208         }
209    }
210 }
211
212 void DictManagerWidget::settingsButtonClicked() {
213     #ifndef Q_WS_MAEMO_5
214     if(!_changed || QMessageBox::question(this,
215             tr("Save"), tr("Do you want to save changes?"),
216             QMessageBox::Save, QMessageBox::Cancel) == QMessageBox::Save) {
217         _save = true;
218         saveChanges();
219         _save = false;
220     }
221     #endif
222    QList<QListWidgetItem*> selected = dictListWidget->selectedItems();
223    if(selected.count() > 0) {
224        dictsHash[selected[0]]->dictDialog()->changeSettings(this);
225    }
226    refreshDictsList();
227 }
228
229
230 void DictManagerWidget::changed() {
231     _changed=true;
232 }
233
234
235 #ifndef Q_WS_MAEMO_5
236     void DictManagerWidget::save() {
237         _save = true;
238         hide();
239     }
240 #endif