251257165942e5d3c261be4e78ba3932a8e19a58
[emufront] / src / views / emufronteditview.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 "emufronteditview.h"
22
23 EmuFrontEditView::EmuFrontEditView(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 EmuFrontEditView::postInit()
42 {
43     connectSignals();
44     setHiddenColumns();
45     hideColumns();
46 }
47
48 void EmuFrontEditView::layout()
49 {
50     QHBoxLayout *mainLayout = new QHBoxLayout;
51     mainLayout->addWidget(objectList);
52     mainLayout->addWidget(buttonBox);
53     setLayout(mainLayout);
54 }
55
56 void EmuFrontEditView::hideColumns()
57 {
58     foreach(int c, hiddenColumns)
59         objectList->hideColumn(c);
60 }
61
62 void EmuFrontEditView::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 EmuFrontEditView::editButtonClicked()
73 {
74     setButtonsEnabled(false);
75     qDebug() << "Edit button clicked";
76 }
77
78 void EmuFrontEditView::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 EmuFrontEditView::addButtonClicked()
91 {
92     int row = objectList->currentIndex().isValid() ?
93         objectList->currentIndex().row() : 0;
94     try {
95         model->insertRows(row, 1);
96     } catch(EmuFrontException e) {
97         errorMessage->showMessage(tr("Failed inserting rows: %1").arg(e.what()));
98         return;
99     }
100     QModelIndex ind = model->index(row, 1);
101     if (!ind.isValid()){
102         return;
103     }
104     objectList->setCurrentIndex(ind);
105     objectList->edit(ind);
106 }
107
108 void EmuFrontEditView::listObjectClicked(const QModelIndex &index)
109 {
110     setButtonsEnabled(index.isValid());
111 }
112
113 void EmuFrontEditView::setButtonsEnabled(bool b)
114 {
115     editButton->setEnabled(b);
116     deleteButton->setEnabled(b);
117 }
118
119 void EmuFrontEditView::setHiddenColumns()
120 {
121     // default implementation
122 }