Implemented remove rows.
[emufront] / src / dialogs / emufrontdatadialog.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 version 2 as published by
9 // the Free Software Foundation and appearing in the file gpl.txt included in the
10 // packaging of this file.
11 //
12 // EmuFront 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 EmuFront.  If not, see <http://www.gnu.org/licenses/>.
19
20 #include <QtGui>
21 #include "emufrontdatadialog.h"
22
23 EmuFrontDataDialog::EmuFrontDataDialog(QWidget *parent) :
24     EmuFrontDialog(parent)
25 {
26     editButton = new QPushButton(tr("&Edit"));
27     editButton->setEnabled(false);
28     addButton = new QPushButton(tr("&Add"));
29     deleteButton = new QPushButton(tr("&Delete"));
30     deleteButton->setEnabled(false);
31     objectList = new QTableView(this);
32     buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok, Qt::Vertical);
33     buttonBox->addButton(editButton, QDialogButtonBox::ActionRole);
34     buttonBox->addButton(addButton, QDialogButtonBox::ActionRole);
35     buttonBox->addButton(deleteButton, QDialogButtonBox::ActionRole);
36     // this be called from the implementing classes:
37     //connectSignals();
38     layout();
39 }
40
41 void EmuFrontDataDialog::postInit()
42 {
43     connectSignals();
44     setHiddenColumns();
45     hideColumns();
46 }
47
48 void EmuFrontDataDialog::layout()
49 {
50     QHBoxLayout *mainLayout = new QHBoxLayout;
51     mainLayout->addWidget(objectList);
52     mainLayout->addWidget(buttonBox);
53     setLayout(mainLayout);
54 }
55
56 void EmuFrontDataDialog::hideColumns()
57 {
58     foreach(int c, hiddenColumns)
59         objectList->hideColumn(c);
60 }
61
62 void EmuFrontDataDialog::connectSignals()
63 {
64     connect(buttonBox, SIGNAL(accepted()), this, SLOT(accept()));
65     connect(objectList, SIGNAL(clicked(const QModelIndex &)),
66         this, SLOT(listObjectClicked(const QModelIndex &)));
67     connect(editButton, SIGNAL(clicked()), this, SLOT(editButtonClicked()));
68     connect(addButton, SIGNAL(clicked()), this, SLOT(addButtonClicked()));
69     connect(deleteButton, SIGNAL(clicked()), this, SLOT(deleteButtonClicked()));
70 }
71
72 void EmuFrontDataDialog::editButtonClicked()
73 {
74     setButtonsEnabled(false);
75     qDebug() << "Edit button clicked";
76 }
77
78 void EmuFrontDataDialog::deleteButtonClicked()
79 {
80     setButtonsEnabled(false);
81     if (!objectList->currentIndex().isValid())
82         return;
83     int row = objectList->currentIndex().row();
84     if ( !model->removeRows(row, 1) ) {
85         errorMessage->showMessage(tr("Failed removing selected item."));
86     }
87 }
88
89
90 void EmuFrontDataDialog::addButtonClicked()
91 {
92     int row = objectList->currentIndex().isValid() ?
93         objectList->currentIndex().row() : 0;
94     model->insertRows(row, 1);
95     QModelIndex ind = model->index(row, 1);
96     if (!ind.isValid()){
97         return;
98     }
99     objectList->setCurrentIndex(ind);
100     objectList->edit(ind);
101 }
102
103 void EmuFrontDataDialog::listObjectClicked(const QModelIndex &index)
104 {
105     setButtonsEnabled(index.isValid());
106 }
107
108 void EmuFrontDataDialog::setButtonsEnabled(bool b)
109 {
110     editButton->setEnabled(b);
111     deleteButton->setEnabled(b);
112 }