Cleaning up
[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 as published by
9 // the Free Software Foundation, either version 3 of the License, or
10 // (at your option) any later version.
11 //
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.
16 //
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/>.
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         qDebug() << "Db insert ok";
78     else qDebug() << "Db insert failed";
79 }
80
81 void DbObjectDialog::addObject()
82 {
83     if (!nameDialog) initEditDialog();
84     deleteCurrentObject();
85     dbObject = createObject();
86     nameDialog->setDataObject(dbObject);
87     activateNameDialog();
88 }
89
90 void DbObjectDialog::editObject()
91 {
92     QModelIndex index = objectList->currentIndex();
93     if (!index.isValid())
94         return;
95     if (!nameDialog) initEditDialog();
96     deleteCurrentObject();
97     dbObject = dbManager->getDataObjectFromModel(&index);
98     nameDialog->setDataObject(dbObject);
99     activateNameDialog();
100 }
101
102 bool DbObjectDialog::deleteItem()
103 {
104     QModelIndex index = objectList->currentIndex();
105     if (!index.isValid()) return false;
106     try
107     {
108         EmuFrontObject *ob = dbManager->getDataObjectFromModel(&index);
109
110         if (!ob)
111         {
112             errorMessage->showMessage(tr("Couldn't find the selected data object from data model!"));
113             return false;
114         }
115
116         int numBindings = dbManager->countDataObjectRefs(ob->getId());
117
118         if (numBindings > 0 && !confirmDelete(ob->getName(), numBindings))
119         { return false; }
120         deleteCurrentObject();
121
122         bool delOk = dbManager->deleteDataObjectFromModel(&index);
123         if (!delOk)
124         {
125             errorMessage->showMessage(tr("Deleting data object %1 failed!").arg(ob->getName()));
126             return false;
127         }
128         updateList();
129         objectList->setFocus();
130     }
131     catch(EmuFrontException e)
132     {
133         errorMessage->showMessage(e.what());
134     }
135     return false;
136 }
137
138 void DbObjectDialog::updateDb(const EmuFrontObject *ob) const
139 {
140     if (!ob) return;
141     if ( !dbManager->updateDataObjectToModel(ob) )
142     { errorMessage->showMessage("Database update failed!"); }
143 }
144
145 void DbObjectDialog::updateList() const
146 {
147     if (!dbManager) return;
148     dbManager->resetModel();
149 }
150
151 void DbObjectDialog::addButtonClicked()
152 {
153     disableSelection();
154     addObject();
155 }
156
157 void DbObjectDialog::editButtonClicked()
158 {
159     disableSelection();
160     editObject();
161 }
162
163 void DbObjectDialog::deleteButtonClicked()
164 {
165     QItemSelectionModel *selModel = objectList->selectionModel();
166     if (!selModel->hasSelection()) return;
167
168     QAbstractItemModel *tblModel = objectList->model();
169     QModelIndex index = selModel->currentIndex();
170     QVariant vName = tblModel->data(index);
171     QString name = vName.toString();
172     disableSelection();
173
174     QString msg =  tr("Do you want to delete") + name + "?";
175     int yn = QMessageBox::question(this, "Confirm", msg, QMessageBox::Yes | QMessageBox::No, QMessageBox::No);
176     if (yn == QMessageBox::Yes)
177     {
178         deleteItem();
179     }
180 }
181
182 void DbObjectDialog::layout()
183 {
184     QHBoxLayout *mainLayout = new QHBoxLayout;
185     mainLayout->addWidget(objectList);
186     mainLayout->addWidget(buttonBox);
187     setLayout(mainLayout);
188 }
189
190 void DbObjectDialog::listObjectClicked(const QModelIndex &index)
191 {
192     const QModelIndex *x;
193     x = &index;
194     qDebug() << "Row " << x->row() << ", column " << x->column() << " clicked.";
195     setButtonsEnabled(index.isValid());
196     if(!index.isValid()) 
197         return;
198 }
199
200 void DbObjectDialog::setButtonsEnabled(bool enabled)
201 {
202     editButton->setEnabled(enabled);
203     deleteButton->setEnabled(enabled);
204 }
205
206 void DbObjectDialog::disableSelection()
207 {
208     setButtonsEnabled(false);
209 }
210
211 void DbObjectDialog::activateNameDialog()
212 {
213     if (!nameDialog) return;
214     nameDialog->show();
215     nameDialog->raise();
216     nameDialog->activateWindow();
217 }
218
219 void DbObjectDialog::initDataTable()
220 {
221    objectList->setModel(dbManager->getDataModel());
222    objectList->setSelectionMode(QAbstractItemView::SingleSelection);
223    objectList->resizeColumnsToContents();
224 }
225
226 void DbObjectDialog::updateReject()
227 {
228     qDebug() << "Update rejected ... going to delete current object.";
229     // we don't want to keep this in memory
230     deleteCurrentObject();
231 }
232
233 void DbObjectDialog::updateData()
234 {
235     qDebug() << "Update accepted.";
236     // update data model
237     if (!dbObject) return;
238
239     qDebug() << "dbObject is not 0";
240
241     qDebug() << "We have a " + dbObject->getName();
242
243     qDebug() << "Data will be inserted/updated...";
244
245     // if data object id > -1 we are updating the data otherwise we are inserting new data
246     if (dbObject->getId() > -1) updateDb(dbObject);
247     else insertDb(dbObject);
248
249     // we don't need dbObject anymore
250     deleteCurrentObject();
251     dbObject = 0;
252     updateList();
253 }
254
255 void DbObjectDialog::deleteCurrentObject()
256 {
257     delete dbObject;
258 }
259
260 bool DbObjectDialog::confirmDelete(QString name, int numRefs)
261 {
262     int r = QMessageBox::warning(this, tr("Confirm delete"),
263                                  QString("Do you really want to delete %1 with %2 data bindings?")
264                                  .arg(name).arg(numRefs),
265                                  QMessageBox::Yes | QMessageBox::No);
266     if ( r == QMessageBox::No )
267         return false;
268     return true;
269 }