From e267c7d65574f9047e8cc678cb7cb4e0be339fdb Mon Sep 17 00:00:00 2001 From: =?utf8?q?Mikko=20Kein=C3=A4nen?= Date: Thu, 2 Dec 2010 22:49:25 +0200 Subject: [PATCH 1/1] Implemented insertRows to model. --- src/db/emufrontfileobjectmodel.cpp | 18 ++++++++++++++++++ src/db/emufrontfileobjectmodel.h | 1 + src/dialogs/emufrontdatadialog.cpp | 10 ++++++++++ 3 files changed, 29 insertions(+) diff --git a/src/db/emufrontfileobjectmodel.cpp b/src/db/emufrontfileobjectmodel.cpp index 9d5ea27..6d9b1ad 100644 --- a/src/db/emufrontfileobjectmodel.cpp +++ b/src/db/emufrontfileobjectmodel.cpp @@ -90,3 +90,21 @@ bool EmuFrontFileObjectModel::setName(int id, const QString &name) query.bindValue(":id", id); return query.exec(); } + +bool EmuFrontFileObjectModel::insertRows(int row, int count, const QModelIndex &parent) +{ + if (parent.isValid()) + return false; + if (rowCount() < row) + row = rowCount() + 1; + qDebug() << "Inserting " << count << " rows from row " << row; + QSqlQuery q; + q.prepare(QString("INSERT INTO %1 (id, name, fileid) " + " VALUES (NULL, '', NULL) ").arg(tableName)); + beginInsertRows(QModelIndex(), row, row + count - 1); + for (int i = 0; i < count; ++i) { + q.exec(); + } + endInsertRows(); + return true; +} diff --git a/src/db/emufrontfileobjectmodel.h b/src/db/emufrontfileobjectmodel.h index 36398ac..f29235f 100644 --- a/src/db/emufrontfileobjectmodel.h +++ b/src/db/emufrontfileobjectmodel.h @@ -29,6 +29,7 @@ public: EmuFrontFileObjectModel(QObject *parent = 0); 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); enum { EmuFrontFileObject_Id, EmuFrontFileObject_Name, diff --git a/src/dialogs/emufrontdatadialog.cpp b/src/dialogs/emufrontdatadialog.cpp index 2529c1a..e8ac4e4 100644 --- a/src/dialogs/emufrontdatadialog.cpp +++ b/src/dialogs/emufrontdatadialog.cpp @@ -82,4 +82,14 @@ void EmuFrontDataDialog::deleteButtonClicked() void EmuFrontDataDialog::addButtonClicked() { qDebug() << "Delete button clicked"; + int row = objectList->currentIndex().row(); + if (row == -1) row = 0; + model->insertRows(row, 1); + QModelIndex ind = model->index(row, 1); + if (!ind.isValid()){ + qDebug() << "Invalid index"; + return; + } + objectList->setCurrentIndex(ind); + objectList->edit(ind); } -- 1.7.9.5