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);
43 addNewDictButton = new QPushButton(tr("Add"));
44 removeDictButton = new QPushButton(tr("Remove"));
45 settingsButton = new QPushButton(tr("Settings"));
47 removeDictButton->setEnabled(false);
48 settingsButton->setEnabled(false);
50 buttonGroup = new QHBoxLayout;
52 buttonGroup->addWidget(addNewDictButton);
53 buttonGroup->addWidget(removeDictButton);
54 buttonGroup->addWidget(settingsButton);
56 verticalLayout->addLayout(buttonGroup, Qt::AlignBottom);
59 connect(addNewDictButton, SIGNAL(clicked()),
60 this, SLOT(addNewDictButtonClicked()));
62 connect(removeDictButton, SIGNAL(clicked()),
63 this, SLOT(removeButtonClicked()));
65 connect(settingsButton, SIGNAL(clicked()),
66 this, SLOT(settingsButtonClicked()));
68 connect(dictListWidget, SIGNAL(itemClicked(QListWidgetItem*)),
69 this, SLOT(itemSelected(QListWidgetItem*)));
76 void DictManagerWidget::refreshDictsList() {
78 dictListWidget->clear();
82 QHash<CommonDictInterface*, bool> dicts = backbone->getDictionaries();
85 QHashIterator<CommonDictInterface*, bool> i(dicts);
90 QListWidgetItem* item = new QListWidgetItem;
91 QString name = i.key()->langFrom() + " - " + i.key()->langTo() + " (" +
92 i.key()->type() + " " + i.key()->name() + ")";
94 item->setFlags(item->flags() | Qt::ItemIsUserCheckable);
96 item->setCheckState(Qt::Checked);
99 item->setCheckState(Qt::Unchecked);
102 dictListWidget->addItem(item);
103 dictsHash.insert(item, i.key());
107 void DictManagerWidget::showEvent(QShowEvent *e) {
110 removeDictButton->setEnabled(false);
111 settingsButton->setEnabled(false);
112 QWidget::showEvent(e);
115 void DictManagerWidget::hideEvent(QHideEvent *e)
117 QList<CommonDictInterface*> checkedDicts;
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]);
125 backbone->selectedDictionaries(checkedDicts);
129 QWidget::hideEvent(e);
133 void DictManagerWidget::addNewDictButtonClicked() {
134 CommonDictInterface* selectedPlugin =
135 DictTypeSelectDialog::addNewDict(backbone->getPlugins(),this);
136 if(selectedPlugin != NULL) {
137 qDebug()<< selectedPlugin->type();
139 selectedPlugin->dictDialog()->addNewDictionary(this);
141 if(settings != NULL) {
142 CommonDictInterface* newDict = selectedPlugin->getNew(settings);
143 backbone->addDictionary(newDict);
149 void DictManagerWidget::itemSelected(QListWidgetItem *item) {
150 removeDictButton->setEnabled(true);
151 settingsButton->setEnabled(true);
154 void DictManagerWidget::removeButtonClicked() {
155 QList<QListWidgetItem*> selected = dictListWidget->selectedItems();
156 if(selected.count() > 0) {
157 backbone->removeDictionary(dictsHash[selected[0]]);
162 void DictManagerWidget::settingsButtonClicked() {
163 QList<QListWidgetItem*> selected = dictListWidget->selectedItems();
164 if(selected.count() > 0) {
165 dictsHash[selected[0]]->dictDialog()->changeSettings(this);