Implemented insertRows to model.
[emufront] / src / db / emufrontfileobjectmodel.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 "emufrontfileobjectmodel.h"
22
23 EmuFrontFileObjectModel::EmuFrontFileObjectModel(QObject *parent) :
24     EmuFrontQueryModel(parent)
25 { }
26
27 Qt::ItemFlags EmuFrontFileObjectModel::flags(const QModelIndex &index) const
28 {
29     Qt::ItemFlags flags = QSqlQueryModel::flags(index);
30     if (index.column() == EmuFrontFileObject_Name) {
31         flags |= Qt::ItemIsEditable;
32     }
33     return flags;
34 }
35
36 bool EmuFrontFileObjectModel::setData(const QModelIndex &index, const QVariant &value, int /*role*/)
37 {
38     if(index.column() != EmuFrontFileObject_Name)
39         return false;
40
41     QModelIndex primaryKeyIndex
42         = QSqlQueryModel::index(index.row(), EmuFrontFileObject_Id);
43
44     int id = data(primaryKeyIndex).toInt();
45     clear();
46
47     bool ok;
48     if (index.column() == EmuFrontFileObject_Name) {
49         ok = setName(id, value.toString());
50     }
51
52     refresh();
53     return ok;
54 }
55
56 void EmuFrontFileObjectModel::refresh()
57  {
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"));
67  }
68
69 QString EmuFrontFileObjectModel::constructSelect(QString where) const
70 {
71     return QString("SELECT maintbl.id AS FileObjectId, "
72             "maintbl.name AS Name, "
73             "file.id AS FileId, "
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 "
79             "FROM %1 AS maintbl "
80             "LEFT OUTER JOIN file ON maintbl.fileid=file.id "
81             "%2 "
82             "ORDER BY Name").arg(tableName).arg(where);
83 }
84
85 bool EmuFrontFileObjectModel::setName(int id, const QString &name)
86 {
87     QSqlQuery query;
88     query.prepare(QString("update %1 set name = :name where id = :id").arg(tableName));
89     query.bindValue(":name", name);
90     query.bindValue(":id", id);
91     return query.exec();
92 }
93
94 bool EmuFrontFileObjectModel::insertRows(int row, int count, const QModelIndex &parent)
95 {
96     if (parent.isValid())
97         return false;
98     if (rowCount() < row)
99         row = rowCount() + 1;
100     qDebug() << "Inserting " << count << " rows from row " << row;
101     QSqlQuery q;
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) {
106         q.exec();
107     }
108     endInsertRows();
109     return true;
110 }