Continued implementing ExternalExecutableModel.
[emufront] / src / models / externalexecutablemodel.cpp
1 /*
2 ** EmuFront
3 ** Copyright 2010 Mikko Keinänen
4 **
5 ** This file is part of EmuFront.
6 **
7 **
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.
12 **
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.
17 **
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/>.
20 */
21
22 #include "externalexecutablemodel.h"
23
24 ExternalExecutableModel::ExternalExecutableModel(QObject *parent) :
25     EmuFrontQueryModel(parent)
26 {
27     editableColumns << Executable_Name;
28     editableColumns << Executable_Options;
29     editableColumns << Executable_Executable;
30     editableColumns << Executable_SetupId;
31     refresh();
32 }
33
34 void ExternalExecutableModel::refresh()
35 {
36     setQuery(constructSelect());
37     setHeaderData(Executable_Id, Qt::Horizontal, tr("Id"));
38     setHeaderData(Executable_Name, Qt::Horizontal, tr("Name"));
39     setHeaderData(Executable_Executable, Qt::Horizontal, tr("Executable"));
40     setHeaderData(Executable_Options, Qt::Horizontal, tr("Options"));
41     setHeaderData(Executable_TypeId, Qt::Horizontal, tr("Type"));
42     setHeaderData(Executable_SetupId, Qt::Horizontal, tr("Setup id"));
43     setHeaderData(Executable_SetupName, Qt::Horizontal, tr("Setup"));
44 }
45
46 QString ExternalExecutableModel::constructSelect(QString where) const
47 {
48     return QString("SELECT "
49                    "executable.id AS ExecutableId, "
50                    "executable.name AS ExecutableName, "
51                    "executable.executable AS Executable, "
52                    "executable.options AS ExecutableOptions, "
53                    "executable.type AS ExecutableType, "
54                    "setup.id As ExecutableSetupId, "
55                    "platform.name || ' ' || mediatype.name AS SetupName "
56                    "FROM executable "
57                    "INNER JOIN setup ON executable.setupid = setup.id "
58                    "INNER JOIN platform ON setup.platformid=platform.id "
59                    "INNER JOIN mediatype ON setup.mediatypeid=mediatype.id "
60                    "%1 "
61                    "ORDER BY executable.name").arg(where);
62 }
63
64 Qt::ItemFlags ExternalExecutableModel::flags(const QModelIndex &index) const
65 {
66     Qt::ItemFlags flags = QSqlQueryModel::flags(index);
67     int col = index.column();
68     if (editableColumns.contains(index.column())) {
69        flags  |= Qt::ItemIsEditable;
70     }
71     return flags;
72 }
73
74 bool ExternalExecutableModel::setData(const QModelIndex &index, const QVariant &value, int role)
75 {
76     if (!editableColumns.contains(index.column()))
77         return false;
78
79     QModelIndex primaryKeyIndex = QSqlQueryModel::index(index.row(), Executable_Id);
80     int id = data(primaryKeyIndex).toInt();
81     clear();
82     bool ok;
83     switch(index.column())
84     {
85         case Executable_Name:
86             ok = setExecutableName(id, value.toString());
87             break;
88         case Executable_Executable:
89             ok = setExecutable(id, value.toString());
90             break;
91         case Executable_Options:
92             ok = setOptions(id, value.toString());
93             break;
94         case Executable_SetupId:
95             ok = setSetup(id, value.toInt());
96             break;
97         default:
98             ok = false;
99     }
100     refresh();
101     return ok;
102 }
103
104 bool ExternalExecutableModel::insertRows(int row, int count, const QModelIndex &parent)
105 {
106     // TODO
107     return false;
108 }
109
110 bool ExternalExecutableModel::removeRows(int row, int count, const QModelIndex &parent)
111 {
112     // TODO
113     return false;
114 }
115
116 bool ExternalExecutableModel::setSetup(int isd, int setupId)
117 {
118     // TODO
119     return false;
120 }
121
122 bool ExternalExecutableModel::setExecutable(int id, QString name)
123 {
124     // TODO
125     return false;
126 }
127
128 bool ExternalExecutableModel::setOptions(int id, QString options)
129 {
130     // TODO
131     return false;
132 }
133
134 bool ExternalExecutableModel::setExecutableName(int id, QString name)
135 {
136     // TODO
137     return false;
138 }