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 QString msg = tr("Are you sure you want to delete the selected item? If other data objects are assigned to the selected item, they will be also removed!");
87 int row = objectList->currentIndex().row();
88 if ( !model->removeRows(row, 1) ) {
89 errorMessage->showMessage(tr("Failed removing selected item."));
94 void EmuFrontEditView::addButtonClicked()
96 int row = objectList->currentIndex().isValid() ?
97 objectList->currentIndex().row() : 0;
99 model->insertRows(row, 1);
100 } catch(EmuFrontException e) {
101 errorMessage->showMessage(tr("Failed inserting rows: %1").arg(e.what()));
104 QModelIndex ind = model->index(row, 1);
108 objectList->setCurrentIndex(ind);
109 objectList->edit(ind);
112 void EmuFrontEditView::listObjectClicked(const QModelIndex &index)
114 setButtonsEnabled(index.isValid());
117 void EmuFrontEditView::setButtonsEnabled(bool b)
119 editButton->setEnabled(b);
120 deleteButton->setEnabled(b);
123 void EmuFrontEditView::setHiddenColumns()
125 // default implementation
128 bool EmuFrontEditView::confirm(QString &msg)
130 int r = QMessageBox::warning(this, tr("Confirm"), msg, QMessageBox::Yes | QMessageBox::No);
131 if ( r == QMessageBox::No )