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