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