More exception handling.
[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     try {
115         dbObject = dbManager->getDataObjectFromModel(&index); // throws EmuFrontException
116     } catch (EmuFrontException &e) { errorMessage->showMessage(e.what()); }
117     activateNameDialog();
118     nameDialog->setDataObject(dbObject);
119 }
120
121 bool DbObjectDialog::deleteItem()
122 {
123     qDebug() << "deleteItem called";
124     QModelIndex index = objectList->currentIndex();
125     if (!index.isValid()) return false;
126     try
127     {
128         EmuFrontObject *ob = dbManager->getDataObjectFromModel(&index); // throws EmuFrontException
129
130         qDebug() << "Trying to delete " << ob->getName();
131
132         if (!ob)
133         {
134             errorMessage->showMessage(tr("Couldn't find the selected data object from data model!"));
135             return false;
136         }
137
138         int numBindings = dbManager->countDataObjectRefs(ob->getId());
139
140         if (numBindings > 0 && !confirmDelete(ob->getName(), numBindings))
141         { return false; }
142         deleteCurrentObject();
143
144         bool delOk = dbManager->deleteDataObject(ob->getId());
145         if (!delOk)
146         {
147             errorMessage->showMessage(tr("Deleting data object %1 failed!").arg(ob->getName()));
148             return false;
149         }
150         updateList();
151         objectList->setFocus();
152         setUIEnabled(true);
153     }
154     catch(EmuFrontException e)
155     {
156         errorMessage->showMessage(e.what());
157     }
158     return false;
159 }
160
161 void DbObjectDialog::updateDb(const EmuFrontObject *ob) const
162 {
163     if (!ob) return;
164     if ( !dbManager->updateDataObjectToModel(ob) )
165     { errorMessage->showMessage("Database update failed!"); }
166 }
167
168 void DbObjectDialog::updateList() const
169 {
170     if (!dbManager) return;
171     dbManager->resetModel();
172     objectList->resizeColumnsToContents();
173 }
174
175 void DbObjectDialog::addButtonClicked()
176 {
177     disableSelection();
178     addObject();
179 }
180
181 void DbObjectDialog::editButtonClicked()
182 {
183     disableSelection();
184     editObject();
185 }
186
187 void DbObjectDialog::deleteButtonClicked()
188 {
189     QItemSelectionModel *selModel = objectList->selectionModel();
190     if (!selModel->hasSelection()) return;
191
192     QAbstractItemModel *tblModel = objectList->model();
193     QModelIndex index = selModel->currentIndex();
194     QVariant vName = tblModel->data(index);
195     QString name = vName.toString();
196     disableSelection();
197
198     QString msg =  tr("Do you want to delete") + name + "?";
199     int yn = QMessageBox::question(this, "Confirm", msg, QMessageBox::Yes | QMessageBox::No, QMessageBox::No);
200     if (yn == QMessageBox::Yes)
201     {
202         deleteItem();
203     }
204 }
205
206 void DbObjectDialog::layout()
207 {
208     QHBoxLayout *mainLayout = new QHBoxLayout;
209     mainLayout->addWidget(objectList);
210     mainLayout->addWidget(buttonBox);
211     setLayout(mainLayout);
212 }
213
214 void DbObjectDialog::listObjectClicked(const QModelIndex &index)
215 {
216     const QModelIndex *x;
217     x = &index;
218     qDebug() << "Row " << x->row() << ", column " << x->column() << " clicked.";
219     setButtonsEnabled(index.isValid());
220     if(!index.isValid()) 
221         return;
222 }
223
224 void DbObjectDialog::enableUi()
225 {
226     setUIEnabled(true);
227 }
228
229 void DbObjectDialog::setButtonsEnabled(bool enabled)
230 {
231     addButton->setEnabled(enabled);
232     editButton->setEnabled(enabled);
233     deleteButton->setEnabled(enabled);
234 }
235
236 void DbObjectDialog::setUIEnabled(bool enabled)
237 {
238     buttonBox->setEnabled(enabled);
239     objectList->setEnabled(enabled);
240 }
241
242 void DbObjectDialog::disableSelection()
243 {
244     setUIEnabled(false);
245     //setButtonsEnabled(false);
246 }
247
248 void DbObjectDialog::activateNameDialog(bool updateData)
249 {
250     if (!nameDialog) return;
251     nameDialog->show();
252     nameDialog->raise();
253     if (updateData)
254         nameDialog->updateData();
255     nameDialog->activateWindow();
256 }
257
258 void DbObjectDialog::initDataTable()
259 {
260    objectList->setModel(dbManager->getDataModel());
261    objectList->setSelectionMode(QAbstractItemView::SingleSelection);
262    objectList->resizeColumnsToContents();
263 }
264
265 void DbObjectDialog::updateReject()
266 {
267     addButton->setEnabled(true);
268     setUIEnabled(true);
269     // we don't want to keep this in memory
270     deleteCurrentObject();
271 }
272
273 void DbObjectDialog::updateData()
274 {
275     // update data model
276     if (!dbObject) return;
277
278     // if data object id > -1 we are updating the data otherwise we are inserting new data
279     if (dbObject->getId() > -1) updateDb(dbObject);
280     else insertDb(dbObject);
281
282     // we don't need dbObject anymore
283     deleteCurrentObject();
284     dbObject = 0;
285     updateList();
286     setUIEnabled(true);
287 }
288
289 /* Implementation specific delete must be used!
290 void DbObjectDialog::deleteCurrentObject()
291 {
292     delete dbObject;
293 }*/
294
295 bool DbObjectDialog::confirmDelete(QString name, int numRefs)
296 {
297     int r = QMessageBox::warning(this, tr("Confirm delete"),
298                                  QString("Do you really want to delete %1 with %2 data bindings?")
299                                  .arg(name).arg(numRefs),
300                                  QMessageBox::Yes | QMessageBox::No);
301     if ( r == QMessageBox::No )
302         return false;
303     return true;
304 }
305
306 void DbObjectDialog::refreshDataModel()
307 {
308     dbManager->resetModel();
309 }
310
311 void DbObjectDialog::hideColumns()
312 {
313     foreach(int c, hiddenColumns)
314         objectList->hideColumn(c);
315 }
316
317 void DbObjectDialog::closeEvent(QCloseEvent *ev)
318 {
319     qDebug() << "DbObjectDialog closing!";
320     setUIEnabled(true);
321     cleanUp();
322 }
323