2 // Copyright 2010 Mikko Keinänen
4 // This file is part of EmuFront.
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.
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.
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/>.
21 #include <QSqlTableModel>
22 #include "dbobjectdialog.h"
23 #include "../db/databasemanager.h"
25 DbObjectDialog::DbObjectDialog(QWidget *parent)
26 : EmuFrontDialog(parent)
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
47 DbObjectDialog::~DbObjectDialog()
52 void DbObjectDialog::connectSignals()
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()));
64 void DbObjectDialog::updateList() const
66 if (!dbManager) return;
67 dbManager->resetModel();
70 void DbObjectDialog::addButtonClicked()
76 void DbObjectDialog::editButtonClicked()
82 void DbObjectDialog::deleteButtonClicked()
84 QItemSelectionModel *selModel = objectList->selectionModel();
85 if (!selModel->hasSelection()) return;
87 QAbstractItemModel *tblModel = objectList->model();
88 QModelIndex index = selModel->currentIndex();
89 QVariant vName = tblModel->data(index);
90 QString name = vName.toString();
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)
97 qDebug() << "Deleting item..." << name << ".";
102 void DbObjectDialog::layout()
104 QHBoxLayout *mainLayout = new QHBoxLayout;
105 mainLayout->addWidget(objectList);
106 mainLayout->addWidget(buttonBox);
107 setLayout(mainLayout);
110 void DbObjectDialog::listObjectClicked(const QModelIndex &index)
112 const QModelIndex *x;
114 qDebug() << "Row " << x->row() << ", column " << x->column() << " clicked.";
115 setButtonsEnabled(index.isValid());
120 void DbObjectDialog::setButtonsEnabled(bool enabled)
122 editButton->setEnabled(enabled);
123 deleteButton->setEnabled(enabled);
126 void DbObjectDialog::disableSelection()
128 setButtonsEnabled(false);
131 void DbObjectDialog::updateData()
133 qDebug() << "DbObjectDialog::updateData()";
137 void DbObjectDialog::activateNameDialog() const
139 if (!nameDialog) return;
142 nameDialog->activateWindow();