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 //model = new DictManagerModel(, this);
58 ctxt->setContextProperty("dictModel", &(*model));
60 QGraphicsObject *rootObject = qmlView->rootObject();
61 //connect(rootObject, SIGNAL(selectedRow(int)),
62 // this, SLOT(pluginSelected(int)));
64 qmlView->setResizeMode(QDeclarativeView::SizeRootObjectToView);
65 verticalLayout->addWidget(qmlView);
71 dictList = new QListWidget;
72 verticalLayout->addWidget(dictList);
74 dictList->setSelectionMode(QAbstractItemView::SingleSelection);
75 dictList->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
77 addNewDictButton = new QPushButton(tr("Add"));
78 removeDictButton = new QPushButton(tr("Remove"));
79 settingsButton = new QPushButton(tr("Settings"));
81 removeDictButton->setEnabled(false);
82 settingsButton->setEnabled(false);
84 buttonGroup = new QHBoxLayout;
86 buttonGroup->addWidget(addNewDictButton);
87 buttonGroup->addWidget(removeDictButton);
88 buttonGroup->addWidget(settingsButton);
90 verticalLayout->addLayout(buttonGroup, Qt::AlignBottom);
93 connect(addNewDictButton, SIGNAL(clicked()),
94 this, SLOT(saveChanges()));
95 connect(addNewDictButton, SIGNAL(clicked()),
96 this, SLOT(addNewDictButtonClicked()));
98 connect(removeDictButton, SIGNAL(clicked()),
99 this, SLOT(saveChanges()));
100 connect(removeDictButton, SIGNAL(clicked()),
101 this, SLOT(removeButtonClicked()));
103 connect(settingsButton, SIGNAL(clicked()),
104 this, SLOT(saveChanges()));
105 connect(settingsButton, SIGNAL(clicked()),
106 this, SLOT(settingsButtonClicked()));
108 connect(dictList, SIGNAL(itemClicked(QListWidgetItem*)),
109 this, SLOT(itemSelected(QListWidgetItem*)));
111 connect(dictList, SIGNAL(itemChanged(QListWidgetItem*)),
112 this, SLOT(changed()));
116 //pozmieniać connecty
117 // connect(dictList, SIGNAL(itemActivated(QListWidgetItem*)),
118 // this, SLOT(saveChanges()));
119 // connect(dictList, SIGNAL(itemActivated(QListWidgetItem*)),
120 // this, SLOT(itemSelected(QListWidgetItem*)));
121 // connect(dictList, SIGNAL(itemActivated(QListWidgetItem*)),
122 // settingsButton, SIGNAL(clicked()));
130 setMinimumSize(500,300);
131 //closeButton = new QPushButton(tr("Save"));
132 //buttonGroup->addWidget(closeButton);
134 setMinimumWidth(sizeHint().width()*1.2);
135 setMaximumWidth(sizeHint().width()*2);
136 setMinimumHeight(sizeHint().height());
137 setMaximumHeight(sizeHint().height()*2);
138 //connect(closeButton, SIGNAL(clicked()), this, SLOT(save()));
143 void DictManagerWidget::refreshDictsList() {
146 QHash<CommonDictInterface*, bool> dicts = guiInterface->getDictionaries();
149 model = new DictManagerModel(dicts, this);
152 model->setDictionaries(dicts);
160 removeDictButton->setEnabled(false);
161 settingsButton->setEnabled(false);
163 QHash<CommonDictInterface*, bool> dicts = guiInterface->getDictionaries();
165 QHashIterator<CommonDictInterface*, bool> i(dicts);
170 QListWidgetItem* item = new QListWidgetItem();
172 if (i.key()->type() == "stardict") {
173 name = i.key()->name() + " (" + i.key()->type() + ")";
176 name = i.key()->langFrom() + " - " + i.key()->langTo() +
177 " (" + i.key()->type() + " " +
178 i.key()->name() + ")";
181 item->setFlags(item->flags() | Qt::ItemIsUserCheckable);
183 item->setCheckState(Qt::Checked);
186 item->setCheckState(Qt::Unchecked);
188 item->setIcon(*i.key()->icon());
190 dictList->addItem(item);
191 dictsHash.insert(item, i.key());
194 dictList->sortItems();
198 void DictManagerWidget::showEvent(QShowEvent *e) {
204 QWidget::showEvent(e);
207 void DictManagerWidget::saveChanges() {
213 QMessageBox::question(this, tr("Save"),
214 tr("Do you want to save changes?"),
215 QMessageBox::Save, QMessageBox::Cancel) == QMessageBox::Save) {
217 QList<CommonDictInterface*> checkedDicts;
219 for(int i=0; i<dictList->count(); i++) {
220 QListWidgetItem* item = dictList->item(i);
221 if(item->checkState() == Qt::Checked) {
222 checkedDicts.push_back(dictsHash[item]);
226 emit selectedDictionaries(checkedDicts);
230 void DictManagerWidget::hideEvent(QHideEvent *e) {
232 QWidget::hideEvent(e);
236 void DictManagerWidget::addNewDictButtonClicked() {
238 if(!_changed || QMessageBox::question(this,
239 tr("Save"), tr("Do you want to save changes?"),
240 QMessageBox::Save, QMessageBox::Cancel) == QMessageBox::Save) {
247 CommonDictInterface* selectedPlugin =
248 DictTypeSelectDialog::addNewDict(guiInterface->getPlugins(),this);
251 selectedPlugin->dictDialog()->addNewDictionary(this->parentWidget());
254 CommonDictInterface* newDict = selectedPlugin->getNew(settings);
257 Q_EMIT addDictionary(newDict);
264 void DictManagerWidget::itemSelected(QListWidgetItem *) {
265 removeDictButton->setEnabled(true);
266 settingsButton->setEnabled(true);
267 dictList->setFocus();
270 void DictManagerWidget::removeButtonClicked() {
271 if(QMessageBox::question(this, tr("Remove dictionary"),
272 tr("Do you want to remove selected dictionary?"),
273 QMessageBox::Yes, QMessageBox::No) == QMessageBox::Yes) {
275 QList<QListWidgetItem*> selected = dictList->selectedItems();
276 if(selected.count() > 0) {
277 emit removeDictionary(dictsHash[selected[0]]);
283 void DictManagerWidget::settingsButtonClicked() {
285 if(!_changed || QMessageBox::question(this,
286 tr("Save"), tr("Do you want to save changes?"),
287 QMessageBox::Save, QMessageBox::Cancel) == QMessageBox::Save) {
293 QList<QListWidgetItem*> selected = dictList->selectedItems();
294 if(selected.count() > 0) {
295 dictsHash[selected[0]]->dictDialog()->changeSettings(this->parentWidget());
301 void DictManagerWidget::changed() {
307 void DictManagerWidget::save() {
314 void DictManagerWidget::keyPressEvent(QKeyEvent *e) {
315 if (!e->modifiers() || (e->modifiers() & Qt::KeypadModifier && e->key() == Qt::Key_Enter)) {