Update / reject signals were not emitted. Fixed.
[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 as published by
9 // the Free Software Foundation, either version 3 of the License, or
10 // (at your option) any later version.
11 //
12 // Foobar 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 Foobar.  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 explicitically delete widgets within a parented layout
49     // they are automatically parented and will be deleted
50     // dbManager is also parented and will be implicitically deleted
51     // this must be deleted in an implementing class
52     //delete dbObject;
53 }
54
55 void DbObjectDialog::connectSignals()
56 {
57     qDebug() << "DbObjectDialog connecting signals";
58     connect(buttonBox, SIGNAL(accepted()), this, SLOT(close()));
59     connect(objectList, SIGNAL(clicked(const QModelIndex &)),
60         this, SLOT(listObjectClicked(const QModelIndex &)));
61     connect(editButton, SIGNAL(clicked()), this, SLOT(editButtonClicked()));
62     connect(addButton, SIGNAL(clicked()), this, SLOT(addButtonClicked()));
63     connect(deleteButton, SIGNAL(clicked()), this, SLOT(deleteButtonClicked()));
64     connect(nameDialog, SIGNAL(dataObjectUpdated()), this, SLOT(updateData()));
65     connect(nameDialog, SIGNAL(updateRejected()), this, SLOT(updateReject()));
66     connect(nameDialog, SIGNAL(test()), this, SLOT(testSlot()));
67 }
68
69 void DbObjectDialog::testSlot()
70 {
71     qDebug() << "TEST SIGNAL RECEIVED!";
72 }
73
74 void DbObjectDialog::insertDb(const EmuFrontObject *ob) const
75 {
76     dbManager->insertDataObjectToModel(ob);
77 }
78
79 void DbObjectDialog::addObject()
80 {
81     if (!nameDialog) initEditDialog();
82     deleteCurrentObject();
83     dbObject = createObject();
84     nameDialog->setDataObject(dbObject);
85     activateNameDialog();
86 }
87
88 void DbObjectDialog::editObject()
89 {
90     QModelIndex index = objectList->currentIndex();
91     if (!index.isValid())
92         return;
93     if (!nameDialog) initEditDialog();
94     deleteCurrentObject();
95     dbObject = dbManager->getDataObjectFromModel(&index);
96     nameDialog->setDataObject(dbObject);
97     activateNameDialog();
98 }
99
100 bool DbObjectDialog::deleteItem()
101 {
102     QModelIndex index = objectList->currentIndex();
103     if (!index.isValid()) return false;
104
105     EmuFrontObject *ob = dbManager->getDataObjectFromModel(&index);
106     if (!ob) return false;
107
108     int numBindings = dbManager->countDataObjectRefs(ob->getId());
109     if (numBindings > 0 && !confirmDelete(ob->getName(), numBindings))
110     {
111             return false;
112     }
113     deleteCurrentObject();
114     bool delOk = dbManager->deleteDataObjectFromModel(&index);
115     if (!delOk)
116     {
117         qDebug() << "delete failed";
118         return false;
119     }
120     updateList();
121     objectList->setFocus();
122     return false;
123 }
124
125 void DbObjectDialog::updateDb(const EmuFrontObject *ob) const
126 {
127     if (!ob) return;
128     qDebug() << "Updating platform " << ob->getName();
129     dbManager->updateDataObjectToModel(ob);
130 }
131
132 void DbObjectDialog::updateList() const
133 {
134     if (!dbManager) return;
135     qDebug() << "Going to reset the data model";
136     dbManager->resetModel();
137 }
138
139 void DbObjectDialog::addButtonClicked()
140 {
141     disableSelection();
142     addObject();
143 }
144
145 void DbObjectDialog::editButtonClicked()
146 {
147     disableSelection();
148     editObject();
149 }
150
151 void DbObjectDialog::deleteButtonClicked()
152 {
153     QItemSelectionModel *selModel = objectList->selectionModel();
154     if (!selModel->hasSelection()) return;
155
156     QAbstractItemModel *tblModel = objectList->model();
157     QModelIndex index = selModel->currentIndex();
158     QVariant vName = tblModel->data(index);
159     QString name = vName.toString();
160     disableSelection();
161
162     QString msg =  tr("Do you want to delete") + name + "?";
163     int yn = QMessageBox::question(this, "Confirm", msg, QMessageBox::Yes | QMessageBox::No, QMessageBox::No);
164     if (yn == QMessageBox::Yes)
165     {
166         qDebug() << "Deleting item..." << name << ".";
167         deleteItem();
168     }
169 }
170
171 void DbObjectDialog::layout()
172 {
173     QHBoxLayout *mainLayout = new QHBoxLayout;
174     mainLayout->addWidget(objectList);
175     mainLayout->addWidget(buttonBox);
176     setLayout(mainLayout);
177 }
178
179 void DbObjectDialog::listObjectClicked(const QModelIndex &index)
180 {
181     const QModelIndex *x;
182     x = &index;
183     qDebug() << "Row " << x->row() << ", column " << x->column() << " clicked.";
184     setButtonsEnabled(index.isValid());
185     if(!index.isValid()) 
186         return;
187 }
188
189 void DbObjectDialog::setButtonsEnabled(bool enabled)
190 {
191     editButton->setEnabled(enabled);
192     deleteButton->setEnabled(enabled);
193 }
194
195 void DbObjectDialog::disableSelection()
196 {
197     setButtonsEnabled(false);
198 }
199
200 void DbObjectDialog::activateNameDialog()
201 {
202     if (!nameDialog) return;
203     nameDialog->show();
204     nameDialog->raise();
205     nameDialog->activateWindow();
206 }
207
208 void DbObjectDialog::initDataTable()
209 {
210    objectList->setModel(dbManager->getDataModel());
211    objectList->setSelectionMode(QAbstractItemView::SingleSelection);
212    objectList->resizeColumnsToContents();
213 }
214
215 void DbObjectDialog::updateReject()
216 {
217     qDebug() << "Update rejected ... going to delete current object.";
218     // we don't want to keep this in memory
219     deleteCurrentObject();
220 }
221
222 void DbObjectDialog::updateData()
223 {
224     qDebug() << "Update accepted.";
225     // update data model
226     if (!dbObject) return;
227
228     qDebug() << "dbObject is not 0";
229
230     qDebug() << "We have a " + dbObject->getName();
231
232     qDebug() << "Data will be inserted/updated...";
233
234     // if data object id > -1 we are updating the data otherwise we are inserting new data
235     if (dbObject->getId() > -1) updateDb(dbObject);
236     else insertDb(dbObject);
237
238     // we don't need dbObject anymore
239     deleteCurrentObject();
240     dbObject = 0;
241     updateList();
242 }
243
244 void DbObjectDialog::deleteCurrentObject()
245 {
246     delete dbObject;
247 }
248
249 bool DbObjectDialog::confirmDelete(QString name, int numRefs)
250 {
251     int r = QMessageBox::warning(this, tr("Confirm delete"),
252                                  QString("Do you really want to delete %1 with %2 data bindings?")
253                                  .arg(name).arg(numRefs),
254                                  QMessageBox::Yes | QMessageBox::No);
255     if ( r == QMessageBox::No )
256         return false;
257     return true;
258 }