457529d7e98c762d77691f7391355057fd45e39a
[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 //Created by Mateusz Półrola
23
24 #include "DictManagerWidget.h"
25 #include "DictTypeSelectDialog.h"
26 #include <QDebug>
27 #include "../../includes/DictDialog.h"
28
29 DictManagerWidget::DictManagerWidget(GUIInterface *parent) :
30     QWidget(parent) {
31
32     this->guiInterface = parent;
33
34
35     verticalLayout = new QVBoxLayout;
36     setLayout(verticalLayout);
37
38     dictListWidget = new QListWidget;
39     verticalLayout->addWidget(dictListWidget);
40
41     dictListWidget->setSelectionMode(QAbstractItemView::SingleSelection);
42     dictListWidget->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
43
44     addNewDictButton = new QPushButton(tr("Add"));
45     removeDictButton = new QPushButton(tr("Remove"));
46     settingsButton = new QPushButton(tr("Settings"));
47
48     removeDictButton->setEnabled(false);
49     settingsButton->setEnabled(false);
50
51     buttonGroup = new QHBoxLayout;
52
53     buttonGroup->addWidget(addNewDictButton);
54     buttonGroup->addWidget(removeDictButton);
55     buttonGroup->addWidget(settingsButton);
56
57     verticalLayout->addLayout(buttonGroup, Qt::AlignBottom);
58
59
60     connect(addNewDictButton, SIGNAL(clicked()),
61             this, SLOT(addNewDictButtonClicked()));
62
63     connect(removeDictButton, SIGNAL(clicked()),
64             this, SLOT(removeButtonClicked()));
65
66     connect(settingsButton, SIGNAL(clicked()),
67             this, SLOT(settingsButtonClicked()));
68
69     connect(dictListWidget, SIGNAL(itemClicked(QListWidgetItem*)),
70             this, SLOT(itemSelected(QListWidgetItem*)));
71
72     refreshDictsList();
73
74 }
75
76
77 void DictManagerWidget::refreshDictsList() {
78
79     dictListWidget->clear();
80     dictsHash.clear();
81     removeDictButton->setEnabled(false);
82     settingsButton->setEnabled(false);
83
84     QHash<CommonDictInterface*, bool> dicts = guiInterface->getDictionaries();
85
86     QHashIterator<CommonDictInterface*, bool> i(dicts);
87
88     while(i.hasNext()) {
89         i.next();
90         QListWidgetItem* item = new QListWidgetItem;
91         QString name = i.key()->langFrom() + " - " + i.key()->langTo() + " (" +
92                        i.key()->type() + " " + i.key()->name() + ")";
93         item->setText(name);
94         item->setFlags(item->flags() | Qt::ItemIsUserCheckable);
95         if(i.value()) {
96             item->setCheckState(Qt::Checked);
97         }
98         else {
99             item->setCheckState(Qt::Unchecked);
100         }
101         item->setIcon(*i.key()->icon());
102
103         dictListWidget->addItem(item);
104         dictsHash.insert(item, i.key());
105     }
106 }
107
108 void DictManagerWidget::showEvent(QShowEvent *e) {
109     refreshDictsList();
110     QWidget::showEvent(e);
111 }
112
113 void DictManagerWidget::hideEvent(QHideEvent *e)
114 {
115     QList<CommonDictInterface*> checkedDicts;
116
117     for(int i=0; i<dictListWidget->count(); i++) {
118         QListWidgetItem* item = dictListWidget->item(i);
119         if(item->checkState() == Qt::Checked) {
120             checkedDicts.push_back(dictsHash[item]);
121         }
122     }
123     emit selectedDictionaries(checkedDicts);
124
125     QWidget::hideEvent(e);
126 }
127
128
129 void DictManagerWidget::addNewDictButtonClicked() {
130     CommonDictInterface* selectedPlugin =
131             DictTypeSelectDialog::addNewDict(guiInterface->getPlugins(),this);
132     if(selectedPlugin != NULL) {
133         Settings* settings =
134                 selectedPlugin->dictDialog()->addNewDictionary(this);
135
136         if(settings != NULL) {
137             CommonDictInterface* newDict = selectedPlugin->getNew(settings);
138             emit addDictionary(newDict);
139             refreshDictsList();
140         }
141     }
142 }
143
144 void DictManagerWidget::itemSelected(QListWidgetItem *) {
145     removeDictButton->setEnabled(true);
146     settingsButton->setEnabled(true);
147 }
148
149 void DictManagerWidget::removeButtonClicked() {
150     QList<QListWidgetItem*> selected = dictListWidget->selectedItems();
151     if(selected.count() > 0) {
152         emit removeDictionary(dictsHash[selected[0]]);
153         refreshDictsList();
154     }
155 }
156
157 void DictManagerWidget::settingsButtonClicked() {
158     QList<QListWidgetItem*> selected = dictListWidget->selectedItems();
159     if(selected.count() > 0) {
160         dictsHash[selected[0]]->dictDialog()->changeSettings(this);
161         refreshDictsList();
162     }
163 }