a7fe6b73f224c1441fc1c7da71bf57b6272d94fe
[emufront] / src / models / setupmodel.cpp
1 // EmuFront
2 // Copyright 2010 Mikko Keinänen
3 //
4 // This file is part of EmuFront.
5 //
6 //
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.
11 //
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.
16 //
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/>.
19
20 #include "setupmodel.h"
21 #include <QtSql>
22
23 SetupModel::SetupModel(QObject *parent) :
24     EmuFrontQueryModel(parent)
25 {
26     refresh();
27 }
28
29 void SetupModel::refresh()
30 {
31     setQuery(constructSelect());
32     setHeaderData(Setup_Id, Qt::Horizontal, tr("Id"));
33     setHeaderData(Setup_PlatformId, Qt::Horizontal, tr("Platform id"));
34     setHeaderData(Setup_MediaTypeId, Qt::Horizontal, tr("Media type id"));
35     setHeaderData(Setup_FileTypeExtensions, Qt::Horizontal, tr("File types"));
36     setHeaderData(Setup_Name, Qt::Horizontal, tr("Name"));
37 }
38
39 QString SetupModel::constructSelect(QString where) const
40 {
41     return QString(
42         "SELECT setup.id AS SetupId, "
43         "setup.platformid AS PlatformId, "
44         "setup.mediatypeid AS MediaTypeId, "
45         "setup.filetypeextensions AS SupportedFileTypeExtensions, "
46         "platform.name || ' ' || mediatype.name AS SetupName "
47         "FROM setup "
48         "INNER JOIN platform ON setup.platformid=platform.id "
49         "INNER JOIN mediatype ON setup.mediatypeid=mediatype.id %1 "
50         "ORDER BY SetupName"
51         ).arg(where);
52 }
53
54 Qt::ItemFlags SetupModel::flags(const QModelIndex &index) const
55 {
56     Qt::ItemFlags flags = QSqlQueryModel::flags(index);
57     if (index.column() == Setup_PlatformId) {
58         flags |= Qt::ItemIsEditable;
59     }
60     return flags;
61 }
62
63 bool SetupModel::setData(const QModelIndex &index, const QVariant &value, int /*role*/)
64 {
65     if(index.column() != Setup_PlatformId)
66         return false;
67
68     QModelIndex primaryKeyIndex
69         = QSqlQueryModel::index(index.row(), Setup_Id);
70
71     int id = data(primaryKeyIndex).toInt();
72     clear();
73
74     bool ok;
75     if (index.column() == Setup_PlatformId) {
76         ok = setPlatform(id, value.toInt());
77     }
78
79     refresh();
80     return ok;
81 }
82
83 bool SetupModel::setPlatform(int id, int platformId)
84 {
85     qDebug() << "updating setup " << id << " to platform " << platformId;
86     QSqlQuery query;
87     query.prepare(QString("update setup set platformid = :platformid where id = :id"));
88     query.bindValue(":platformid", platformId);
89     query.bindValue(":id", id);
90     qDebug() << query.lastQuery();
91     qDebug() << query.lastError();
92     return query.exec();
93 }