fig delete settings
[mdictionary] / trunk / src / base / gui / DictManagerWidget.cpp
1 /*******************************************************************************
2
3     This file is part of mDictionary.
4
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.
9
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.
14
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/>.
17
18     Copyright 2010 Comarch S.A.
19
20 *******************************************************************************/
21
22 //! \file DictManagerWidget.cpp
23 //! \brief Dicrionaries management widget
24 //! \author Mateusz Półrola <mateusz.polrola@comarch.pl>
25
26 #include "DictManagerWidget.h"
27 #include "DictTypeSelectDialog.h"
28 #include <QDebug>
29 #include "../../includes/DictDialog.h"
30
31 DictManagerWidget::DictManagerWidget(GUIInterface *parent) :
32     QDialog(parent) {
33
34
35
36     setWindowTitle(tr("Dictionaries"));
37     this->guiInterface = parent;
38
39     verticalLayout = new QVBoxLayout;
40     setLayout(verticalLayout);
41
42     dictListWidget = new QListWidget;
43     verticalLayout->addWidget(dictListWidget);
44
45     dictListWidget->setSelectionMode(QAbstractItemView::SingleSelection);
46     dictListWidget->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
47
48     addNewDictButton = new QPushButton(tr("Add"));
49     removeDictButton = new QPushButton(tr("Remove"));
50     settingsButton = new QPushButton(tr("Settings"));
51
52     removeDictButton->setEnabled(false);
53     settingsButton->setEnabled(false);
54
55     buttonGroup = new QHBoxLayout;
56
57     buttonGroup->addWidget(addNewDictButton);
58     buttonGroup->addWidget(removeDictButton);
59     buttonGroup->addWidget(settingsButton);
60
61     verticalLayout->addLayout(buttonGroup, Qt::AlignBottom);
62
63
64     connect(addNewDictButton, SIGNAL(clicked()),
65             this, SLOT(saveChanges()));
66     connect(addNewDictButton, SIGNAL(clicked()),
67             this, SLOT(addNewDictButtonClicked()));
68
69     connect(removeDictButton, SIGNAL(clicked()),
70             this, SLOT(saveChanges()));
71     connect(removeDictButton, SIGNAL(clicked()),
72             this, SLOT(removeButtonClicked()));
73
74     connect(settingsButton, SIGNAL(clicked()),
75             this, SLOT(saveChanges()));
76     connect(settingsButton, SIGNAL(clicked()),
77             this, SLOT(settingsButtonClicked()));
78
79     connect(dictListWidget, SIGNAL(itemClicked(QListWidgetItem*)),
80             this, SLOT(itemSelected(QListWidgetItem*)));
81
82     connect(dictListWidget, SIGNAL(itemChanged(QListWidgetItem*)),
83             this, SLOT(changed()));
84
85     refreshDictsList();
86
87     #ifndef Q_WS_MAEMO_5
88         setMinimumSize(500,300);
89         closeButton = new QPushButton(tr("Save"));
90         buttonGroup->addWidget(closeButton);
91         connect(closeButton, SIGNAL(clicked()), this, SLOT(save()));
92     #endif
93 }
94
95
96 void DictManagerWidget::refreshDictsList() {
97
98     dictListWidget->clear();
99     dictsHash.clear();
100     removeDictButton->setEnabled(false);
101     settingsButton->setEnabled(false);
102
103     QHash<CommonDictInterface*, bool> dicts = guiInterface->getDictionaries();
104
105     QHashIterator<CommonDictInterface*, bool> i(dicts);
106
107     while(i.hasNext()) {
108         i.next();
109         QListWidgetItem* item = new QListWidgetItem();
110         QString name = i.key()->langFrom() + " - " + i.key()->langTo() + " (" +
111                        i.key()->type() + " " + i.key()->name() + ")";
112         item->setText(name);
113         item->setFlags(item->flags() | Qt::ItemIsUserCheckable);
114         if(i.value()) {
115             item->setCheckState(Qt::Checked);
116         }
117         else {
118             item->setCheckState(Qt::Unchecked);
119         }
120         item->setIcon(*i.key()->icon());
121
122         dictListWidget->addItem(item);
123         dictsHash.insert(item, i.key());
124     }
125 }
126
127 void DictManagerWidget::showEvent(QShowEvent *e) {
128     _changed = false;
129     #ifndef Q_WS_MAEMO_5
130       _save = false;
131     #endif
132     refreshDictsList();
133     QWidget::showEvent(e);
134 }
135
136 void DictManagerWidget::saveChanges() {
137
138     #ifndef Q_WS_MAEMO_5
139     if(_save) {
140     #else
141     if(_changed &&
142             QMessageBox::question(this, tr("Save"), tr("Do you want to save changes?"),
143             QMessageBox::Save, QMessageBox::Cancel) == QMessageBox::Save) {
144     #endif
145         QList<CommonDictInterface*> checkedDicts;
146
147         for(int i=0; i<dictListWidget->count(); i++) {
148             QListWidgetItem* item = dictListWidget->item(i);
149             if(item->checkState() == Qt::Checked) {
150                 checkedDicts.push_back(dictsHash[item]);
151             }
152         }
153         _changed = false;
154         emit selectedDictionaries(checkedDicts);
155     }
156 }
157
158 void DictManagerWidget::hideEvent(QHideEvent *e)
159 {
160     saveChanges();
161     QWidget::hideEvent(e);
162 }
163
164
165 void DictManagerWidget::addNewDictButtonClicked() {
166     #ifndef Q_WS_MAEMO_5
167     if(!_changed || QMessageBox::question(this,
168             "Save", "Do you want to save changes?",
169             QMessageBox::Save, QMessageBox::Cancel) == QMessageBox::Save) {
170         _save = true;
171         saveChanges();
172         _save = false;
173     }
174     #endif
175
176    CommonDictInterface* selectedPlugin =
177            DictTypeSelectDialog::addNewDict(guiInterface->getPlugins(),this);
178    if(selectedPlugin != NULL) {
179        Settings* settings =
180                selectedPlugin->dictDialog()->addNewDictionary(this);
181
182        if(settings != NULL) {
183            CommonDictInterface* newDict = selectedPlugin->getNew(settings);
184            delete settings;
185            Q_EMIT addDictionary(newDict);
186        }
187    }
188    refreshDictsList();
189 }
190
191 void DictManagerWidget::itemSelected(QListWidgetItem *) {
192     removeDictButton->setEnabled(true);
193     settingsButton->setEnabled(true);
194 }
195
196 void DictManagerWidget::removeButtonClicked() {
197     if(QMessageBox::question(this, tr("Remove dictionary"),
198             tr("Do you want to remove selected dictionary?"),
199             QMessageBox::Ok, QMessageBox::Cancel) == QMessageBox::Ok) {
200         QList<QListWidgetItem*> selected = dictListWidget->selectedItems();
201         if(selected.count() > 0) {
202             emit removeDictionary(dictsHash[selected[0]]);
203             refreshDictsList();
204         }
205    }
206 }
207
208 void DictManagerWidget::settingsButtonClicked() {
209     #ifndef Q_WS_MAEMO_5
210     if(!_changed || QMessageBox::question(this,
211             "Save", "Do you want to save changes?",
212             QMessageBox::Save, QMessageBox::Cancel) == QMessageBox::Save) {
213         _save = true;
214         saveChanges();
215         _save = false;
216     }
217     #endif
218    QList<QListWidgetItem*> selected = dictListWidget->selectedItems();
219    if(selected.count() > 0) {
220        dictsHash[selected[0]]->dictDialog()->changeSettings(this);
221    }
222    refreshDictsList();
223 }
224
225
226 void DictManagerWidget::changed() {
227     _changed=true;
228 }
229
230
231 #ifndef Q_WS_MAEMO_5
232     void DictManagerWidget::save() {
233         _save = true;
234         hide();
235     }
236 #endif