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()));
68 void DbObjectDialog::insertDb(const EmuFrontObject *ob) const
70 dbManager->insertDataObjectToModel(ob);
73 void DbObjectDialog::addObject()
75 if (!nameDialog) return;
76 deleteCurrentObject();
77 dbObject = createObject();
78 nameDialog->setDataObject(dbObject);
82 void DbObjectDialog::editObject()
84 QModelIndex index = objectList->currentIndex();
87 deleteCurrentObject();
88 dbObject = dbManager->getDataObjectFromModel(&index);
89 nameDialog->setDataObject(dbObject);
93 bool DbObjectDialog::deleteItem()
95 QModelIndex index = objectList->currentIndex();
96 if (!index.isValid()) return false;
98 EmuFrontObject *ob = dbManager->getDataObjectFromModel(&index);
99 if (!ob) return false;
101 int numBindings = dbManager->countDataObjectRefs(ob->getId());
102 if (numBindings > 0 && !confirmDelete(ob->getName(), numBindings))
106 deleteCurrentObject();
107 bool delOk = dbManager->deleteDataObjectFromModel(&index);
110 qDebug() << "delete failed";
114 objectList->setFocus();
118 void DbObjectDialog::updateDb(const EmuFrontObject *ob) const
121 qDebug() << "Updating platform " << ob->getName();
122 dbManager->updateDataObjectToModel(ob);
125 void DbObjectDialog::updateList() const
127 if (!dbManager) return;
128 qDebug() << "Going to reset the data model";
129 dbManager->resetModel();
132 void DbObjectDialog::addButtonClicked()
138 void DbObjectDialog::editButtonClicked()
144 void DbObjectDialog::deleteButtonClicked()
146 QItemSelectionModel *selModel = objectList->selectionModel();
147 if (!selModel->hasSelection()) return;
149 QAbstractItemModel *tblModel = objectList->model();
150 QModelIndex index = selModel->currentIndex();
151 QVariant vName = tblModel->data(index);
152 QString name = vName.toString();
155 QString msg = tr("Do you want to delete") + name + "?";
156 int yn = QMessageBox::question(this, "Confirm", msg, QMessageBox::Yes | QMessageBox::No, QMessageBox::No);
157 if (yn == QMessageBox::Yes)
159 qDebug() << "Deleting item..." << name << ".";
164 void DbObjectDialog::layout()
166 QHBoxLayout *mainLayout = new QHBoxLayout;
167 mainLayout->addWidget(objectList);
168 mainLayout->addWidget(buttonBox);
169 setLayout(mainLayout);
172 void DbObjectDialog::listObjectClicked(const QModelIndex &index)
174 const QModelIndex *x;
176 qDebug() << "Row " << x->row() << ", column " << x->column() << " clicked.";
177 setButtonsEnabled(index.isValid());
182 void DbObjectDialog::setButtonsEnabled(bool enabled)
184 editButton->setEnabled(enabled);
185 deleteButton->setEnabled(enabled);
188 void DbObjectDialog::disableSelection()
190 setButtonsEnabled(false);
193 void DbObjectDialog::activateNameDialog()
195 if (!nameDialog) return;
198 nameDialog->activateWindow();
201 void DbObjectDialog::initDataTable()
203 objectList->setModel(dbManager->getDataModel());
204 objectList->setSelectionMode(QAbstractItemView::SingleSelection);
205 objectList->resizeColumnsToContents();
208 void DbObjectDialog::updateReject()
210 qDebug() << "Update rejected ... going to delete current object.";
211 // we don't want to keep this in memory
212 deleteCurrentObject();
215 void DbObjectDialog::updateData()
217 qDebug() << "Update accepted.";
219 if (!dbObject) return;
221 qDebug() << "dbObject is not 0";
223 qDebug() << "We have a " + dbObject->getName();
225 qDebug() << "Data will be inserted/updated...";
227 // if data object id > -1 we are updating the data otherwise we are inserting new data
228 if (dbObject->getId() > -1) updateDb(dbObject);
229 else insertDb(dbObject);
231 // we don't need dbObject anymore
232 deleteCurrentObject();
237 void DbObjectDialog::deleteCurrentObject()
242 bool DbObjectDialog::confirmDelete(QString name, int numRefs)
244 int r = QMessageBox::warning(this, tr("Confirm delete"),
245 QString("Do you really want to delete %1 with %2 data bindings?")
246 .arg(name).arg(numRefs),
247 QMessageBox::Yes | QMessageBox::No);
248 if ( r == QMessageBox::No )