19133b570ccaf93a970d2057a13aadf00b57f53b
[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(addNewDictButtonClicked()));
66
67     connect(removeDictButton, SIGNAL(clicked()),
68             this, SLOT(removeButtonClicked()));
69
70     connect(settingsButton, SIGNAL(clicked()),
71             this, SLOT(settingsButtonClicked()));
72
73     connect(dictListWidget, SIGNAL(itemClicked(QListWidgetItem*)),
74             this, SLOT(itemSelected(QListWidgetItem*)));
75
76     connect(dictListWidget, SIGNAL(itemActivated(QListWidgetItem*)),
77             this, SLOT(changed()));
78
79     refreshDictsList();
80
81     #ifndef Q_WS_MAEMO_5
82         setMinimumSize(500,300);
83         closeButton = new QPushButton(tr("Save"));
84         buttonGroup->addWidget(closeButton);
85         connect(closeButton, SIGNAL(clicked()), this, SLOT(save()));
86     #endif
87 }
88
89
90 void DictManagerWidget::refreshDictsList() {
91
92     dictListWidget->clear();
93     dictsHash.clear();
94     removeDictButton->setEnabled(false);
95     settingsButton->setEnabled(false);
96
97     QHash<CommonDictInterface*, bool> dicts = guiInterface->getDictionaries();
98
99     QHashIterator<CommonDictInterface*, bool> i(dicts);
100
101     while(i.hasNext()) {
102         i.next();
103         QListWidgetItem* item = new QListWidgetItem;
104         QString name = i.key()->langFrom() + " - " + i.key()->langTo() + " (" +
105                        i.key()->type() + " " + i.key()->name() + ")";
106         item->setText(name);
107         item->setFlags(item->flags() | Qt::ItemIsUserCheckable);
108         if(i.value()) {
109             item->setCheckState(Qt::Checked);
110         }
111         else {
112             item->setCheckState(Qt::Unchecked);
113         }
114         item->setIcon(*i.key()->icon());
115
116         dictListWidget->addItem(item);
117         dictsHash.insert(item, i.key());
118     }
119 }
120
121 void DictManagerWidget::showEvent(QShowEvent *e) {
122     _changed = false;
123     refreshDictsList();
124     QWidget::showEvent(e);
125 }
126
127 void DictManagerWidget::hideEvent(QHideEvent *e)
128 {
129     #ifndef Q_WS_MAEMO_5
130     if(_save) {
131     #else
132     if(_changed &&
133             QMessageBox::question(this, "Save", "Do you want to save changes?",
134             QMessageBox::Save, QMessageBox::Cancel) == QMessageBox::Save) {
135     #endif
136         QList<CommonDictInterface*> checkedDicts;
137
138         for(int i=0; i<dictListWidget->count(); i++) {
139             QListWidgetItem* item = dictListWidget->item(i);
140             if(item->checkState() == Qt::Checked) {
141                 checkedDicts.push_back(dictsHash[item]);
142             }
143         }
144         emit selectedDictionaries(checkedDicts);
145     }
146
147     _changed = false;
148     QWidget::hideEvent(e);
149 }
150
151
152 void DictManagerWidget::addNewDictButtonClicked() {
153     CommonDictInterface* selectedPlugin =
154             DictTypeSelectDialog::addNewDict(guiInterface->getPlugins(),this);
155     if(selectedPlugin != NULL) {
156         Settings* settings =
157                 selectedPlugin->dictDialog()->addNewDictionary(this);
158
159         if(settings != NULL) {
160             CommonDictInterface* newDict = selectedPlugin->getNew(settings);
161             emit addDictionary(newDict);
162             refreshDictsList();
163         }
164     }
165 }
166
167 void DictManagerWidget::itemSelected(QListWidgetItem *) {
168     removeDictButton->setEnabled(true);
169     settingsButton->setEnabled(true);
170 }
171
172 void DictManagerWidget::removeButtonClicked() {
173     QList<QListWidgetItem*> selected = dictListWidget->selectedItems();
174     if(selected.count() > 0) {
175         emit removeDictionary(dictsHash[selected[0]]);
176         refreshDictsList();
177     }
178 }
179
180 void DictManagerWidget::settingsButtonClicked() {
181     QList<QListWidgetItem*> selected = dictListWidget->selectedItems();
182     if(selected.count() > 0) {
183         dictsHash[selected[0]]->dictDialog()->changeSettings(this);
184         refreshDictsList();
185     }
186 }
187
188
189 void DictManagerWidget::changed() {
190     _changed=true;
191 }
192
193
194 #ifndef Q_WS_MAEMO_5
195     void DictManagerWidget::save() {
196         _save = true;
197         hide();
198     }
199 #endif