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