qml - webView widget
[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     initalizeUI();
40
41     setModal(true);
42 }
43
44 void DictManagerWidget::initalizeUI() {
45     verticalLayout = new QVBoxLayout;
46     setLayout(verticalLayout);
47
48     dictList = new QListWidget;
49     verticalLayout->addWidget(dictList);
50
51     dictList->setSelectionMode(QAbstractItemView::SingleSelection);
52     dictList->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
53
54     addNewDictButton = new QPushButton(tr("Add"));
55     removeDictButton = new QPushButton(tr("Remove"));
56     settingsButton = new QPushButton(tr("Settings"));
57
58     removeDictButton->setEnabled(false);
59     settingsButton->setEnabled(false);
60
61     buttonGroup = new QHBoxLayout;
62
63     buttonGroup->addWidget(addNewDictButton);
64     buttonGroup->addWidget(removeDictButton);
65     buttonGroup->addWidget(settingsButton);
66
67     verticalLayout->addLayout(buttonGroup, Qt::AlignBottom);
68
69
70     connect(addNewDictButton, SIGNAL(clicked()),
71             this, SLOT(saveChanges()));
72     connect(addNewDictButton, SIGNAL(clicked()),
73             this, SLOT(addNewDictButtonClicked()));
74
75     connect(removeDictButton, SIGNAL(clicked()),
76             this, SLOT(saveChanges()));
77     connect(removeDictButton, SIGNAL(clicked()),
78             this, SLOT(removeButtonClicked()));
79
80     connect(settingsButton, SIGNAL(clicked()),
81             this, SLOT(saveChanges()));
82     connect(settingsButton, SIGNAL(clicked()),
83             this, SLOT(settingsButtonClicked()));
84
85     connect(dictList, SIGNAL(itemClicked(QListWidgetItem*)),
86             this, SLOT(itemSelected(QListWidgetItem*)));
87
88     connect(dictList, SIGNAL(itemChanged(QListWidgetItem*)),
89             this, SLOT(changed()));
90
91     #ifndef Q_WS_MAEMO_5
92         connect(dictList, SIGNAL(itemActivated(QListWidgetItem*)),
93                 this, SLOT(saveChanges()));
94         connect(dictList, SIGNAL(itemActivated(QListWidgetItem*)),
95                 this, SLOT(itemSelected(QListWidgetItem*)));
96         connect(dictList, SIGNAL(itemActivated(QListWidgetItem*)),
97                 settingsButton, SIGNAL(clicked()));
98     #endif
99
100     refreshDictsList();
101
102     #ifndef Q_WS_MAEMO_5
103         setMinimumSize(500,300);
104         closeButton = new QPushButton(tr("Save"));
105         buttonGroup->addWidget(closeButton);
106
107         setMinimumWidth(sizeHint().width()*1.2);
108         setMaximumWidth(sizeHint().width()*2);
109         setMinimumHeight(sizeHint().height());
110         setMaximumHeight(sizeHint().height()*2);
111         connect(closeButton, SIGNAL(clicked()), this, SLOT(save()));
112     #endif
113 }
114
115
116 void DictManagerWidget::refreshDictsList() {
117
118     dictList->clear();
119     dictsHash.clear();
120     removeDictButton->setEnabled(false);
121     settingsButton->setEnabled(false);
122
123     QHash<CommonDictInterface*, bool> dicts = guiInterface->getDictionaries();
124
125     QHashIterator<CommonDictInterface*, bool> i(dicts);
126
127     while(i.hasNext()) {
128         i.next();
129         QListWidgetItem* item = new QListWidgetItem();
130         QString name;
131         if (i.key()->type() == "stardict") {
132             name = i.key()->name() + " (" + i.key()->type() + ")";
133         }
134         else {
135             name = i.key()->langFrom() + " - " + i.key()->langTo() +
136                    " (" + i.key()->type() + " " +
137                    i.key()->name() + ")";
138         }
139         item->setText(name);
140         item->setFlags(item->flags() | Qt::ItemIsUserCheckable);
141         if(i.value()) {
142             item->setCheckState(Qt::Checked);
143         }
144         else {
145             item->setCheckState(Qt::Unchecked);
146         }
147         item->setIcon(*i.key()->icon());
148
149         dictList->addItem(item);
150         dictsHash.insert(item, i.key());
151     }
152
153     dictList->sortItems();
154 }
155
156 void DictManagerWidget::showEvent(QShowEvent *e) {
157     _changed = false;
158     #ifndef Q_WS_MAEMO_5
159       _save = false;
160     #endif
161     refreshDictsList();
162     QWidget::showEvent(e);
163 }
164
165 void DictManagerWidget::saveChanges() {
166
167     #ifndef Q_WS_MAEMO_5
168         if(_save) {
169     #else
170         if(_changed &&
171                 QMessageBox::question(this, tr("Save"),
172                                       tr("Do you want to save changes?"),
173                 QMessageBox::Save, QMessageBox::Cancel) == QMessageBox::Save) {
174     #endif
175         QList<CommonDictInterface*> checkedDicts;
176
177         for(int i=0; i<dictList->count(); i++) {
178             QListWidgetItem* item = dictList->item(i);
179             if(item->checkState() == Qt::Checked) {
180                 checkedDicts.push_back(dictsHash[item]);
181             }
182         }
183         _changed = false;
184         emit selectedDictionaries(checkedDicts);
185     }
186 }
187
188 void DictManagerWidget::hideEvent(QHideEvent *e) {
189     saveChanges();
190     QWidget::hideEvent(e);
191 }
192
193
194 void DictManagerWidget::addNewDictButtonClicked() {
195     #ifndef Q_WS_MAEMO_5
196     if(!_changed || QMessageBox::question(this,
197             tr("Save"), tr("Do you want to save changes?"),
198             QMessageBox::Save, QMessageBox::Cancel) == QMessageBox::Save) {
199         _save = true;
200         saveChanges();
201         _save = false;
202     }
203     #endif
204
205    CommonDictInterface* selectedPlugin =
206            DictTypeSelectDialog::addNewDict(guiInterface->getPlugins(),this);
207    if(selectedPlugin) {
208        Settings* settings =
209                selectedPlugin->dictDialog()->addNewDictionary(this->parentWidget());
210
211        if(settings) {
212            CommonDictInterface* newDict = selectedPlugin->getNew(settings);
213            if(newDict) {
214                delete settings;
215                Q_EMIT addDictionary(newDict);
216            }
217        }
218    }
219    refreshDictsList();
220 }
221
222 void DictManagerWidget::itemSelected(QListWidgetItem *) {
223     removeDictButton->setEnabled(true);
224     settingsButton->setEnabled(true);
225     dictList->setFocus();
226 }
227
228 void DictManagerWidget::removeButtonClicked() {
229     if(QMessageBox::question(this, tr("Remove dictionary"),
230             tr("Do you want to remove selected dictionary?"),
231             QMessageBox::Yes, QMessageBox::No) == QMessageBox::Yes) {
232
233         QList<QListWidgetItem*> selected = dictList->selectedItems();
234         if(selected.count() > 0) {
235             emit removeDictionary(dictsHash[selected[0]]);
236             refreshDictsList();
237         }
238    }
239 }
240
241 void DictManagerWidget::settingsButtonClicked() {
242     #ifndef Q_WS_MAEMO_5
243     if(!_changed || QMessageBox::question(this,
244             tr("Save"), tr("Do you want to save changes?"),
245             QMessageBox::Save, QMessageBox::Cancel) == QMessageBox::Save) {
246         _save = true;
247         saveChanges();
248         _save = false;
249     }
250     #endif
251    QList<QListWidgetItem*> selected = dictList->selectedItems();
252    if(selected.count() > 0) {
253        dictsHash[selected[0]]->dictDialog()->changeSettings(this->parentWidget());
254    }
255    refreshDictsList();
256 }
257
258
259 void DictManagerWidget::changed() {
260     _changed=true;
261 }
262
263
264 #ifndef Q_WS_MAEMO_5
265     void DictManagerWidget::save() {
266         _save = true;
267         hide();
268     }
269 #endif
270
271
272 void DictManagerWidget::keyPressEvent(QKeyEvent *e) {
273     if (!e->modifiers() || (e->modifiers() & Qt::KeypadModifier && e->key() == Qt::Key_Enter)) {
274         switch (e->key()) {
275             case Qt::Key_Escape:
276                reject();
277                break;
278             default:
279                e->ignore();
280                return;
281         }
282     } else {
283         e->ignore();
284     }
285 }