Updated comments
[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     #endif
81 }
82
83
84 void DictManagerWidget::refreshDictsList() {
85
86     dictListWidget->clear();
87     dictsHash.clear();
88     removeDictButton->setEnabled(false);
89     settingsButton->setEnabled(false);
90
91     QHash<CommonDictInterface*, bool> dicts = guiInterface->getDictionaries();
92
93     QHashIterator<CommonDictInterface*, bool> i(dicts);
94
95     while(i.hasNext()) {
96         i.next();
97         QListWidgetItem* item = new QListWidgetItem;
98         QString name = i.key()->langFrom() + " - " + i.key()->langTo() + " (" +
99                        i.key()->type() + " " + i.key()->name() + ")";
100         item->setText(name);
101         item->setFlags(item->flags() | Qt::ItemIsUserCheckable);
102         if(i.value()) {
103             item->setCheckState(Qt::Checked);
104         }
105         else {
106             item->setCheckState(Qt::Unchecked);
107         }
108         item->setIcon(*i.key()->icon());
109
110         dictListWidget->addItem(item);
111         dictsHash.insert(item, i.key());
112     }
113 }
114
115 void DictManagerWidget::showEvent(QShowEvent *e) {
116     refreshDictsList();
117     QWidget::showEvent(e);
118 }
119
120 void DictManagerWidget::hideEvent(QHideEvent *e)
121 {
122     QList<CommonDictInterface*> checkedDicts;
123
124     for(int i=0; i<dictListWidget->count(); i++) {
125         QListWidgetItem* item = dictListWidget->item(i);
126         if(item->checkState() == Qt::Checked) {
127             checkedDicts.push_back(dictsHash[item]);
128         }
129     }
130     emit selectedDictionaries(checkedDicts);
131
132     QWidget::hideEvent(e);
133 }
134
135
136 void DictManagerWidget::addNewDictButtonClicked() {
137     CommonDictInterface* selectedPlugin =
138             DictTypeSelectDialog::addNewDict(guiInterface->getPlugins(),this);
139     if(selectedPlugin != NULL) {
140         Settings* settings =
141                 selectedPlugin->dictDialog()->addNewDictionary(this);
142
143         if(settings != NULL) {
144             CommonDictInterface* newDict = selectedPlugin->getNew(settings);
145             emit addDictionary(newDict);
146             refreshDictsList();
147         }
148     }
149 }
150
151 void DictManagerWidget::itemSelected(QListWidgetItem *) {
152     removeDictButton->setEnabled(true);
153     settingsButton->setEnabled(true);
154 }
155
156 void DictManagerWidget::removeButtonClicked() {
157     QList<QListWidgetItem*> selected = dictListWidget->selectedItems();
158     if(selected.count() > 0) {
159         emit removeDictionary(dictsHash[selected[0]]);
160         refreshDictsList();
161     }
162 }
163
164 void DictManagerWidget::settingsButtonClicked() {
165     QList<QListWidgetItem*> selected = dictListWidget->selectedItems();
166     if(selected.count() > 0) {
167         dictsHash[selected[0]]->dictDialog()->changeSettings(this);
168         refreshDictsList();
169     }
170 }