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()));
128 //z modelu sygnał, oraz z okienka zmian ustawień w pluginie, gdy są zmiany
130 // connect(dictList, SIGNAL(itemChanged(QListWidgetItem*)),
131 // this, SLOT(changed()));
132 connect(model, SIGNAL(itemChanged()),
133 this, SLOT(changed()));
134 //pozmieniać connecty, to jest na dwuklik mysza na liście, sprawdzić, zrobic alternatywne sloty
135 connect(rootObject, SIGNAL(itemActivated(int)),
136 this, SLOT(saveChanges()));
137 connect(rootObject, SIGNAL(itemActivated(int)),
138 this, SLOT(settingsButtonClicked()));
140 // connect(dictList, SIGNAL(itemActivated(QListWidgetItem*)),
141 // this, SLOT(saveChanges()));
142 // connect(dictList, SIGNAL(itemActivated(QListWidgetItem*)),
143 // this, SLOT(itemSelected(QListWidgetItem*)));
144 // connect(dictList, SIGNAL(itemActivated(QListWidgetItem*)),
145 // settingsButton, SIGNAL(clicked()));
153 setMinimumSize(500,300);
154 //closeButton = new QPushButton(tr("Save"));
155 //buttonGroup->addWidget(closeButton);
157 // setMinimumWidth(sizeHint().width()*1.2);
158 // setMaximumWidth(sizeHint().width()*2);
159 // setMinimumHeight(sizeHint().height());
160 // setMaximumHeight(sizeHint().height()*2);
161 //connect(closeButton, SIGNAL(clicked()), this, SLOT(save()));
162 connect(rootObject, SIGNAL(saveButtonClicked()), this, SLOT(save()));
167 void DictManagerWidget::refreshDictsList() {
170 QHash<CommonDictInterface*, bool> dicts = guiInterface->getDictionaries();
173 model = new DictManagerModel(dicts, this);
176 model->setDictionaries(dicts);
184 removeDictButton->setEnabled(false);
185 settingsButton->setEnabled(false);
187 QHash<CommonDictInterface*, bool> dicts = guiInterface->getDictionaries();
189 QHashIterator<CommonDictInterface*, bool> i(dicts);
194 QListWidgetItem* item = new QListWidgetItem();
196 if (i.key()->type() == "stardict") {
197 name = i.key()->name() + " (" + i.key()->type() + ")";
200 name = i.key()->langFrom() + " - " + i.key()->langTo() +
201 " (" + i.key()->type() + " " +
202 i.key()->name() + ")";
205 item->setFlags(item->flags() | Qt::ItemIsUserCheckable);
207 item->setCheckState(Qt::Checked);
210 item->setCheckState(Qt::Unchecked);
212 item->setIcon(*i.key()->icon());
214 dictList->addItem(item);
215 dictsHash.insert(item, i.key());
218 dictList->sortItems();
222 void DictManagerWidget::showEvent(QShowEvent *e) {
228 QWidget::showEvent(e);
231 void DictManagerWidget::saveChanges() {
237 QMessageBox::question(this, tr("Save"),
238 tr("Do you want to save changes?"),
239 QMessageBox::Save, QMessageBox::Cancel) == QMessageBox::Save) {
241 QList<CommonDictInterface*> checkedDicts;
243 for(int i=0; i<dictList->count(); i++) {
244 QListWidgetItem* item = dictList->item(i);
245 if(item->checkState() == Qt::Checked) {
246 checkedDicts.push_back(dictsHash[item]);
250 emit selectedDictionaries(checkedDicts);
254 void DictManagerWidget::hideEvent(QHideEvent *e) {
256 QWidget::hideEvent(e);
260 void DictManagerWidget::addNewDictButtonClicked() {
262 if(!_changed || QMessageBox::question(this,
263 tr("Save"), tr("Do you want to save changes?"),
264 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 QList<QListWidgetItem*> selected = dictList->selectedItems();
300 if(selected.count() > 0) {
301 emit removeDictionary(dictsHash[selected[0]]);
307 void DictManagerWidget::settingsButtonClicked() {
309 if(!_changed || QMessageBox::question(this,
310 tr("Save"), tr("Do you want to save changes?"),
311 QMessageBox::Save, QMessageBox::Cancel) == QMessageBox::Save) {
317 QList<QListWidgetItem*> selected = dictList->selectedItems();
318 if(selected.count() > 0) {
319 dictsHash[selected[0]]->dictDialog()->changeSettings(this->parentWidget());
325 void DictManagerWidget::changed() {
331 void DictManagerWidget::save() {
338 void DictManagerWidget::keyPressEvent(QKeyEvent *e) {
339 if (!e->modifiers() || (e->modifiers() & Qt::KeypadModifier && e->key() == Qt::Key_Enter)) {