aa57aa11206273065b4af688b7dc9778a68a3f98
[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 explicitly delete widgets within a parented layout
49     // they are automatically parented and will be deleted
50     // dbManager is also parented and will be implicitly deleted
51     // this must be deleted in an implementing class
52     //delete dbObject;
53     // hiddenColumn QList will be deleted implicitly, since it
54     // implements implicit data sharing
55 }
56
57 void DbObjectDialog::connectSignals()
58 {
59     connect(buttonBox, SIGNAL(accepted()), this, SLOT(accept()));
60     connect(objectList, SIGNAL(clicked(const QModelIndex &)),
61         this, SLOT(listObjectClicked(const QModelIndex &)));
62     connect(editButton, SIGNAL(clicked()), this, SLOT(editButtonClicked()));
63     connect(addButton, SIGNAL(clicked()), this, SLOT(addButtonClicked()));
64     connect(deleteButton, SIGNAL(clicked()), this, SLOT(deleteButtonClicked()));
65 }
66
67 void DbObjectDialog::connectNameDialogSignals()
68 {
69     connect(nameDialog, SIGNAL(dataObjectUpdated()), this, SLOT(updateData()));
70     connect(nameDialog, SIGNAL(updateRejected()), this, SLOT(updateReject()));
71     connect(nameDialog, SIGNAL(test()), this, SLOT(testSlot()));
72     connect(nameDialog, SIGNAL(dialogClosed()), this, SLOT(enableUi()));
73 }
74     
75 void DbObjectDialog::testSlot()
76 {
77     qDebug() << "TEST SIGNAL RECEIVED!";
78 }
79
80 void DbObjectDialog::insertDb(const EmuFrontObject *ob) const
81 {
82     if (!dbManager->insertDataObjectToModel(ob))
83         errorMessage->showMessage(tr("Inserting data object %1 failed.").arg(ob->getName()));
84 }
85
86 void DbObjectDialog::createEditDialog()
87 {
88     initEditDialog();
89     // call this from implementing classes:
90     //connectNameDialogSignals();
91 }
92
93 void DbObjectDialog::addObject()
94 {
95     setUIEnabled(false);
96     if (!nameDialog) {
97        createEditDialog();
98     }
99     deleteCurrentObject();
100     dbObject = createObject();
101     nameDialog->setDataObject(dbObject);
102     activateNameDialog();
103 }
104
105 void DbObjectDialog::editObject()
106 {
107     QModelIndex index = objectList->currentIndex();
108     if (!index.isValid())
109         return;
110     if (!nameDialog) {
111         createEditDialog();
112     }
113     deleteCurrentObject();
114     try {
115         dbObject = dbManager->getDataObjectFromModel(&index); // throws EmuFrontException
116     } catch (EmuFrontException &e) {
117         errorMessage->showMessage(e.what());
118         return;
119     }
120     activateNameDialog();
121     nameDialog->setDataObject(dbObject);
122 }
123
124 bool DbObjectDialog::deleteItem()
125 {
126     qDebug() << "deleteItem called";
127     QModelIndex index = objectList->currentIndex();
128     if (!index.isValid()) return false;
129     try
130     {
131         EmuFrontObject *ob = dbManager->getDataObjectFromModel(&index); // throws EmuFrontException
132
133         qDebug() << "Trying to delete " << ob->getName();
134
135         if (!ob)
136         {
137             errorMessage->showMessage(tr("Couldn't find the selected data object from data model!"));
138             return false;
139         }
140
141         int numBindings = dbManager->countDataObjectRefs(ob->getId());
142
143         if (numBindings > 0 && !confirmDelete(ob->getName(), numBindings))
144         { return false; }
145         deleteCurrentObject();
146
147         bool delOk = dbManager->deleteDataObject(ob->getId());
148         if (!delOk)
149         {
150             errorMessage->showMessage(tr("Deleting data object %1 failed!").arg(ob->getName()));
151             return false;
152         }
153         updateList();
154         objectList->setFocus();
155         setUIEnabled(true);
156     }
157     catch(EmuFrontException e)
158     {
159         errorMessage->showMessage(e.what());
160     }
161     return false;
162 }
163
164 void DbObjectDialog::updateDb(const EmuFrontObject *ob) const
165 {
166     if (!ob) return;
167     if ( !dbManager->updateDataObjectToModel(ob) )
168     { errorMessage->showMessage("Database update failed!"); }
169 }
170
171 void DbObjectDialog::updateList() const
172 {
173     if (!dbManager) return;
174     dbManager->resetModel();
175     objectList->resizeColumnsToContents();
176 }
177
178 void DbObjectDialog::addButtonClicked()
179 {
180     disableSelection();
181     addObject();
182 }
183
184 void DbObjectDialog::editButtonClicked()
185 {
186     disableSelection();
187     editObject();
188 }
189
190 void DbObjectDialog::deleteButtonClicked()
191 {
192     QItemSelectionModel *selModel = objectList->selectionModel();
193     if (!selModel->hasSelection()) return;
194
195     QAbstractItemModel *tblModel = objectList->model();
196     QModelIndex index = selModel->currentIndex();
197     QVariant vName = tblModel->data(index);
198     QString name = vName.toString();
199     disableSelection();
200
201     QString msg =  tr("Do you want to delete") + name + "?";
202     int yn = QMessageBox::question(this, "Confirm", msg, QMessageBox::Yes | QMessageBox::No, QMessageBox::No);
203     if (yn == QMessageBox::Yes)
204     {
205         deleteItem();
206     }
207 }
208
209 void DbObjectDialog::layout()
210 {
211     QHBoxLayout *mainLayout = new QHBoxLayout;
212     mainLayout->addWidget(objectList);
213     mainLayout->addWidget(buttonBox);
214     setLayout(mainLayout);
215 }
216
217 void DbObjectDialog::listObjectClicked(const QModelIndex &index)
218 {
219     const QModelIndex *x;
220     x = &index;
221     qDebug() << "Row " << x->row() << ", column " << x->column() << " clicked.";
222     setButtonsEnabled(index.isValid());
223     if(!index.isValid()) 
224         return;
225 }
226
227 void DbObjectDialog::enableUi()
228 {
229     setUIEnabled(true);
230 }
231
232 void DbObjectDialog::setButtonsEnabled(bool enabled)
233 {
234     addButton->setEnabled(enabled);
235     editButton->setEnabled(enabled);
236     deleteButton->setEnabled(enabled);
237 }
238
239 void DbObjectDialog::setUIEnabled(bool enabled)
240 {
241     buttonBox->setEnabled(enabled);
242     objectList->setEnabled(enabled);
243 }
244
245 void DbObjectDialog::disableSelection()
246 {
247     setUIEnabled(false);
248     //setButtonsEnabled(false);
249 }
250
251 void DbObjectDialog::activateNameDialog(bool updateData)
252 {
253     if (!nameDialog) return;
254     nameDialog->show();
255     nameDialog->raise();
256     if (updateData)
257         nameDialog->updateData();
258     nameDialog->activateWindow();
259 }
260
261 void DbObjectDialog::initDataTable()
262 {
263    objectList->setModel(dbManager->getDataModel());
264    objectList->setSelectionMode(QAbstractItemView::SingleSelection);
265    objectList->resizeColumnsToContents();
266 }
267
268 void DbObjectDialog::updateReject()
269 {
270     addButton->setEnabled(true);
271     setUIEnabled(true);
272     // we don't want to keep this in memory
273     deleteCurrentObject();
274 }
275
276 void DbObjectDialog::updateData()
277 {
278     // update data model
279     if (!dbObject) return;
280
281     // if data object id > -1 we are updating the data otherwise we are inserting new data
282     if (dbObject->getId() > -1) updateDb(dbObject);
283     else insertDb(dbObject);
284
285     // we don't need dbObject anymore
286     deleteCurrentObject();
287     dbObject = 0;
288     updateList();
289     setUIEnabled(true);
290 }
291
292 /* Implementation specific delete must be used!
293 void DbObjectDialog::deleteCurrentObject()
294 {
295     delete dbObject;
296 }*/
297
298 bool DbObjectDialog::confirmDelete(QString name, int numRefs)
299 {
300     int r = QMessageBox::warning(this, tr("Confirm delete"),
301                                  QString("Do you really want to delete %1 with %2 data bindings?")
302                                  .arg(name).arg(numRefs),
303                                  QMessageBox::Yes | QMessageBox::No);
304     if ( r == QMessageBox::No )
305         return false;
306     return true;
307 }
308
309 void DbObjectDialog::refreshDataModel()
310 {
311     dbManager->resetModel();
312 }
313
314 void DbObjectDialog::hideColumns()
315 {
316     foreach(int c, hiddenColumns)
317         objectList->hideColumn(c);
318 }
319
320 /* Enables UI. Deletes nameDialog object and current data object */
321 void DbObjectDialog::closeEvent(QCloseEvent *ev)
322 {
323     qDebug() << "DbObjectDialog closing!";
324     setUIEnabled(true);
325     cleanUp();
326 }