signals not connected for SetupEditDialog!
[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     connect(buttonBox, SIGNAL(accepted()), this, SLOT(close()));
58     connect(objectList, SIGNAL(clicked(const QModelIndex &)),
59         this, SLOT(listObjectClicked(const QModelIndex &)));
60     connect(editButton, SIGNAL(clicked()), this, SLOT(editButtonClicked()));
61     connect(addButton, SIGNAL(clicked()), this, SLOT(addButtonClicked()));
62     connect(deleteButton, SIGNAL(clicked()), this, SLOT(deleteButtonClicked()));
63     connect(nameDialog, SIGNAL(dataObjectUpdated()), this, SLOT(updateData()));
64     connect(nameDialog, SIGNAL(updateRejected()), this, SLOT(updateReject()));
65     connect(nameDialog, SIGNAL(test()), this, SLOT(testSlot()));
66 }
67     
68 void DbObjectDialog::testSlot()
69 {
70     qDebug() << "TEST SIGNAL RECEIVED!";
71 }
72
73 void DbObjectDialog::insertDb(const EmuFrontObject *ob) const
74 {
75     if (!dbManager->insertDataObjectToModel(ob))
76         errorMessage->showMessage(tr("Inserting data object %1 failed.").arg(ob->getName()));
77 }
78
79 void DbObjectDialog::addObject()
80 {
81     if (!nameDialog) initEditDialog();
82     deleteCurrentObject();
83     dbObject = createObject();
84     nameDialog->setDataObject(dbObject);
85     activateNameDialog();
86 }
87
88 void DbObjectDialog::editObject()
89 {
90     QModelIndex index = objectList->currentIndex();
91     if (!index.isValid())
92         return;
93     if (!nameDialog) initEditDialog();
94     deleteCurrentObject();
95     dbObject = dbManager->getDataObjectFromModel(&index);
96     activateNameDialog();
97     nameDialog->setDataObject(dbObject);
98 }
99
100 bool DbObjectDialog::deleteItem()
101 {
102     qDebug() << "deleteItem called";
103     QModelIndex index = objectList->currentIndex();
104     if (!index.isValid()) return false;
105     try
106     {
107         EmuFrontObject *ob = dbManager->getDataObjectFromModel(&index);
108
109         qDebug() << "Trying to delete " << ob->getName();
110
111         if (!ob)
112         {
113             errorMessage->showMessage(tr("Couldn't find the selected data object from data model!"));
114             return false;
115         }
116
117         int numBindings = dbManager->countDataObjectRefs(ob->getId());
118
119         if (numBindings > 0 && !confirmDelete(ob->getName(), numBindings))
120         { return false; }
121         deleteCurrentObject();
122
123         bool delOk = dbManager->deleteDataObject(ob->getId());
124         if (!delOk)
125         {
126             errorMessage->showMessage(tr("Deleting data object %1 failed!").arg(ob->getName()));
127             return false;
128         }
129         updateList();
130         objectList->setFocus();
131     }
132     catch(EmuFrontException e)
133     {
134         errorMessage->showMessage(e.what());
135     }
136     return false;
137 }
138
139 void DbObjectDialog::updateDb(const EmuFrontObject *ob) const
140 {
141     if (!ob) return;
142     if ( !dbManager->updateDataObjectToModel(ob) )
143     { errorMessage->showMessage("Database update failed!"); }
144 }
145
146 void DbObjectDialog::updateList() const
147 {
148     if (!dbManager) return;
149     dbManager->resetModel();
150 }
151
152 void DbObjectDialog::addButtonClicked()
153 {
154     disableSelection();
155     addObject();
156 }
157
158 void DbObjectDialog::editButtonClicked()
159 {
160     disableSelection();
161     editObject();
162 }
163
164 void DbObjectDialog::deleteButtonClicked()
165 {
166     QItemSelectionModel *selModel = objectList->selectionModel();
167     if (!selModel->hasSelection()) return;
168
169     QAbstractItemModel *tblModel = objectList->model();
170     QModelIndex index = selModel->currentIndex();
171     QVariant vName = tblModel->data(index);
172     QString name = vName.toString();
173     disableSelection();
174
175     QString msg =  tr("Do you want to delete") + name + "?";
176     int yn = QMessageBox::question(this, "Confirm", msg, QMessageBox::Yes | QMessageBox::No, QMessageBox::No);
177     if (yn == QMessageBox::Yes)
178     {
179         deleteItem();
180     }
181 }
182
183 void DbObjectDialog::layout()
184 {
185     QHBoxLayout *mainLayout = new QHBoxLayout;
186     mainLayout->addWidget(objectList);
187     mainLayout->addWidget(buttonBox);
188     setLayout(mainLayout);
189 }
190
191 void DbObjectDialog::listObjectClicked(const QModelIndex &index)
192 {
193     const QModelIndex *x;
194     x = &index;
195     qDebug() << "Row " << x->row() << ", column " << x->column() << " clicked.";
196     setButtonsEnabled(index.isValid());
197     if(!index.isValid()) 
198         return;
199 }
200
201 void DbObjectDialog::setButtonsEnabled(bool enabled)
202 {
203     editButton->setEnabled(enabled);
204     deleteButton->setEnabled(enabled);
205 }
206
207 void DbObjectDialog::disableSelection()
208 {
209     setButtonsEnabled(false);
210 }
211
212 void DbObjectDialog::activateNameDialog(bool updateData)
213 {
214     if (!nameDialog) return;
215     nameDialog->show();
216     nameDialog->raise();
217     if (updateData)
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     qDebug() << "DbObjectDialog::updateData()";
238     // update data model
239     if (!dbObject) return;
240
241
242     // if data object id > -1 we are updating the data otherwise we are inserting new data
243     if (dbObject->getId() > -1) updateDb(dbObject);
244     else insertDb(dbObject);
245
246     // we don't need dbObject anymore
247     deleteCurrentObject();
248     dbObject = 0;
249     updateList();
250 }
251
252 void DbObjectDialog::deleteCurrentObject()
253 {
254     delete dbObject;
255 }
256
257 bool DbObjectDialog::confirmDelete(QString name, int numRefs)
258 {
259     int r = QMessageBox::warning(this, tr("Confirm delete"),
260                                  QString("Do you really want to delete %1 with %2 data bindings?")
261                                  .arg(name).arg(numRefs),
262                                  QMessageBox::Yes | QMessageBox::No);
263     if ( r == QMessageBox::No )
264         return false;
265     return true;
266 }
267
268 void DbObjectDialog::refreshDataModel()
269 {
270     dbManager->resetModel();
271 }