Re-used the ComboBoxDelegate created earlier for MediaTypeModel in
[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 || index.column() == Setup_MediaTypeId) {
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 && index.column() != Setup_MediaTypeId)
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     switch(index.column()) {
76
77     case Setup_PlatformId:
78         ok = setPlatform(id, value.toInt());
79         break;
80
81     case Setup_MediaTypeId:
82         ok = setMediaType(id, value.toInt());
83         break;
84     };
85     refresh();
86     return ok;
87 }
88
89 bool SetupModel::setPlatform(int id, int platformId)
90 {
91     QSqlQuery query;
92     query.prepare(QString("update setup set platformid = :platformid where id = :id"));
93     query.bindValue(":platformid", platformId);
94     query.bindValue(":id", id);
95     return query.exec();
96 }
97
98 bool SetupModel::setMediaType(int id, int mediaTypeId)
99 {
100     QSqlQuery query;
101     query.prepare(QString("update setup set mediatypeid = :mediatypeid where id = :id"));
102     query.bindValue(":mediatypeid", mediaTypeId);
103     query.bindValue(":id", id);
104     return query.exec();
105 }