Added new style, fixed some bugs in google plugin dialog and dict manager 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 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     dictList = new QListWidget;
45     verticalLayout->addWidget(dictList);
46
47     dictList->setSelectionMode(QAbstractItemView::SingleSelection);
48     dictList->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(dictList, SIGNAL(itemClicked(QListWidgetItem*)),
82             this, SLOT(itemSelected(QListWidgetItem*)));
83
84     connect(dictList, 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     dictList->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         dictList->addItem(item);
125         dictsHash.insert(item, i.key());
126     }
127
128     dictList->sortItems();
129 }
130
131 void DictManagerWidget::showEvent(QShowEvent *e) {
132     _changed = false;
133     #ifndef Q_WS_MAEMO_5
134       _save = false;
135     #endif
136     refreshDictsList();
137     QWidget::showEvent(e);
138 }
139
140 void DictManagerWidget::saveChanges() {
141
142     #ifndef Q_WS_MAEMO_5
143         if(_save) {
144     #else
145         if(_changed &&
146                 QMessageBox::question(this, tr("Save"),
147                                       tr("Do you want to save changes?"),
148                 QMessageBox::Save, QMessageBox::Cancel) == QMessageBox::Save) {
149     #endif
150         QList<CommonDictInterface*> checkedDicts;
151
152         for(int i=0; i<dictList->count(); i++) {
153             QListWidgetItem* item = dictList->item(i);
154             if(item->checkState() == Qt::Checked) {
155                 checkedDicts.push_back(dictsHash[item]);
156             }
157         }
158         _changed = false;
159         emit selectedDictionaries(checkedDicts);
160     }
161 }
162
163 void DictManagerWidget::hideEvent(QHideEvent *e) {
164     saveChanges();
165     QWidget::hideEvent(e);
166 }
167
168
169 void DictManagerWidget::addNewDictButtonClicked() {
170     #ifndef Q_WS_MAEMO_5
171     if(!_changed || QMessageBox::question(this,
172             tr("Save"), tr("Do you want to save changes?"),
173             QMessageBox::Save, QMessageBox::Cancel) == QMessageBox::Save) {
174         _save = true;
175         saveChanges();
176         _save = false;
177     }
178     #endif
179
180    CommonDictInterface* selectedPlugin =
181            DictTypeSelectDialog::addNewDict(guiInterface->getPlugins(),this);
182    if(selectedPlugin) {
183        Settings* settings =
184                selectedPlugin->dictDialog()->addNewDictionary(this->parentWidget());
185
186        if(settings) {
187            CommonDictInterface* newDict = selectedPlugin->getNew(settings);
188            if(newDict) {
189                delete settings;
190                Q_EMIT addDictionary(newDict);
191            }
192        }
193    }
194    refreshDictsList();
195 }
196
197 void DictManagerWidget::itemSelected(QListWidgetItem *) {
198     removeDictButton->setEnabled(true);
199     settingsButton->setEnabled(true);
200 }
201
202 void DictManagerWidget::removeButtonClicked() {
203     if(QMessageBox::question(this, tr("Remove dictionary"),
204             tr("Do you want to remove selected dictionary?"),
205             QMessageBox::Yes, QMessageBox::No) == QMessageBox::Yes) {
206
207         QList<QListWidgetItem*> selected = dictList->selectedItems();
208         if(selected.count() > 0) {
209             emit removeDictionary(dictsHash[selected[0]]);
210             refreshDictsList();
211         }
212    }
213 }
214
215 void DictManagerWidget::settingsButtonClicked() {
216     #ifndef Q_WS_MAEMO_5
217     if(!_changed || QMessageBox::question(this,
218             tr("Save"), tr("Do you want to save changes?"),
219             QMessageBox::Save, QMessageBox::Cancel) == QMessageBox::Save) {
220         _save = true;
221         saveChanges();
222         _save = false;
223     }
224     #endif
225    QList<QListWidgetItem*> selected = dictList->selectedItems();
226    if(selected.count() > 0) {
227        dictsHash[selected[0]]->dictDialog()->changeSettings(this->parentWidget());
228    }
229    refreshDictsList();
230 }
231
232
233 void DictManagerWidget::changed() {
234     _changed=true;
235 }
236
237
238 #ifndef Q_WS_MAEMO_5
239     void DictManagerWidget::save() {
240         _save = true;
241         hide();
242     }
243 #endif