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