Fixed setup insert sql
[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     if ( dbManager->insertDataObjectToModel(ob) )
77         qDebug() << "Db insert ok";
78     else qDebug() << "Db insert failed";
79 }
80
81 void DbObjectDialog::addObject()
82 {
83     if (!nameDialog) initEditDialog();
84     deleteCurrentObject();
85     dbObject = createObject();
86     nameDialog->setDataObject(dbObject);
87     activateNameDialog();
88 }
89
90 void DbObjectDialog::editObject()
91 {
92     QModelIndex index = objectList->currentIndex();
93     if (!index.isValid())
94         return;
95     if (!nameDialog) initEditDialog();
96     deleteCurrentObject();
97     dbObject = dbManager->getDataObjectFromModel(&index);
98     nameDialog->setDataObject(dbObject);
99     activateNameDialog();
100 }
101
102 bool DbObjectDialog::deleteItem()
103 {
104     QModelIndex index = objectList->currentIndex();
105     if (!index.isValid()) return false;
106
107     EmuFrontObject *ob = dbManager->getDataObjectFromModel(&index);
108     if (!ob) return false;
109
110     int numBindings = dbManager->countDataObjectRefs(ob->getId());
111     if (numBindings > 0 && !confirmDelete(ob->getName(), numBindings))
112     {
113             return false;
114     }
115     deleteCurrentObject();
116     bool delOk = dbManager->deleteDataObjectFromModel(&index);
117     if (!delOk)
118     {
119         qDebug() << "delete failed";
120         return false;
121     }
122     updateList();
123     objectList->setFocus();
124     return false;
125 }
126
127 void DbObjectDialog::updateDb(const EmuFrontObject *ob) const
128 {
129     if (!ob) return;
130     qDebug() << "Updating platform " << ob->getName();
131     if ( dbManager->updateDataObjectToModel(ob) )
132     { qDebug() << "Db update ok!"; }
133     else qDebug() << "Db update failed";
134
135 }
136
137 void DbObjectDialog::updateList() const
138 {
139     if (!dbManager) return;
140     qDebug() << "Going to reset the data model";
141     dbManager->resetModel();
142 }
143
144 void DbObjectDialog::addButtonClicked()
145 {
146     disableSelection();
147     addObject();
148 }
149
150 void DbObjectDialog::editButtonClicked()
151 {
152     disableSelection();
153     editObject();
154 }
155
156 void DbObjectDialog::deleteButtonClicked()
157 {
158     QItemSelectionModel *selModel = objectList->selectionModel();
159     if (!selModel->hasSelection()) return;
160
161     QAbstractItemModel *tblModel = objectList->model();
162     QModelIndex index = selModel->currentIndex();
163     QVariant vName = tblModel->data(index);
164     QString name = vName.toString();
165     disableSelection();
166
167     QString msg =  tr("Do you want to delete") + name + "?";
168     int yn = QMessageBox::question(this, "Confirm", msg, QMessageBox::Yes | QMessageBox::No, QMessageBox::No);
169     if (yn == QMessageBox::Yes)
170     {
171         qDebug() << "Deleting item..." << name << ".";
172         deleteItem();
173     }
174 }
175
176 void DbObjectDialog::layout()
177 {
178     QHBoxLayout *mainLayout = new QHBoxLayout;
179     mainLayout->addWidget(objectList);
180     mainLayout->addWidget(buttonBox);
181     setLayout(mainLayout);
182 }
183
184 void DbObjectDialog::listObjectClicked(const QModelIndex &index)
185 {
186     const QModelIndex *x;
187     x = &index;
188     qDebug() << "Row " << x->row() << ", column " << x->column() << " clicked.";
189     setButtonsEnabled(index.isValid());
190     if(!index.isValid()) 
191         return;
192 }
193
194 void DbObjectDialog::setButtonsEnabled(bool enabled)
195 {
196     editButton->setEnabled(enabled);
197     deleteButton->setEnabled(enabled);
198 }
199
200 void DbObjectDialog::disableSelection()
201 {
202     setButtonsEnabled(false);
203 }
204
205 void DbObjectDialog::activateNameDialog()
206 {
207     if (!nameDialog) return;
208     nameDialog->show();
209     nameDialog->raise();
210     nameDialog->activateWindow();
211 }
212
213 void DbObjectDialog::initDataTable()
214 {
215    objectList->setModel(dbManager->getDataModel());
216    objectList->setSelectionMode(QAbstractItemView::SingleSelection);
217    objectList->resizeColumnsToContents();
218 }
219
220 void DbObjectDialog::updateReject()
221 {
222     qDebug() << "Update rejected ... going to delete current object.";
223     // we don't want to keep this in memory
224     deleteCurrentObject();
225 }
226
227 void DbObjectDialog::updateData()
228 {
229     qDebug() << "Update accepted.";
230     // update data model
231     if (!dbObject) return;
232
233     qDebug() << "dbObject is not 0";
234
235     qDebug() << "We have a " + dbObject->getName();
236
237     qDebug() << "Data will be inserted/updated...";
238
239     // if data object id > -1 we are updating the data otherwise we are inserting new data
240     if (dbObject->getId() > -1) updateDb(dbObject);
241     else insertDb(dbObject);
242
243     // we don't need dbObject anymore
244     deleteCurrentObject();
245     dbObject = 0;
246     updateList();
247 }
248
249 void DbObjectDialog::deleteCurrentObject()
250 {
251     delete dbObject;
252 }
253
254 bool DbObjectDialog::confirmDelete(QString name, int numRefs)
255 {
256     int r = QMessageBox::warning(this, tr("Confirm delete"),
257                                  QString("Do you really want to delete %1 with %2 data bindings?")
258                                  .arg(name).arg(numRefs),
259                                  QMessageBox::Yes | QMessageBox::No);
260     if ( r == QMessageBox::No )
261         return false;
262     return true;
263 }