Fixed gui: search progress bar, result list
[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(this);
40     setLayout(verticalLayout);
41
42     dictListWidget = new QListWidget(this);
43     verticalLayout->addWidget(dictListWidget);
44
45     dictListWidget->setSelectionMode(QAbstractItemView::SingleSelection);
46     dictListWidget->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
47
48     addNewDictButton = new QPushButton(tr("Add"),this);
49     removeDictButton = new QPushButton(tr("Remove"),this);
50     settingsButton = new QPushButton(tr("Settings"),this);
51
52     removeDictButton->setEnabled(false);
53     settingsButton->setEnabled(false);
54
55     buttonGroup = new QHBoxLayout(this);
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     #ifndef Q_WS_MAEMO_5
130         _save = false;
131     #endif
132     refreshDictsList();
133     QWidget::showEvent(e);
134 }
135
136 void DictManagerWidget::saveChanges() {
137
138     #ifndef Q_WS_MAEMO_5
139     if(_save) {
140     #else
141     if(_changed &&
142             QMessageBox::question(this, "Save", "Do you want to save changes?",
143             QMessageBox::Save, QMessageBox::Cancel) == QMessageBox::Save) {
144     #endif
145         QList<CommonDictInterface*> checkedDicts;
146
147         for(int i=0; i<dictListWidget->count(); i++) {
148             QListWidgetItem* item = dictListWidget->item(i);
149             if(item->checkState() == Qt::Checked) {
150                 checkedDicts.push_back(dictsHash[item]);
151             }
152         }
153         emit selectedDictionaries(checkedDicts);
154     }
155
156     _changed = false;
157 }
158
159 void DictManagerWidget::hideEvent(QHideEvent *e)
160 {
161     saveChanges();
162     QWidget::hideEvent(e);
163 }
164
165
166 void DictManagerWidget::addNewDictButtonClicked() {
167     CommonDictInterface* selectedPlugin =
168             DictTypeSelectDialog::addNewDict(guiInterface->getPlugins(),this);
169     if(selectedPlugin != NULL) {
170         Settings* settings =
171                 selectedPlugin->dictDialog()->addNewDictionary(this);
172
173         if(settings != NULL) {
174             CommonDictInterface* newDict = selectedPlugin->getNew(settings);
175             emit addDictionary(newDict);
176             refreshDictsList();
177         }
178     }
179 }
180
181 void DictManagerWidget::itemSelected(QListWidgetItem *) {
182     removeDictButton->setEnabled(true);
183     settingsButton->setEnabled(true);
184 }
185
186 void DictManagerWidget::removeButtonClicked() {
187     if(QMessageBox::question(this, "Remove dictionary",
188             "Do you want to remove selected dictionary?",
189             QMessageBox::Ok, QMessageBox::Cancel) == QMessageBox::Ok) {
190         QList<QListWidgetItem*> selected = dictListWidget->selectedItems();
191         if(selected.count() > 0) {
192             emit removeDictionary(dictsHash[selected[0]]);
193             refreshDictsList();
194         }
195    }
196 }
197
198 void DictManagerWidget::settingsButtonClicked() {
199     QList<QListWidgetItem*> selected = dictListWidget->selectedItems();
200     if(selected.count() > 0) {
201         dictsHash[selected[0]]->dictDialog()->changeSettings(this);
202         refreshDictsList();
203     }
204 }
205
206
207 void DictManagerWidget::changed() {
208     _changed=true;
209 }
210
211
212 #ifndef Q_WS_MAEMO_5
213     void DictManagerWidget::save() {
214         _save = true;
215         hide();
216     }
217 #endif