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 "../../include/DictDialog.h"
30 #include "MenuWidget.h"
32 DictManagerWidget::DictManagerWidget(GUIInterface *parent) :
34 setWindowTitle(tr("Dictionaries"));
35 this->guiInterface = parent;
40 void DictManagerWidget::initalizeUI() {
41 verticalLayout = new QVBoxLayout;
42 setLayout(verticalLayout);
44 dictList = new QListWidget;
45 verticalLayout->addWidget(dictList);
47 dictList->setSelectionMode(QAbstractItemView::SingleSelection);
48 dictList->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
50 addNewDictButton = new QPushButton(tr("Add"));
51 removeDictButton = new QPushButton(tr("Remove"));
52 settingsButton = new QPushButton(tr("Settings"));
54 removeDictButton->setEnabled(false);
55 settingsButton->setEnabled(false);
57 buttonGroup = new QHBoxLayout;
59 buttonGroup->addWidget(addNewDictButton);
60 buttonGroup->addWidget(removeDictButton);
61 buttonGroup->addWidget(settingsButton);
63 verticalLayout->addLayout(buttonGroup, Qt::AlignBottom);
66 connect(addNewDictButton, SIGNAL(clicked()),
67 this, SLOT(saveChanges()));
68 connect(addNewDictButton, SIGNAL(clicked()),
69 this, SLOT(addNewDictButtonClicked()));
71 connect(removeDictButton, SIGNAL(clicked()),
72 this, SLOT(saveChanges()));
73 connect(removeDictButton, SIGNAL(clicked()),
74 this, SLOT(removeButtonClicked()));
76 connect(settingsButton, SIGNAL(clicked()),
77 this, SLOT(saveChanges()));
78 connect(settingsButton, SIGNAL(clicked()),
79 this, SLOT(settingsButtonClicked()));
81 connect(dictList, SIGNAL(itemClicked(QListWidgetItem*)),
82 this, SLOT(itemSelected(QListWidgetItem*)));
84 connect(dictList, SIGNAL(itemChanged(QListWidgetItem*)),
85 this, SLOT(changed()));
90 setMinimumSize(500,300);
91 closeButton = new QPushButton(tr("Save"));
92 buttonGroup->addWidget(closeButton);
93 connect(closeButton, SIGNAL(clicked()), this, SLOT(save()));
98 void DictManagerWidget::refreshDictsList() {
102 removeDictButton->setEnabled(false);
103 settingsButton->setEnabled(false);
105 QHash<CommonDictInterface*, bool> dicts = guiInterface->getDictionaries();
107 QHashIterator<CommonDictInterface*, bool> i(dicts);
111 QListWidgetItem* item = new QListWidgetItem();
112 QString name = i.key()->langFrom() + " - " + i.key()->langTo() + " (" +
113 i.key()->type() + " " + i.key()->name() + ")";
115 item->setFlags(item->flags() | Qt::ItemIsUserCheckable);
117 item->setCheckState(Qt::Checked);
120 item->setCheckState(Qt::Unchecked);
122 item->setIcon(*i.key()->icon());
124 dictList->addItem(item);
125 dictsHash.insert(item, i.key());
128 dictList->sortItems();
131 void DictManagerWidget::showEvent(QShowEvent *e) {
137 QWidget::showEvent(e);
140 void DictManagerWidget::saveChanges() {
146 QMessageBox::question(this, tr("Save"),
147 tr("Do you want to save changes?"),
148 QMessageBox::Save, QMessageBox::Cancel) == QMessageBox::Save) {
150 QList<CommonDictInterface*> checkedDicts;
152 for(int i=0; i<dictList->count(); i++) {
153 QListWidgetItem* item = dictList->item(i);
154 if(item->checkState() == Qt::Checked) {
155 checkedDicts.push_back(dictsHash[item]);
159 emit selectedDictionaries(checkedDicts);
163 void DictManagerWidget::hideEvent(QHideEvent *e) {
165 QWidget::hideEvent(e);
169 void DictManagerWidget::addNewDictButtonClicked() {
171 if(!_changed || QMessageBox::question(this,
172 tr("Save"), tr("Do you want to save changes?"),
173 QMessageBox::Save, QMessageBox::Cancel) == QMessageBox::Save) {
180 CommonDictInterface* selectedPlugin =
181 DictTypeSelectDialog::addNewDict(guiInterface->getPlugins(),this);
184 selectedPlugin->dictDialog()->addNewDictionary(this->parentWidget());
187 CommonDictInterface* newDict = selectedPlugin->getNew(settings);
190 Q_EMIT addDictionary(newDict);
197 void DictManagerWidget::itemSelected(QListWidgetItem *) {
198 removeDictButton->setEnabled(true);
199 settingsButton->setEnabled(true);
202 void DictManagerWidget::removeButtonClicked() {
203 if(QMessageBox::question(this, tr("Remove dictionary"),
204 tr("Do you want to remove selected dictionary?"),
205 QMessageBox::Yes, QMessageBox::No) == QMessageBox::Yes) {
207 QList<QListWidgetItem*> selected = dictList->selectedItems();
208 if(selected.count() > 0) {
209 emit removeDictionary(dictsHash[selected[0]]);
215 void DictManagerWidget::settingsButtonClicked() {
217 if(!_changed || QMessageBox::question(this,
218 tr("Save"), tr("Do you want to save changes?"),
219 QMessageBox::Save, QMessageBox::Cancel) == QMessageBox::Save) {
225 QList<QListWidgetItem*> selected = dictList->selectedItems();
226 if(selected.count() > 0) {
227 dictsHash[selected[0]]->dictDialog()->changeSettings(this->parentWidget());
233 void DictManagerWidget::changed() {
239 void DictManagerWidget::save() {