Added new database object classes: the base class EmuFrontObject and
[emufront] / src / dialogs / dbobjectdialog.cpp
1 #include <QtGui>
2 #include "dbobjectdialog.h"
3 #include "../db/databasemanager.h"
4
5 DbObjectDialog::DbObjectDialog(QWidget *parent)
6     : EmuFrontDialog(parent)
7 {
8     dbObject = 0;
9     dbManager = new DatabaseManager(this);
10     editButton = new QPushButton(tr("&Edit")); 
11     editButton->setEnabled(false);
12     addButton = new QPushButton(tr("&Add"));
13     deleteButton = new QPushButton(tr("&Delete"));
14     deleteButton->setEnabled(false);
15     objectList = new QTableView(this);
16     buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok, Qt::Vertical);
17     buttonBox->addButton(editButton, QDialogButtonBox::ActionRole);
18     buttonBox->addButton(addButton, QDialogButtonBox::ActionRole);
19     buttonBox->addButton(deleteButton, QDialogButtonBox::ActionRole);
20     // nameDialog will be created on request
21     
22     connectSignals();
23     layout();
24
25
26 void DbObjectDialog::connectSignals()
27 {
28     connect(buttonBox, SIGNAL(accepted()), this, SLOT(close()));
29     connect(objectList, SIGNAL(clicked(const QModelIndex &)), 
30                 this, SLOT(listObjectClicked(const QModelIndex &)));
31     connect(editButton, SIGNAL(clicked()), this, SLOT(editButtonClicked()));
32     connect(addButton, SIGNAL(clicked()), this, SLOT(addButtonClicked()));
33     connect(deleteButton, SIGNAL(clicked()), this, SLOT(deleteButtonClicked()));
34     connect(nameDialog, SIGNAL(accepted()), this, SLOT(updateList()));
35 }
36
37 void DbObjectDialog::updateList() const
38 {
39         // fetch items from database (virtual function for this)
40         // update the item list
41 }
42
43 void DbObjectDialog::addButtonClicked()
44 {
45     disableSelection();
46     addObject();
47 }
48
49 void DbObjectDialog::editButtonClicked()
50 {
51     disableSelection();
52 }
53
54 void DbObjectDialog::deleteButtonClicked()
55 {
56     disableSelection();
57 }
58
59 void DbObjectDialog::layout()
60 {
61     QHBoxLayout *mainLayout = new QHBoxLayout;
62     mainLayout->addWidget(objectList);
63     mainLayout->addWidget(buttonBox);
64     setLayout(mainLayout);
65 }
66
67 void DbObjectDialog::listObjectClicked(const QModelIndex &index)
68 {
69     setButtonsEnabled(index.isValid());
70     if(!index.isValid()) 
71         return;
72 }
73
74 void DbObjectDialog::setButtonsEnabled(bool enabled)
75 {
76     editButton->setEnabled(enabled);
77     deleteButton->setEnabled(enabled);
78 }
79
80 void DbObjectDialog::disableSelection()
81 {
82     setButtonsEnabled(false);
83 }