Added saving changes before proceed to new dialog
[mdictionary] / trunk / src / base / 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 Dicrionaries 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 "../../includes/DictDialog.h"
30
31 DictManagerWidget::DictManagerWidget(GUIInterface *parent) :
32     QDialog(parent) {
33
34
35
36     setWindowTitle(tr("Dictionaries"));
37     this->guiInterface = parent;
38
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     _save = false;
130     refreshDictsList();
131     QWidget::showEvent(e);
132 }
133
134 void DictManagerWidget::saveChanges() {
135
136     #ifndef Q_WS_MAEMO_5
137     if(_save) {
138     #else
139     if(_changed &&
140             QMessageBox::question(this, "Save", "Do you want to save changes?",
141             QMessageBox::Save, QMessageBox::Cancel) == QMessageBox::Save) {
142     #endif
143         QList<CommonDictInterface*> checkedDicts;
144
145         for(int i=0; i<dictListWidget->count(); i++) {
146             QListWidgetItem* item = dictListWidget->item(i);
147             if(item->checkState() == Qt::Checked) {
148                 checkedDicts.push_back(dictsHash[item]);
149             }
150         }
151         emit selectedDictionaries(checkedDicts);
152     }
153
154     _changed = false;
155 }
156
157 void DictManagerWidget::hideEvent(QHideEvent *e)
158 {
159     saveChanges();
160     QWidget::hideEvent(e);
161 }
162
163
164 void DictManagerWidget::addNewDictButtonClicked() {
165     CommonDictInterface* selectedPlugin =
166             DictTypeSelectDialog::addNewDict(guiInterface->getPlugins(),this);
167     if(selectedPlugin != NULL) {
168         Settings* settings =
169                 selectedPlugin->dictDialog()->addNewDictionary(this);
170
171         if(settings != NULL) {
172             CommonDictInterface* newDict = selectedPlugin->getNew(settings);
173             emit addDictionary(newDict);
174             refreshDictsList();
175         }
176     }
177 }
178
179 void DictManagerWidget::itemSelected(QListWidgetItem *) {
180     removeDictButton->setEnabled(true);
181     settingsButton->setEnabled(true);
182 }
183
184 void DictManagerWidget::removeButtonClicked() {
185     if(QMessageBox::question(this, "Remove dictionary",
186             "Do you want to remove selected dictionary?",
187             QMessageBox::Ok, QMessageBox::Cancel) == QMessageBox::Ok) {
188         QList<QListWidgetItem*> selected = dictListWidget->selectedItems();
189         if(selected.count() > 0) {
190             emit removeDictionary(dictsHash[selected[0]]);
191             refreshDictsList();
192         }
193    }
194 }
195
196 void DictManagerWidget::settingsButtonClicked() {
197     QList<QListWidgetItem*> selected = dictListWidget->selectedItems();
198     if(selected.count() > 0) {
199         dictsHash[selected[0]]->dictDialog()->changeSettings(this);
200         refreshDictsList();
201     }
202 }
203
204
205 void DictManagerWidget::changed() {
206     _changed=true;
207 }
208
209
210 #ifndef Q_WS_MAEMO_5
211     void DictManagerWidget::save() {
212         _save = true;
213         hide();
214     }
215 #endif