features of DictManagerWidget restored in qml version.
[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     #endif
137
138     #ifdef Q_WS_MAEMO_5
139     refreshDictsList();
140     #endif
141
142     #ifndef Q_WS_MAEMO_5
143         setMinimumSize(500,300);
144         //closeButton = new QPushButton(tr("Save"));
145         //buttonGroup->addWidget(closeButton);
146
147 //        setMinimumWidth(sizeHint().width()*1.2);
148 //        setMaximumWidth(sizeHint().width()*2);
149 //        setMinimumHeight(sizeHint().height());
150 //        setMaximumHeight(sizeHint().height()*2);
151         connect(rootObject, SIGNAL(saveButtonClicked()), this, SLOT(save()));
152     #endif
153 }
154
155
156 void DictManagerWidget::refreshDictsList() {
157
158     #ifndef Q_WS_MAEMO_5
159     QHash<CommonDictInterface*, bool> dicts = guiInterface->getDictionaries();
160
161     if (model == 0){
162         model = new DictManagerModel(dicts, this);
163     } else {
164         model->clear();
165         model->setDictionaries(dicts);
166     }
167
168     #endif
169
170     #ifdef Q_WS_MAEMO_5
171     dictList->clear();
172     dictsHash.clear();
173     removeDictButton->setEnabled(false);
174     settingsButton->setEnabled(false);
175
176     QHash<CommonDictInterface*, bool> dicts = guiInterface->getDictionaries();
177
178     QHashIterator<CommonDictInterface*, bool> i(dicts);
179
180     while(i.hasNext()) {
181         i.next();
182
183         QListWidgetItem* item = new QListWidgetItem();
184         QString name;
185         if (i.key()->type() == "stardict") {
186             name = i.key()->name() + " (" + i.key()->type() + ")";
187         }
188         else {
189             name = i.key()->langFrom() + " - " + i.key()->langTo() +
190                    " (" + i.key()->type() + " " +
191                    i.key()->name() + ")";
192         }
193         item->setText(name);
194         item->setFlags(item->flags() | Qt::ItemIsUserCheckable);
195         if(i.value()) {
196             item->setCheckState(Qt::Checked);
197         }
198         else {
199             item->setCheckState(Qt::Unchecked);
200         }
201         item->setIcon(*i.key()->icon());
202
203         dictList->addItem(item);
204         dictsHash.insert(item, i.key());
205     }
206
207     dictList->sortItems();
208     #endif
209 }
210
211 void DictManagerWidget::showEvent(QShowEvent *e) {
212     _changed = false;
213     #ifndef Q_WS_MAEMO_5
214       _save = false;
215     #endif
216     refreshDictsList();
217     QWidget::showEvent(e);
218 }
219
220 void DictManagerWidget::saveChanges() {
221
222     #ifndef Q_WS_MAEMO_5
223         if(_save) {
224     #else
225         if(_changed &&
226                 QMessageBox::question(this, tr("Save"),
227                                       tr("Do you want to save changes?"),
228                 QMessageBox::Save, QMessageBox::Cancel) == QMessageBox::Save) {
229             QList<CommonDictInterface*> checkedDicts;
230
231             for(int i=0; i<dictList->count(); i++) {
232                 QListWidgetItem* item = dictList->item(i);
233                 if(item->checkState() == Qt::Checked) {
234                     checkedDicts.push_back(dictsHash[item]);
235                 }
236             }
237     #endif
238         QList<CommonDictInterface*> checkedDicts;
239
240         for(int i=0; i<model->dictList().count(); i++) {
241 //            QListWidgetItem* item = dictList->item(i);
242             CommonDictInterface* item = model->dictList()[i];
243             if(model->dictionaries()[item] == true) {
244                 checkedDicts.push_back(item);
245             }
246         }
247         _changed = false;
248         emit selectedDictionaries(checkedDicts);
249     }
250 }
251
252 void DictManagerWidget::hideEvent(QHideEvent *e) {
253     saveChanges();
254     QWidget::hideEvent(e);
255 }
256
257
258 void DictManagerWidget::addNewDictButtonClicked() {
259     #ifndef Q_WS_MAEMO_5
260     qDebug()<<"1 "<<_changed;
261     if(!_changed || QMessageBox::question(this,
262             tr("Save"), tr("Do you want to save changes?"),
263             QMessageBox::Save, QMessageBox::Cancel) == QMessageBox::Save) {
264         qDebug()<<"2";
265         _save = true;
266         saveChanges();
267         _save = false;
268     }
269     #endif
270
271    CommonDictInterface* selectedPlugin =
272            DictTypeSelectDialog::addNewDict(guiInterface->getPlugins(),this);
273    if(selectedPlugin) {
274        Settings* settings =
275                selectedPlugin->dictDialog()->addNewDictionary(this->parentWidget());
276
277        if(settings) {
278            CommonDictInterface* newDict = selectedPlugin->getNew(settings);
279            if(newDict) {
280                delete settings;
281                Q_EMIT addDictionary(newDict);
282            }
283        }
284    }
285    refreshDictsList();
286 }
287
288 void DictManagerWidget::itemSelected(QListWidgetItem *) {
289     removeDictButton->setEnabled(true);
290     settingsButton->setEnabled(true);
291     dictList->setFocus();
292 }
293
294 void DictManagerWidget::removeButtonClicked() {
295     if(QMessageBox::question(this, tr("Remove dictionary"),
296             tr("Do you want to remove selected dictionary?"),
297             QMessageBox::Yes, QMessageBox::No) == QMessageBox::Yes) {
298 #ifndef Q_WS_MAEMO_5
299         emit removeDictionary(model->currentDict());
300         refreshDictsList();
301 #else
302         QList<QListWidgetItem*> selected = dictList->selectedItems();
303         if(selected.count() > 0) {
304             emit removeDictionary(dictsHash[selected[0]]);
305             refreshDictsList();
306         }
307 #endif
308    }
309 }
310
311 void DictManagerWidget::settingsButtonClicked() {
312     #ifndef Q_WS_MAEMO_5
313     if(!_changed || QMessageBox::question(this,
314             tr("Save"), tr("Do you want to save changes?"),
315             QMessageBox::Save, QMessageBox::Cancel) == QMessageBox::Save) {
316         _save = true;
317         saveChanges();
318         _save = false;
319     }
320     model->currentDict()->dictDialog()->changeSettings(this->parentWidget());
321     refreshDictsList();
322     #else
323    QList<QListWidgetItem*> selected = dictList->selectedItems();
324    if(selected.count() > 0) {
325        dictsHash[selected[0]]->dictDialog()->changeSettings(this->parentWidget());
326    }
327    refreshDictsList();
328 #endif
329 }
330
331
332 void DictManagerWidget::changed() {
333     _changed=true;
334 }
335
336
337 #ifndef Q_WS_MAEMO_5
338     void DictManagerWidget::save() {
339         _save = true;
340         hide();
341     }
342 #endif
343
344
345 void DictManagerWidget::keyPressEvent(QKeyEvent *e) {
346     if (!e->modifiers() || (e->modifiers() & Qt::KeypadModifier && e->key() == Qt::Key_Enter)) {
347         switch (e->key()) {
348             case Qt::Key_Escape:
349                reject();
350                break;
351             default:
352                e->ignore();
353                return;
354         }
355     } else {
356         e->ignore();
357     }
358 }