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 connect(rootObject, SIGNAL(addButtonClicked()),
116 this, SLOT(saveChanges()));
117 connect(rootObject, SIGNAL(addButtonClicked()),
118 this, SLOT(addNewDictButtonClicked()));
120 connect(rootObject, SIGNAL(removeButtonClicked()),
121 this, SLOT(saveChanges()));
122 connect(rootObject, SIGNAL(removeButtonClicked()),
123 this, SLOT(removeButtonClicked()));
125 connect(rootObject, SIGNAL(settingsButtonClicked()),
126 this, SLOT(saveChanges()));
127 connect(rootObject, SIGNAL(settingsButtonClicked()),
128 this, SLOT(settingsButtonClicked()));
130 //z modelu sygnał, oraz z okienka zmian ustawień w pluginie, gdy są zmiany
132 // connect(dictList, SIGNAL(itemChanged(QListWidgetItem*)),
133 // this, SLOT(changed()));
134 connect(model, SIGNAL(itemChanged()),
135 this, SLOT(changed()));
136 //pozmieniać connecty, to jest na dwuklik mysza na liście, sprawdzić, zrobic alternatywne sloty
137 connect(rootObject, SIGNAL(itemActivated(int)),
138 this, SLOT(saveChanges()));
139 connect(rootObject, SIGNAL(itemActivated(int)),
140 settingsButton, SIGNAL(clicked()));
142 // connect(dictList, SIGNAL(itemActivated(QListWidgetItem*)),
143 // this, SLOT(saveChanges()));
144 // connect(dictList, SIGNAL(itemActivated(QListWidgetItem*)),
145 // this, SLOT(itemSelected(QListWidgetItem*)));
146 // connect(dictList, SIGNAL(itemActivated(QListWidgetItem*)),
147 // settingsButton, SIGNAL(clicked()));
155 setMinimumSize(500,300);
156 //closeButton = new QPushButton(tr("Save"));
157 //buttonGroup->addWidget(closeButton);
159 // setMinimumWidth(sizeHint().width()*1.2);
160 // setMaximumWidth(sizeHint().width()*2);
161 // setMinimumHeight(sizeHint().height());
162 // setMaximumHeight(sizeHint().height()*2);
163 //connect(closeButton, SIGNAL(clicked()), this, SLOT(save()));
164 connect(rootObject, SIGNAL(saveButtonClicked()), this, SLOT(save()));
169 void DictManagerWidget::refreshDictsList() {
172 QHash<CommonDictInterface*, bool> dicts = guiInterface->getDictionaries();
175 model = new DictManagerModel(dicts, this);
178 model->setDictionaries(dicts);
186 removeDictButton->setEnabled(false);
187 settingsButton->setEnabled(false);
189 QHash<CommonDictInterface*, bool> dicts = guiInterface->getDictionaries();
191 QHashIterator<CommonDictInterface*, bool> i(dicts);
196 QListWidgetItem* item = new QListWidgetItem();
198 if (i.key()->type() == "stardict") {
199 name = i.key()->name() + " (" + i.key()->type() + ")";
202 name = i.key()->langFrom() + " - " + i.key()->langTo() +
203 " (" + i.key()->type() + " " +
204 i.key()->name() + ")";
207 item->setFlags(item->flags() | Qt::ItemIsUserCheckable);
209 item->setCheckState(Qt::Checked);
212 item->setCheckState(Qt::Unchecked);
214 item->setIcon(*i.key()->icon());
216 dictList->addItem(item);
217 dictsHash.insert(item, i.key());
220 dictList->sortItems();
224 void DictManagerWidget::showEvent(QShowEvent *e) {
230 QWidget::showEvent(e);
233 void DictManagerWidget::saveChanges() {
239 QMessageBox::question(this, tr("Save"),
240 tr("Do you want to save changes?"),
241 QMessageBox::Save, QMessageBox::Cancel) == QMessageBox::Save) {
243 QList<CommonDictInterface*> checkedDicts;
245 for(int i=0; i<dictList->count(); i++) {
246 QListWidgetItem* item = dictList->item(i);
247 if(item->checkState() == Qt::Checked) {
248 checkedDicts.push_back(dictsHash[item]);
252 emit selectedDictionaries(checkedDicts);
256 void DictManagerWidget::hideEvent(QHideEvent *e) {
258 QWidget::hideEvent(e);
262 void DictManagerWidget::addNewDictButtonClicked() {
264 if(!_changed || QMessageBox::question(this,
265 tr("Save"), tr("Do you want to save changes?"),
266 QMessageBox::Save, QMessageBox::Cancel) == QMessageBox::Save) {
273 CommonDictInterface* selectedPlugin =
274 DictTypeSelectDialog::addNewDict(guiInterface->getPlugins(),this);
277 selectedPlugin->dictDialog()->addNewDictionary(this->parentWidget());
280 CommonDictInterface* newDict = selectedPlugin->getNew(settings);
283 Q_EMIT addDictionary(newDict);
290 void DictManagerWidget::itemSelected(QListWidgetItem *) {
291 removeDictButton->setEnabled(true);
292 settingsButton->setEnabled(true);
293 dictList->setFocus();
296 void DictManagerWidget::removeButtonClicked() {
297 if(QMessageBox::question(this, tr("Remove dictionary"),
298 tr("Do you want to remove selected dictionary?"),
299 QMessageBox::Yes, QMessageBox::No) == QMessageBox::Yes) {
301 QList<QListWidgetItem*> selected = dictList->selectedItems();
302 if(selected.count() > 0) {
303 emit removeDictionary(dictsHash[selected[0]]);
309 void DictManagerWidget::settingsButtonClicked() {
311 if(!_changed || QMessageBox::question(this,
312 tr("Save"), tr("Do you want to save changes?"),
313 QMessageBox::Save, QMessageBox::Cancel) == QMessageBox::Save) {
319 QList<QListWidgetItem*> selected = dictList->selectedItems();
320 if(selected.count() > 0) {
321 dictsHash[selected[0]]->dictDialog()->changeSettings(this->parentWidget());
327 void DictManagerWidget::changed() {
333 void DictManagerWidget::save() {
340 void DictManagerWidget::keyPressEvent(QKeyEvent *e) {
341 if (!e->modifiers() || (e->modifiers() & Qt::KeypadModifier && e->key() == Qt::Key_Enter)) {