d874a13ecde75434171b51ff3ef678b478296ee7
[emufront] / src / views / emufronteditview.cpp
1 /*
2 ** EmuFront
3 ** Copyright 2010 Mikko Keinänen
4 **
5 ** This file is part of EmuFront.
6 **
7 **
8 ** EmuFront is free software: you can redistribute it and/or modify
9 ** it under the terms of the GNU General Public License version 2 as published by
10 ** the Free Software Foundation and appearing in the file gpl.txt included in the
11 ** packaging of this file.
12 **
13 ** EmuFront is distributed in the hope that it will be useful,
14 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
15 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 ** GNU General Public License for more details.
17 **
18 ** You should have received a copy of the GNU General Public License
19 ** along with EmuFront.  If not, see <http://www.gnu.org/licenses/>.
20 */
21 #include <QtGui>
22 #include "emufronteditview.h"
23
24 EmuFrontEditView::EmuFrontEditView(QWidget *parent) :
25     EmuFrontDialog(parent)
26 {
27     editButton = new QPushButton(tr("&Edit"));
28     editButton->setEnabled(false);
29     addButton = new QPushButton(tr("&Add"));
30     deleteButton = new QPushButton(tr("&Delete"));
31     deleteButton->setEnabled(false);
32     objectList = new QTableView(this);
33     buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok, Qt::Vertical);
34     buttonBox->addButton(editButton, QDialogButtonBox::ActionRole);
35     buttonBox->addButton(addButton, QDialogButtonBox::ActionRole);
36     buttonBox->addButton(deleteButton, QDialogButtonBox::ActionRole);
37     // this be called from the implementing classes:
38     //connectSignals();
39     layout();
40 }
41
42 void EmuFrontEditView::postInit()
43 {
44     connectSignals();
45     setHiddenColumns();
46     hideColumns();
47 }
48
49 void EmuFrontEditView::layout()
50 {
51     QHBoxLayout *mainLayout = new QHBoxLayout;
52     mainLayout->addWidget(objectList);
53     mainLayout->addWidget(buttonBox);
54     setLayout(mainLayout);
55 }
56
57 void EmuFrontEditView::hideColumns()
58 {
59     foreach(int c, hiddenColumns)
60         objectList->hideColumn(c);
61 }
62
63 void EmuFrontEditView::connectSignals()
64 {
65     connect(buttonBox, SIGNAL(accepted()), this, SLOT(accept()));
66     connect(objectList, SIGNAL(clicked(const QModelIndex &)),
67         this, SLOT(listObjectClicked(const QModelIndex &)));
68     connect(editButton, SIGNAL(clicked()), this, SLOT(editButtonClicked()));
69     connect(addButton, SIGNAL(clicked()), this, SLOT(addButtonClicked()));
70     connect(deleteButton, SIGNAL(clicked()), this, SLOT(deleteButtonClicked()));
71 }
72
73 void EmuFrontEditView::editButtonClicked()
74 {
75     setButtonsEnabled(false);
76     qDebug() << "Edit button clicked";
77 }
78
79 void EmuFrontEditView::deleteButtonClicked()
80 {
81     setButtonsEnabled(false);
82     if (!objectList->currentIndex().isValid())
83         return;
84     int row = objectList->currentIndex().row();
85     if ( !model->removeRows(row, 1) ) {
86         errorMessage->showMessage(tr("Failed removing selected item."));
87     }
88 }
89
90
91 void EmuFrontEditView::addButtonClicked()
92 {
93     int row = objectList->currentIndex().isValid() ?
94         objectList->currentIndex().row() : 0;
95     try {
96         model->insertRows(row, 1);
97     } catch(EmuFrontException e) {
98         errorMessage->showMessage(tr("Failed inserting rows: %1").arg(e.what()));
99         return;
100     }
101     QModelIndex ind = model->index(row, 1);
102     if (!ind.isValid()){
103         return;
104     }
105     objectList->setCurrentIndex(ind);
106     objectList->edit(ind);
107 }
108
109 void EmuFrontEditView::listObjectClicked(const QModelIndex &index)
110 {
111     setButtonsEnabled(index.isValid());
112 }
113
114 void EmuFrontEditView::setButtonsEnabled(bool b)
115 {
116     editButton->setEnabled(b);
117     deleteButton->setEnabled(b);
118 }
119
120 void EmuFrontEditView::setHiddenColumns()
121 {
122     // default implementation
123 }