Some data was hidden from user.
[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(close()));
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     connect(nameDialog, SIGNAL(dataObjectUpdated()), this, SLOT(updateData()));
66     connect(nameDialog, SIGNAL(updateRejected()), this, SLOT(updateReject()));
67     connect(nameDialog, SIGNAL(test()), this, SLOT(testSlot()));
68 }
69     
70 void DbObjectDialog::testSlot()
71 {
72     qDebug() << "TEST SIGNAL RECEIVED!";
73 }
74
75 void DbObjectDialog::insertDb(const EmuFrontObject *ob) const
76 {
77     if (!dbManager->insertDataObjectToModel(ob))
78         errorMessage->showMessage(tr("Inserting data object %1 failed.").arg(ob->getName()));
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     activateNameDialog();
99     nameDialog->setDataObject(dbObject);
100 }
101
102 bool DbObjectDialog::deleteItem()
103 {
104     qDebug() << "deleteItem called";
105     QModelIndex index = objectList->currentIndex();
106     if (!index.isValid()) return false;
107     try
108     {
109         EmuFrontObject *ob = dbManager->getDataObjectFromModel(&index);
110
111         qDebug() << "Trying to delete " << ob->getName();
112
113         if (!ob)
114         {
115             errorMessage->showMessage(tr("Couldn't find the selected data object from data model!"));
116             return false;
117         }
118
119         int numBindings = dbManager->countDataObjectRefs(ob->getId());
120
121         if (numBindings > 0 && !confirmDelete(ob->getName(), numBindings))
122         { return false; }
123         deleteCurrentObject();
124
125         bool delOk = dbManager->deleteDataObject(ob->getId());
126         if (!delOk)
127         {
128             errorMessage->showMessage(tr("Deleting data object %1 failed!").arg(ob->getName()));
129             return false;
130         }
131         updateList();
132         objectList->setFocus();
133     }
134     catch(EmuFrontException e)
135     {
136         errorMessage->showMessage(e.what());
137     }
138     return false;
139 }
140
141 void DbObjectDialog::updateDb(const EmuFrontObject *ob) const
142 {
143     if (!ob) return;
144     if ( !dbManager->updateDataObjectToModel(ob) )
145     { errorMessage->showMessage("Database update failed!"); }
146 }
147
148 void DbObjectDialog::updateList() const
149 {
150     if (!dbManager) return;
151     dbManager->resetModel();
152 }
153
154 void DbObjectDialog::addButtonClicked()
155 {
156     disableSelection();
157     addObject();
158 }
159
160 void DbObjectDialog::editButtonClicked()
161 {
162     disableSelection();
163     editObject();
164 }
165
166 void DbObjectDialog::deleteButtonClicked()
167 {
168     QItemSelectionModel *selModel = objectList->selectionModel();
169     if (!selModel->hasSelection()) return;
170
171     QAbstractItemModel *tblModel = objectList->model();
172     QModelIndex index = selModel->currentIndex();
173     QVariant vName = tblModel->data(index);
174     QString name = vName.toString();
175     disableSelection();
176
177     QString msg =  tr("Do you want to delete") + name + "?";
178     int yn = QMessageBox::question(this, "Confirm", msg, QMessageBox::Yes | QMessageBox::No, QMessageBox::No);
179     if (yn == QMessageBox::Yes)
180     {
181         deleteItem();
182     }
183 }
184
185 void DbObjectDialog::layout()
186 {
187     QHBoxLayout *mainLayout = new QHBoxLayout;
188     mainLayout->addWidget(objectList);
189     mainLayout->addWidget(buttonBox);
190     setLayout(mainLayout);
191 }
192
193 void DbObjectDialog::listObjectClicked(const QModelIndex &index)
194 {
195     const QModelIndex *x;
196     x = &index;
197     qDebug() << "Row " << x->row() << ", column " << x->column() << " clicked.";
198     setButtonsEnabled(index.isValid());
199     if(!index.isValid()) 
200         return;
201 }
202
203 void DbObjectDialog::setButtonsEnabled(bool enabled)
204 {
205     editButton->setEnabled(enabled);
206     deleteButton->setEnabled(enabled);
207 }
208
209 void DbObjectDialog::disableSelection()
210 {
211     setButtonsEnabled(false);
212 }
213
214 void DbObjectDialog::activateNameDialog(bool updateData)
215 {
216     if (!nameDialog) return;
217     nameDialog->show();
218     nameDialog->raise();
219     if (updateData)
220         nameDialog->updateData();
221     nameDialog->activateWindow();
222 }
223
224 void DbObjectDialog::initDataTable()
225 {
226    objectList->setModel(dbManager->getDataModel());
227    objectList->setSelectionMode(QAbstractItemView::SingleSelection);
228    objectList->resizeColumnsToContents();
229 }
230
231 void DbObjectDialog::updateReject()
232 {
233     // we don't want to keep this in memory
234     deleteCurrentObject();
235 }
236
237 void DbObjectDialog::updateData()
238 {
239     qDebug() << "DbObjectDialog::updateData()";
240     // update data model
241     if (!dbObject) return;
242
243
244     // if data object id > -1 we are updating the data otherwise we are inserting new data
245     if (dbObject->getId() > -1) updateDb(dbObject);
246     else insertDb(dbObject);
247
248     // we don't need dbObject anymore
249     deleteCurrentObject();
250     dbObject = 0;
251     updateList();
252 }
253
254 void DbObjectDialog::deleteCurrentObject()
255 {
256     delete dbObject;
257 }
258
259 bool DbObjectDialog::confirmDelete(QString name, int numRefs)
260 {
261     int r = QMessageBox::warning(this, tr("Confirm delete"),
262                                  QString("Do you really want to delete %1 with %2 data bindings?")
263                                  .arg(name).arg(numRefs),
264                                  QMessageBox::Yes | QMessageBox::No);
265     if ( r == QMessageBox::No )
266         return false;
267     return true;
268 }
269
270 void DbObjectDialog::refreshDataModel()
271 {
272     dbManager->resetModel();
273 }
274
275 void DbObjectDialog::hideColumns()
276 {
277     foreach(int c, hiddenColumns)
278         objectList->hideColumn(c);
279 }