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);
54 ctxt = qmlView->rootContext();
57 ctxt->setContextProperty("dictModel", &(*model));
58 qmlView->setSource(QUrl::fromLocalFile("/usr/share/mdictionary/qml/DictManagerWidget.qml"));
60 QGraphicsObject *rootObject = qmlView->rootObject();
62 qmlView->setResizeMode(QDeclarativeView::SizeRootObjectToView);
63 verticalLayout->addWidget(qmlView);
68 dictList = new QListWidget;
69 verticalLayout->addWidget(dictList);
71 dictList->setSelectionMode(QAbstractItemView::SingleSelection);
72 dictList->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
74 addNewDictButton = new QPushButton(tr("Add"));
75 removeDictButton = new QPushButton(tr("Remove"));
76 settingsButton = new QPushButton(tr("Settings"));
78 removeDictButton->setEnabled(false);
79 settingsButton->setEnabled(false);
81 buttonGroup = new QHBoxLayout;
83 buttonGroup->addWidget(addNewDictButton);
84 buttonGroup->addWidget(removeDictButton);
85 buttonGroup->addWidget(settingsButton);
87 verticalLayout->addLayout(buttonGroup, Qt::AlignBottom);
90 connect(addNewDictButton, SIGNAL(clicked()),
91 this, SLOT(saveChanges()));
92 connect(addNewDictButton, SIGNAL(clicked()),
93 this, SLOT(addNewDictButtonClicked()));
95 connect(removeDictButton, SIGNAL(clicked()),
96 this, SLOT(saveChanges()));
97 connect(removeDictButton, SIGNAL(clicked()),
98 this, SLOT(removeButtonClicked()));
100 connect(settingsButton, SIGNAL(clicked()),
101 this, SLOT(saveChanges()));
102 connect(settingsButton, SIGNAL(clicked()),
103 this, SLOT(settingsButtonClicked()));
105 connect(dictList, SIGNAL(itemClicked(QListWidgetItem*)),
106 this, SLOT(itemSelected(QListWidgetItem*)));
108 connect(dictList, SIGNAL(itemChanged(QListWidgetItem*)),
109 this, SLOT(changed()));
113 connect(rootObject, SIGNAL(addButtonClicked()),
114 this, SLOT(saveChanges()));
115 connect(rootObject, SIGNAL(addButtonClicked()),
116 this, SLOT(addNewDictButtonClicked()));
118 connect(rootObject, SIGNAL(removeButtonClicked()),
119 this, SLOT(saveChanges()));
120 connect(rootObject, SIGNAL(removeButtonClicked()),
121 this, SLOT(removeButtonClicked()));
123 connect(rootObject, SIGNAL(settingsButtonClicked()),
124 this, SLOT(saveChanges()));
125 connect(rootObject, SIGNAL(settingsButtonClicked()),
126 this, SLOT(settingsButtonClicked()));
129 connect(model, SIGNAL(itemChanged()),
130 this, SLOT(changed()));
131 connect(rootObject, SIGNAL(itemActivated(int)),
132 this, SLOT(saveChanges()));
133 connect(rootObject, SIGNAL(itemActivated(int)),
134 this, SLOT(settingsButtonClicked()));
136 connect(this, SIGNAL(setFocusOnQML()), rootObject, SLOT(setFocus()));
145 setMinimumSize(500,300);
146 //closeButton = new QPushButton(tr("Save"));
147 //buttonGroup->addWidget(closeButton);
149 // setMinimumWidth(sizeHint().width()*1.2);
150 // setMaximumWidth(sizeHint().width()*2);
151 // setMinimumHeight(sizeHint().height());
152 // setMaximumHeight(sizeHint().height()*2);
153 connect(rootObject, SIGNAL(saveButtonClicked()), this, SLOT(save()));
158 void DictManagerWidget::refreshDictsList() {
161 QHash<CommonDictInterface*, bool> dicts = guiInterface->getDictionaries();
164 model = new DictManagerModel(dicts, this);
167 model->setDictionaries(dicts);
176 removeDictButton->setEnabled(false);
177 settingsButton->setEnabled(false);
179 QHash<CommonDictInterface*, bool> dicts = guiInterface->getDictionaries();
181 QHashIterator<CommonDictInterface*, bool> i(dicts);
186 QListWidgetItem* item = new QListWidgetItem();
188 if (i.key()->type() == "stardict") {
189 name = i.key()->name() + " (" + i.key()->type() + ")";
192 name = i.key()->langFrom() + " - " + i.key()->langTo() +
193 " (" + i.key()->type() + " " +
194 i.key()->name() + ")";
197 item->setFlags(item->flags() | Qt::ItemIsUserCheckable);
199 item->setCheckState(Qt::Checked);
202 item->setCheckState(Qt::Unchecked);
204 item->setIcon(*i.key()->icon());
206 dictList->addItem(item);
207 dictsHash.insert(item, i.key());
210 dictList->sortItems();
214 void DictManagerWidget::showEvent(QShowEvent *e) {
220 QWidget::showEvent(e);
223 void DictManagerWidget::saveChanges() {
229 QMessageBox::question(this, tr("Save"),
230 tr("Do you want to save changes?"),
231 QMessageBox::Save, QMessageBox::Cancel) == QMessageBox::Save) {
232 QList<CommonDictInterface*> checkedDicts;
234 for(int i=0; i<dictList->count(); i++) {
235 QListWidgetItem* item = dictList->item(i);
236 if(item->checkState() == Qt::Checked) {
237 checkedDicts.push_back(dictsHash[item]);
241 QList<CommonDictInterface*> checkedDicts;
243 for(int i=0; i<model->dictList().count(); i++) {
244 // QListWidgetItem* item = dictList->item(i);
245 CommonDictInterface* item = model->dictList()[i];
246 if(model->dictionaries()[item] == true) {
247 checkedDicts.push_back(item);
251 emit selectedDictionaries(checkedDicts);
255 void DictManagerWidget::hideEvent(QHideEvent *e) {
257 QWidget::hideEvent(e);
261 void DictManagerWidget::addNewDictButtonClicked() {
263 if(!_changed || QMessageBox::question(this,
264 tr("Save"), tr("Do you want to save changes?"),
265 QMessageBox::Save, QMessageBox::Cancel) == QMessageBox::Save) {
272 CommonDictInterface* selectedPlugin =
273 DictTypeSelectDialog::addNewDict(guiInterface->getPlugins(),this);
276 selectedPlugin->dictDialog()->addNewDictionary(this->parentWidget());
279 CommonDictInterface* newDict = selectedPlugin->getNew(settings);
282 Q_EMIT addDictionary(newDict);
289 void DictManagerWidget::itemSelected(QListWidgetItem *) {
290 removeDictButton->setEnabled(true);
291 settingsButton->setEnabled(true);
292 dictList->setFocus();
295 void DictManagerWidget::removeButtonClicked() {
296 if(QMessageBox::question(this, tr("Remove dictionary"),
297 tr("Do you want to remove selected dictionary?"),
298 QMessageBox::Yes, QMessageBox::No) == QMessageBox::Yes) {
300 emit removeDictionary(model->currentDict());
303 QList<QListWidgetItem*> selected = dictList->selectedItems();
304 if(selected.count() > 0) {
305 emit removeDictionary(dictsHash[selected[0]]);
312 void DictManagerWidget::settingsButtonClicked() {
314 if(!_changed || QMessageBox::question(this,
315 tr("Save"), tr("Do you want to save changes?"),
316 QMessageBox::Save, QMessageBox::Cancel) == QMessageBox::Save) {
321 model->currentDict()->dictDialog()->changeSettings(this->parentWidget());
324 QList<QListWidgetItem*> selected = dictList->selectedItems();
325 if(selected.count() > 0) {
326 dictsHash[selected[0]]->dictDialog()->changeSettings(this->parentWidget());
333 void DictManagerWidget::changed() {
339 void DictManagerWidget::save() {
344 void DictManagerWidget::setFocusOnElement(){
345 Q_EMIT setFocusOnQML();
350 void DictManagerWidget::keyPressEvent(QKeyEvent *e) {
351 if (!e->modifiers() || (e->modifiers() & Qt::KeypadModifier && e->key() == Qt::Key_Enter)) {