Data object removal implemented in the db layer. UI refreshing added
[emufront] / src / dialogs / dbobjectdialog.cpp
1 // EmuFront
2 // Copyright 2010 Mikko Keinänen
3 //
4 // This file is part of EmuFront.
5 //
6 //
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.
11 //
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.
16 //
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/>.
19
20 #include <QtGui>
21 #include <QSqlTableModel>
22 #include "dbobjectdialog.h"
23 #include "../db/databasemanager.h"
24
25 DbObjectDialog::DbObjectDialog(QWidget *parent)
26     : EmuFrontDialog(parent)
27 {
28     dbObject = 0;
29     dbManager = 0;
30     nameDialog = 0;
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:
42     //connectSignals();
43     layout();
44
45
46 DbObjectDialog::~DbObjectDialog()
47 {
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
52     //delete dbObject;
53 }
54
55 void DbObjectDialog::connectSignals()
56 {
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()));
66     connect(nameDialog, SIGNAL(test()), this, SLOT(testSlot()));
67 }
68     
69 void DbObjectDialog::testSlot()
70 {
71     qDebug() << "TEST SIGNAL RECEIVED!";
72 }
73
74 void DbObjectDialog::insertDb(const EmuFrontObject *ob) const
75 {
76     if (!dbManager->insertDataObjectToModel(ob))
77         errorMessage->showMessage(tr("Inserting data object %1 failed.").arg(ob->getName()));
78 }
79
80 void DbObjectDialog::addObject()
81 {
82     if (!nameDialog) initEditDialog();
83     deleteCurrentObject();
84     dbObject = createObject();
85     nameDialog->setDataObject(dbObject);
86     activateNameDialog();
87 }
88
89 void DbObjectDialog::editObject()
90 {
91     QModelIndex index = objectList->currentIndex();
92     if (!index.isValid())
93         return;
94     if (!nameDialog) initEditDialog();
95     deleteCurrentObject();
96     dbObject = dbManager->getDataObjectFromModel(&index);
97     nameDialog->setDataObject(dbObject);
98     activateNameDialog();
99 }
100
101 bool DbObjectDialog::deleteItem()
102 {
103     qDebug() << "deleteItem called";
104     QModelIndex index = objectList->currentIndex();
105     if (!index.isValid()) return false;
106     try
107     {
108         EmuFrontObject *ob = dbManager->getDataObjectFromModel(&index);
109
110         qDebug() << "Trying to delete " << ob->getName();
111
112         if (!ob)
113         {
114             errorMessage->showMessage(tr("Couldn't find the selected data object from data model!"));
115             return false;
116         }
117
118         int numBindings = dbManager->countDataObjectRefs(ob->getId());
119
120         if (numBindings > 0 && !confirmDelete(ob->getName(), numBindings))
121         { return false; }
122         deleteCurrentObject();
123
124         bool delOk = dbManager->deleteDataObject(ob->getId());
125         if (!delOk)
126         {
127             errorMessage->showMessage(tr("Deleting data object %1 failed!").arg(ob->getName()));
128             return false;
129         }
130         updateList();
131         objectList->setFocus();
132     }
133     catch(EmuFrontException e)
134     {
135         errorMessage->showMessage(e.what());
136     }
137     return false;
138 }
139
140 void DbObjectDialog::updateDb(const EmuFrontObject *ob) const
141 {
142     if (!ob) return;
143     if ( !dbManager->updateDataObjectToModel(ob) )
144     { errorMessage->showMessage("Database update failed!"); }
145 }
146
147 void DbObjectDialog::updateList() const
148 {
149     if (!dbManager) return;
150     dbManager->resetModel();
151 }
152
153 void DbObjectDialog::addButtonClicked()
154 {
155     disableSelection();
156     addObject();
157 }
158
159 void DbObjectDialog::editButtonClicked()
160 {
161     disableSelection();
162     editObject();
163 }
164
165 void DbObjectDialog::deleteButtonClicked()
166 {
167     QItemSelectionModel *selModel = objectList->selectionModel();
168     if (!selModel->hasSelection()) return;
169
170     QAbstractItemModel *tblModel = objectList->model();
171     QModelIndex index = selModel->currentIndex();
172     QVariant vName = tblModel->data(index);
173     QString name = vName.toString();
174     disableSelection();
175
176     QString msg =  tr("Do you want to delete") + name + "?";
177     int yn = QMessageBox::question(this, "Confirm", msg, QMessageBox::Yes | QMessageBox::No, QMessageBox::No);
178     if (yn == QMessageBox::Yes)
179     {
180         deleteItem();
181     }
182 }
183
184 void DbObjectDialog::layout()
185 {
186     QHBoxLayout *mainLayout = new QHBoxLayout;
187     mainLayout->addWidget(objectList);
188     mainLayout->addWidget(buttonBox);
189     setLayout(mainLayout);
190 }
191
192 void DbObjectDialog::listObjectClicked(const QModelIndex &index)
193 {
194     const QModelIndex *x;
195     x = &index;
196     qDebug() << "Row " << x->row() << ", column " << x->column() << " clicked.";
197     setButtonsEnabled(index.isValid());
198     if(!index.isValid()) 
199         return;
200 }
201
202 void DbObjectDialog::setButtonsEnabled(bool enabled)
203 {
204     editButton->setEnabled(enabled);
205     deleteButton->setEnabled(enabled);
206 }
207
208 void DbObjectDialog::disableSelection()
209 {
210     setButtonsEnabled(false);
211 }
212
213 void DbObjectDialog::activateNameDialog()
214 {
215     if (!nameDialog) return;
216     nameDialog->show();
217     nameDialog->raise();
218     nameDialog->updateData();
219     nameDialog->activateWindow();
220 }
221
222 void DbObjectDialog::initDataTable()
223 {
224    objectList->setModel(dbManager->getDataModel());
225    objectList->setSelectionMode(QAbstractItemView::SingleSelection);
226    objectList->resizeColumnsToContents();
227 }
228
229 void DbObjectDialog::updateReject()
230 {
231     // we don't want to keep this in memory
232     deleteCurrentObject();
233 }
234
235 void DbObjectDialog::updateData()
236 {
237     // update data model
238     if (!dbObject) return;
239
240     // if data object id > -1 we are updating the data otherwise we are inserting new data
241     if (dbObject->getId() > -1) updateDb(dbObject);
242     else insertDb(dbObject);
243
244     // we don't need dbObject anymore
245     deleteCurrentObject();
246     dbObject = 0;
247     updateList();
248 }
249
250 void DbObjectDialog::deleteCurrentObject()
251 {
252     delete dbObject;
253 }
254
255 bool DbObjectDialog::confirmDelete(QString name, int numRefs)
256 {
257     int r = QMessageBox::warning(this, tr("Confirm delete"),
258                                  QString("Do you really want to delete %1 with %2 data bindings?")
259                                  .arg(name).arg(numRefs),
260                                  QMessageBox::Yes | QMessageBox::No);
261     if ( r == QMessageBox::No )
262         return false;
263     return true;
264 }
265
266 void DbObjectDialog::refreshDataModel()
267 {
268     dbManager->resetModel();
269 }