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