From 4a49bbf32a6131b794cca59d4830f512a42fd43b Mon Sep 17 00:00:00 2001 From: =?utf8?q?Mikko=20Kein=C3=A4nen?= Date: Fri, 3 Dec 2010 00:11:44 +0200 Subject: [PATCH] Implemented remove rows. --- src/db/emufrontfileobjectmodel.cpp | 26 ++++++++++++++++++++++++++ src/db/emufrontfileobjectmodel.h | 1 + src/dialogs/emufrontdatadialog.cpp | 25 ++++++++++++++++++++++--- src/dialogs/emufrontdatadialog.h | 2 ++ 4 files changed, 51 insertions(+), 3 deletions(-) diff --git a/src/db/emufrontfileobjectmodel.cpp b/src/db/emufrontfileobjectmodel.cpp index 48c1a6f..f287ca1 100644 --- a/src/db/emufrontfileobjectmodel.cpp +++ b/src/db/emufrontfileobjectmodel.cpp @@ -105,5 +105,31 @@ bool EmuFrontFileObjectModel::insertRows(int row, int count, const QModelIndex & q.exec(); } endInsertRows(); + refresh(); + return true; +} + +bool EmuFrontFileObjectModel::removeRows(int row, int count, const QModelIndex &parent) +{ + if (parent.isValid()) { + return false; // This is a flat model + } + if (rowCount() < row + count - 1) + return false; + + QSqlQuery q; + q.prepare(QString("DELETE FROM %1 WHERE id=:id").arg(tableName)); + QModelIndex primaryIndex; + int id = -1; + beginRemoveRows(QModelIndex(), row, row + count - 1); + for(int i = 0; i < count; ++i) { + primaryIndex = QSqlQueryModel::index(row + i, EmuFrontFileObject_Id); + id = data(primaryIndex).toInt(); + qDebug() << "Removing data item with id " << id; + q.bindValue(":id", id); + q.exec(); + } + endRemoveRows(); + refresh(); return true; } diff --git a/src/db/emufrontfileobjectmodel.h b/src/db/emufrontfileobjectmodel.h index f29235f..3a8a4f5 100644 --- a/src/db/emufrontfileobjectmodel.h +++ b/src/db/emufrontfileobjectmodel.h @@ -30,6 +30,7 @@ public: Qt::ItemFlags flags(const QModelIndex &index) const; bool setData(const QModelIndex &index, const QVariant &value, int role); bool insertRows(int row, int count, const QModelIndex &parent); + bool removeRows(int row, int count, const QModelIndex &parent); enum { EmuFrontFileObject_Id, EmuFrontFileObject_Name, diff --git a/src/dialogs/emufrontdatadialog.cpp b/src/dialogs/emufrontdatadialog.cpp index 61d2557..0d54504 100644 --- a/src/dialogs/emufrontdatadialog.cpp +++ b/src/dialogs/emufrontdatadialog.cpp @@ -62,8 +62,8 @@ void EmuFrontDataDialog::hideColumns() void EmuFrontDataDialog::connectSignals() { connect(buttonBox, SIGNAL(accepted()), this, SLOT(accept())); - /*connect(objectList, SIGNAL(clicked(const QModelIndex &)), - this, SLOT(listObjectClicked(const QModelIndex &)));*/ + connect(objectList, SIGNAL(clicked(const QModelIndex &)), + this, SLOT(listObjectClicked(const QModelIndex &))); connect(editButton, SIGNAL(clicked()), this, SLOT(editButtonClicked())); connect(addButton, SIGNAL(clicked()), this, SLOT(addButtonClicked())); connect(deleteButton, SIGNAL(clicked()), this, SLOT(deleteButtonClicked())); @@ -71,14 +71,22 @@ void EmuFrontDataDialog::connectSignals() void EmuFrontDataDialog::editButtonClicked() { + setButtonsEnabled(false); qDebug() << "Edit button clicked"; } void EmuFrontDataDialog::deleteButtonClicked() { - qDebug() << "Delete button clicked"; + setButtonsEnabled(false); + if (!objectList->currentIndex().isValid()) + return; + int row = objectList->currentIndex().row(); + if ( !model->removeRows(row, 1) ) { + errorMessage->showMessage(tr("Failed removing selected item.")); + } } + void EmuFrontDataDialog::addButtonClicked() { int row = objectList->currentIndex().isValid() ? @@ -91,3 +99,14 @@ void EmuFrontDataDialog::addButtonClicked() objectList->setCurrentIndex(ind); objectList->edit(ind); } + +void EmuFrontDataDialog::listObjectClicked(const QModelIndex &index) +{ + setButtonsEnabled(index.isValid()); +} + +void EmuFrontDataDialog::setButtonsEnabled(bool b) +{ + editButton->setEnabled(b); + deleteButton->setEnabled(b); +} diff --git a/src/dialogs/emufrontdatadialog.h b/src/dialogs/emufrontdatadialog.h index ede863a..1809dd0 100644 --- a/src/dialogs/emufrontdatadialog.h +++ b/src/dialogs/emufrontdatadialog.h @@ -36,6 +36,7 @@ private slots: void editButtonClicked(); void addButtonClicked(); void deleteButtonClicked(); + void listObjectClicked(const QModelIndex &); protected: EmuFrontQueryModel *model; @@ -52,6 +53,7 @@ private: virtual void setHiddenColumns() = 0; void hideColumns(); virtual void connectSignals(); + void setButtonsEnabled(bool); }; -- 1.7.9.5