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;
47 void DictManagerWidget::initalizeUI() {
48 verticalLayout = new QVBoxLayout;
49 setLayout(verticalLayout);
52 qmlView = new QDeclarativeView(this);
53 qmlView->setSource(QUrl::fromLocalFile("/usr/share/mdictionary/qml/DictManagerWidget.qml"));
54 ctxt = qmlView->rootContext();
57 ctxt->setContextProperty("dictModel", &(*model));
59 QGraphicsObject *rootObject = qmlView->rootObject();
60 //connect(rootObject, SIGNAL(selectedRow(int)),
61 // this, SLOT(pluginSelected(int)));
63 qmlView->setResizeMode(QDeclarativeView::SizeRootObjectToView);
64 verticalLayout->addWidget(qmlView);
70 dictList = new QListWidget;
71 verticalLayout->addWidget(dictList);
73 dictList->setSelectionMode(QAbstractItemView::SingleSelection);
74 dictList->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
76 addNewDictButton = new QPushButton(tr("Add"));
77 removeDictButton = new QPushButton(tr("Remove"));
78 settingsButton = new QPushButton(tr("Settings"));
80 removeDictButton->setEnabled(false);
81 settingsButton->setEnabled(false);
83 buttonGroup = new QHBoxLayout;
85 buttonGroup->addWidget(addNewDictButton);
86 buttonGroup->addWidget(removeDictButton);
87 buttonGroup->addWidget(settingsButton);
89 verticalLayout->addLayout(buttonGroup, Qt::AlignBottom);
92 connect(addNewDictButton, SIGNAL(clicked()),
93 this, SLOT(saveChanges()));
94 connect(addNewDictButton, SIGNAL(clicked()),
95 this, SLOT(addNewDictButtonClicked()));
97 connect(removeDictButton, SIGNAL(clicked()),
98 this, SLOT(saveChanges()));
99 connect(removeDictButton, SIGNAL(clicked()),
100 this, SLOT(removeButtonClicked()));
102 connect(settingsButton, SIGNAL(clicked()),
103 this, SLOT(saveChanges()));
104 connect(settingsButton, SIGNAL(clicked()),
105 this, SLOT(settingsButtonClicked()));
107 connect(dictList, SIGNAL(itemClicked(QListWidgetItem*)),
108 this, SLOT(itemSelected(QListWidgetItem*)));
110 connect(dictList, SIGNAL(itemChanged(QListWidgetItem*)),
111 this, SLOT(changed()));
115 //pozmieniać connecty
116 // connect(dictList, SIGNAL(itemActivated(QListWidgetItem*)),
117 // this, SLOT(saveChanges()));
118 // connect(dictList, SIGNAL(itemActivated(QListWidgetItem*)),
119 // this, SLOT(itemSelected(QListWidgetItem*)));
120 // connect(dictList, SIGNAL(itemActivated(QListWidgetItem*)),
121 // settingsButton, SIGNAL(clicked()));
129 setMinimumSize(500,300);
130 //closeButton = new QPushButton(tr("Save"));
131 //buttonGroup->addWidget(closeButton);
133 // setMinimumWidth(sizeHint().width()*1.2);
134 // setMaximumWidth(sizeHint().width()*2);
135 // setMinimumHeight(sizeHint().height());
136 // setMaximumHeight(sizeHint().height()*2);
137 //connect(closeButton, SIGNAL(clicked()), this, SLOT(save()));
142 void DictManagerWidget::refreshDictsList() {
145 QHash<CommonDictInterface*, bool> dicts = guiInterface->getDictionaries();
149 model = new DictManagerModel(dicts, this);
153 model->setDictionaries(dicts);
161 removeDictButton->setEnabled(false);
162 settingsButton->setEnabled(false);
164 QHash<CommonDictInterface*, bool> dicts = guiInterface->getDictionaries();
166 QHashIterator<CommonDictInterface*, bool> i(dicts);
171 QListWidgetItem* item = new QListWidgetItem();
173 if (i.key()->type() == "stardict") {
174 name = i.key()->name() + " (" + i.key()->type() + ")";
177 name = i.key()->langFrom() + " - " + i.key()->langTo() +
178 " (" + i.key()->type() + " " +
179 i.key()->name() + ")";
182 item->setFlags(item->flags() | Qt::ItemIsUserCheckable);
184 item->setCheckState(Qt::Checked);
187 item->setCheckState(Qt::Unchecked);
189 item->setIcon(*i.key()->icon());
191 dictList->addItem(item);
192 dictsHash.insert(item, i.key());
195 dictList->sortItems();
199 void DictManagerWidget::showEvent(QShowEvent *e) {
205 QWidget::showEvent(e);
208 void DictManagerWidget::saveChanges() {
214 QMessageBox::question(this, tr("Save"),
215 tr("Do you want to save changes?"),
216 QMessageBox::Save, QMessageBox::Cancel) == QMessageBox::Save) {
218 QList<CommonDictInterface*> checkedDicts;
220 for(int i=0; i<dictList->count(); i++) {
221 QListWidgetItem* item = dictList->item(i);
222 if(item->checkState() == Qt::Checked) {
223 checkedDicts.push_back(dictsHash[item]);
227 emit selectedDictionaries(checkedDicts);
231 void DictManagerWidget::hideEvent(QHideEvent *e) {
233 QWidget::hideEvent(e);
237 void DictManagerWidget::addNewDictButtonClicked() {
239 if(!_changed || QMessageBox::question(this,
240 tr("Save"), tr("Do you want to save changes?"),
241 QMessageBox::Save, QMessageBox::Cancel) == QMessageBox::Save) {
248 CommonDictInterface* selectedPlugin =
249 DictTypeSelectDialog::addNewDict(guiInterface->getPlugins(),this);
252 selectedPlugin->dictDialog()->addNewDictionary(this->parentWidget());
255 CommonDictInterface* newDict = selectedPlugin->getNew(settings);
258 Q_EMIT addDictionary(newDict);
265 void DictManagerWidget::itemSelected(QListWidgetItem *) {
266 removeDictButton->setEnabled(true);
267 settingsButton->setEnabled(true);
268 dictList->setFocus();
271 void DictManagerWidget::removeButtonClicked() {
272 if(QMessageBox::question(this, tr("Remove dictionary"),
273 tr("Do you want to remove selected dictionary?"),
274 QMessageBox::Yes, QMessageBox::No) == QMessageBox::Yes) {
276 QList<QListWidgetItem*> selected = dictList->selectedItems();
277 if(selected.count() > 0) {
278 emit removeDictionary(dictsHash[selected[0]]);
284 void DictManagerWidget::settingsButtonClicked() {
286 if(!_changed || QMessageBox::question(this,
287 tr("Save"), tr("Do you want to save changes?"),
288 QMessageBox::Save, QMessageBox::Cancel) == QMessageBox::Save) {
294 QList<QListWidgetItem*> selected = dictList->selectedItems();
295 if(selected.count() > 0) {
296 dictsHash[selected[0]]->dictDialog()->changeSettings(this->parentWidget());
302 void DictManagerWidget::changed() {
308 void DictManagerWidget::save() {
315 void DictManagerWidget::keyPressEvent(QKeyEvent *e) {
316 if (!e->modifiers() || (e->modifiers() & Qt::KeypadModifier && e->key() == Qt::Key_Enter)) {