Small fix to copyright
[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     //dbManager = new DatabaseManager(this);
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     // nameDialog will be created on request
42     
43     //connectSignals();
44     layout();
45
46
47 DbObjectDialog::~DbObjectDialog()
48 {
49     delete dbObject;
50 }
51
52 void DbObjectDialog::connectSignals()
53 {
54     connect(buttonBox, SIGNAL(accepted()), this, SLOT(close()));
55     connect(objectList, SIGNAL(clicked(const QModelIndex &)),
56         this, SLOT(listObjectClicked(const QModelIndex &)));
57     connect(editButton, SIGNAL(clicked()), this, SLOT(editButtonClicked()));
58     connect(addButton, SIGNAL(clicked()), this, SLOT(addButtonClicked()));
59     connect(deleteButton, SIGNAL(clicked()), this, SLOT(deleteButtonClicked()));
60     //connect(nameDialog, SIGNAL(accepted()), this, SLOT(updateList()));
61     connect(nameDialog, SIGNAL(dataObjectUpdated()), this, SLOT(updateData()));
62 }
63
64 void DbObjectDialog::updateList() const
65 {
66     if (!dbManager) return;
67     dbManager->resetModel();
68 }
69
70 void DbObjectDialog::addButtonClicked()
71 {
72     disableSelection();
73     addObject();
74 }
75
76 void DbObjectDialog::editButtonClicked()
77 {
78     disableSelection();
79     editObject();
80 }
81
82 void DbObjectDialog::deleteButtonClicked()
83 {
84     QItemSelectionModel *selModel = objectList->selectionModel();
85     if (!selModel->hasSelection()) return;
86
87     QAbstractItemModel *tblModel = objectList->model();
88     QModelIndex index = selModel->currentIndex();
89     QVariant vName = tblModel->data(index);
90     QString name = vName.toString();
91     disableSelection();
92
93     QString msg =  tr("Do you want to delete") + name + "?";
94     int yn = QMessageBox::question(this, "Confirm", msg, QMessageBox::Yes | QMessageBox::No, QMessageBox::No);
95     if (yn == QMessageBox::Yes)
96     {
97         qDebug() << "Deleting item..." << name << ".";
98         deleteItem();
99     }
100 }
101
102 void DbObjectDialog::layout()
103 {
104     QHBoxLayout *mainLayout = new QHBoxLayout;
105     mainLayout->addWidget(objectList);
106     mainLayout->addWidget(buttonBox);
107     setLayout(mainLayout);
108 }
109
110 void DbObjectDialog::listObjectClicked(const QModelIndex &index)
111 {
112     const QModelIndex *x;
113     x = &index;
114     qDebug() << "Row " << x->row() << ", column " << x->column() << " clicked.";
115     setButtonsEnabled(index.isValid());
116     if(!index.isValid()) 
117         return;
118 }
119
120 void DbObjectDialog::setButtonsEnabled(bool enabled)
121 {
122     editButton->setEnabled(enabled);
123     deleteButton->setEnabled(enabled);
124 }
125
126 void DbObjectDialog::disableSelection()
127 {
128     setButtonsEnabled(false);
129 }
130
131 void DbObjectDialog::updateData()
132 {
133     qDebug() << "DbObjectDialog::updateData()";
134     updateList();
135 }
136
137 void DbObjectDialog::activateNameDialog() const
138 {
139     if (!nameDialog) return;
140     nameDialog->show();
141     nameDialog->raise();
142     nameDialog->activateWindow();
143 }
144