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 dictListWidget = new QListWidget;
45 verticalLayout->addWidget(dictListWidget);
47 dictListWidget->setSelectionMode(QAbstractItemView::SingleSelection);
48 dictListWidget->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(dictListWidget, SIGNAL(itemClicked(QListWidgetItem*)),
82 this, SLOT(itemSelected(QListWidgetItem*)));
84 connect(dictListWidget, 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() {
100 dictListWidget->clear();
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 dictListWidget->addItem(item);
125 dictsHash.insert(item, i.key());
129 void DictManagerWidget::showEvent(QShowEvent *e) {
135 QWidget::showEvent(e);
138 void DictManagerWidget::saveChanges() {
144 QMessageBox::question(this, tr("Save"),
145 tr("Do you want to save changes?"),
146 QMessageBox::Save, QMessageBox::Cancel) == QMessageBox::Save) {
148 QList<CommonDictInterface*> checkedDicts;
150 for(int i=0; i<dictListWidget->count(); i++) {
151 QListWidgetItem* item = dictListWidget->item(i);
152 if(item->checkState() == Qt::Checked) {
153 checkedDicts.push_back(dictsHash[item]);
157 emit selectedDictionaries(checkedDicts);
161 void DictManagerWidget::hideEvent(QHideEvent *e) {
163 QWidget::hideEvent(e);
167 void DictManagerWidget::addNewDictButtonClicked() {
169 if(!_changed || QMessageBox::question(this,
170 tr("Save"), tr("Do you want to save changes?"),
171 QMessageBox::Save, QMessageBox::Cancel) == QMessageBox::Save) {
178 CommonDictInterface* selectedPlugin =
179 DictTypeSelectDialog::addNewDict(
180 guiInterface->getPlugins(),
181 menuWidget->applicationMenu());
183 if(selectedPlugin != NULL) {
185 selectedPlugin->dictDialog()->addNewDictionary(menuWidget->applicationMenu());
187 if(settings != NULL) {
188 CommonDictInterface* newDict = selectedPlugin->getNew(settings);
191 Q_EMIT addDictionary(newDict);
198 void DictManagerWidget::itemSelected(QListWidgetItem *) {
199 removeDictButton->setEnabled(true);
200 settingsButton->setEnabled(true);
203 void DictManagerWidget::removeButtonClicked() {
204 if(QMessageBox::question(this, tr("Remove dictionary"),
205 tr("Do you want to remove selected dictionary?"),
206 QMessageBox::Yes, QMessageBox::No) == QMessageBox::Yes) {
208 QList<QListWidgetItem*> selected = dictListWidget->selectedItems();
209 if(selected.count() > 0) {
210 emit removeDictionary(dictsHash[selected[0]]);
216 void DictManagerWidget::settingsButtonClicked() {
218 if(!_changed || QMessageBox::question(this,
219 tr("Save"), tr("Do you want to save changes?"),
220 QMessageBox::Save, QMessageBox::Cancel) == QMessageBox::Save) {
226 QList<QListWidgetItem*> selected = dictListWidget->selectedItems();
227 if(selected.count() > 0) {
228 dictsHash[selected[0]]->dictDialog()->changeSettings(menuWidget->applicationMenu());
234 void DictManagerWidget::changed() {
240 void DictManagerWidget::save() {