Emitting a dataChanged signal from setData after succesful update and connecting...
[emufront] / src / models / emufrontfileobjectmodel.cpp
1 /*
2 ** EmuFront
3 ** Copyright 2010 Mikko Keinänen
4 **
5 ** This file is part of EmuFront.
6 **
7 **
8 ** EmuFront is free software: you can redistribute it and/or modify
9 ** it under the terms of the GNU General Public License version 2 as published by
10 ** the Free Software Foundation and appearing in the file gpl.txt included in the
11 ** packaging of this file.
12 **
13 ** EmuFront is distributed in the hope that it will be useful,
14 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
15 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 ** GNU General Public License for more details.
17 **
18 ** You should have received a copy of the GNU General Public License
19 ** along with EmuFront.  If not, see <http://www.gnu.org/licenses/>.
20 */
21
22 #include <QtSql>
23 #include "emufrontfileobjectmodel.h"
24 #include "emufrontfile.h"
25 #include "emufrontfileobject.h"
26
27 EmuFrontFileObjectModel::EmuFrontFileObjectModel(QObject *parent) :
28     EmuFrontQueryModel(parent)
29 { }
30
31 Qt::ItemFlags EmuFrontFileObjectModel::flags(const QModelIndex &index) const
32 {
33     Qt::ItemFlags flags = QSqlQueryModel::flags(index);
34     if (index.column() == EmuFrontFileObject_Name) {
35         flags |= Qt::ItemIsEditable;
36     }
37     return flags;
38 }
39
40 bool EmuFrontFileObjectModel::setData(const QModelIndex &index, const QVariant &value, int /*role*/)
41 {
42     if(index.column() != EmuFrontFileObject_Name)
43         return false;
44
45     QModelIndex primaryKeyIndex
46         = QSqlQueryModel::index(index.row(), EmuFrontFileObject_Id);
47
48     int id = data(primaryKeyIndex).toInt();
49     clear();
50
51     bool ok;
52     if (index.column() == EmuFrontFileObject_Name) {
53         ok = setName(id, value.toString());
54     }
55
56     refresh();
57         if (ok) emit dataChanged();
58     return ok;
59 }
60
61 void EmuFrontFileObjectModel::refresh()
62  {
63      setQuery(constructSelect());
64      setHeaderData(EmuFrontFileObject_Id, Qt::Horizontal, tr("ID"));
65      setHeaderData(EmuFrontFileObject_Name, Qt::Horizontal, tr("Name"));
66      setHeaderData(EmuFrontFileObject_FileId, Qt::Horizontal, tr("FileID"));
67      setHeaderData(EmuFrontFileObject_FileName, Qt::Horizontal, tr("File Name"));
68      setHeaderData(EmuFrontFileObject_FileCheckSum, Qt::Horizontal, tr("File Checksum"));
69      setHeaderData(EmuFrontFileObject_FileSize, Qt::Horizontal, tr("File Size"));
70      setHeaderData(EmuFrontFileObject_FileType, Qt::Horizontal, tr("File Type"));
71      setHeaderData(EmuFrontFileObject_FileUpdateTime, Qt::Horizontal, tr("File Updated"));
72  }
73
74 QString EmuFrontFileObjectModel::constructSelect(QString where) const
75 {
76     return QString("SELECT maintbl.id AS FileObjectId, "
77             "maintbl.name AS Name, "
78             "file.id AS FileId, "
79             "file.name AS FileName, "
80             "file.type AS FileType, "
81             "file.checksum AS FileChecksum, "
82             "file.size AS FileSize, "
83             "file.updatetime AS FileUpdateTime "
84             "FROM %1 AS maintbl "
85             "LEFT OUTER JOIN file ON maintbl.fileid=file.id "
86             "%2 "
87             "ORDER BY Name").arg(tableName).arg(where);
88 }
89
90 bool EmuFrontFileObjectModel::setName(int id, const QString &name)
91 {
92     QSqlQuery query;
93     query.prepare(QString("update %1 set name = :name where id = :id").arg(tableName));
94     query.bindValue(":name", name);
95     query.bindValue(":id", id);
96     return query.exec();
97 }
98
99 bool EmuFrontFileObjectModel::insertRows(int row, int count, const QModelIndex &parent)
100 {
101     if (parent.isValid())
102         return false; // This is a flat model
103     if (rowCount() < row)
104         row = rowCount() + 1;
105     QSqlQuery q;
106     q.prepare(QString("INSERT INTO %1 (id, name, fileid) "
107         " VALUES (NULL, '', NULL) ").arg(tableName));
108     beginInsertRows(QModelIndex(), row, row + count - 1);
109     for (int i = 0; i < count; ++i) {
110         q.exec();
111     }
112     endInsertRows();
113     refresh();
114     return true;
115 }
116
117 bool EmuFrontFileObjectModel::removeRows(int row, int count, const QModelIndex &parent)
118 {
119      if (parent.isValid()) {
120         return false; // This is a flat model
121     }
122     if (rowCount() < row + count - 1)
123         return false;
124
125     QSqlQuery q;
126     q.prepare(QString("DELETE FROM %1 WHERE id=:id").arg(tableName));
127     QModelIndex primaryIndex;
128     int id = -1;
129     beginRemoveRows(QModelIndex(), row, row + count - 1);
130     for(int i = 0; i < count; ++i) {
131         primaryIndex = QSqlQueryModel::index(row + i, EmuFrontFileObject_Id);
132         id = data(primaryIndex).toInt();
133         qDebug() << "Removing data item with id " << id;
134         q.bindValue(":id", id);
135         q.exec();
136     }
137     endRemoveRows();
138     refresh();
139     return true;
140 }
141
142 // Implemented for EmuFrontQueryModel:
143 EmuFrontObject* EmuFrontFileObjectModel::recordToDataObject(const QSqlRecord* record)
144 {
145     int id = record->value(EmuFrontFileObject_Id).toInt();
146     QString name = record->value(EmuFrontFileObject_Name).toString();
147     int fileId = record->value(EmuFrontFileObject_FileId).toInt();
148     EmuFrontFile *f = 0;
149     /*if (fileId > 0)
150     {
151         // TODO: need fileModel
152         EmuFrontObject *o = fileModel.getDataObject(fileId);
153         f = dynamic_cast<EmuFrontFile*>(o);
154     }*/
155     EmuFrontObject *efo = createEmuFrontFileObject(id, name, f);
156     return efo;
157 }
158
159 QString EmuFrontFileObjectModel::constructFilterById(int id) const
160 {
161     return QString("maintbl.id = %1").arg(id);
162 }