Implemented StringListDelegate.
[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 const QString SetupModel::FILE_TYPE_EXTENSION_SEPARATOR = QString("|");
24
25 SetupModel::SetupModel(QObject *parent) :
26     EmuFrontQueryModel(parent)
27 {
28     refresh();
29 }
30
31 void SetupModel::refresh()
32 {
33     setQuery(constructSelect());
34     setHeaderData(Setup_Id, Qt::Horizontal, tr("Id"));
35     setHeaderData(Setup_PlatformId, Qt::Horizontal, tr("Platform id"));
36     setHeaderData(Setup_MediaTypeId, Qt::Horizontal, tr("Media type id"));
37     setHeaderData(Setup_FileTypeExtensions, Qt::Horizontal, tr("File types"));
38     setHeaderData(Setup_Name, Qt::Horizontal, tr("Name"));
39 }
40
41 QString SetupModel::constructSelect(QString where) const
42 {
43     return QString(
44         "SELECT setup.id AS SetupId, "
45         "setup.platformid AS PlatformId, "
46         "setup.mediatypeid AS MediaTypeId, "
47         "setup.filetypeextensions AS SupportedFileTypeExtensions, "
48         "platform.name || ' ' || mediatype.name AS SetupName "
49         "FROM setup "
50         "INNER JOIN platform ON setup.platformid=platform.id "
51         "INNER JOIN mediatype ON setup.mediatypeid=mediatype.id %1 "
52         "ORDER BY SetupName"
53         ).arg(where);
54 }
55
56 Qt::ItemFlags SetupModel::flags(const QModelIndex &index) const
57 {
58     Qt::ItemFlags flags = QSqlQueryModel::flags(index);
59     int col = index.column();
60     if (col == Setup_PlatformId ||
61         col == Setup_MediaTypeId ||
62         col == Setup_FileTypeExtensions) {
63         flags |= Qt::ItemIsEditable;
64     }
65     return flags;
66 }
67
68 bool SetupModel::setData(const QModelIndex &index, const QVariant &value, int /*role*/)
69 {
70     int col = index.column();
71     if(col != Setup_PlatformId &&
72         col != Setup_MediaTypeId &&
73         col != Setup_FileTypeExtensions)
74         return false;
75
76     QModelIndex primaryKeyIndex
77         = QSqlQueryModel::index(index.row(), Setup_Id);
78
79     int id = data(primaryKeyIndex).toInt();
80     clear();
81
82     bool ok;
83     switch(index.column()) {
84
85     case Setup_PlatformId:
86         ok = setPlatform(id, value.toInt());
87         break;
88
89     case Setup_MediaTypeId:
90         ok = setMediaType(id, value.toInt());
91         break;
92
93     case Setup_FileTypeExtensions:
94         ok = setSupportedExtensions(id, value.toString());
95         break;
96
97     default:
98         qDebug() << "Setup model, this shouldn't be happening!";
99     };
100     refresh();
101     return ok;
102 }
103
104 bool SetupModel::setPlatform(int id, int platformId)
105 {
106     QSqlQuery query;
107     query.prepare(QString("update setup set platformid = :platformid where id = :id"));
108     query.bindValue(":platformid", platformId);
109     query.bindValue(":id", id);
110     return query.exec();
111 }
112
113 bool SetupModel::setMediaType(int id, int mediaTypeId)
114 {
115     QSqlQuery query;
116     query.prepare(QString("update setup set mediatypeid = :mediatypeid where id = :id"));
117     query.bindValue(":mediatypeid", mediaTypeId);
118     query.bindValue(":id", id);
119     return query.exec();
120 }
121
122 bool SetupModel::setSupportedExtensions(int id, QString exts)
123 {
124     QSqlQuery query;
125     query.prepare(QString("update setup set filetypeextensions = :exts where id = :id"));
126     query.bindValue(":exts", exts);
127     query.bindValue(":id", id);
128     return query.exec();
129 }
130
131