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 version 2 as published by
9 // the Free Software Foundation and appearing in the file gpl.txt included in the
10 // packaging of this file.
12 // EmuFront 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 EmuFront. 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 explicitly delete widgets within a parented layout
49 // they are automatically parented and will be deleted
50 // dbManager is also parented and will be implicitly deleted
51 // this must be deleted in an implementing class
53 // hiddenColumn QList will be deleted implicitly, since it
54 // implements implicit data sharing
57 void DbObjectDialog::connectSignals()
59 connect(buttonBox, SIGNAL(accepted()), this, SLOT(accept()));
60 connect(objectList, SIGNAL(clicked(const QModelIndex &)),
61 this, SLOT(listObjectClicked(const QModelIndex &)));
62 connect(editButton, SIGNAL(clicked()), this, SLOT(editButtonClicked()));
63 connect(addButton, SIGNAL(clicked()), this, SLOT(addButtonClicked()));
64 connect(deleteButton, SIGNAL(clicked()), this, SLOT(deleteButtonClicked()));
67 void DbObjectDialog::connectNameDialogSignals()
69 connect(nameDialog, SIGNAL(dataObjectUpdated()), this, SLOT(updateData()));
70 connect(nameDialog, SIGNAL(updateRejected()), this, SLOT(updateReject()));
71 connect(nameDialog, SIGNAL(test()), this, SLOT(testSlot()));
72 connect(nameDialog, SIGNAL(dialogClosed()), this, SLOT(enableUi()));
75 void DbObjectDialog::testSlot()
77 qDebug() << "TEST SIGNAL RECEIVED!";
80 void DbObjectDialog::insertDb(const EmuFrontObject *ob) const
82 if (!dbManager->insertDataObjectToModel(ob))
83 errorMessage->showMessage(tr("Inserting data object %1 failed.").arg(ob->getName()));
86 void DbObjectDialog::createEditDialog()
89 // call this from implementing classes:
90 //connectNameDialogSignals();
93 void DbObjectDialog::addObject()
99 deleteCurrentObject();
100 dbObject = createObject();
101 nameDialog->setDataObject(dbObject);
102 activateNameDialog();
105 void DbObjectDialog::editObject()
107 QModelIndex index = objectList->currentIndex();
108 if (!index.isValid())
113 deleteCurrentObject();
115 dbObject = dbManager->getDataObjectFromModel(&index); // throws EmuFrontException
116 } catch (EmuFrontException &e) {
117 errorMessage->showMessage(e.what());
120 activateNameDialog();
121 nameDialog->setDataObject(dbObject);
124 bool DbObjectDialog::deleteItem()
126 qDebug() << "deleteItem called";
127 QModelIndex index = objectList->currentIndex();
128 if (!index.isValid()) return false;
131 EmuFrontObject *ob = dbManager->getDataObjectFromModel(&index); // throws EmuFrontException
133 qDebug() << "Trying to delete " << ob->getName();
137 errorMessage->showMessage(tr("Couldn't find the selected data object from data model!"));
141 int numBindings = dbManager->countDataObjectRefs(ob->getId());
143 if (numBindings > 0 && !confirmDelete(ob->getName(), numBindings))
145 deleteCurrentObject();
147 bool delOk = dbManager->deleteDataObject(ob->getId());
150 errorMessage->showMessage(tr("Deleting data object %1 failed!").arg(ob->getName()));
154 objectList->setFocus();
157 catch(EmuFrontException e)
159 errorMessage->showMessage(e.what());
164 void DbObjectDialog::updateDb(const EmuFrontObject *ob) const
167 if ( !dbManager->updateDataObjectToModel(ob) )
168 { errorMessage->showMessage("Database update failed!"); }
171 void DbObjectDialog::updateList() const
173 if (!dbManager) return;
174 dbManager->resetModel();
175 objectList->resizeColumnsToContents();
178 void DbObjectDialog::addButtonClicked()
184 void DbObjectDialog::editButtonClicked()
190 void DbObjectDialog::deleteButtonClicked()
192 QItemSelectionModel *selModel = objectList->selectionModel();
193 if (!selModel->hasSelection()) return;
195 QAbstractItemModel *tblModel = objectList->model();
196 QModelIndex index = selModel->currentIndex();
197 QVariant vName = tblModel->data(index);
198 QString name = vName.toString();
201 QString msg = tr("Do you want to delete") + name + "?";
202 int yn = QMessageBox::question(this, "Confirm", msg, QMessageBox::Yes | QMessageBox::No, QMessageBox::No);
203 if (yn == QMessageBox::Yes)
209 void DbObjectDialog::layout()
211 QHBoxLayout *mainLayout = new QHBoxLayout;
212 mainLayout->addWidget(objectList);
213 mainLayout->addWidget(buttonBox);
214 setLayout(mainLayout);
217 void DbObjectDialog::listObjectClicked(const QModelIndex &index)
219 const QModelIndex *x;
221 qDebug() << "Row " << x->row() << ", column " << x->column() << " clicked.";
222 setButtonsEnabled(index.isValid());
227 void DbObjectDialog::enableUi()
232 void DbObjectDialog::setButtonsEnabled(bool enabled)
234 addButton->setEnabled(enabled);
235 editButton->setEnabled(enabled);
236 deleteButton->setEnabled(enabled);
239 void DbObjectDialog::setUIEnabled(bool enabled)
241 buttonBox->setEnabled(enabled);
242 objectList->setEnabled(enabled);
245 void DbObjectDialog::disableSelection()
248 //setButtonsEnabled(false);
251 void DbObjectDialog::activateNameDialog(bool updateData)
253 if (!nameDialog) return;
257 nameDialog->updateData();
258 nameDialog->activateWindow();
261 void DbObjectDialog::initDataTable()
263 objectList->setModel(dbManager->getDataModel());
264 objectList->setSelectionMode(QAbstractItemView::SingleSelection);
265 objectList->resizeColumnsToContents();
268 void DbObjectDialog::updateReject()
270 addButton->setEnabled(true);
272 // we don't want to keep this in memory
273 deleteCurrentObject();
276 void DbObjectDialog::updateData()
279 if (!dbObject) return;
281 // if data object id > -1 we are updating the data otherwise we are inserting new data
282 if (dbObject->getId() > -1) updateDb(dbObject);
283 else insertDb(dbObject);
285 // we don't need dbObject anymore
286 deleteCurrentObject();
292 /* Implementation specific delete must be used!
293 void DbObjectDialog::deleteCurrentObject()
298 bool DbObjectDialog::confirmDelete(QString name, int numRefs)
300 int r = QMessageBox::warning(this, tr("Confirm delete"),
301 QString("Do you really want to delete %1 with %2 data bindings?")
302 .arg(name).arg(numRefs),
303 QMessageBox::Yes | QMessageBox::No);
304 if ( r == QMessageBox::No )
309 void DbObjectDialog::refreshDataModel()
311 dbManager->resetModel();
314 void DbObjectDialog::hideColumns()
316 foreach(int c, hiddenColumns)
317 objectList->hideColumn(c);
320 /* Enables UI. Deletes nameDialog object and current data object */
321 void DbObjectDialog::closeEvent(QCloseEvent *ev)
323 qDebug() << "DbObjectDialog closing!";