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