From fd3c9cfa20cff5d1cc40dd9ccc365191c9aa35e1 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Mikko=20Kein=C3=A4nen?= Date: Thu, 9 Dec 2010 23:59:16 +0200 Subject: [PATCH] Continued implementing FilePathModel. --- src/models/filepathmodel.cpp | 114 +++++++++++++++++++++++++++++++++++++--- src/models/filepathmodel.h | 6 +++ src/models/setupmodel.cpp | 4 +- src/views/filepatheditview.cpp | 4 +- 4 files changed, 117 insertions(+), 11 deletions(-) diff --git a/src/models/filepathmodel.cpp b/src/models/filepathmodel.cpp index b863047..3f38bb5 100644 --- a/src/models/filepathmodel.cpp +++ b/src/models/filepathmodel.cpp @@ -18,6 +18,8 @@ // along with EmuFront. If not, see . #include "filepathmodel.h" +#include "emufrontfile.h" +#include "emufrontexception.h" #include FilePathModel::FilePathModel(QObject *parent) : @@ -56,24 +58,122 @@ QString FilePathModel::constructSelect(QString where) const Qt::ItemFlags FilePathModel::flags(const QModelIndex &index) const { Qt::ItemFlags flags = QSqlQueryModel::flags(index); - // TODO + int col = index.column(); + if (col == FilePath_SetupId || + col == FilePath_Name) { + flags |= Qt::ItemIsEditable; + } return flags; } bool FilePathModel::setData(const QModelIndex &index, const QVariant &value, int role) { - // TODO - return false; + int col = index.column(); + if (col != FilePath_SetupId && + col != FilePath_Name) { + return false; + } + + QModelIndex primaryKeyIndex = QSqlQueryModel::index(index.row(), FilePath_Id); + + int id = data(primaryKeyIndex).toInt(); + clear(); + + bool ok; + switch(index.column()) { + case FilePath_SetupId: + ok = setSetup(id, value.toInt()); + break; + case FilePath_Name: + ok = setFilePath(id, value.toString()); + break; + default: + qDebug() << "File path model, this shouldn't be happening!"; + } + refresh(); + return ok; } bool FilePathModel::insertRows(int row, int count, const QModelIndex &parent) { - // TODO - return false; + if (parent.isValid()) + return false; // This is a flat model + if (rowCount() < row) + row = rowCount() + 1; + int supId = -1; + QSqlQuery q; + q.exec(QString("SELECT setup.id, " + // The following is to get the correct order: + "platform.name || ' ' || mediatype.name AS SetupName " + "FROM setup " + "INNER JOIN platform ON setup.platformid=platform.id " + "INNER JOIN mediatype ON setup.mediatypeid=mediatype.id " + "ORDER BY SetupName " + "LIMIT 1")); + if (q.first()) { + supId = q.value(0).toInt(); + qDebug() << "Got id " << supId << " for default setup."; + } + else { + throw EmuFrontException(tr("No setups yet available for file path configuration!")); + } + q.prepare(QString("INSERT INTO filepath " + "(id, name, filetypeid, setupid, lastscanned) " + "VALUES (NULL, '', :filetype, :setupid, :lastscanned )")); + beginInsertRows(QModelIndex(), row, row + count - 1); + for(int i = 0; i < count; ++i) { + q.bindValue(":filetype", EmuFrontFile::FileType_MediaImage); + q.bindValue(":setupid", supId); + q.bindValue(":lastscanned", 0); + if (!q.exec()) { + throw EmuFrontException(tr("Failed creating new filepath row: %1"). + arg(q.lastError().text())); + } + } + endInsertRows(); + refresh(); + return true; } bool FilePathModel::removeRows(int row, int count, const QModelIndex &parent) { - // TODO - return false; + if (parent.isValid()) { + return false; // This is a flat model + } + if (rowCount() < row + count - 1) + return false; + + QSqlQuery q; + q.prepare(QString("DELETE FROM filepath WHERE id=:id")); + QModelIndex primaryIndex; + int id = -1; + beginRemoveRows(QModelIndex(), row, row + count - 1); + for(int i = 0; i < count; ++i) { + primaryIndex = QSqlQueryModel::index(row + i, FilePath_Id); + id = data(primaryIndex).toInt(); + qDebug() << "Removing data item with id " << id; + q.bindValue(":id", id); + q.exec(); + } + endRemoveRows(); + refresh(); + return true; +} + +bool FilePathModel::setSetup(int id, int setupId) +{ + QSqlQuery q; + q.prepare(QString("UPDATE filepath SET setupid = :setupid WHERE id = :id")); + q.bindValue(":setupid", setupId); + q.bindValue(":id", id); + return q.exec(); +} + +bool FilePathModel::setFilePath(int id, QString filePath) +{ + QSqlQuery q; + q.prepare(QString("UPDATE filepath SET name = :name WHERE id = :id")); + q.bindValue(":name", filePath); + q.bindValue(":id", id); + return q.exec(); } diff --git a/src/models/filepathmodel.h b/src/models/filepathmodel.h index 76bad4f..d501182 100644 --- a/src/models/filepathmodel.h +++ b/src/models/filepathmodel.h @@ -39,9 +39,15 @@ public: FilePath_FileTypeId }; + enum { + FileType_MediaImage + }; + protected: virtual void refresh(); virtual QString constructSelect(QString where = "") const; + virtual bool setFilePath(int id, QString filePath); + virtual bool setSetup(int isd, int setupId); }; #endif // FILEPATHMODEL_H diff --git a/src/models/setupmodel.cpp b/src/models/setupmodel.cpp index 031d495..a1aebc4 100644 --- a/src/models/setupmodel.cpp +++ b/src/models/setupmodel.cpp @@ -142,16 +142,16 @@ bool SetupModel::insertRows(int row, int count, const QModelIndex &parent) QSqlQuery q; q.exec(QString("SELECT id FROM platform ORDER BY name LIMIT 1")); if (q.first()){ - qDebug() << "Got id " << plfId << " for default platform."; plfId = q.value(0).toInt(); + qDebug() << "Got id " << plfId << " for default platform."; } else { throw EmuFrontException(tr("No platforms yet available for setup configuration!")); } q.exec(QString("SELECT id FROM mediatype ORDER BY name LIMIT 1")); if (q.first()) { - qDebug() << "Got id " << mdtId << " for default media type."; mdtId = q.value(0).toInt(); + qDebug() << "Got id " << mdtId << " for default media type."; } else { throw EmuFrontException(tr("No media types yet available for setup configuration!")); diff --git a/src/views/filepatheditview.cpp b/src/views/filepatheditview.cpp index fe41d47..19e4194 100644 --- a/src/views/filepatheditview.cpp +++ b/src/views/filepatheditview.cpp @@ -28,13 +28,13 @@ FilePathEditView::FilePathEditView(QWidget *parent) : { model = new FilePathModel(this); objectList->setModel(model); - /*SetupModel *stupMdl = new SetupModel(this); + SetupModel *stupMdl = new SetupModel(this); ComboBoxDelegate *setupDelegate = new ComboBoxDelegate( stupMdl, SetupModel::Setup_Id, SetupModel::Setup_Name, this ); - objectList->setItemDelegateForColumn(FilePathModel::FilePath_SetupId, setupDelegate);*/ + objectList->setItemDelegateForColumn(FilePathModel::FilePath_SetupId, setupDelegate); postInit(); } -- 1.7.9.5