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;
42 void DictManagerWidget::initalizeUI() {
43 verticalLayout = new QVBoxLayout;
44 setLayout(verticalLayout);
46 dictList = new QListWidget;
47 verticalLayout->addWidget(dictList);
49 dictList->setSelectionMode(QAbstractItemView::SingleSelection);
50 dictList->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
52 addNewDictButton = new QPushButton(tr("Add"));
53 removeDictButton = new QPushButton(tr("Remove"));
54 settingsButton = new QPushButton(tr("Settings"));
56 removeDictButton->setEnabled(false);
57 settingsButton->setEnabled(false);
59 buttonGroup = new QHBoxLayout;
61 buttonGroup->addWidget(addNewDictButton);
62 buttonGroup->addWidget(removeDictButton);
63 buttonGroup->addWidget(settingsButton);
65 verticalLayout->addLayout(buttonGroup, Qt::AlignBottom);
68 connect(addNewDictButton, SIGNAL(clicked()),
69 this, SLOT(saveChanges()));
70 connect(addNewDictButton, SIGNAL(clicked()),
71 this, SLOT(addNewDictButtonClicked()));
73 connect(removeDictButton, SIGNAL(clicked()),
74 this, SLOT(saveChanges()));
75 connect(removeDictButton, SIGNAL(clicked()),
76 this, SLOT(removeButtonClicked()));
78 connect(settingsButton, SIGNAL(clicked()),
79 this, SLOT(saveChanges()));
80 connect(settingsButton, SIGNAL(clicked()),
81 this, SLOT(settingsButtonClicked()));
83 connect(dictList, SIGNAL(itemClicked(QListWidgetItem*)),
84 this, SLOT(itemSelected(QListWidgetItem*)));
86 connect(dictList, SIGNAL(itemChanged(QListWidgetItem*)),
87 this, SLOT(changed()));
90 connect(dictList, SIGNAL(itemActivated(QListWidgetItem*)),
91 this, SLOT(saveChanges()));
92 connect(dictList, SIGNAL(itemActivated(QListWidgetItem*)),
93 this, SLOT(itemSelected(QListWidgetItem*)));
94 connect(dictList, SIGNAL(itemActivated(QListWidgetItem*)),
95 settingsButton, SIGNAL(clicked()));
101 setMinimumSize(500,300);
102 closeButton = new QPushButton(tr("Save"));
103 buttonGroup->addWidget(closeButton);
105 setMinimumWidth(sizeHint().width()*1.2);
106 setMaximumWidth(sizeHint().width()*2);
107 setMinimumHeight(sizeHint().height());
108 setMaximumHeight(sizeHint().height()*2);
109 connect(closeButton, SIGNAL(clicked()), this, SLOT(save()));
114 void DictManagerWidget::refreshDictsList() {
118 removeDictButton->setEnabled(false);
119 settingsButton->setEnabled(false);
121 QHash<CommonDictInterface*, bool> dicts = guiInterface->getDictionaries();
123 QHashIterator<CommonDictInterface*, bool> i(dicts);
127 QListWidgetItem* item = new QListWidgetItem();
128 QString name = i.key()->langFrom() + " - " + i.key()->langTo() + " (" +
129 i.key()->type() + " " + i.key()->name() + ")";
131 item->setFlags(item->flags() | Qt::ItemIsUserCheckable);
133 item->setCheckState(Qt::Checked);
136 item->setCheckState(Qt::Unchecked);
138 item->setIcon(*i.key()->icon());
140 dictList->addItem(item);
141 dictsHash.insert(item, i.key());
144 dictList->sortItems();
147 void DictManagerWidget::showEvent(QShowEvent *e) {
153 QWidget::showEvent(e);
156 void DictManagerWidget::saveChanges() {
162 QMessageBox::question(this, tr("Save"),
163 tr("Do you want to save changes?"),
164 QMessageBox::Save, QMessageBox::Cancel) == QMessageBox::Save) {
166 QList<CommonDictInterface*> checkedDicts;
168 for(int i=0; i<dictList->count(); i++) {
169 QListWidgetItem* item = dictList->item(i);
170 if(item->checkState() == Qt::Checked) {
171 checkedDicts.push_back(dictsHash[item]);
175 emit selectedDictionaries(checkedDicts);
179 void DictManagerWidget::hideEvent(QHideEvent *e) {
181 QWidget::hideEvent(e);
185 void DictManagerWidget::addNewDictButtonClicked() {
187 if(!_changed || QMessageBox::question(this,
188 tr("Save"), tr("Do you want to save changes?"),
189 QMessageBox::Save, QMessageBox::Cancel) == QMessageBox::Save) {
196 CommonDictInterface* selectedPlugin =
197 DictTypeSelectDialog::addNewDict(guiInterface->getPlugins(),this);
200 selectedPlugin->dictDialog()->addNewDictionary(this->parentWidget());
203 CommonDictInterface* newDict = selectedPlugin->getNew(settings);
206 Q_EMIT addDictionary(newDict);
213 void DictManagerWidget::itemSelected(QListWidgetItem *) {
214 removeDictButton->setEnabled(true);
215 settingsButton->setEnabled(true);
216 dictList->setFocus();
219 void DictManagerWidget::removeButtonClicked() {
220 if(QMessageBox::question(this, tr("Remove dictionary"),
221 tr("Do you want to remove selected dictionary?"),
222 QMessageBox::Yes, QMessageBox::No) == QMessageBox::Yes) {
224 QList<QListWidgetItem*> selected = dictList->selectedItems();
225 if(selected.count() > 0) {
226 emit removeDictionary(dictsHash[selected[0]]);
232 void DictManagerWidget::settingsButtonClicked() {
234 if(!_changed || QMessageBox::question(this,
235 tr("Save"), tr("Do you want to save changes?"),
236 QMessageBox::Save, QMessageBox::Cancel) == QMessageBox::Save) {
242 QList<QListWidgetItem*> selected = dictList->selectedItems();
243 if(selected.count() > 0) {
244 dictsHash[selected[0]]->dictDialog()->changeSettings(this->parentWidget());
250 void DictManagerWidget::changed() {
256 void DictManagerWidget::save() {
263 void DictManagerWidget::keyPressEvent(QKeyEvent *e) {
264 if (!e->modifiers() || (e->modifiers() & Qt::KeypadModifier && e->key() == Qt::Key_Enter)) {