Started implementing better MVC support: thanks Petro for the idea! ;)
[emufront] / src / db / platformmodel.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 <QtSql>
21 #include "platformmodel.h"
22
23 PlatformModel::PlatformModel(QObject *parent) :
24     EmuFrontQueryModel(parent)
25 {
26     tableName = "platform";
27     refresh();
28 }
29
30 Qt::ItemFlags PlatformModel::flags(const QModelIndex &index) const
31 {
32     Qt::ItemFlags flags = QSqlQueryModel::flags(index);
33     if (index.column() == EmuFrontFileObject_Name) {
34         flags |= Qt::ItemIsEditable;
35     }
36     return flags;
37 }
38
39 bool PlatformModel::setData(const QModelIndex &index, const QVariant &value, int /*role*/)
40 {
41     if(index.column() != EmuFrontFileObject_Name)
42         return false;
43
44     QModelIndex primaryKeyIndex
45         = QSqlQueryModel::index(index.row(), EmuFrontFileObject_Id);
46
47     int id = data(primaryKeyIndex).toInt();
48     clear();
49
50     bool ok;
51     if (index.column() == EmuFrontFileObject_Name) {
52         ok = setName(id, value.toString());
53     }
54
55     refresh();
56     return ok;
57 }
58
59 void PlatformModel::refresh()
60  {
61      setQuery(constructSelect());
62      setHeaderData(EmuFrontFileObject_Id, Qt::Horizontal, tr("ID"));
63      setHeaderData(EmuFrontFileObject_Name, Qt::Horizontal, tr("Name"));
64      setHeaderData(EmuFrontFileObject_FileId, Qt::Horizontal, tr("FileID"));
65      setHeaderData(EmuFrontFileObject_FileName, Qt::Horizontal, tr("File Name"));
66      setHeaderData(EmuFrontFileObject_FileCheckSum, Qt::Horizontal, tr("File Checksum"));
67      setHeaderData(EmuFrontFileObject_FileSize, Qt::Horizontal, tr("File Size"));
68      setHeaderData(EmuFrontFileObject_FileType, Qt::Horizontal, tr("File Type"));
69      setHeaderData(EmuFrontFileObject_FileUpdateTime, Qt::Horizontal, tr("File Updated"));
70  }
71
72 QString PlatformModel::constructSelect(QString where) const
73 {
74     return QString("SELECT maintbl.id AS FileObjectId, "
75             "maintbl.name AS Name, "
76             "file.id AS FileId, "
77             "file.name AS FileName, "
78             "file.type AS FileType, "
79             "file.checksum AS FileChecksum, "
80             "file.size AS FileSize, "
81             "file.updatetime AS FileUpdateTime "
82             "FROM %1 AS maintbl "
83             "LEFT OUTER JOIN file ON maintbl.fileid=file.id "
84             "%2 "
85             "ORDER BY Name").arg(tableName).arg(where);
86 }
87
88 bool PlatformModel::setName(int id, const QString &name)
89 {
90     QSqlQuery query;
91     query.prepare("update platform set name = :name where id = :id");
92     query.bindValue(":name", name);
93     query.bindValue(":id", id);
94     return query.exec();
95 }
96