2 // Copyright 2010 Mikko Keinänen
4 // This file is part of EmuFront.
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.
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.
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/>.
21 #include "emufrontfileobjectmodel.h"
23 EmuFrontFileObjectModel::EmuFrontFileObjectModel(QObject *parent) :
24 EmuFrontQueryModel(parent)
27 Qt::ItemFlags EmuFrontFileObjectModel::flags(const QModelIndex &index) const
29 Qt::ItemFlags flags = QSqlQueryModel::flags(index);
30 if (index.column() == EmuFrontFileObject_Name) {
31 flags |= Qt::ItemIsEditable;
36 bool EmuFrontFileObjectModel::setData(const QModelIndex &index, const QVariant &value, int /*role*/)
38 if(index.column() != EmuFrontFileObject_Name)
41 QModelIndex primaryKeyIndex
42 = QSqlQueryModel::index(index.row(), EmuFrontFileObject_Id);
44 int id = data(primaryKeyIndex).toInt();
48 if (index.column() == EmuFrontFileObject_Name) {
49 ok = setName(id, value.toString());
56 void EmuFrontFileObjectModel::refresh()
58 setQuery(constructSelect());
59 setHeaderData(EmuFrontFileObject_Id, Qt::Horizontal, tr("ID"));
60 setHeaderData(EmuFrontFileObject_Name, Qt::Horizontal, tr("Name"));
61 setHeaderData(EmuFrontFileObject_FileId, Qt::Horizontal, tr("FileID"));
62 setHeaderData(EmuFrontFileObject_FileName, Qt::Horizontal, tr("File Name"));
63 setHeaderData(EmuFrontFileObject_FileCheckSum, Qt::Horizontal, tr("File Checksum"));
64 setHeaderData(EmuFrontFileObject_FileSize, Qt::Horizontal, tr("File Size"));
65 setHeaderData(EmuFrontFileObject_FileType, Qt::Horizontal, tr("File Type"));
66 setHeaderData(EmuFrontFileObject_FileUpdateTime, Qt::Horizontal, tr("File Updated"));
69 QString EmuFrontFileObjectModel::constructSelect(QString where) const
71 return QString("SELECT maintbl.id AS FileObjectId, "
72 "maintbl.name AS Name, "
74 "file.name AS FileName, "
75 "file.type AS FileType, "
76 "file.checksum AS FileChecksum, "
77 "file.size AS FileSize, "
78 "file.updatetime AS FileUpdateTime "
80 "LEFT OUTER JOIN file ON maintbl.fileid=file.id "
82 "ORDER BY Name").arg(tableName).arg(where);
85 bool EmuFrontFileObjectModel::setName(int id, const QString &name)
88 query.prepare(QString("update %1 set name = :name where id = :id").arg(tableName));
89 query.bindValue(":name", name);
90 query.bindValue(":id", id);
94 bool EmuFrontFileObjectModel::insertRows(int row, int count, const QModelIndex &parent)
100 qDebug() << "Inserting " << count << " rows from row " << row;
102 q.prepare(QString("INSERT INTO %1 (id, name, fileid) "
103 " VALUES (NULL, '', NULL) ").arg(tableName));
104 beginInsertRows(QModelIndex(), row, row + count - 1);
105 for (int i = 0; i < count; ++i) {