3 ** Copyright 2010 Mikko Keinänen
5 ** This file is part of EmuFront.
8 ** EmuFront is free software: you can redistribute it and/or modify
9 ** it under the terms of the GNU General Public License version 2 as published by
10 ** the Free Software Foundation and appearing in the file gpl.txt included in the
11 ** packaging of this file.
13 ** EmuFront is distributed in the hope that it will be useful,
14 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
15 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 ** GNU General Public License for more details.
18 ** You should have received a copy of the GNU General Public License
19 ** along with EmuFront. If not, see <http://www.gnu.org/licenses/>.
22 #include "externalexecutablemodel.h"
23 #include "executable.h"
24 #include "emufrontexception.h"
27 ExternalExecutableModel::ExternalExecutableModel(QObject *parent) :
28 EmuFrontQueryModel(parent)
30 editableColumns << Executable_Name;
31 editableColumns << Executable_Options;
32 editableColumns << Executable_Executable;
33 editableColumns << Executable_SetupId;
37 void ExternalExecutableModel::refresh()
39 setQuery(constructSelect());
40 setHeaderData(Executable_Id, Qt::Horizontal, tr("Id"));
41 setHeaderData(Executable_Name, Qt::Horizontal, tr("Name"));
42 setHeaderData(Executable_Executable, Qt::Horizontal, tr("Executable"));
43 setHeaderData(Executable_Options, Qt::Horizontal, tr("Options"));
44 setHeaderData(Executable_TypeId, Qt::Horizontal, tr("Type"));
45 setHeaderData(Executable_SetupId, Qt::Horizontal, tr("Setup id"));
46 setHeaderData(Executable_SetupName, Qt::Horizontal, tr("Setup"));
49 QString ExternalExecutableModel::constructSelect(QString where) const
51 return QString("SELECT "
52 "executable.id AS ExecutableId, "
53 "executable.name AS ExecutableName, "
54 "executable.executable AS Executable, "
55 "executable.options AS ExecutableOptions, "
56 "executable.type AS ExecutableType, "
57 "setup.id As ExecutableSetupId, "
58 "platform.name || ' ' || mediatype.name AS SetupName "
60 "INNER JOIN setup ON executable.setupid = setup.id "
61 "INNER JOIN platform ON setup.platformid=platform.id "
62 "INNER JOIN mediatype ON setup.mediatypeid=mediatype.id "
64 "ORDER BY executable.name").arg(where);
67 Qt::ItemFlags ExternalExecutableModel::flags(const QModelIndex &index) const
69 Qt::ItemFlags flags = QSqlQueryModel::flags(index);
70 int col = index.column();
71 if (editableColumns.contains(index.column())) {
72 flags |= Qt::ItemIsEditable;
77 bool ExternalExecutableModel::setData(const QModelIndex &index, const QVariant &value, int role)
79 if (!editableColumns.contains(index.column()))
82 QModelIndex primaryKeyIndex = QSqlQueryModel::index(index.row(), Executable_Id);
83 int id = data(primaryKeyIndex).toInt();
86 switch(index.column())
89 ok = setExecutableName(id, value.toString());
91 case Executable_Executable:
92 ok = setExecutable(id, value.toString());
94 case Executable_Options:
95 ok = setOptions(id, value.toString());
97 case Executable_SetupId:
98 ok = setSetup(id, value.toInt());
107 bool ExternalExecutableModel::insertRows(int row, int count, const QModelIndex &parent)
109 if (parent.isValid())
110 return false; // This is a flat model
111 if (rowCount() < row)
112 row = rowCount() + 1;
114 // Fetch a setup for an initial selection
117 q.exec(QString("SELECT setup.id, "
118 // The following is to get the correct order:
119 "platform.name || ' ' || mediatype.name AS SetupName "
121 "INNER JOIN platform ON setup.platformid=platform.id "
122 "INNER JOIN mediatype ON setup.mediatypeid=mediatype.id "
123 "ORDER BY SetupName "
126 supId = q.value(0).toInt();
127 qDebug() << "Got id " << supId << " for default setup.";
130 throw EmuFrontException(tr("No setups yet available for file path configuration!"));
132 q.prepare(QString("INSERT INTO executable "
133 "(id, name, executable, options, type, setupid) "
134 "VALUES (NULL, '', '', '', :type, :supid)"
136 beginInsertRows(QModelIndex(), row, row + count - 1);
137 for(int i = 0; i < count; ++i) {
138 q.bindValue(":supid", supId);
139 q.bindValue(":type", Executable::ExecutableType_Emulator);
141 throw EmuFrontException(tr("Failed creating new external executable row: %1").
142 arg(q.lastError().text()));
150 bool ExternalExecutableModel::removeRows(int row, int count, const QModelIndex &parent)
152 if (parent.isValid()) {
153 return false; // This is a flat model
155 if (rowCount() < row + count - 1)
159 q.prepare(QString("DELETE FROM executable WHERE id=:id"));
160 QModelIndex primaryIndex;
162 beginRemoveRows(QModelIndex(), row, row + count - 1);
163 for(int i = 0; i < count; ++i) {
164 primaryIndex = QSqlQueryModel::index(row + i, Executable_Id);
165 id = data(primaryIndex).toInt();
166 qDebug() << "Removing data item with id " << id;
167 q.bindValue(":id", id);
175 bool ExternalExecutableModel::setSetup(int id, int setupId)
178 q.prepare(QString("UPDATE executable SET setupid = :supid WHERE id = :id"));
179 q.bindValue(":supid", setupId);
180 q.bindValue(":id", id);
184 bool ExternalExecutableModel::setExecutable(int id, QString name)
187 q.prepare(QString("UPDATE executable SET executable = :exec WHERE id = :id"));
188 q.bindValue(":exec", name);
189 q.bindValue(":id", id);
193 bool ExternalExecutableModel::setOptions(int id, QString options)
196 q.prepare(QString("UPDATE executable SET options = :opts WHERE id = :id"));
197 q.bindValue(":opts", options);
198 q.bindValue(":id", id);
202 bool ExternalExecutableModel::setExecutableName(int id, QString name)
205 q.prepare(QString("UPDATE executable SET name = :name WHERE id = :id"));
206 q.bindValue(":name", name);
207 q.bindValue(":id", id);