Merge branch 'master' into qmake
[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 //! \author Mateusz Półrola <mateusz.polrola@comarch.pl>
24
25 #include "DictManagerWidget.h"
26 #include "DictTypeSelectDialog.h"
27 #include <QDebug>
28 #include "../../common/DictDialog.h"
29
30 DictManagerWidget::DictManagerWidget(GUIInterface *parent) :
31     QDialog(parent) {
32     setWindowTitle(tr("Dictionaries"));
33     this->guiInterface = parent;
34
35     initalizeUI();
36 }
37
38 void DictManagerWidget::initalizeUI() {
39     verticalLayout = new QVBoxLayout;
40     setLayout(verticalLayout);
41
42     dictListWidget = new QListWidget;
43     verticalLayout->addWidget(dictListWidget);
44
45     dictListWidget->setSelectionMode(QAbstractItemView::SingleSelection);
46     dictListWidget->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
47
48     addNewDictButton = new QPushButton(tr("Add"));
49     removeDictButton = new QPushButton(tr("Remove"));
50     settingsButton = new QPushButton(tr("Settings"));
51
52     removeDictButton->setEnabled(false);
53     settingsButton->setEnabled(false);
54
55     buttonGroup = new QHBoxLayout;
56
57     buttonGroup->addWidget(addNewDictButton);
58     buttonGroup->addWidget(removeDictButton);
59     buttonGroup->addWidget(settingsButton);
60
61     verticalLayout->addLayout(buttonGroup, Qt::AlignBottom);
62
63
64     connect(addNewDictButton, SIGNAL(clicked()),
65             this, SLOT(saveChanges()));
66     connect(addNewDictButton, SIGNAL(clicked()),
67             this, SLOT(addNewDictButtonClicked()));
68
69     connect(removeDictButton, SIGNAL(clicked()),
70             this, SLOT(saveChanges()));
71     connect(removeDictButton, SIGNAL(clicked()),
72             this, SLOT(removeButtonClicked()));
73
74     connect(settingsButton, SIGNAL(clicked()),
75             this, SLOT(saveChanges()));
76     connect(settingsButton, SIGNAL(clicked()),
77             this, SLOT(settingsButtonClicked()));
78
79     connect(dictListWidget, SIGNAL(itemClicked(QListWidgetItem*)),
80             this, SLOT(itemSelected(QListWidgetItem*)));
81
82     connect(dictListWidget, SIGNAL(itemChanged(QListWidgetItem*)),
83             this, SLOT(changed()));
84
85     refreshDictsList();
86
87     #ifndef Q_WS_MAEMO_5
88         setMinimumSize(500,300);
89         closeButton = new QPushButton(tr("Save"));
90         buttonGroup->addWidget(closeButton);
91         connect(closeButton, SIGNAL(clicked()), this, SLOT(save()));
92     #endif
93 }
94
95
96 void DictManagerWidget::refreshDictsList() {
97
98     dictListWidget->clear();
99     dictsHash.clear();
100     removeDictButton->setEnabled(false);
101     settingsButton->setEnabled(false);
102
103     QHash<CommonDictInterface*, bool> dicts = guiInterface->getDictionaries();
104
105     QHashIterator<CommonDictInterface*, bool> i(dicts);
106
107     while(i.hasNext()) {
108         i.next();
109         QListWidgetItem* item = new QListWidgetItem();
110         QString name = i.key()->langFrom() + " - " + i.key()->langTo() + " (" +
111                        i.key()->type() + " " + i.key()->name() + ")";
112         item->setText(name);
113         item->setFlags(item->flags() | Qt::ItemIsUserCheckable);
114         if(i.value()) {
115             item->setCheckState(Qt::Checked);
116         }
117         else {
118             item->setCheckState(Qt::Unchecked);
119         }
120         item->setIcon(*i.key()->icon());
121
122         dictListWidget->addItem(item);
123         dictsHash.insert(item, i.key());
124     }
125 }
126
127 void DictManagerWidget::showEvent(QShowEvent *e) {
128     _changed = false;
129     #ifndef Q_WS_MAEMO_5
130       _save = false;
131     #endif
132     refreshDictsList();
133     QWidget::showEvent(e);
134 }
135
136 void DictManagerWidget::saveChanges() {
137
138     #ifndef Q_WS_MAEMO_5
139         if(_save) {
140     #else
141         if(_changed &&
142                 QMessageBox::question(this, tr("Save"),
143                                       tr("Do you want to save changes?"),
144                 QMessageBox::Save, QMessageBox::Cancel) == QMessageBox::Save) {
145     #endif
146         QList<CommonDictInterface*> checkedDicts;
147
148         for(int i=0; i<dictListWidget->count(); i++) {
149             QListWidgetItem* item = dictListWidget->item(i);
150             if(item->checkState() == Qt::Checked) {
151                 checkedDicts.push_back(dictsHash[item]);
152             }
153         }
154         _changed = false;
155         emit selectedDictionaries(checkedDicts);
156     }
157 }
158
159 void DictManagerWidget::hideEvent(QHideEvent *e) {
160     saveChanges();
161     QWidget::hideEvent(e);
162 }
163
164
165 void DictManagerWidget::addNewDictButtonClicked() {
166     #ifndef Q_WS_MAEMO_5
167     if(!_changed || QMessageBox::question(this,
168             "Save", "Do you want to save changes?",
169             QMessageBox::Save, QMessageBox::Cancel) == QMessageBox::Save) {
170         _save = true;
171         saveChanges();
172         _save = false;
173     }
174     #endif
175
176    CommonDictInterface* selectedPlugin =
177            DictTypeSelectDialog::addNewDict(guiInterface->getPlugins(),this);
178    if(selectedPlugin != NULL) {
179        Settings* settings =
180                selectedPlugin->dictDialog()->addNewDictionary(this);
181
182        if(settings != NULL) {
183            CommonDictInterface* newDict = selectedPlugin->getNew(settings);
184            delete settings;
185            Q_EMIT addDictionary(newDict);
186        }
187    }
188    refreshDictsList();
189 }
190
191 void DictManagerWidget::itemSelected(QListWidgetItem *) {
192     removeDictButton->setEnabled(true);
193     settingsButton->setEnabled(true);
194 }
195
196 void DictManagerWidget::removeButtonClicked() {
197     if(QMessageBox::question(this, tr("Remove dictionary"),
198             tr("Do you want to remove selected dictionary?"),
199             QMessageBox::Yes, QMessageBox::No) == QMessageBox::Yes) {
200
201         QList<QListWidgetItem*> selected = dictListWidget->selectedItems();
202         if(selected.count() > 0) {
203             emit removeDictionary(dictsHash[selected[0]]);
204             refreshDictsList();
205         }
206    }
207 }
208
209 void DictManagerWidget::settingsButtonClicked() {
210     #ifndef Q_WS_MAEMO_5
211     if(!_changed || QMessageBox::question(this,
212             "Save", "Do you want to save changes?",
213             QMessageBox::Save, QMessageBox::Cancel) == QMessageBox::Save) {
214         _save = true;
215         saveChanges();
216         _save = false;
217     }
218     #endif
219    QList<QListWidgetItem*> selected = dictListWidget->selectedItems();
220    if(selected.count() > 0) {
221        dictsHash[selected[0]]->dictDialog()->changeSettings(this);
222    }
223    refreshDictsList();
224 }
225
226
227 void DictManagerWidget::changed() {
228     _changed=true;
229 }
230
231
232 #ifndef Q_WS_MAEMO_5
233     void DictManagerWidget::save() {
234         _save = true;
235         hide();
236     }
237 #endif