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"
23 const QString SetupModel::FILE_TYPE_EXTENSION_SEPARATOR = QString("|");
25 SetupModel::SetupModel(QObject *parent) :
26 EmuFrontQueryModel(parent)
31 void SetupModel::refresh()
33 setQuery(constructSelect());
34 setHeaderData(Setup_Id, Qt::Horizontal, tr("Id"));
35 setHeaderData(Setup_PlatformId, Qt::Horizontal, tr("Platform id"));
36 setHeaderData(Setup_MediaTypeId, Qt::Horizontal, tr("Media type id"));
37 setHeaderData(Setup_FileTypeExtensions, Qt::Horizontal, tr("File types"));
38 setHeaderData(Setup_Name, Qt::Horizontal, tr("Name"));
41 QString SetupModel::constructSelect(QString where) const
44 "SELECT setup.id AS SetupId, "
45 "setup.platformid AS PlatformId, "
46 "setup.mediatypeid AS MediaTypeId, "
47 "setup.filetypeextensions AS SupportedFileTypeExtensions, "
48 "platform.name || ' ' || mediatype.name AS SetupName "
50 "INNER JOIN platform ON setup.platformid=platform.id "
51 "INNER JOIN mediatype ON setup.mediatypeid=mediatype.id %1 "
56 Qt::ItemFlags SetupModel::flags(const QModelIndex &index) const
58 Qt::ItemFlags flags = QSqlQueryModel::flags(index);
59 int col = index.column();
60 if (col == Setup_PlatformId ||
61 col == Setup_MediaTypeId ||
62 col == Setup_FileTypeExtensions) {
63 flags |= Qt::ItemIsEditable;
68 bool SetupModel::setData(const QModelIndex &index, const QVariant &value, int /*role*/)
70 int col = index.column();
71 if(col != Setup_PlatformId &&
72 col != Setup_MediaTypeId &&
73 col != Setup_FileTypeExtensions)
76 QModelIndex primaryKeyIndex
77 = QSqlQueryModel::index(index.row(), Setup_Id);
79 int id = data(primaryKeyIndex).toInt();
83 switch(index.column()) {
85 case Setup_PlatformId:
86 ok = setPlatform(id, value.toInt());
89 case Setup_MediaTypeId:
90 ok = setMediaType(id, value.toInt());
93 case Setup_FileTypeExtensions:
94 ok = setSupportedExtensions(id, value.toString());
98 qDebug() << "Setup model, this shouldn't be happening!";
104 bool SetupModel::setPlatform(int id, int platformId)
107 query.prepare(QString("update setup set platformid = :platformid where id = :id"));
108 query.bindValue(":platformid", platformId);
109 query.bindValue(":id", id);
113 bool SetupModel::setMediaType(int id, int mediaTypeId)
116 query.prepare(QString("update setup set mediatypeid = :mediatypeid where id = :id"));
117 query.bindValue(":mediatypeid", mediaTypeId);
118 query.bindValue(":id", id);
122 bool SetupModel::setSupportedExtensions(int id, QString exts)
125 query.prepare(QString("update setup set filetypeextensions = :exts where id = :id"));
126 query.bindValue(":exts", exts);
127 query.bindValue(":id", id);