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