Added confirmation dialog for deleting items.
[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     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!");
85     if (!confirm(msg))
86         return;
87     int row = objectList->currentIndex().row();
88     if ( !model->removeRows(row, 1) ) {
89         errorMessage->showMessage(tr("Failed removing selected item."));
90     }
91 }
92
93
94 void EmuFrontEditView::addButtonClicked()
95 {
96     int row = objectList->currentIndex().isValid() ?
97         objectList->currentIndex().row() : 0;
98     try {
99         model->insertRows(row, 1);
100     } catch(EmuFrontException e) {
101         errorMessage->showMessage(tr("Failed inserting rows: %1").arg(e.what()));
102         return;
103     }
104     QModelIndex ind = model->index(row, 1);
105     if (!ind.isValid()){
106         return;
107     }
108     objectList->setCurrentIndex(ind);
109     objectList->edit(ind);
110 }
111
112 void EmuFrontEditView::listObjectClicked(const QModelIndex &index)
113 {
114     setButtonsEnabled(index.isValid());
115 }
116
117 void EmuFrontEditView::setButtonsEnabled(bool b)
118 {
119     editButton->setEnabled(b);
120     deleteButton->setEnabled(b);
121 }
122
123 void EmuFrontEditView::setHiddenColumns()
124 {
125     // default implementation
126 }
127
128 bool EmuFrontEditView::confirm(QString &msg)
129 {
130     int r = QMessageBox::warning(this, tr("Confirm"), msg, QMessageBox::Yes | QMessageBox::No);
131     if ( r == QMessageBox::No )
132         return false;
133     return true;
134 }
135
136