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(close()));
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()));
65 connect(nameDialog, SIGNAL(dataObjectUpdated()), this, SLOT(updateData()));
66 connect(nameDialog, SIGNAL(updateRejected()), this, SLOT(updateReject()));
67 connect(nameDialog, SIGNAL(test()), this, SLOT(testSlot()));
68 connect(nameDialog, SIGNAL(dialogClosed()), this, SLOT(enableUi()));
71 void DbObjectDialog::testSlot()
73 qDebug() << "TEST SIGNAL RECEIVED!";
76 void DbObjectDialog::insertDb(const EmuFrontObject *ob) const
78 if (!dbManager->insertDataObjectToModel(ob))
79 errorMessage->showMessage(tr("Inserting data object %1 failed.").arg(ob->getName()));
82 void DbObjectDialog::addObject()
85 if (!nameDialog) initEditDialog();
86 deleteCurrentObject();
87 dbObject = createObject();
88 nameDialog->setDataObject(dbObject);
92 void DbObjectDialog::editObject()
94 QModelIndex index = objectList->currentIndex();
97 if (!nameDialog) initEditDialog();
98 deleteCurrentObject();
99 dbObject = dbManager->getDataObjectFromModel(&index);
100 activateNameDialog();
101 nameDialog->setDataObject(dbObject);
104 bool DbObjectDialog::deleteItem()
106 qDebug() << "deleteItem called";
107 QModelIndex index = objectList->currentIndex();
108 if (!index.isValid()) return false;
111 EmuFrontObject *ob = dbManager->getDataObjectFromModel(&index);
113 qDebug() << "Trying to delete " << ob->getName();
117 errorMessage->showMessage(tr("Couldn't find the selected data object from data model!"));
121 int numBindings = dbManager->countDataObjectRefs(ob->getId());
123 if (numBindings > 0 && !confirmDelete(ob->getName(), numBindings))
125 deleteCurrentObject();
127 bool delOk = dbManager->deleteDataObject(ob->getId());
130 errorMessage->showMessage(tr("Deleting data object %1 failed!").arg(ob->getName()));
134 objectList->setFocus();
137 catch(EmuFrontException e)
139 errorMessage->showMessage(e.what());
144 void DbObjectDialog::updateDb(const EmuFrontObject *ob) const
147 if ( !dbManager->updateDataObjectToModel(ob) )
148 { errorMessage->showMessage("Database update failed!"); }
151 void DbObjectDialog::updateList() const
153 if (!dbManager) return;
154 dbManager->resetModel();
157 void DbObjectDialog::addButtonClicked()
163 void DbObjectDialog::editButtonClicked()
169 void DbObjectDialog::deleteButtonClicked()
171 QItemSelectionModel *selModel = objectList->selectionModel();
172 if (!selModel->hasSelection()) return;
174 QAbstractItemModel *tblModel = objectList->model();
175 QModelIndex index = selModel->currentIndex();
176 QVariant vName = tblModel->data(index);
177 QString name = vName.toString();
180 QString msg = tr("Do you want to delete") + name + "?";
181 int yn = QMessageBox::question(this, "Confirm", msg, QMessageBox::Yes | QMessageBox::No, QMessageBox::No);
182 if (yn == QMessageBox::Yes)
188 void DbObjectDialog::layout()
190 QHBoxLayout *mainLayout = new QHBoxLayout;
191 mainLayout->addWidget(objectList);
192 mainLayout->addWidget(buttonBox);
193 setLayout(mainLayout);
196 void DbObjectDialog::listObjectClicked(const QModelIndex &index)
198 const QModelIndex *x;
200 qDebug() << "Row " << x->row() << ", column " << x->column() << " clicked.";
201 setButtonsEnabled(index.isValid());
206 void DbObjectDialog::enableUi()
211 void DbObjectDialog::setButtonsEnabled(bool enabled)
213 addButton->setEnabled(enabled);
214 editButton->setEnabled(enabled);
215 deleteButton->setEnabled(enabled);
218 void DbObjectDialog::setUIEnabled(bool enabled)
220 buttonBox->setEnabled(enabled);
221 objectList->setEnabled(enabled);
224 void DbObjectDialog::disableSelection()
227 //setButtonsEnabled(false);
230 void DbObjectDialog::activateNameDialog(bool updateData)
232 if (!nameDialog) return;
236 nameDialog->updateData();
237 nameDialog->activateWindow();
240 void DbObjectDialog::initDataTable()
242 objectList->setModel(dbManager->getDataModel());
243 objectList->setSelectionMode(QAbstractItemView::SingleSelection);
244 objectList->resizeColumnsToContents();
247 void DbObjectDialog::updateReject()
249 addButton->setEnabled(true);
251 // we don't want to keep this in memory
252 deleteCurrentObject();
255 void DbObjectDialog::updateData()
257 qDebug() << "DbObjectDialog::updateData()";
259 if (!dbObject) return;
262 // if data object id > -1 we are updating the data otherwise we are inserting new data
263 if (dbObject->getId() > -1) updateDb(dbObject);
264 else insertDb(dbObject);
266 // we don't need dbObject anymore
267 deleteCurrentObject();
273 /* Implementation specific delete must be used!
274 void DbObjectDialog::deleteCurrentObject()
279 bool DbObjectDialog::confirmDelete(QString name, int numRefs)
281 int r = QMessageBox::warning(this, tr("Confirm delete"),
282 QString("Do you really want to delete %1 with %2 data bindings?")
283 .arg(name).arg(numRefs),
284 QMessageBox::Yes | QMessageBox::No);
285 if ( r == QMessageBox::No )
290 void DbObjectDialog::refreshDataModel()
292 dbManager->resetModel();
295 void DbObjectDialog::hideColumns()
297 foreach(int c, hiddenColumns)
298 objectList->hideColumn(c);