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