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 Implements dictionaries management widget
25 \author Mateusz Półrola <mateusz.polrola@comarch.pl>
28 #include "DictManagerWidget.h"
29 #include "DictTypeSelectDialog.h"
31 #include "../../include/DictDialog.h"
32 #include "MenuWidget.h"
34 DictManagerWidget::DictManagerWidget(GUIInterface *parent) :
36 setWindowTitle(tr("Dictionaries"));
37 this->guiInterface = parent;
44 void DictManagerWidget::initalizeUI() {
45 verticalLayout = new QVBoxLayout;
46 setLayout(verticalLayout);
48 dictList = new QListWidget;
49 verticalLayout->addWidget(dictList);
51 dictList->setSelectionMode(QAbstractItemView::SingleSelection);
52 dictList->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
54 addNewDictButton = new QPushButton(tr("Add"));
55 removeDictButton = new QPushButton(tr("Remove"));
56 settingsButton = new QPushButton(tr("Settings"));
58 removeDictButton->setEnabled(false);
59 settingsButton->setEnabled(false);
61 buttonGroup = new QHBoxLayout;
63 buttonGroup->addWidget(addNewDictButton);
64 buttonGroup->addWidget(removeDictButton);
65 buttonGroup->addWidget(settingsButton);
67 verticalLayout->addLayout(buttonGroup, Qt::AlignBottom);
70 connect(addNewDictButton, SIGNAL(clicked()),
71 this, SLOT(saveChanges()));
72 connect(addNewDictButton, SIGNAL(clicked()),
73 this, SLOT(addNewDictButtonClicked()));
75 connect(removeDictButton, SIGNAL(clicked()),
76 this, SLOT(saveChanges()));
77 connect(removeDictButton, SIGNAL(clicked()),
78 this, SLOT(removeButtonClicked()));
80 connect(settingsButton, SIGNAL(clicked()),
81 this, SLOT(saveChanges()));
82 connect(settingsButton, SIGNAL(clicked()),
83 this, SLOT(settingsButtonClicked()));
85 connect(dictList, SIGNAL(itemClicked(QListWidgetItem*)),
86 this, SLOT(itemSelected(QListWidgetItem*)));
88 connect(dictList, SIGNAL(itemChanged(QListWidgetItem*)),
89 this, SLOT(changed()));
92 connect(dictList, SIGNAL(itemActivated(QListWidgetItem*)),
93 this, SLOT(saveChanges()));
94 connect(dictList, SIGNAL(itemActivated(QListWidgetItem*)),
95 this, SLOT(itemSelected(QListWidgetItem*)));
96 connect(dictList, SIGNAL(itemActivated(QListWidgetItem*)),
97 settingsButton, SIGNAL(clicked()));
103 setMinimumSize(500,300);
104 closeButton = new QPushButton(tr("Save"));
105 buttonGroup->addWidget(closeButton);
107 setMinimumWidth(sizeHint().width()*1.2);
108 setMaximumWidth(sizeHint().width()*2);
109 setMinimumHeight(sizeHint().height());
110 setMaximumHeight(sizeHint().height()*2);
111 connect(closeButton, SIGNAL(clicked()), this, SLOT(save()));
116 void DictManagerWidget::refreshDictsList() {
120 removeDictButton->setEnabled(false);
121 settingsButton->setEnabled(false);
123 QHash<CommonDictInterface*, bool> dicts = guiInterface->getDictionaries();
125 QHashIterator<CommonDictInterface*, bool> i(dicts);
129 QListWidgetItem* item = new QListWidgetItem();
131 if (i.key()->type() == "stardict") {
132 name = i.key()->name() + " (" + i.key()->type() + ")";
135 name = i.key()->langFrom() + " - " + i.key()->langTo() +
136 " (" + i.key()->type() + " " +
137 i.key()->name() + ")";
140 item->setFlags(item->flags() | Qt::ItemIsUserCheckable);
142 item->setCheckState(Qt::Checked);
145 item->setCheckState(Qt::Unchecked);
147 item->setIcon(*i.key()->icon());
149 dictList->addItem(item);
150 dictsHash.insert(item, i.key());
153 dictList->sortItems();
156 void DictManagerWidget::showEvent(QShowEvent *e) {
162 QWidget::showEvent(e);
165 void DictManagerWidget::saveChanges() {
171 QMessageBox::question(this, tr("Save"),
172 tr("Do you want to save changes?"),
173 QMessageBox::Save, QMessageBox::Cancel) == QMessageBox::Save) {
175 QList<CommonDictInterface*> checkedDicts;
177 for(int i=0; i<dictList->count(); i++) {
178 QListWidgetItem* item = dictList->item(i);
179 if(item->checkState() == Qt::Checked) {
180 checkedDicts.push_back(dictsHash[item]);
184 emit selectedDictionaries(checkedDicts);
188 void DictManagerWidget::hideEvent(QHideEvent *e) {
190 QWidget::hideEvent(e);
194 void DictManagerWidget::addNewDictButtonClicked() {
196 if(!_changed || QMessageBox::question(this,
197 tr("Save"), tr("Do you want to save changes?"),
198 QMessageBox::Save, QMessageBox::Cancel) == QMessageBox::Save) {
205 CommonDictInterface* selectedPlugin =
206 DictTypeSelectDialog::addNewDict(guiInterface->getPlugins(),this);
209 selectedPlugin->dictDialog()->addNewDictionary(this->parentWidget());
212 CommonDictInterface* newDict = selectedPlugin->getNew(settings);
215 Q_EMIT addDictionary(newDict);
222 void DictManagerWidget::itemSelected(QListWidgetItem *) {
223 removeDictButton->setEnabled(true);
224 settingsButton->setEnabled(true);
225 dictList->setFocus();
228 void DictManagerWidget::removeButtonClicked() {
229 if(QMessageBox::question(this, tr("Remove dictionary"),
230 tr("Do you want to remove selected dictionary?"),
231 QMessageBox::Yes, QMessageBox::No) == QMessageBox::Yes) {
233 QList<QListWidgetItem*> selected = dictList->selectedItems();
234 if(selected.count() > 0) {
235 emit removeDictionary(dictsHash[selected[0]]);
241 void DictManagerWidget::settingsButtonClicked() {
243 if(!_changed || QMessageBox::question(this,
244 tr("Save"), tr("Do you want to save changes?"),
245 QMessageBox::Save, QMessageBox::Cancel) == QMessageBox::Save) {
251 QList<QListWidgetItem*> selected = dictList->selectedItems();
252 if(selected.count() > 0) {
253 dictsHash[selected[0]]->dictDialog()->changeSettings(this->parentWidget());
259 void DictManagerWidget::changed() {
265 void DictManagerWidget::save() {
272 void DictManagerWidget::keyPressEvent(QKeyEvent *e) {
273 if (!e->modifiers() || (e->modifiers() & Qt::KeypadModifier && e->key() == Qt::Key_Enter)) {