1 /*******************************************************************************
3 This file is part of mDictionary.
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.
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.
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/>.
18 Copyright 2010 Comarch S.A.
20 *******************************************************************************/
22 //Created by Mateusz Półrola
24 #include "DictManagerWidget.h"
25 #include "DictTypeSelectDialog.h"
27 #include "../../includes/DictDialog.h"
29 DictManagerWidget::DictManagerWidget(Backbone* backbone, QWidget *parent) :
32 this->backbone = backbone;
35 verticalLayout = new QVBoxLayout;
36 setLayout(verticalLayout);
38 dictListWidget = new QListWidget;
39 verticalLayout->addWidget(dictListWidget);
41 dictListWidget->setSelectionMode(QAbstractItemView::SingleSelection);
42 dictListWidget->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
44 addNewDictButton = new QPushButton(tr("Add"));
45 removeDictButton = new QPushButton(tr("Remove"));
46 settingsButton = new QPushButton(tr("Settings"));
48 removeDictButton->setEnabled(false);
49 settingsButton->setEnabled(false);
51 buttonGroup = new QHBoxLayout;
53 buttonGroup->addWidget(addNewDictButton);
54 buttonGroup->addWidget(removeDictButton);
55 buttonGroup->addWidget(settingsButton);
57 verticalLayout->addLayout(buttonGroup, Qt::AlignBottom);
60 connect(addNewDictButton, SIGNAL(clicked()),
61 this, SLOT(addNewDictButtonClicked()));
63 connect(removeDictButton, SIGNAL(clicked()),
64 this, SLOT(removeButtonClicked()));
66 connect(settingsButton, SIGNAL(clicked()),
67 this, SLOT(settingsButtonClicked()));
69 connect(dictListWidget, SIGNAL(itemClicked(QListWidgetItem*)),
70 this, SLOT(itemSelected(QListWidgetItem*)));
77 void DictManagerWidget::refreshDictsList() {
79 dictListWidget->clear();
81 removeDictButton->setEnabled(false);
82 settingsButton->setEnabled(false);
84 QHash<CommonDictInterface*, bool> dicts = backbone->getDictionaries();
87 QHashIterator<CommonDictInterface*, bool> i(dicts);
91 QListWidgetItem* item = new QListWidgetItem;
92 QString name = i.key()->langFrom() + " - " + i.key()->langTo() + " (" +
93 i.key()->type() + " " + i.key()->name() + ")";
95 item->setFlags(item->flags() | Qt::ItemIsUserCheckable);
97 item->setCheckState(Qt::Checked);
100 item->setCheckState(Qt::Unchecked);
103 dictListWidget->addItem(item);
104 dictsHash.insert(item, i.key());
108 void DictManagerWidget::showEvent(QShowEvent *e) {
111 QWidget::showEvent(e);
114 void DictManagerWidget::hideEvent(QHideEvent *e)
116 QList<CommonDictInterface*> checkedDicts;
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]);
124 backbone->selectedDictionaries(checkedDicts);
126 QWidget::hideEvent(e);
130 void DictManagerWidget::addNewDictButtonClicked() {
131 CommonDictInterface* selectedPlugin =
132 DictTypeSelectDialog::addNewDict(backbone->getPlugins(),this);
133 if(selectedPlugin != NULL) {
135 selectedPlugin->dictDialog()->addNewDictionary(this);
137 if(settings != NULL) {
138 CommonDictInterface* newDict = selectedPlugin->getNew(settings);
139 backbone->addDictionary(newDict);
145 void DictManagerWidget::itemSelected(QListWidgetItem *) {
146 removeDictButton->setEnabled(true);
147 settingsButton->setEnabled(true);
150 void DictManagerWidget::removeButtonClicked() {
151 QList<QListWidgetItem*> selected = dictListWidget->selectedItems();
152 if(selected.count() > 0) {
153 backbone->removeDictionary(dictsHash[selected[0]]);
158 void DictManagerWidget::settingsButtonClicked() {
159 QList<QListWidgetItem*> selected = dictListWidget->selectedItems();
160 if(selected.count() > 0) {
161 dictsHash[selected[0]]->dictDialog()->changeSettings(this);