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