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