2 // Copyright 2010 Mikko Keinänen
4 // This file is part of EmuFront.
7 // EmuFront is free software: you can redistribute it and/or modify
8 // it under the terms of the GNU General Public License version 2 as published by
9 // the Free Software Foundation and appearing in the file gpl.txt included in the
10 // packaging of this file.
12 // EmuFront is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 // GNU General Public License for more details.
17 // You should have received a copy of the GNU General Public License
18 // along with EmuFront. If not, see <http://www.gnu.org/licenses/>.
20 #include "setupmodel.h"
21 #include "emufrontexception.h"
24 const QString SetupModel::FILE_TYPE_EXTENSION_SEPARATOR = QString("|");
26 SetupModel::SetupModel(QObject *parent) :
27 EmuFrontQueryModel(parent)
32 void SetupModel::refresh()
34 setQuery(constructSelect());
35 setHeaderData(Setup_Id, Qt::Horizontal, tr("Id"));
36 setHeaderData(Setup_PlatformId, Qt::Horizontal, tr("Platform id"));
37 setHeaderData(Setup_MediaTypeId, Qt::Horizontal, tr("Media type id"));
38 setHeaderData(Setup_FileTypeExtensions, Qt::Horizontal, tr("File types"));
39 setHeaderData(Setup_Name, Qt::Horizontal, tr("Name"));
42 QString SetupModel::constructSelect(QString where) const
45 "SELECT setup.id AS SetupId, "
46 "setup.platformid AS PlatformId, "
47 "setup.mediatypeid AS MediaTypeId, "
48 "setup.filetypeextensions AS SupportedFileTypeExtensions, "
49 "platform.name || ' ' || mediatype.name AS SetupName "
51 "INNER JOIN platform ON setup.platformid=platform.id "
52 "INNER JOIN mediatype ON setup.mediatypeid=mediatype.id %1 "
57 Qt::ItemFlags SetupModel::flags(const QModelIndex &index) const
59 Qt::ItemFlags flags = QSqlQueryModel::flags(index);
60 int col = index.column();
61 if (col == Setup_PlatformId ||
62 col == Setup_MediaTypeId ||
63 col == Setup_FileTypeExtensions) {
64 flags |= Qt::ItemIsEditable;
69 bool SetupModel::setData(const QModelIndex &index, const QVariant &value, int /*role*/)
71 int col = index.column();
72 if(col != Setup_PlatformId &&
73 col != Setup_MediaTypeId &&
74 col != Setup_FileTypeExtensions)
77 QModelIndex primaryKeyIndex
78 = QSqlQueryModel::index(index.row(), Setup_Id);
80 int id = data(primaryKeyIndex).toInt();
84 switch(index.column()) {
86 case Setup_PlatformId:
87 ok = setPlatform(id, value.toInt());
90 case Setup_MediaTypeId:
91 ok = setMediaType(id, value.toInt());
94 case Setup_FileTypeExtensions:
95 ok = setSupportedExtensions(id, value.toString());
99 qDebug() << "Setup model, this shouldn't be happening!";
105 bool SetupModel::setPlatform(int id, int platformId)
108 query.prepare(QString("update setup set platformid = :platformid where id = :id"));
109 query.bindValue(":platformid", platformId);
110 query.bindValue(":id", id);
114 bool SetupModel::setMediaType(int id, int mediaTypeId)
117 query.prepare(QString("update setup set mediatypeid = :mediatypeid where id = :id"));
118 query.bindValue(":mediatypeid", mediaTypeId);
119 query.bindValue(":id", id);
123 bool SetupModel::setSupportedExtensions(int id, QString exts)
126 query.prepare(QString("update setup set filetypeextensions = :exts where id = :id"));
127 query.bindValue(":exts", exts);
128 query.bindValue(":id", id);
132 bool SetupModel::insertRows(int row, int count, const QModelIndex &parent)
134 if (parent.isValid())
135 return false; // This is a flat model
136 if (rowCount() < row)
137 row = rowCount() + 1;
138 // we need a default value for platformid and mediatypeid and if none is yet
139 // available an error message must be shown!
143 q.exec(QString("SELECT id FROM platform ORDER BY name LIMIT 1"));
145 plfId = q.value(0).toInt();
146 qDebug() << "Got id " << plfId << " for default platform.";
149 throw EmuFrontException(tr("No platforms yet available for setup configuration!"));
151 q.exec(QString("SELECT id FROM mediatype ORDER BY name LIMIT 1"));
153 mdtId = q.value(0).toInt();
154 qDebug() << "Got id " << mdtId << " for default media type.";
157 throw EmuFrontException(tr("No media types yet available for setup configuration!"));
159 q.prepare(QString("INSERT INTO setup (id, platformid, mediatypeid, filetypeextensions) "
160 " VALUES (NULL, :plfid, :mdtid, '') "));
161 beginInsertRows(QModelIndex(), row, row + count - 1);
162 for (int i = 0; i < count; ++i) {
163 q.bindValue(":plfid", plfId);
164 q.bindValue(":mdtid", mdtId);
166 throw EmuFrontException(tr("Failed creating new setup: %1").
167 arg(q.lastError().text()));
175 bool SetupModel::removeRows(int row, int count, const QModelIndex &parent)
177 if (parent.isValid()) {
178 return false; // This is a flat model
180 if (rowCount() < row + count - 1)
184 q.prepare(QString("DELETE FROM setup WHERE id=:id"));
185 QModelIndex primaryIndex;
187 beginRemoveRows(QModelIndex(), row, row + count - 1);
188 for(int i = 0; i < count; ++i) {
189 primaryIndex = QSqlQueryModel::index(row + i, Setup_Id);
190 id = data(primaryIndex).toInt();
191 qDebug() << "Removing data item with id " << id;
192 q.bindValue(":id", id);