3 ** Copyright 2010 Mikko Keinänen
5 ** This file is part of EmuFront.
8 ** EmuFront is free software: you can redistribute it and/or modify
9 ** it under the terms of the GNU General Public License version 2 as published by
10 ** the Free Software Foundation and appearing in the file gpl.txt included in the
11 ** packaging of this file.
13 ** EmuFront is distributed in the hope that it will be useful,
14 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
15 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 ** GNU General Public License for more details.
18 ** You should have received a copy of the GNU General Public License
19 ** along with EmuFront. If not, see <http://www.gnu.org/licenses/>.
22 #include <QSqlTableModel>
23 #include "dbobjectdialog.h"
24 #include "databasemanager.h"
26 DbObjectDialog::DbObjectDialog(QWidget *parent)
27 : EmuFrontDialog(parent)
32 editButton = new QPushButton(tr("&Edit"));
33 editButton->setEnabled(false);
34 addButton = new QPushButton(tr("&Add"));
35 deleteButton = new QPushButton(tr("&Delete"));
36 deleteButton->setEnabled(false);
37 objectList = new QTableView(this);
38 buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok, Qt::Vertical);
39 buttonBox->addButton(editButton, QDialogButtonBox::ActionRole);
40 buttonBox->addButton(addButton, QDialogButtonBox::ActionRole);
41 buttonBox->addButton(deleteButton, QDialogButtonBox::ActionRole);
42 // this be called from the implementing classes:
47 DbObjectDialog::~DbObjectDialog()
49 // no need to explicitly delete widgets within a parented layout
50 // they are automatically parented and will be deleted
51 // dbManager is also parented and will be implicitly deleted
52 // this must be deleted in an implementing class
54 // hiddenColumn QList will be deleted implicitly, since it
55 // implements implicit data sharing
58 void DbObjectDialog::connectSignals()
60 connect(buttonBox, SIGNAL(accepted()), this, SLOT(accept()));
61 connect(objectList, SIGNAL(clicked(const QModelIndex &)),
62 this, SLOT(listObjectClicked(const QModelIndex &)));
63 connect(editButton, SIGNAL(clicked()), this, SLOT(editButtonClicked()));
64 connect(addButton, SIGNAL(clicked()), this, SLOT(addButtonClicked()));
65 connect(deleteButton, SIGNAL(clicked()), this, SLOT(deleteButtonClicked()));
68 void DbObjectDialog::connectNameDialogSignals()
70 connect(nameDialog, SIGNAL(dataObjectUpdated()), this, SLOT(updateData()));
71 connect(nameDialog, SIGNAL(updateRejected()), this, SLOT(updateReject()));
72 connect(nameDialog, SIGNAL(test()), this, SLOT(testSlot()));
73 connect(nameDialog, SIGNAL(dialogClosed()), this, SLOT(enableUi()));
76 void DbObjectDialog::testSlot()
78 qDebug() << "TEST SIGNAL RECEIVED!";
81 void DbObjectDialog::insertDb(const EmuFrontObject *ob) const
83 if (!dbManager->insertDataObjectToModel(ob))
84 errorMessage->showMessage(tr("Inserting data object %1 failed.").arg(ob->getName()));
87 void DbObjectDialog::createEditDialog()
90 // call this from implementing classes:
91 //connectNameDialogSignals();
94 void DbObjectDialog::addObject()
100 deleteCurrentObject();
101 dbObject = createObject();
102 nameDialog->setDataObject(dbObject);
103 activateNameDialog();
106 void DbObjectDialog::editObject()
108 QModelIndex index = objectList->currentIndex();
109 if (!index.isValid())
114 deleteCurrentObject();
116 dbObject = dbManager->getDataObjectFromModel(&index); // throws EmuFrontException
117 } catch (EmuFrontException &e) {
118 errorMessage->showMessage(e.what());
121 activateNameDialog();
122 nameDialog->setDataObject(dbObject);
125 bool DbObjectDialog::deleteItem()
127 qDebug() << "deleteItem called";
128 QModelIndex index = objectList->currentIndex();
129 if (!index.isValid()) return false;
132 EmuFrontObject *ob = dbManager->getDataObjectFromModel(&index); // throws EmuFrontException
134 qDebug() << "Trying to delete " << ob->getName();
138 errorMessage->showMessage(tr("Couldn't find the selected data object from data model!"));
142 int numBindings = dbManager->countDataObjectRefs(ob->getId());
144 if (numBindings > 0 && !confirmDelete(ob->getName(), numBindings))
146 deleteCurrentObject();
148 bool delOk = dbManager->deleteDataObject(ob->getId());
151 errorMessage->showMessage(tr("Deleting data object %1 failed!").arg(ob->getName()));
155 objectList->setFocus();
158 catch(EmuFrontException e)
160 errorMessage->showMessage(e.what());
165 void DbObjectDialog::updateDb(const EmuFrontObject *ob) const
168 if ( !dbManager->updateDataObjectToModel(ob) )
169 { errorMessage->showMessage("Database update failed!"); }
172 void DbObjectDialog::updateList() const
174 if (!dbManager) return;
175 dbManager->resetModel();
176 objectList->resizeColumnsToContents();
179 void DbObjectDialog::addButtonClicked()
185 void DbObjectDialog::editButtonClicked()
191 void DbObjectDialog::deleteButtonClicked()
193 QItemSelectionModel *selModel = objectList->selectionModel();
194 if (!selModel->hasSelection()) return;
196 QAbstractItemModel *tblModel = objectList->model();
197 QModelIndex index = selModel->currentIndex();
198 QVariant vName = tblModel->data(index);
199 QString name = vName.toString();
202 QString msg = tr("Do you want to delete") + name + "?";
203 int yn = QMessageBox::question(this, "Confirm", msg, QMessageBox::Yes | QMessageBox::No, QMessageBox::No);
204 if (yn == QMessageBox::Yes)
210 void DbObjectDialog::layout()
212 QHBoxLayout *mainLayout = new QHBoxLayout;
213 mainLayout->addWidget(objectList);
214 mainLayout->addWidget(buttonBox);
215 setLayout(mainLayout);
218 void DbObjectDialog::listObjectClicked(const QModelIndex &index)
220 const QModelIndex *x;
222 qDebug() << "Row " << x->row() << ", column " << x->column() << " clicked.";
223 setButtonsEnabled(index.isValid());
228 void DbObjectDialog::enableUi()
233 void DbObjectDialog::setButtonsEnabled(bool enabled)
235 addButton->setEnabled(enabled);
236 editButton->setEnabled(enabled);
237 deleteButton->setEnabled(enabled);
240 void DbObjectDialog::setUIEnabled(bool enabled)
242 buttonBox->setEnabled(enabled);
243 objectList->setEnabled(enabled);
246 void DbObjectDialog::disableSelection()
249 //setButtonsEnabled(false);
252 void DbObjectDialog::activateNameDialog(bool updateData)
254 if (!nameDialog) return;
258 nameDialog->updateData();
259 nameDialog->activateWindow();
262 void DbObjectDialog::initDataTable()
264 objectList->setModel(dbManager->getDataModel());
265 objectList->setSelectionMode(QAbstractItemView::SingleSelection);
266 objectList->resizeColumnsToContents();
269 void DbObjectDialog::updateReject()
271 addButton->setEnabled(true);
273 // we don't want to keep this in memory
274 deleteCurrentObject();
277 void DbObjectDialog::updateData()
280 if (!dbObject) return;
282 // if data object id > -1 we are updating the data otherwise we are inserting new data
283 if (dbObject->getId() > -1) updateDb(dbObject);
284 else insertDb(dbObject);
286 // we don't need dbObject anymore
287 deleteCurrentObject();
293 /* Implementation specific delete must be used!
294 void DbObjectDialog::deleteCurrentObject()
299 bool DbObjectDialog::confirmDelete(QString name, int numRefs)
301 int r = QMessageBox::warning(this, tr("Confirm delete"),
302 QString("Do you really want to delete %1 with %2 data bindings?")
303 .arg(name).arg(numRefs),
304 QMessageBox::Yes | QMessageBox::No);
305 if ( r == QMessageBox::No )
310 void DbObjectDialog::refreshDataModel()
312 dbManager->resetModel();
315 void DbObjectDialog::hideColumns()
317 foreach(int c, hiddenColumns)
318 objectList->hideColumn(c);
321 /* Enables UI. Deletes nameDialog object and current data object */
322 void DbObjectDialog::closeEvent(QCloseEvent *ev)
324 qDebug() << "DbObjectDialog closing!";