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)
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:
45 DbObjectDialog::~DbObjectDialog()
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
54 void DbObjectDialog::connectSignals()
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()));
66 void DbObjectDialog::insertDb(const EmuFrontObject *ob) const
68 dbManager->insertDataObjectToModel(ob);
71 void DbObjectDialog::addObject()
73 deleteCurrentObject();
74 dbObject = createObject();
75 nameDialog->setDataObject(dbObject);
79 void DbObjectDialog::editObject()
81 QModelIndex index = objectList->currentIndex();
84 deleteCurrentObject();
85 dbObject = dbManager->getDataObjectFromModel(&index);
86 nameDialog->setDataObject(dbObject);
90 bool DbObjectDialog::deleteItem()
92 QModelIndex index = objectList->currentIndex();
93 if (!index.isValid()) return false;
95 EmuFrontObject *ob = dbManager->getDataObjectFromModel(&index);
96 if (!ob) return false;
98 int numBindings = dbManager->countDataObjectRefs(ob->getId());
99 if (numBindings > 0 && !confirmDelete(ob->getName(), numBindings))
103 deleteCurrentObject();
104 bool delOk = dbManager->deleteDataObjectFromModel(&index);
107 qDebug() << "delete failed";
111 objectList->setFocus();
115 void DbObjectDialog::updateDb(const EmuFrontObject *ob) const
118 qDebug() << "Updating platform " << ob->getName();
119 dbManager->updateDataObjectToModel(ob);
122 void DbObjectDialog::updateList() const
124 if (!dbManager) return;
125 dbManager->resetModel();
128 void DbObjectDialog::addButtonClicked()
134 void DbObjectDialog::editButtonClicked()
140 void DbObjectDialog::deleteButtonClicked()
142 QItemSelectionModel *selModel = objectList->selectionModel();
143 if (!selModel->hasSelection()) return;
145 QAbstractItemModel *tblModel = objectList->model();
146 QModelIndex index = selModel->currentIndex();
147 QVariant vName = tblModel->data(index);
148 QString name = vName.toString();
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)
155 qDebug() << "Deleting item..." << name << ".";
160 void DbObjectDialog::layout()
162 QHBoxLayout *mainLayout = new QHBoxLayout;
163 mainLayout->addWidget(objectList);
164 mainLayout->addWidget(buttonBox);
165 setLayout(mainLayout);
168 void DbObjectDialog::listObjectClicked(const QModelIndex &index)
170 const QModelIndex *x;
172 qDebug() << "Row " << x->row() << ", column " << x->column() << " clicked.";
173 setButtonsEnabled(index.isValid());
178 void DbObjectDialog::setButtonsEnabled(bool enabled)
180 editButton->setEnabled(enabled);
181 deleteButton->setEnabled(enabled);
184 void DbObjectDialog::disableSelection()
186 setButtonsEnabled(false);
189 void DbObjectDialog::activateNameDialog()
191 if (!nameDialog) return;
194 nameDialog->activateWindow();
197 void DbObjectDialog::initDataTable()
199 objectList->setModel(dbManager->getDataModel());
200 objectList->setSelectionMode(QAbstractItemView::SingleSelection);
201 objectList->resizeColumnsToContents();
204 void DbObjectDialog::updateReject()
206 qDebug() << "Update rejected ... going to delete current object.";
207 // we don't want to keep this in memory
208 deleteCurrentObject();
211 void DbObjectDialog::updateData()
213 qDebug() << "Update accepted.";
215 if (!dbObject) return;
217 qDebug() << "dbObject is not 0";
219 qDebug() << "We have a " + dbObject->getName();
221 qDebug() << "Data will be inserted/updated...";
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);
227 // we don't need dbObject anymore
228 deleteCurrentObject();
233 void DbObjectDialog::deleteCurrentObject()
238 bool DbObjectDialog::confirmDelete(QString name, int numRefs)
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 )