Fixed bugs: dialogs remained disabled (signals were not attached to
[emufront] / src / dialogs / dbobjectdialog.cpp
1 // EmuFront
2 // Copyright 2010 Mikko Keinänen
3 //
4 // This file is part of EmuFront.
5 //
6 //
7 // EmuFront is free software: you can redistribute it and/or modify
8 // it under the terms of the GNU General Public License version 2 as published by
9 // the Free Software Foundation and appearing in the file gpl.txt included in the
10 // packaging of this file.
11 //
12 // EmuFront is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 // GNU General Public License for more details.
16 //
17 // You should have received a copy of the GNU General Public License
18 // along with EmuFront.  If not, see <http://www.gnu.org/licenses/>.
19
20 #include <QtGui>
21 #include <QSqlTableModel>
22 #include "dbobjectdialog.h"
23 #include "../db/databasemanager.h"
24
25 DbObjectDialog::DbObjectDialog(QWidget *parent)
26     : EmuFrontDialog(parent)
27 {
28     dbObject = 0;
29     dbManager = 0;
30     nameDialog = 0;
31     editButton = new QPushButton(tr("&Edit")); 
32     editButton->setEnabled(false);
33     addButton = new QPushButton(tr("&Add"));
34     deleteButton = new QPushButton(tr("&Delete"));
35     deleteButton->setEnabled(false);
36     objectList = new QTableView(this);
37     buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok, Qt::Vertical);
38     buttonBox->addButton(editButton, QDialogButtonBox::ActionRole);
39     buttonBox->addButton(addButton, QDialogButtonBox::ActionRole);
40     buttonBox->addButton(deleteButton, QDialogButtonBox::ActionRole);
41     // this be called from the implementing classes:
42     //connectSignals();
43     layout();
44
45
46 DbObjectDialog::~DbObjectDialog()
47 {
48     // no need to explicitly delete widgets within a parented layout
49     // they are automatically parented and will be deleted
50     // dbManager is also parented and will be implicitly deleted
51     // this must be deleted in an implementing class
52     //delete dbObject;
53     // hiddenColumn QList will be deleted implicitly, since it
54     // implements implicit data sharing
55 }
56
57 void DbObjectDialog::connectSignals()
58 {
59     connect(buttonBox, SIGNAL(accepted()), this, SLOT(close()));
60     connect(objectList, SIGNAL(clicked(const QModelIndex &)),
61         this, SLOT(listObjectClicked(const QModelIndex &)));
62     connect(editButton, SIGNAL(clicked()), this, SLOT(editButtonClicked()));
63     connect(addButton, SIGNAL(clicked()), this, SLOT(addButtonClicked()));
64     connect(deleteButton, SIGNAL(clicked()), this, SLOT(deleteButtonClicked()));
65 }
66
67 void DbObjectDialog::connectNameDialogSignals()
68 {
69     connect(nameDialog, SIGNAL(dataObjectUpdated()), this, SLOT(updateData()));
70     connect(nameDialog, SIGNAL(updateRejected()), this, SLOT(updateReject()));
71     connect(nameDialog, SIGNAL(test()), this, SLOT(testSlot()));
72     connect(nameDialog, SIGNAL(dialogClosed()), this, SLOT(enableUi()));
73 }
74     
75 void DbObjectDialog::testSlot()
76 {
77     qDebug() << "TEST SIGNAL RECEIVED!";
78 }
79
80 void DbObjectDialog::insertDb(const EmuFrontObject *ob) const
81 {
82     if (!dbManager->insertDataObjectToModel(ob))
83         errorMessage->showMessage(tr("Inserting data object %1 failed.").arg(ob->getName()));
84 }
85
86 void DbObjectDialog::createEditDialog()
87 {
88     initEditDialog();
89     // call this from implementing classes:
90     //connectNameDialogSignals();
91 }
92
93 void DbObjectDialog::addObject()
94 {
95     setUIEnabled(false);
96     if (!nameDialog) {
97        createEditDialog();
98     }
99     deleteCurrentObject();
100     dbObject = createObject();
101     nameDialog->setDataObject(dbObject);
102     activateNameDialog();
103 }
104
105 void DbObjectDialog::editObject()
106 {
107     QModelIndex index = objectList->currentIndex();
108     if (!index.isValid())
109         return;
110     if (!nameDialog) {
111         createEditDialog();
112     }
113     deleteCurrentObject();
114     dbObject = dbManager->getDataObjectFromModel(&index);
115     activateNameDialog();
116     nameDialog->setDataObject(dbObject);
117 }
118
119 bool DbObjectDialog::deleteItem()
120 {
121     qDebug() << "deleteItem called";
122     QModelIndex index = objectList->currentIndex();
123     if (!index.isValid()) return false;
124     try
125     {
126         EmuFrontObject *ob = dbManager->getDataObjectFromModel(&index);
127
128         qDebug() << "Trying to delete " << ob->getName();
129
130         if (!ob)
131         {
132             errorMessage->showMessage(tr("Couldn't find the selected data object from data model!"));
133             return false;
134         }
135
136         int numBindings = dbManager->countDataObjectRefs(ob->getId());
137
138         if (numBindings > 0 && !confirmDelete(ob->getName(), numBindings))
139         { return false; }
140         deleteCurrentObject();
141
142         bool delOk = dbManager->deleteDataObject(ob->getId());
143         if (!delOk)
144         {
145             errorMessage->showMessage(tr("Deleting data object %1 failed!").arg(ob->getName()));
146             return false;
147         }
148         updateList();
149         objectList->setFocus();
150         setUIEnabled(true);
151     }
152     catch(EmuFrontException e)
153     {
154         errorMessage->showMessage(e.what());
155     }
156     return false;
157 }
158
159 void DbObjectDialog::updateDb(const EmuFrontObject *ob) const
160 {
161     if (!ob) return;
162     if ( !dbManager->updateDataObjectToModel(ob) )
163     { errorMessage->showMessage("Database update failed!"); }
164 }
165
166 void DbObjectDialog::updateList() const
167 {
168     if (!dbManager) return;
169     dbManager->resetModel();
170     objectList->resizeColumnsToContents();
171 }
172
173 void DbObjectDialog::addButtonClicked()
174 {
175     disableSelection();
176     addObject();
177 }
178
179 void DbObjectDialog::editButtonClicked()
180 {
181     disableSelection();
182     editObject();
183 }
184
185 void DbObjectDialog::deleteButtonClicked()
186 {
187     QItemSelectionModel *selModel = objectList->selectionModel();
188     if (!selModel->hasSelection()) return;
189
190     QAbstractItemModel *tblModel = objectList->model();
191     QModelIndex index = selModel->currentIndex();
192     QVariant vName = tblModel->data(index);
193     QString name = vName.toString();
194     disableSelection();
195
196     QString msg =  tr("Do you want to delete") + name + "?";
197     int yn = QMessageBox::question(this, "Confirm", msg, QMessageBox::Yes | QMessageBox::No, QMessageBox::No);
198     if (yn == QMessageBox::Yes)
199     {
200         deleteItem();
201     }
202 }
203
204 void DbObjectDialog::layout()
205 {
206     QHBoxLayout *mainLayout = new QHBoxLayout;
207     mainLayout->addWidget(objectList);
208     mainLayout->addWidget(buttonBox);
209     setLayout(mainLayout);
210 }
211
212 void DbObjectDialog::listObjectClicked(const QModelIndex &index)
213 {
214     const QModelIndex *x;
215     x = &index;
216     qDebug() << "Row " << x->row() << ", column " << x->column() << " clicked.";
217     setButtonsEnabled(index.isValid());
218     if(!index.isValid()) 
219         return;
220 }
221
222 void DbObjectDialog::enableUi()
223 {
224     setUIEnabled(true);
225 }
226
227 void DbObjectDialog::setButtonsEnabled(bool enabled)
228 {
229     addButton->setEnabled(enabled);
230     editButton->setEnabled(enabled);
231     deleteButton->setEnabled(enabled);
232 }
233
234 void DbObjectDialog::setUIEnabled(bool enabled)
235 {
236     buttonBox->setEnabled(enabled);
237     objectList->setEnabled(enabled);
238 }
239
240 void DbObjectDialog::disableSelection()
241 {
242     setUIEnabled(false);
243     //setButtonsEnabled(false);
244 }
245
246 void DbObjectDialog::activateNameDialog(bool updateData)
247 {
248     if (!nameDialog) return;
249     nameDialog->show();
250     nameDialog->raise();
251     if (updateData)
252         nameDialog->updateData();
253     nameDialog->activateWindow();
254 }
255
256 void DbObjectDialog::initDataTable()
257 {
258    objectList->setModel(dbManager->getDataModel());
259    objectList->setSelectionMode(QAbstractItemView::SingleSelection);
260    objectList->resizeColumnsToContents();
261 }
262
263 void DbObjectDialog::updateReject()
264 {
265     addButton->setEnabled(true);
266     setUIEnabled(true);
267     // we don't want to keep this in memory
268     deleteCurrentObject();
269 }
270
271 void DbObjectDialog::updateData()
272 {
273     // update data model
274     if (!dbObject) return;
275
276     // if data object id > -1 we are updating the data otherwise we are inserting new data
277     if (dbObject->getId() > -1) updateDb(dbObject);
278     else insertDb(dbObject);
279
280     // we don't need dbObject anymore
281     deleteCurrentObject();
282     dbObject = 0;
283     updateList();
284     setUIEnabled(true);
285 }
286
287 /* Implementation specific delete must be used!
288 void DbObjectDialog::deleteCurrentObject()
289 {
290     delete dbObject;
291 }*/
292
293 bool DbObjectDialog::confirmDelete(QString name, int numRefs)
294 {
295     int r = QMessageBox::warning(this, tr("Confirm delete"),
296                                  QString("Do you really want to delete %1 with %2 data bindings?")
297                                  .arg(name).arg(numRefs),
298                                  QMessageBox::Yes | QMessageBox::No);
299     if ( r == QMessageBox::No )
300         return false;
301     return true;
302 }
303
304 void DbObjectDialog::refreshDataModel()
305 {
306     dbManager->resetModel();
307 }
308
309 void DbObjectDialog::hideColumns()
310 {
311     foreach(int c, hiddenColumns)
312         objectList->hideColumn(c);
313 }
314
315 void DbObjectDialog::closeEvent(QCloseEvent *ev)
316 {
317     qDebug() << "DbObjectDialog closing!";
318     setUIEnabled(true);
319     cleanUp();
320 }
321