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()));
143 setMinimumSize(500,300);
144 //closeButton = new QPushButton(tr("Save"));
145 //buttonGroup->addWidget(closeButton);
147 // setMinimumWidth(sizeHint().width()*1.2);
148 // setMaximumWidth(sizeHint().width()*2);
149 // setMinimumHeight(sizeHint().height());
150 // setMaximumHeight(sizeHint().height()*2);
151 connect(rootObject, SIGNAL(saveButtonClicked()), this, SLOT(save()));
156 void DictManagerWidget::refreshDictsList() {
159 QHash<CommonDictInterface*, bool> dicts = guiInterface->getDictionaries();
162 model = new DictManagerModel(dicts, this);
165 model->setDictionaries(dicts);
173 removeDictButton->setEnabled(false);
174 settingsButton->setEnabled(false);
176 QHash<CommonDictInterface*, bool> dicts = guiInterface->getDictionaries();
178 QHashIterator<CommonDictInterface*, bool> i(dicts);
183 QListWidgetItem* item = new QListWidgetItem();
185 if (i.key()->type() == "stardict") {
186 name = i.key()->name() + " (" + i.key()->type() + ")";
189 name = i.key()->langFrom() + " - " + i.key()->langTo() +
190 " (" + i.key()->type() + " " +
191 i.key()->name() + ")";
194 item->setFlags(item->flags() | Qt::ItemIsUserCheckable);
196 item->setCheckState(Qt::Checked);
199 item->setCheckState(Qt::Unchecked);
201 item->setIcon(*i.key()->icon());
203 dictList->addItem(item);
204 dictsHash.insert(item, i.key());
207 dictList->sortItems();
211 void DictManagerWidget::showEvent(QShowEvent *e) {
217 QWidget::showEvent(e);
220 void DictManagerWidget::saveChanges() {
226 QMessageBox::question(this, tr("Save"),
227 tr("Do you want to save changes?"),
228 QMessageBox::Save, QMessageBox::Cancel) == QMessageBox::Save) {
229 QList<CommonDictInterface*> checkedDicts;
231 for(int i=0; i<dictList->count(); i++) {
232 QListWidgetItem* item = dictList->item(i);
233 if(item->checkState() == Qt::Checked) {
234 checkedDicts.push_back(dictsHash[item]);
238 QList<CommonDictInterface*> checkedDicts;
240 for(int i=0; i<model->dictList().count(); i++) {
241 // QListWidgetItem* item = dictList->item(i);
242 CommonDictInterface* item = model->dictList()[i];
243 if(model->dictionaries()[item] == true) {
244 checkedDicts.push_back(item);
248 emit selectedDictionaries(checkedDicts);
252 void DictManagerWidget::hideEvent(QHideEvent *e) {
254 QWidget::hideEvent(e);
258 void DictManagerWidget::addNewDictButtonClicked() {
260 qDebug()<<"1 "<<_changed;
261 if(!_changed || QMessageBox::question(this,
262 tr("Save"), tr("Do you want to save changes?"),
263 QMessageBox::Save, QMessageBox::Cancel) == QMessageBox::Save) {
271 CommonDictInterface* selectedPlugin =
272 DictTypeSelectDialog::addNewDict(guiInterface->getPlugins(),this);
275 selectedPlugin->dictDialog()->addNewDictionary(this->parentWidget());
278 CommonDictInterface* newDict = selectedPlugin->getNew(settings);
281 Q_EMIT addDictionary(newDict);
288 void DictManagerWidget::itemSelected(QListWidgetItem *) {
289 removeDictButton->setEnabled(true);
290 settingsButton->setEnabled(true);
291 dictList->setFocus();
294 void DictManagerWidget::removeButtonClicked() {
295 if(QMessageBox::question(this, tr("Remove dictionary"),
296 tr("Do you want to remove selected dictionary?"),
297 QMessageBox::Yes, QMessageBox::No) == QMessageBox::Yes) {
299 emit removeDictionary(model->currentDict());
302 QList<QListWidgetItem*> selected = dictList->selectedItems();
303 if(selected.count() > 0) {
304 emit removeDictionary(dictsHash[selected[0]]);
311 void DictManagerWidget::settingsButtonClicked() {
313 if(!_changed || QMessageBox::question(this,
314 tr("Save"), tr("Do you want to save changes?"),
315 QMessageBox::Save, QMessageBox::Cancel) == QMessageBox::Save) {
320 model->currentDict()->dictDialog()->changeSettings(this->parentWidget());
323 QList<QListWidgetItem*> selected = dictList->selectedItems();
324 if(selected.count() > 0) {
325 dictsHash[selected[0]]->dictDialog()->changeSettings(this->parentWidget());
332 void DictManagerWidget::changed() {
338 void DictManagerWidget::save() {
345 void DictManagerWidget::keyPressEvent(QKeyEvent *e) {
346 if (!e->modifiers() || (e->modifiers() & Qt::KeypadModifier && e->key() == Qt::Key_Enter)) {