2 // Copyright 2010 Mikko Keinänen
4 // This file is part of EmuFront.
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.
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.
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/>.
21 #include <QSqlTableModel>
22 #include "dbobjectdialog.h"
23 #include "../db/databasemanager.h"
25 DbObjectDialog::DbObjectDialog(QWidget *parent)
26 : EmuFrontDialog(parent)
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:
46 DbObjectDialog::~DbObjectDialog()
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
55 void DbObjectDialog::connectSignals()
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()));
69 void DbObjectDialog::testSlot()
71 qDebug() << "TEST SIGNAL RECEIVED!";
74 void DbObjectDialog::insertDb(const EmuFrontObject *ob) const
76 if ( dbManager->insertDataObjectToModel(ob) )
77 qDebug() << "Db insert ok";
78 else qDebug() << "Db insert failed";
81 void DbObjectDialog::addObject()
83 if (!nameDialog) initEditDialog();
84 deleteCurrentObject();
85 dbObject = createObject();
86 nameDialog->setDataObject(dbObject);
90 void DbObjectDialog::editObject()
92 QModelIndex index = objectList->currentIndex();
95 if (!nameDialog) initEditDialog();
96 deleteCurrentObject();
97 dbObject = dbManager->getDataObjectFromModel(&index);
98 nameDialog->setDataObject(dbObject);
102 bool DbObjectDialog::deleteItem()
104 QModelIndex index = objectList->currentIndex();
105 if (!index.isValid()) return false;
107 EmuFrontObject *ob = dbManager->getDataObjectFromModel(&index);
108 if (!ob) return false;
110 int numBindings = dbManager->countDataObjectRefs(ob->getId());
111 if (numBindings > 0 && !confirmDelete(ob->getName(), numBindings))
115 deleteCurrentObject();
116 bool delOk = dbManager->deleteDataObjectFromModel(&index);
119 qDebug() << "delete failed";
123 objectList->setFocus();
127 void DbObjectDialog::updateDb(const EmuFrontObject *ob) const
130 qDebug() << "Updating platform " << ob->getName();
131 if ( dbManager->updateDataObjectToModel(ob) )
132 { qDebug() << "Db update ok!"; }
133 else qDebug() << "Db update failed";
137 void DbObjectDialog::updateList() const
139 if (!dbManager) return;
140 qDebug() << "Going to reset the data model";
141 dbManager->resetModel();
144 void DbObjectDialog::addButtonClicked()
150 void DbObjectDialog::editButtonClicked()
156 void DbObjectDialog::deleteButtonClicked()
158 QItemSelectionModel *selModel = objectList->selectionModel();
159 if (!selModel->hasSelection()) return;
161 QAbstractItemModel *tblModel = objectList->model();
162 QModelIndex index = selModel->currentIndex();
163 QVariant vName = tblModel->data(index);
164 QString name = vName.toString();
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)
171 qDebug() << "Deleting item..." << name << ".";
176 void DbObjectDialog::layout()
178 QHBoxLayout *mainLayout = new QHBoxLayout;
179 mainLayout->addWidget(objectList);
180 mainLayout->addWidget(buttonBox);
181 setLayout(mainLayout);
184 void DbObjectDialog::listObjectClicked(const QModelIndex &index)
186 const QModelIndex *x;
188 qDebug() << "Row " << x->row() << ", column " << x->column() << " clicked.";
189 setButtonsEnabled(index.isValid());
194 void DbObjectDialog::setButtonsEnabled(bool enabled)
196 editButton->setEnabled(enabled);
197 deleteButton->setEnabled(enabled);
200 void DbObjectDialog::disableSelection()
202 setButtonsEnabled(false);
205 void DbObjectDialog::activateNameDialog()
207 if (!nameDialog) return;
210 nameDialog->activateWindow();
213 void DbObjectDialog::initDataTable()
215 objectList->setModel(dbManager->getDataModel());
216 objectList->setSelectionMode(QAbstractItemView::SingleSelection);
217 objectList->resizeColumnsToContents();
220 void DbObjectDialog::updateReject()
222 qDebug() << "Update rejected ... going to delete current object.";
223 // we don't want to keep this in memory
224 deleteCurrentObject();
227 void DbObjectDialog::updateData()
229 qDebug() << "Update accepted.";
231 if (!dbObject) return;
233 qDebug() << "dbObject is not 0";
235 qDebug() << "We have a " + dbObject->getName();
237 qDebug() << "Data will be inserted/updated...";
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);
243 // we don't need dbObject anymore
244 deleteCurrentObject();
249 void DbObjectDialog::deleteCurrentObject()
254 bool DbObjectDialog::confirmDelete(QString name, int numRefs)
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 )