Fixed setup dialog crashing when user created another instance of setup
[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     connect(nameDialog, SIGNAL(dataObjectUpdated()), this, SLOT(updateData()));
66     connect(nameDialog, SIGNAL(updateRejected()), this, SLOT(updateReject()));
67     connect(nameDialog, SIGNAL(test()), this, SLOT(testSlot()));
68 }
69     
70 void DbObjectDialog::testSlot()
71 {
72     qDebug() << "TEST SIGNAL RECEIVED!";
73 }
74
75 void DbObjectDialog::insertDb(const EmuFrontObject *ob) const
76 {
77     if (!dbManager->insertDataObjectToModel(ob))
78         errorMessage->showMessage(tr("Inserting data object %1 failed.").arg(ob->getName()));
79 }
80
81 void DbObjectDialog::addObject()
82 {
83     setUIEnabled(false);
84     if (!nameDialog) initEditDialog();
85     deleteCurrentObject();
86     dbObject = createObject();
87     nameDialog->setDataObject(dbObject);
88     activateNameDialog();
89 }
90
91 void DbObjectDialog::editObject()
92 {
93     QModelIndex index = objectList->currentIndex();
94     if (!index.isValid())
95         return;
96     if (!nameDialog) initEditDialog();
97     deleteCurrentObject();
98     dbObject = dbManager->getDataObjectFromModel(&index);
99     activateNameDialog();
100     nameDialog->setDataObject(dbObject);
101 }
102
103 bool DbObjectDialog::deleteItem()
104 {
105     qDebug() << "deleteItem called";
106     QModelIndex index = objectList->currentIndex();
107     if (!index.isValid()) return false;
108     try
109     {
110         EmuFrontObject *ob = dbManager->getDataObjectFromModel(&index);
111
112         qDebug() << "Trying to delete " << ob->getName();
113
114         if (!ob)
115         {
116             errorMessage->showMessage(tr("Couldn't find the selected data object from data model!"));
117             return false;
118         }
119
120         int numBindings = dbManager->countDataObjectRefs(ob->getId());
121
122         if (numBindings > 0 && !confirmDelete(ob->getName(), numBindings))
123         { return false; }
124         deleteCurrentObject();
125
126         bool delOk = dbManager->deleteDataObject(ob->getId());
127         if (!delOk)
128         {
129             errorMessage->showMessage(tr("Deleting data object %1 failed!").arg(ob->getName()));
130             return false;
131         }
132         updateList();
133         objectList->setFocus();
134         setUIEnabled(true);
135     }
136     catch(EmuFrontException e)
137     {
138         errorMessage->showMessage(e.what());
139     }
140     return false;
141 }
142
143 void DbObjectDialog::updateDb(const EmuFrontObject *ob) const
144 {
145     if (!ob) return;
146     if ( !dbManager->updateDataObjectToModel(ob) )
147     { errorMessage->showMessage("Database update failed!"); }
148 }
149
150 void DbObjectDialog::updateList() const
151 {
152     if (!dbManager) return;
153     dbManager->resetModel();
154 }
155
156 void DbObjectDialog::addButtonClicked()
157 {
158     disableSelection();
159     addObject();
160 }
161
162 void DbObjectDialog::editButtonClicked()
163 {
164     disableSelection();
165     editObject();
166 }
167
168 void DbObjectDialog::deleteButtonClicked()
169 {
170     QItemSelectionModel *selModel = objectList->selectionModel();
171     if (!selModel->hasSelection()) return;
172
173     QAbstractItemModel *tblModel = objectList->model();
174     QModelIndex index = selModel->currentIndex();
175     QVariant vName = tblModel->data(index);
176     QString name = vName.toString();
177     disableSelection();
178
179     QString msg =  tr("Do you want to delete") + name + "?";
180     int yn = QMessageBox::question(this, "Confirm", msg, QMessageBox::Yes | QMessageBox::No, QMessageBox::No);
181     if (yn == QMessageBox::Yes)
182     {
183         deleteItem();
184     }
185 }
186
187 void DbObjectDialog::layout()
188 {
189     QHBoxLayout *mainLayout = new QHBoxLayout;
190     mainLayout->addWidget(objectList);
191     mainLayout->addWidget(buttonBox);
192     setLayout(mainLayout);
193 }
194
195 void DbObjectDialog::listObjectClicked(const QModelIndex &index)
196 {
197     const QModelIndex *x;
198     x = &index;
199     qDebug() << "Row " << x->row() << ", column " << x->column() << " clicked.";
200     setButtonsEnabled(index.isValid());
201     if(!index.isValid()) 
202         return;
203 }
204
205 void DbObjectDialog::setButtonsEnabled(bool enabled)
206 {
207     addButton->setEnabled(enabled);
208     editButton->setEnabled(enabled);
209     deleteButton->setEnabled(enabled);
210 }
211
212 void DbObjectDialog::setUIEnabled(bool enabled)
213 {
214     buttonBox->setEnabled(enabled);
215     objectList->setEnabled(enabled);
216 }
217
218 void DbObjectDialog::disableSelection()
219 {
220     setUIEnabled(false);
221     //setButtonsEnabled(false);
222 }
223
224 void DbObjectDialog::activateNameDialog(bool updateData)
225 {
226     if (!nameDialog) return;
227     nameDialog->show();
228     nameDialog->raise();
229     if (updateData)
230         nameDialog->updateData();
231     nameDialog->activateWindow();
232 }
233
234 void DbObjectDialog::initDataTable()
235 {
236    objectList->setModel(dbManager->getDataModel());
237    objectList->setSelectionMode(QAbstractItemView::SingleSelection);
238    objectList->resizeColumnsToContents();
239 }
240
241 void DbObjectDialog::updateReject()
242 {
243     addButton->setEnabled(true);
244     setUIEnabled(true);
245     // we don't want to keep this in memory
246     deleteCurrentObject();
247 }
248
249 void DbObjectDialog::updateData()
250 {
251     qDebug() << "DbObjectDialog::updateData()";
252     // update data model
253     if (!dbObject) return;
254
255
256     // if data object id > -1 we are updating the data otherwise we are inserting new data
257     if (dbObject->getId() > -1) updateDb(dbObject);
258     else insertDb(dbObject);
259
260     // we don't need dbObject anymore
261     deleteCurrentObject();
262     dbObject = 0;
263     updateList();
264     setUIEnabled(true);
265 }
266
267 /* Implementation specific delete must be used!
268 void DbObjectDialog::deleteCurrentObject()
269 {
270     delete dbObject;
271 }*/
272
273 bool DbObjectDialog::confirmDelete(QString name, int numRefs)
274 {
275     int r = QMessageBox::warning(this, tr("Confirm delete"),
276                                  QString("Do you really want to delete %1 with %2 data bindings?")
277                                  .arg(name).arg(numRefs),
278                                  QMessageBox::Yes | QMessageBox::No);
279     if ( r == QMessageBox::No )
280         return false;
281     return true;
282 }
283
284 void DbObjectDialog::refreshDataModel()
285 {
286     dbManager->resetModel();
287 }
288
289 void DbObjectDialog::hideColumns()
290 {
291     foreach(int c, hiddenColumns)
292         objectList->hideColumn(c);
293 }