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 //! \file DictManagerWidget.cpp
23 //! \brief Dictionaries management widget
24 //! \author Mateusz Półrola <mateusz.polrola@comarch.pl>
26 #include "DictManagerWidget.h"
27 #include "DictTypeSelectDialog.h"
29 #include "../../common/DictDialog.h"
31 DictManagerWidget::DictManagerWidget(GUIInterface *parent) :
33 setWindowTitle(tr("Dictionaries"));
34 this->guiInterface = parent;
39 void DictManagerWidget::initalizeUI() {
40 verticalLayout = new QVBoxLayout;
41 setLayout(verticalLayout);
43 dictListWidget = new QListWidget;
44 verticalLayout->addWidget(dictListWidget);
46 dictListWidget->setSelectionMode(QAbstractItemView::SingleSelection);
47 dictListWidget->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
49 addNewDictButton = new QPushButton(tr("Add"));
50 removeDictButton = new QPushButton(tr("Remove"));
51 settingsButton = new QPushButton(tr("Settings"));
53 removeDictButton->setEnabled(false);
54 settingsButton->setEnabled(false);
56 buttonGroup = new QHBoxLayout;
58 buttonGroup->addWidget(addNewDictButton);
59 buttonGroup->addWidget(removeDictButton);
60 buttonGroup->addWidget(settingsButton);
62 verticalLayout->addLayout(buttonGroup, Qt::AlignBottom);
65 connect(addNewDictButton, SIGNAL(clicked()),
66 this, SLOT(saveChanges()));
67 connect(addNewDictButton, SIGNAL(clicked()),
68 this, SLOT(addNewDictButtonClicked()));
70 connect(removeDictButton, SIGNAL(clicked()),
71 this, SLOT(saveChanges()));
72 connect(removeDictButton, SIGNAL(clicked()),
73 this, SLOT(removeButtonClicked()));
75 connect(settingsButton, SIGNAL(clicked()),
76 this, SLOT(saveChanges()));
77 connect(settingsButton, SIGNAL(clicked()),
78 this, SLOT(settingsButtonClicked()));
80 connect(dictListWidget, SIGNAL(itemClicked(QListWidgetItem*)),
81 this, SLOT(itemSelected(QListWidgetItem*)));
83 connect(dictListWidget, SIGNAL(itemChanged(QListWidgetItem*)),
84 this, SLOT(changed()));
89 setMinimumSize(500,300);
90 closeButton = new QPushButton(tr("Save"));
91 buttonGroup->addWidget(closeButton);
92 connect(closeButton, SIGNAL(clicked()), this, SLOT(save()));
97 void DictManagerWidget::refreshDictsList() {
99 dictListWidget->clear();
101 removeDictButton->setEnabled(false);
102 settingsButton->setEnabled(false);
104 QHash<CommonDictInterface*, bool> dicts = guiInterface->getDictionaries();
106 QHashIterator<CommonDictInterface*, bool> i(dicts);
110 QListWidgetItem* item = new QListWidgetItem();
111 QString name = i.key()->langFrom() + " - " + i.key()->langTo() + " (" +
112 i.key()->type() + " " + i.key()->name() + ")";
114 item->setFlags(item->flags() | Qt::ItemIsUserCheckable);
116 item->setCheckState(Qt::Checked);
119 item->setCheckState(Qt::Unchecked);
121 item->setIcon(*i.key()->icon());
123 dictListWidget->addItem(item);
124 dictsHash.insert(item, i.key());
128 void DictManagerWidget::showEvent(QShowEvent *e) {
134 QWidget::showEvent(e);
137 void DictManagerWidget::saveChanges() {
143 QMessageBox::question(this, tr("Save"),
144 tr("Do you want to save changes?"),
145 QMessageBox::Save, QMessageBox::Cancel) == QMessageBox::Save) {
147 QList<CommonDictInterface*> checkedDicts;
149 for(int i=0; i<dictListWidget->count(); i++) {
150 QListWidgetItem* item = dictListWidget->item(i);
151 if(item->checkState() == Qt::Checked) {
152 checkedDicts.push_back(dictsHash[item]);
156 emit selectedDictionaries(checkedDicts);
160 void DictManagerWidget::hideEvent(QHideEvent *e) {
162 QWidget::hideEvent(e);
166 void DictManagerWidget::addNewDictButtonClicked() {
168 if(!_changed || QMessageBox::question(this,
169 tr("Save"), tr("Do you want to save changes?"),
170 QMessageBox::Save, QMessageBox::Cancel) == QMessageBox::Save) {
177 CommonDictInterface* selectedPlugin =
178 DictTypeSelectDialog::addNewDict(guiInterface->getPlugins(),this);
179 if(selectedPlugin != NULL) {
181 selectedPlugin->dictDialog()->addNewDictionary(this);
183 if(settings != NULL) {
184 CommonDictInterface* newDict = selectedPlugin->getNew(settings);
187 Q_EMIT addDictionary(newDict);
194 void DictManagerWidget::itemSelected(QListWidgetItem *) {
195 removeDictButton->setEnabled(true);
196 settingsButton->setEnabled(true);
199 void DictManagerWidget::removeButtonClicked() {
200 if(QMessageBox::question(this, tr("Remove dictionary"),
201 tr("Do you want to remove selected dictionary?"),
202 QMessageBox::Yes, QMessageBox::No) == QMessageBox::Yes) {
204 QList<QListWidgetItem*> selected = dictListWidget->selectedItems();
205 if(selected.count() > 0) {
206 emit removeDictionary(dictsHash[selected[0]]);
212 void DictManagerWidget::settingsButtonClicked() {
214 if(!_changed || QMessageBox::question(this,
215 tr("Save"), tr("Do you want to save changes?"),
216 QMessageBox::Save, QMessageBox::Cancel) == QMessageBox::Save) {
222 QList<QListWidgetItem*> selected = dictListWidget->selectedItems();
223 if(selected.count() > 0) {
224 dictsHash[selected[0]]->dictDialog()->changeSettings(this);
230 void DictManagerWidget::changed() {
236 void DictManagerWidget::save() {