Virtual method insertDb implementation abstracted from child classes to
[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     editButton = new QPushButton(tr("&Edit")); 
31     editButton->setEnabled(false);
32     addButton = new QPushButton(tr("&Add"));
33     deleteButton = new QPushButton(tr("&Delete"));
34     deleteButton->setEnabled(false);
35     objectList = new QTableView(this);
36     buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok, Qt::Vertical);
37     buttonBox->addButton(editButton, QDialogButtonBox::ActionRole);
38     buttonBox->addButton(addButton, QDialogButtonBox::ActionRole);
39     buttonBox->addButton(deleteButton, QDialogButtonBox::ActionRole);
40     // this be called from the implementing classes:
41     //connectSignals();
42     layout();
43
44
45 DbObjectDialog::~DbObjectDialog()
46 {
47     // no need to explicitically delete widgets within a parented layout
48     // they are automatically parented and will be deleted
49     // dbManager is also parented and will be implicitically deleted
50     // this must be deleted in an implementing class
51     //delete dbObject;
52 }
53
54 void DbObjectDialog::connectSignals()
55 {
56     connect(buttonBox, SIGNAL(accepted()), this, SLOT(close()));
57     connect(objectList, SIGNAL(clicked(const QModelIndex &)),
58         this, SLOT(listObjectClicked(const QModelIndex &)));
59     connect(editButton, SIGNAL(clicked()), this, SLOT(editButtonClicked()));
60     connect(addButton, SIGNAL(clicked()), this, SLOT(addButtonClicked()));
61     connect(deleteButton, SIGNAL(clicked()), this, SLOT(deleteButtonClicked()));
62     connect(nameDialog, SIGNAL(dataObjectUpdated()), this, SLOT(updateData()));
63 }
64
65 void DbObjectDialog::insertDb(const EmuFrontObject *ob) const
66 {
67     dbManager->insertDataObjectToModel(ob);
68 }
69
70
71 void DbObjectDialog::editObject()
72 {
73     QModelIndex index = objectList->currentIndex();
74     if (!index.isValid())
75         return;
76     deleteCurrentObject();
77     dbObject = dbManager->getDataObjectFromModel(&index);
78     nameDialog->setDataObject(dbObject);
79     activateNameDialog();
80 }
81
82 void DbObjectDialog::updateDb(const EmuFrontObject *ob) const
83 {
84     if (!ob) return;
85     qDebug() << "Updating platform " << ob->getName();
86     dbManager->updateDataObjectToModel(ob);
87 }
88
89 void DbObjectDialog::updateList() const
90 {
91     if (!dbManager) return;
92     dbManager->resetModel();
93 }
94
95 void DbObjectDialog::addButtonClicked()
96 {
97     disableSelection();
98     addObject();
99 }
100
101 void DbObjectDialog::editButtonClicked()
102 {
103     disableSelection();
104     editObject();
105 }
106
107 void DbObjectDialog::deleteButtonClicked()
108 {
109     QItemSelectionModel *selModel = objectList->selectionModel();
110     if (!selModel->hasSelection()) return;
111
112     QAbstractItemModel *tblModel = objectList->model();
113     QModelIndex index = selModel->currentIndex();
114     QVariant vName = tblModel->data(index);
115     QString name = vName.toString();
116     disableSelection();
117
118     QString msg =  tr("Do you want to delete") + name + "?";
119     int yn = QMessageBox::question(this, "Confirm", msg, QMessageBox::Yes | QMessageBox::No, QMessageBox::No);
120     if (yn == QMessageBox::Yes)
121     {
122         qDebug() << "Deleting item..." << name << ".";
123         deleteItem();
124     }
125 }
126
127 void DbObjectDialog::layout()
128 {
129     QHBoxLayout *mainLayout = new QHBoxLayout;
130     mainLayout->addWidget(objectList);
131     mainLayout->addWidget(buttonBox);
132     setLayout(mainLayout);
133 }
134
135 void DbObjectDialog::listObjectClicked(const QModelIndex &index)
136 {
137     const QModelIndex *x;
138     x = &index;
139     qDebug() << "Row " << x->row() << ", column " << x->column() << " clicked.";
140     setButtonsEnabled(index.isValid());
141     if(!index.isValid()) 
142         return;
143 }
144
145 void DbObjectDialog::setButtonsEnabled(bool enabled)
146 {
147     editButton->setEnabled(enabled);
148     deleteButton->setEnabled(enabled);
149 }
150
151 void DbObjectDialog::disableSelection()
152 {
153     setButtonsEnabled(false);
154 }
155
156 void DbObjectDialog::activateNameDialog()
157 {
158     if (!nameDialog) return;
159     nameDialog->show();
160     nameDialog->raise();
161     nameDialog->activateWindow();
162 }
163
164 void DbObjectDialog::initDataTable()
165 {
166    objectList->setModel(dbManager->getDataModel());
167    objectList->setSelectionMode(QAbstractItemView::SingleSelection);
168    objectList->resizeColumnsToContents();
169 }
170 void DbObjectDialog::updateData()
171 {
172     qDebug() << "Update data";
173     // update data model
174     if (!dbObject) return;
175
176     qDebug() << "dbObject is not 0";
177
178     qDebug() << "We have a " + dbObject->getName();
179
180     qDebug() << "Data will be inserted/updated...";
181
182     // if data object id > -1 we are updating the data otherwise we are inserting new data
183     if (dbObject->getId() > -1) updateDb(dbObject);
184     else insertDb(dbObject);
185
186     // we don't need dbObject anymore
187     deleteCurrentObject();
188     dbObject = 0;
189     updateList();
190 }
191
192 void DbObjectDialog::deleteCurrentObject()
193 {
194     delete dbObject;
195 }
196
197 bool DbObjectDialog::confirmDelete(QString name, int numRefs)
198 {
199     int r = QMessageBox::warning(this, tr("Confirm delete"),
200                                  QString("Do you really want to delete %1 with %2 data bindings?")
201                                  .arg(name).arg(numRefs),
202                                  QMessageBox::Yes | QMessageBox::No);
203     if ( r == QMessageBox::No )
204         return false;
205     return true;
206 }