Fixed problem with connects in DictManagerWidget.cpp. Fixed: remove warnings from...
[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     //z modelu sygnał, oraz z okienka zmian ustawień w pluginie, gdy są zmiany
129 //    oryginalnie:
130 //    connect(dictList, SIGNAL(itemChanged(QListWidgetItem*)),
131 //            this, SLOT(changed()));
132     connect(model, SIGNAL(itemChanged()),
133                 this, SLOT(changed()));
134     //pozmieniać connecty, to jest na dwuklik mysza na liście, sprawdzić, zrobic alternatywne sloty
135             connect(rootObject, SIGNAL(itemActivated(int)),
136                     this, SLOT(saveChanges()));
137             connect(rootObject, SIGNAL(itemActivated(int)),
138                     this, SLOT(settingsButtonClicked()));
139
140 //        connect(dictList, SIGNAL(itemActivated(QListWidgetItem*)),
141 //                this, SLOT(saveChanges()));
142 //        connect(dictList, SIGNAL(itemActivated(QListWidgetItem*)),
143 //                this, SLOT(itemSelected(QListWidgetItem*)));
144 //        connect(dictList, SIGNAL(itemActivated(QListWidgetItem*)),
145 //                settingsButton, SIGNAL(clicked()));
146     #endif
147
148     #ifdef Q_WS_MAEMO_5
149     refreshDictsList();
150     #endif
151
152     #ifndef Q_WS_MAEMO_5
153         setMinimumSize(500,300);
154         //closeButton = new QPushButton(tr("Save"));
155         //buttonGroup->addWidget(closeButton);
156
157 //        setMinimumWidth(sizeHint().width()*1.2);
158 //        setMaximumWidth(sizeHint().width()*2);
159 //        setMinimumHeight(sizeHint().height());
160 //        setMaximumHeight(sizeHint().height()*2);
161         //connect(closeButton, SIGNAL(clicked()), this, SLOT(save()));
162         connect(rootObject, SIGNAL(saveButtonClicked()), this, SLOT(save()));
163     #endif
164 }
165
166
167 void DictManagerWidget::refreshDictsList() {
168
169     #ifndef Q_WS_MAEMO_5
170     QHash<CommonDictInterface*, bool> dicts = guiInterface->getDictionaries();
171
172     if (model == 0){
173         model = new DictManagerModel(dicts, this);
174     } else {
175         model->clear();
176         model->setDictionaries(dicts);
177     }
178
179     #endif
180
181     #ifdef Q_WS_MAEMO_5
182     dictList->clear();
183     dictsHash.clear();
184     removeDictButton->setEnabled(false);
185     settingsButton->setEnabled(false);
186
187     QHash<CommonDictInterface*, bool> dicts = guiInterface->getDictionaries();
188
189     QHashIterator<CommonDictInterface*, bool> i(dicts);
190
191     while(i.hasNext()) {
192         i.next();
193
194         QListWidgetItem* item = new QListWidgetItem();
195         QString name;
196         if (i.key()->type() == "stardict") {
197             name = i.key()->name() + " (" + i.key()->type() + ")";
198         }
199         else {
200             name = i.key()->langFrom() + " - " + i.key()->langTo() +
201                    " (" + i.key()->type() + " " +
202                    i.key()->name() + ")";
203         }
204         item->setText(name);
205         item->setFlags(item->flags() | Qt::ItemIsUserCheckable);
206         if(i.value()) {
207             item->setCheckState(Qt::Checked);
208         }
209         else {
210             item->setCheckState(Qt::Unchecked);
211         }
212         item->setIcon(*i.key()->icon());
213
214         dictList->addItem(item);
215         dictsHash.insert(item, i.key());
216     }
217
218     dictList->sortItems();
219     #endif
220 }
221
222 void DictManagerWidget::showEvent(QShowEvent *e) {
223     _changed = false;
224     #ifndef Q_WS_MAEMO_5
225       _save = false;
226     #endif
227     refreshDictsList();
228     QWidget::showEvent(e);
229 }
230
231 void DictManagerWidget::saveChanges() {
232
233     #ifndef Q_WS_MAEMO_5
234         if(_save) {
235     #else
236         if(_changed &&
237                 QMessageBox::question(this, tr("Save"),
238                                       tr("Do you want to save changes?"),
239                 QMessageBox::Save, QMessageBox::Cancel) == QMessageBox::Save) {
240     #endif
241         QList<CommonDictInterface*> checkedDicts;
242
243         for(int i=0; i<dictList->count(); i++) {
244             QListWidgetItem* item = dictList->item(i);
245             if(item->checkState() == Qt::Checked) {
246                 checkedDicts.push_back(dictsHash[item]);
247             }
248         }
249         _changed = false;
250         emit selectedDictionaries(checkedDicts);
251     }
252 }
253
254 void DictManagerWidget::hideEvent(QHideEvent *e) {
255     saveChanges();
256     QWidget::hideEvent(e);
257 }
258
259
260 void DictManagerWidget::addNewDictButtonClicked() {
261     #ifndef Q_WS_MAEMO_5
262     if(!_changed || QMessageBox::question(this,
263             tr("Save"), tr("Do you want to save changes?"),
264             QMessageBox::Save, QMessageBox::Cancel) == QMessageBox::Save) {
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
299         QList<QListWidgetItem*> selected = dictList->selectedItems();
300         if(selected.count() > 0) {
301             emit removeDictionary(dictsHash[selected[0]]);
302             refreshDictsList();
303         }
304    }
305 }
306
307 void DictManagerWidget::settingsButtonClicked() {
308     #ifndef Q_WS_MAEMO_5
309     if(!_changed || QMessageBox::question(this,
310             tr("Save"), tr("Do you want to save changes?"),
311             QMessageBox::Save, QMessageBox::Cancel) == QMessageBox::Save) {
312         _save = true;
313         saveChanges();
314         _save = false;
315     }
316     #endif
317    QList<QListWidgetItem*> selected = dictList->selectedItems();
318    if(selected.count() > 0) {
319        dictsHash[selected[0]]->dictDialog()->changeSettings(this->parentWidget());
320    }
321    refreshDictsList();
322 }
323
324
325 void DictManagerWidget::changed() {
326     _changed=true;
327 }
328
329
330 #ifndef Q_WS_MAEMO_5
331     void DictManagerWidget::save() {
332         _save = true;
333         hide();
334     }
335 #endif
336
337
338 void DictManagerWidget::keyPressEvent(QKeyEvent *e) {
339     if (!e->modifiers() || (e->modifiers() & Qt::KeypadModifier && e->key() == Qt::Key_Enter)) {
340         switch (e->key()) {
341             case Qt::Key_Escape:
342                reject();
343                break;
344             default:
345                e->ignore();
346                return;
347         }
348     } else {
349         e->ignore();
350     }
351 }