Fix bug with app crash on exit. Add keyboard support in wordList, DictTypeSelectDialo...
[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 Implements dictionaries management widget
24
25     \author Mateusz Półrola <mateusz.polrola@comarch.pl>
26 */
27
28 #include "DictManagerWidget.h"
29 #include "DictTypeSelectDialog.h"
30 #include <QDebug>
31 #include "../../include/DictDialog.h"
32 #include "MenuWidget.h"
33
34 DictManagerWidget::DictManagerWidget(GUIInterface *parent) :
35     QDialog(parent) {
36     setWindowTitle(tr("Dictionaries"));
37     this->guiInterface = parent;
38
39     #ifndef Q_WS_MAEMO_5
40     model = 0;
41     #endif
42     initalizeUI();
43
44     setModal(true);
45 }
46
47 void DictManagerWidget::initalizeUI() {
48     verticalLayout = new QVBoxLayout;
49     setLayout(verticalLayout);
50
51     #ifndef Q_WS_MAEMO_5
52     qmlView = new QDeclarativeView(this);
53
54     ctxt = qmlView->rootContext();
55
56     refreshDictsList();
57     ctxt->setContextProperty("dictModel", &(*model));
58     qmlView->setSource(QUrl::fromLocalFile("/usr/share/mdictionary/qml/DictManagerWidget.qml"));
59
60     QGraphicsObject *rootObject = qmlView->rootObject();
61
62     qmlView->setResizeMode(QDeclarativeView::SizeRootObjectToView);
63     verticalLayout->addWidget(qmlView);
64
65     #endif
66
67     #ifdef Q_WS_MAEMO_5
68     dictList = new QListWidget;
69     verticalLayout->addWidget(dictList);
70
71     dictList->setSelectionMode(QAbstractItemView::SingleSelection);
72     dictList->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
73
74     addNewDictButton = new QPushButton(tr("Add"));
75     removeDictButton = new QPushButton(tr("Remove"));
76     settingsButton = new QPushButton(tr("Settings"));
77
78     removeDictButton->setEnabled(false);
79     settingsButton->setEnabled(false);
80
81     buttonGroup = new QHBoxLayout;
82
83     buttonGroup->addWidget(addNewDictButton);
84     buttonGroup->addWidget(removeDictButton);
85     buttonGroup->addWidget(settingsButton);
86
87     verticalLayout->addLayout(buttonGroup, Qt::AlignBottom);
88
89
90     connect(addNewDictButton, SIGNAL(clicked()),
91             this, SLOT(saveChanges()));
92     connect(addNewDictButton, SIGNAL(clicked()),
93             this, SLOT(addNewDictButtonClicked()));
94
95     connect(removeDictButton, SIGNAL(clicked()),
96             this, SLOT(saveChanges()));
97     connect(removeDictButton, SIGNAL(clicked()),
98             this, SLOT(removeButtonClicked()));
99
100     connect(settingsButton, SIGNAL(clicked()),
101             this, SLOT(saveChanges()));
102     connect(settingsButton, SIGNAL(clicked()),
103             this, SLOT(settingsButtonClicked()));
104
105     connect(dictList, SIGNAL(itemClicked(QListWidgetItem*)),
106             this, SLOT(itemSelected(QListWidgetItem*)));
107
108     connect(dictList, SIGNAL(itemChanged(QListWidgetItem*)),
109             this, SLOT(changed()));
110     #endif
111
112     #ifndef Q_WS_MAEMO_5
113     connect(rootObject, SIGNAL(addButtonClicked()),
114             this, SLOT(saveChanges()));
115     connect(rootObject, SIGNAL(addButtonClicked()),
116             this, SLOT(addNewDictButtonClicked()));
117
118     connect(rootObject, SIGNAL(removeButtonClicked()),
119             this, SLOT(saveChanges()));
120     connect(rootObject, SIGNAL(removeButtonClicked()),
121             this, SLOT(removeButtonClicked()));
122
123     connect(rootObject, SIGNAL(settingsButtonClicked()),
124             this, SLOT(saveChanges()));
125     connect(rootObject, SIGNAL(settingsButtonClicked()),
126             this, SLOT(settingsButtonClicked()));
127
128
129     connect(model, SIGNAL(itemChanged()),
130             this, SLOT(changed()));
131     connect(rootObject, SIGNAL(itemActivated(int)),
132             this, SLOT(saveChanges()));
133     connect(rootObject, SIGNAL(itemActivated(int)),
134             this, SLOT(settingsButtonClicked()));
135
136     connect(this, SIGNAL(setFocusOnQML()), rootObject, SLOT(setFocus()));
137
138     #endif
139
140     #ifdef Q_WS_MAEMO_5
141     refreshDictsList();
142     #endif
143
144     #ifndef Q_WS_MAEMO_5
145         setMinimumSize(500,300);
146         //closeButton = new QPushButton(tr("Save"));
147         //buttonGroup->addWidget(closeButton);
148
149 //        setMinimumWidth(sizeHint().width()*1.2);
150 //        setMaximumWidth(sizeHint().width()*2);
151 //        setMinimumHeight(sizeHint().height());
152 //        setMaximumHeight(sizeHint().height()*2);
153         connect(rootObject, SIGNAL(saveButtonClicked()), this, SLOT(save()));
154     #endif
155 }
156
157
158 void DictManagerWidget::refreshDictsList() {
159
160     #ifndef Q_WS_MAEMO_5
161     QHash<CommonDictInterface*, bool> dicts = guiInterface->getDictionaries();
162
163     if (model == 0){
164         model = new DictManagerModel(dicts, this);
165     } else {
166         model->clear();
167         model->setDictionaries(dicts);
168     }
169     setFocusOnElement();
170
171     #endif
172
173     #ifdef Q_WS_MAEMO_5
174     dictList->clear();
175     dictsHash.clear();
176     removeDictButton->setEnabled(false);
177     settingsButton->setEnabled(false);
178
179     QHash<CommonDictInterface*, bool> dicts = guiInterface->getDictionaries();
180
181     QHashIterator<CommonDictInterface*, bool> i(dicts);
182
183     while(i.hasNext()) {
184         i.next();
185
186         QListWidgetItem* item = new QListWidgetItem();
187         QString name;
188         if (i.key()->type() == "stardict") {
189             name = i.key()->name() + " (" + i.key()->type() + ")";
190         }
191         else {
192             name = i.key()->langFrom() + " - " + i.key()->langTo() +
193                    " (" + i.key()->type() + " " +
194                    i.key()->name() + ")";
195         }
196         item->setText(name);
197         item->setFlags(item->flags() | Qt::ItemIsUserCheckable);
198         if(i.value()) {
199             item->setCheckState(Qt::Checked);
200         }
201         else {
202             item->setCheckState(Qt::Unchecked);
203         }
204         item->setIcon(*i.key()->icon());
205
206         dictList->addItem(item);
207         dictsHash.insert(item, i.key());
208     }
209
210     dictList->sortItems();
211     #endif
212 }
213
214 void DictManagerWidget::showEvent(QShowEvent *e) {
215     _changed = false;
216     #ifndef Q_WS_MAEMO_5
217       _save = false;
218     #endif
219     refreshDictsList();
220     QWidget::showEvent(e);
221 }
222
223 void DictManagerWidget::saveChanges() {
224
225     #ifndef Q_WS_MAEMO_5
226         if(_save) {
227     #else
228         if(_changed &&
229                 QMessageBox::question(this, tr("Save"),
230                                       tr("Do you want to save changes?"),
231                 QMessageBox::Save, QMessageBox::Cancel) == QMessageBox::Save) {
232             QList<CommonDictInterface*> checkedDicts;
233
234             for(int i=0; i<dictList->count(); i++) {
235                 QListWidgetItem* item = dictList->item(i);
236                 if(item->checkState() == Qt::Checked) {
237                     checkedDicts.push_back(dictsHash[item]);
238                 }
239             }
240     #endif
241         QList<CommonDictInterface*> checkedDicts;
242
243         for(int i=0; i<model->dictList().count(); i++) {
244 //            QListWidgetItem* item = dictList->item(i);
245             CommonDictInterface* item = model->dictList()[i];
246             if(model->dictionaries()[item] == true) {
247                 checkedDicts.push_back(item);
248             }
249         }
250         _changed = false;
251         emit selectedDictionaries(checkedDicts);
252     }
253 }
254
255 void DictManagerWidget::hideEvent(QHideEvent *e) {
256     saveChanges();
257     QWidget::hideEvent(e);
258 }
259
260
261 void DictManagerWidget::addNewDictButtonClicked() {
262     #ifndef Q_WS_MAEMO_5
263     if(!_changed || QMessageBox::question(this,
264             tr("Save"), tr("Do you want to save changes?"),
265             QMessageBox::Save, QMessageBox::Cancel) == QMessageBox::Save) {
266         _save = true;
267         saveChanges();
268         _save = false;
269     }
270     #endif
271
272    CommonDictInterface* selectedPlugin =
273            DictTypeSelectDialog::addNewDict(guiInterface->getPlugins(),this);
274    if(selectedPlugin) {
275        Settings* settings =
276                selectedPlugin->dictDialog()->addNewDictionary(this->parentWidget());
277
278        if(settings) {
279            CommonDictInterface* newDict = selectedPlugin->getNew(settings);
280            if(newDict) {
281                delete settings;
282                Q_EMIT addDictionary(newDict);
283            }
284        }
285    }
286    refreshDictsList();
287 }
288
289 void DictManagerWidget::itemSelected(QListWidgetItem *) {
290     removeDictButton->setEnabled(true);
291     settingsButton->setEnabled(true);
292     dictList->setFocus();
293 }
294
295 void DictManagerWidget::removeButtonClicked() {
296     if(QMessageBox::question(this, tr("Remove dictionary"),
297             tr("Do you want to remove selected dictionary?"),
298             QMessageBox::Yes, QMessageBox::No) == QMessageBox::Yes) {
299 #ifndef Q_WS_MAEMO_5
300         emit removeDictionary(model->currentDict());
301         refreshDictsList();
302 #else
303         QList<QListWidgetItem*> selected = dictList->selectedItems();
304         if(selected.count() > 0) {
305             emit removeDictionary(dictsHash[selected[0]]);
306             refreshDictsList();
307         }
308 #endif
309    }
310 }
311
312 void DictManagerWidget::settingsButtonClicked() {
313     #ifndef Q_WS_MAEMO_5
314     if(!_changed || QMessageBox::question(this,
315             tr("Save"), tr("Do you want to save changes?"),
316             QMessageBox::Save, QMessageBox::Cancel) == QMessageBox::Save) {
317         _save = true;
318         saveChanges();
319         _save = false;
320     }
321     model->currentDict()->dictDialog()->changeSettings(this->parentWidget());
322     refreshDictsList();
323     #else
324    QList<QListWidgetItem*> selected = dictList->selectedItems();
325    if(selected.count() > 0) {
326        dictsHash[selected[0]]->dictDialog()->changeSettings(this->parentWidget());
327    }
328    refreshDictsList();
329 #endif
330 }
331
332
333 void DictManagerWidget::changed() {
334     _changed=true;
335 }
336
337
338 #ifndef Q_WS_MAEMO_5
339 void DictManagerWidget::save() {
340     _save = true;
341     hide();
342 }
343
344 void DictManagerWidget::setFocusOnElement(){
345     Q_EMIT setFocusOnQML();
346 }
347 #endif
348
349
350 void DictManagerWidget::keyPressEvent(QKeyEvent *e) {
351     if (!e->modifiers() || (e->modifiers() & Qt::KeypadModifier && e->key() == Qt::Key_Enter)) {
352         switch (e->key()) {
353             case Qt::Key_Escape:
354                reject();
355                break;
356             default:
357                e->ignore();
358                return;
359         }
360     } else {
361         e->ignore();
362     }
363 }