3 ** Copyright 2010 Mikko Keinänen
5 ** This file is part of EmuFront.
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.
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.
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/>.
22 #include "emufronteditview.h"
24 EmuFrontEditView::EmuFrontEditView(QWidget *parent) :
25 EmuFrontDialog(parent)
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:
42 void EmuFrontEditView::postInit()
49 void EmuFrontEditView::layout()
51 QHBoxLayout *mainLayout = new QHBoxLayout;
52 mainLayout->addWidget(objectList);
53 mainLayout->addWidget(buttonBox);
54 setLayout(mainLayout);
57 void EmuFrontEditView::hideColumns()
59 foreach(int c, hiddenColumns)
60 objectList->hideColumn(c);
63 void EmuFrontEditView::connectSignals()
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()));
73 void EmuFrontEditView::editButtonClicked()
75 setButtonsEnabled(false);
76 qDebug() << "Edit button clicked";
79 void EmuFrontEditView::deleteButtonClicked()
81 setButtonsEnabled(false);
82 if (!objectList->currentIndex().isValid())
84 int row = objectList->currentIndex().row();
85 if ( !model->removeRows(row, 1) ) {
86 errorMessage->showMessage(tr("Failed removing selected item."));
91 void EmuFrontEditView::addButtonClicked()
93 int row = objectList->currentIndex().isValid() ?
94 objectList->currentIndex().row() : 0;
96 model->insertRows(row, 1);
97 } catch(EmuFrontException e) {
98 errorMessage->showMessage(tr("Failed inserting rows: %1").arg(e.what()));
101 QModelIndex ind = model->index(row, 1);
105 objectList->setCurrentIndex(ind);
106 objectList->edit(ind);
109 void EmuFrontEditView::listObjectClicked(const QModelIndex &index)
111 setButtonsEnabled(index.isValid());
114 void EmuFrontEditView::setButtonsEnabled(bool b)
116 editButton->setEnabled(b);
117 deleteButton->setEnabled(b);
120 void EmuFrontEditView::setHiddenColumns()
122 // default implementation