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