Fixed bookmarks removing bug
[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     refreshDictsList();
90
91     #ifndef Q_WS_MAEMO_5
92         setMinimumSize(500,300);
93         closeButton = new QPushButton(tr("Save"));
94         buttonGroup->addWidget(closeButton);
95
96         setMinimumWidth(sizeHint().width()*1.2);
97         setMaximumWidth(sizeHint().width()*2);
98         setMinimumHeight(sizeHint().height());
99         setMaximumHeight(sizeHint().height()*2);
100         connect(closeButton, SIGNAL(clicked()), this, SLOT(save()));
101     #endif
102 }
103
104
105 void DictManagerWidget::refreshDictsList() {
106
107     dictList->clear();
108     dictsHash.clear();
109     removeDictButton->setEnabled(false);
110     settingsButton->setEnabled(false);
111
112     QHash<CommonDictInterface*, bool> dicts = guiInterface->getDictionaries();
113
114     QHashIterator<CommonDictInterface*, bool> i(dicts);
115
116     while(i.hasNext()) {
117         i.next();
118         QListWidgetItem* item = new QListWidgetItem();
119         QString name = i.key()->langFrom() + " - " + i.key()->langTo() + " (" +
120                        i.key()->type() + " " + i.key()->name() + ")";
121         item->setText(name);
122         item->setFlags(item->flags() | Qt::ItemIsUserCheckable);
123         if(i.value()) {
124             item->setCheckState(Qt::Checked);
125         }
126         else {
127             item->setCheckState(Qt::Unchecked);
128         }
129         item->setIcon(*i.key()->icon());
130
131         dictList->addItem(item);
132         dictsHash.insert(item, i.key());
133     }
134
135     dictList->sortItems();
136 }
137
138 void DictManagerWidget::showEvent(QShowEvent *e) {
139     _changed = false;
140     #ifndef Q_WS_MAEMO_5
141       _save = false;
142     #endif
143     refreshDictsList();
144     QWidget::showEvent(e);
145 }
146
147 void DictManagerWidget::saveChanges() {
148
149     #ifndef Q_WS_MAEMO_5
150         if(_save) {
151     #else
152         if(_changed &&
153                 QMessageBox::question(this, tr("Save"),
154                                       tr("Do you want to save changes?"),
155                 QMessageBox::Save, QMessageBox::Cancel) == QMessageBox::Save) {
156     #endif
157         QList<CommonDictInterface*> checkedDicts;
158
159         for(int i=0; i<dictList->count(); i++) {
160             QListWidgetItem* item = dictList->item(i);
161             if(item->checkState() == Qt::Checked) {
162                 checkedDicts.push_back(dictsHash[item]);
163             }
164         }
165         _changed = false;
166         emit selectedDictionaries(checkedDicts);
167     }
168 }
169
170 void DictManagerWidget::hideEvent(QHideEvent *e) {
171     saveChanges();
172     QWidget::hideEvent(e);
173 }
174
175
176 void DictManagerWidget::addNewDictButtonClicked() {
177     #ifndef Q_WS_MAEMO_5
178     if(!_changed || QMessageBox::question(this,
179             tr("Save"), tr("Do you want to save changes?"),
180             QMessageBox::Save, QMessageBox::Cancel) == QMessageBox::Save) {
181         _save = true;
182         saveChanges();
183         _save = false;
184     }
185     #endif
186
187    CommonDictInterface* selectedPlugin =
188            DictTypeSelectDialog::addNewDict(guiInterface->getPlugins(),this);
189    if(selectedPlugin) {
190        Settings* settings =
191                selectedPlugin->dictDialog()->addNewDictionary(this->parentWidget());
192
193        if(settings) {
194            CommonDictInterface* newDict = selectedPlugin->getNew(settings);
195            if(newDict) {
196                delete settings;
197                Q_EMIT addDictionary(newDict);
198            }
199        }
200    }
201    refreshDictsList();
202 }
203
204 void DictManagerWidget::itemSelected(QListWidgetItem *) {
205     removeDictButton->setEnabled(true);
206     settingsButton->setEnabled(true);
207 }
208
209 void DictManagerWidget::removeButtonClicked() {
210     if(QMessageBox::question(this, tr("Remove dictionary"),
211             tr("Do you want to remove selected dictionary?"),
212             QMessageBox::Yes, QMessageBox::No) == QMessageBox::Yes) {
213
214         QList<QListWidgetItem*> selected = dictList->selectedItems();
215         if(selected.count() > 0) {
216             emit removeDictionary(dictsHash[selected[0]]);
217             refreshDictsList();
218         }
219    }
220 }
221
222 void DictManagerWidget::settingsButtonClicked() {
223     #ifndef Q_WS_MAEMO_5
224     if(!_changed || QMessageBox::question(this,
225             tr("Save"), tr("Do you want to save changes?"),
226             QMessageBox::Save, QMessageBox::Cancel) == QMessageBox::Save) {
227         _save = true;
228         saveChanges();
229         _save = false;
230     }
231     #endif
232    QList<QListWidgetItem*> selected = dictList->selectedItems();
233    if(selected.count() > 0) {
234        dictsHash[selected[0]]->dictDialog()->changeSettings(this->parentWidget());
235    }
236    refreshDictsList();
237 }
238
239
240 void DictManagerWidget::changed() {
241     _changed=true;
242 }
243
244
245 #ifndef Q_WS_MAEMO_5
246     void DictManagerWidget::save() {
247         _save = true;
248         hide();
249     }
250 #endif