Cleaning up here and there
[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::updateList() const
66 {
67     if (!dbManager) return;
68     dbManager->resetModel();
69 }
70
71 void DbObjectDialog::addButtonClicked()
72 {
73     disableSelection();
74     addObject();
75 }
76
77 void DbObjectDialog::editButtonClicked()
78 {
79     disableSelection();
80     editObject();
81 }
82
83 void DbObjectDialog::deleteButtonClicked()
84 {
85     QItemSelectionModel *selModel = objectList->selectionModel();
86     if (!selModel->hasSelection()) return;
87
88     QAbstractItemModel *tblModel = objectList->model();
89     QModelIndex index = selModel->currentIndex();
90     QVariant vName = tblModel->data(index);
91     QString name = vName.toString();
92     disableSelection();
93
94     QString msg =  tr("Do you want to delete") + name + "?";
95     int yn = QMessageBox::question(this, "Confirm", msg, QMessageBox::Yes | QMessageBox::No, QMessageBox::No);
96     if (yn == QMessageBox::Yes)
97     {
98         qDebug() << "Deleting item..." << name << ".";
99         deleteItem();
100     }
101 }
102
103 void DbObjectDialog::layout()
104 {
105     QHBoxLayout *mainLayout = new QHBoxLayout;
106     mainLayout->addWidget(objectList);
107     mainLayout->addWidget(buttonBox);
108     setLayout(mainLayout);
109 }
110
111 void DbObjectDialog::listObjectClicked(const QModelIndex &index)
112 {
113     const QModelIndex *x;
114     x = &index;
115     qDebug() << "Row " << x->row() << ", column " << x->column() << " clicked.";
116     setButtonsEnabled(index.isValid());
117     if(!index.isValid()) 
118         return;
119 }
120
121 void DbObjectDialog::setButtonsEnabled(bool enabled)
122 {
123     editButton->setEnabled(enabled);
124     deleteButton->setEnabled(enabled);
125 }
126
127 void DbObjectDialog::disableSelection()
128 {
129     setButtonsEnabled(false);
130 }
131
132 void DbObjectDialog::updateData()
133 {
134     qDebug() << "DbObjectDialog::updateData()";
135     updateList();
136 }
137
138 void DbObjectDialog::activateNameDialog()
139 {
140     if (!nameDialog) return;
141     nameDialog->show();
142     nameDialog->raise();
143     nameDialog->activateWindow();
144 }
145