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"
25 #include "setupmodel.h"
26 #include "emufrontexception.h"
29 ExternalExecutableModel::ExternalExecutableModel(QObject *parent) :
30 EmuFrontQueryModel(parent)
32 editableColumns << Executable_Name;
33 editableColumns << Executable_Options;
34 editableColumns << Executable_Executable;
35 editableColumns << Executable_SetupId;
39 void ExternalExecutableModel::refresh()
41 setQuery(constructSelect());
42 setHeaderData(Executable_Id, Qt::Horizontal, tr("Id"));
43 setHeaderData(Executable_Name, Qt::Horizontal, tr("Name"));
44 setHeaderData(Executable_Executable, Qt::Horizontal, tr("Executable"));
45 setHeaderData(Executable_Options, Qt::Horizontal, tr("Options"));
46 setHeaderData(Executable_TypeId, Qt::Horizontal, tr("Type"));
47 setHeaderData(Executable_SetupId, Qt::Horizontal, tr("Setup id"));
48 setHeaderData(Executable_SetupName, Qt::Horizontal, tr("Setup"));
51 QString ExternalExecutableModel::constructSelect(QString where) const
53 return QString("SELECT "
54 "executable.id AS ExecutableId, "
55 "executable.name AS ExecutableName, "
56 "executable.executable AS Executable, "
57 "executable.options AS ExecutableOptions, "
58 "executable.type AS ExecutableType, "
59 "setup.id As ExecutableSetupId, "
60 "platform.name || ' ' || mediatype.name AS SetupName "
62 "INNER JOIN setup ON executable.setupid = setup.id "
63 "INNER JOIN platform ON setup.platformid=platform.id "
64 "INNER JOIN mediatype ON setup.mediatypeid=mediatype.id "
66 "ORDER BY executable.name").arg(where);
69 Qt::ItemFlags ExternalExecutableModel::flags(const QModelIndex &index) const
71 Qt::ItemFlags flags = QSqlQueryModel::flags(index);
72 int col = index.column();
73 if (editableColumns.contains(index.column())) {
74 flags |= Qt::ItemIsEditable;
79 bool ExternalExecutableModel::setData(const QModelIndex &index, const QVariant &value, int role)
81 if (!editableColumns.contains(index.column()))
84 QModelIndex primaryKeyIndex = QSqlQueryModel::index(index.row(), Executable_Id);
85 int id = data(primaryKeyIndex).toInt();
88 switch(index.column())
91 ok = setExecutableName(id, value.toString());
93 case Executable_Executable:
94 ok = setExecutable(id, value.toString());
96 case Executable_Options:
97 ok = setOptions(id, value.toString());
99 case Executable_SetupId:
100 ok = setSetup(id, value.toInt());
106 if (ok) emit dataChanged();
110 bool ExternalExecutableModel::insertRows(int row, int count, const QModelIndex &parent)
112 if (parent.isValid())
113 return false; // This is a flat model
114 if (rowCount() < row)
115 row = rowCount() + 1;
117 // Fetch a setup for an initial selection
120 q.exec(QString("SELECT setup.id, "
121 // The following is to get the correct order:
122 "platform.name || ' ' || mediatype.name AS SetupName "
124 "INNER JOIN platform ON setup.platformid=platform.id "
125 "INNER JOIN mediatype ON setup.mediatypeid=mediatype.id "
126 "ORDER BY SetupName "
129 supId = q.value(0).toInt();
130 qDebug() << "Got id " << supId << " for default setup.";
133 throw EmuFrontException(tr("No setups yet available for file path configuration!"));
135 q.prepare(QString("INSERT INTO executable "
136 "(id, name, executable, options, type, setupid) "
137 "VALUES (NULL, '', '', '', :type, :supid)"
139 beginInsertRows(QModelIndex(), row, row + count - 1);
140 for(int i = 0; i < count; ++i) {
141 q.bindValue(":supid", supId);
142 q.bindValue(":type", Executable::ExecutableType_Emulator);
144 throw EmuFrontException(tr("Failed creating new external executable row: %1").
145 arg(q.lastError().text()));
153 bool ExternalExecutableModel::removeRows(int row, int count, const QModelIndex &parent)
155 if (parent.isValid()) {
156 return false; // This is a flat model
158 if (rowCount() < row + count - 1)
162 q.prepare(QString("DELETE FROM executable WHERE id=:id"));
163 QModelIndex primaryIndex;
165 beginRemoveRows(QModelIndex(), row, row + count - 1);
166 for(int i = 0; i < count; ++i) {
167 primaryIndex = QSqlQueryModel::index(row + i, Executable_Id);
168 id = data(primaryIndex).toInt();
169 qDebug() << "Removing data item with id " << id;
170 q.bindValue(":id", id);
178 bool ExternalExecutableModel::setSetup(int id, int setupId)
181 q.prepare(QString("UPDATE executable SET setupid = :supid WHERE id = :id"));
182 q.bindValue(":supid", setupId);
183 q.bindValue(":id", id);
187 bool ExternalExecutableModel::setExecutable(int id, QString name)
190 q.prepare(QString("UPDATE executable SET executable = :exec WHERE id = :id"));
191 q.bindValue(":exec", name);
192 q.bindValue(":id", id);
196 bool ExternalExecutableModel::setOptions(int id, QString options)
199 q.prepare(QString("UPDATE executable SET options = :opts WHERE id = :id"));
200 q.bindValue(":opts", options);
201 q.bindValue(":id", id);
205 bool ExternalExecutableModel::setExecutableName(int id, QString name)
208 q.prepare(QString("UPDATE executable SET name = :name WHERE id = :id"));
209 q.bindValue(":name", name);
210 q.bindValue(":id", id);
214 void ExternalExecutableModel::filterBySetup(int setupid)
216 QList<QString> filters;
217 filters.append(QString("executable.setupid=%1").arg(setupid));
218 filterDataObjects(filters);
221 // Implemented for EmuFrontQueryModel:
222 EmuFrontObject* ExternalExecutableModel::recordToDataObject(const QSqlRecord* rec)
226 int id = rec->value(Executable_Id).toInt();
227 int supid = rec->value(Executable_SetupId).toInt();
229 EmuFrontObject *ob = supModel.getDataObject(supid);
230 Setup *sup = dynamic_cast<Setup*>(ob);
231 QString name = rec->value(Executable_Name).toString();
232 QString exec = rec->value(Executable_Executable).toString();
233 QString opts = rec->value(Executable_Options).toString();
234 int type = rec->value(Executable_TypeId).toInt();
235 ex = new Executable(id, name, exec, opts, sup, type);
239 QString ExternalExecutableModel::constructFilterById(int id) const
241 return QString("executable.id=%1").arg(id);
244 Executable* ExternalExecutableModel::getExecutable(int row)
246 if (row < 0) return 0;
247 EmuFrontObject *efo = getObject(row);
248 return dynamic_cast<Executable*>(efo);