Added common base model for emu front objects
authorMikko Keinnen <mikko.keinanen@gmail.com>
Wed, 1 Dec 2010 21:37:02 +0000 (23:37 +0200)
committerMikko Keinänen <mikko.keinanen@gmail.com>
Wed, 1 Dec 2010 21:37:02 +0000 (23:37 +0200)
with file member.

src/db/emufrontfileobjectmodel.cpp [new file with mode: 0644]
src/db/emufrontfileobjectmodel.h [new file with mode: 0644]
src/db/platformmodel.cpp
src/db/platformmodel.h
src/emufront.pro

diff --git a/src/db/emufrontfileobjectmodel.cpp b/src/db/emufrontfileobjectmodel.cpp
new file mode 100644 (file)
index 0000000..9d5ea27
--- /dev/null
@@ -0,0 +1,92 @@
+// EmuFront
+// Copyright 2010 Mikko Keinänen
+//
+// This file is part of EmuFront.
+//
+//
+// EmuFront is free software: you can redistribute it and/or modify
+// it under the terms of the GNU General Public License version 2 as published by
+// the Free Software Foundation and appearing in the file gpl.txt included in the
+// packaging of this file.
+//
+// EmuFront is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License
+// along with EmuFront.  If not, see <http://www.gnu.org/licenses/>.
+
+#include <QtSql>
+#include "emufrontfileobjectmodel.h"
+
+EmuFrontFileObjectModel::EmuFrontFileObjectModel(QObject *parent) :
+    EmuFrontQueryModel(parent)
+{ }
+
+Qt::ItemFlags EmuFrontFileObjectModel::flags(const QModelIndex &index) const
+{
+    Qt::ItemFlags flags = QSqlQueryModel::flags(index);
+    if (index.column() == EmuFrontFileObject_Name) {
+        flags |= Qt::ItemIsEditable;
+    }
+    return flags;
+}
+
+bool EmuFrontFileObjectModel::setData(const QModelIndex &index, const QVariant &value, int /*role*/)
+{
+    if(index.column() != EmuFrontFileObject_Name)
+        return false;
+
+    QModelIndex primaryKeyIndex
+        = QSqlQueryModel::index(index.row(), EmuFrontFileObject_Id);
+
+    int id = data(primaryKeyIndex).toInt();
+    clear();
+
+    bool ok;
+    if (index.column() == EmuFrontFileObject_Name) {
+        ok = setName(id, value.toString());
+    }
+
+    refresh();
+    return ok;
+}
+
+void EmuFrontFileObjectModel::refresh()
+ {
+     setQuery(constructSelect());
+     setHeaderData(EmuFrontFileObject_Id, Qt::Horizontal, tr("ID"));
+     setHeaderData(EmuFrontFileObject_Name, Qt::Horizontal, tr("Name"));
+     setHeaderData(EmuFrontFileObject_FileId, Qt::Horizontal, tr("FileID"));
+     setHeaderData(EmuFrontFileObject_FileName, Qt::Horizontal, tr("File Name"));
+     setHeaderData(EmuFrontFileObject_FileCheckSum, Qt::Horizontal, tr("File Checksum"));
+     setHeaderData(EmuFrontFileObject_FileSize, Qt::Horizontal, tr("File Size"));
+     setHeaderData(EmuFrontFileObject_FileType, Qt::Horizontal, tr("File Type"));
+     setHeaderData(EmuFrontFileObject_FileUpdateTime, Qt::Horizontal, tr("File Updated"));
+ }
+
+QString EmuFrontFileObjectModel::constructSelect(QString where) const
+{
+    return QString("SELECT maintbl.id AS FileObjectId, "
+            "maintbl.name AS Name, "
+            "file.id AS FileId, "
+            "file.name AS FileName, "
+            "file.type AS FileType, "
+            "file.checksum AS FileChecksum, "
+            "file.size AS FileSize, "
+            "file.updatetime AS FileUpdateTime "
+            "FROM %1 AS maintbl "
+            "LEFT OUTER JOIN file ON maintbl.fileid=file.id "
+            "%2 "
+            "ORDER BY Name").arg(tableName).arg(where);
+}
+
+bool EmuFrontFileObjectModel::setName(int id, const QString &name)
+{
+    QSqlQuery query;
+    query.prepare(QString("update %1 set name = :name where id = :id").arg(tableName));
+    query.bindValue(":name", name);
+    query.bindValue(":id", id);
+    return query.exec();
+}
diff --git a/src/db/emufrontfileobjectmodel.h b/src/db/emufrontfileobjectmodel.h
new file mode 100644 (file)
index 0000000..36398ac
--- /dev/null
@@ -0,0 +1,49 @@
+// EmuFront
+// Copyright 2010 Mikko Keinänen
+//
+// This file is part of EmuFront.
+//
+//
+// EmuFront is free software: you can redistribute it and/or modify
+// it under the terms of the GNU General Public License version 2 as published by
+// the Free Software Foundation and appearing in the file gpl.txt included in the
+// packaging of this file.
+//
+// EmuFront is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License
+// along with EmuFront.  If not, see <http://www.gnu.org/licenses/>.
+
+#ifndef EMUFRONTFILEOBJECTMODEL_H
+#define EMUFRONTFILEOBJECTMODEL_H
+
+#include "emufrontquerymodel.h"
+
+class EmuFrontFileObjectModel : public EmuFrontQueryModel
+{
+    Q_OBJECT
+public:
+    EmuFrontFileObjectModel(QObject *parent = 0);
+    Qt::ItemFlags flags(const QModelIndex &index) const;
+    bool setData(const QModelIndex &index, const QVariant &value, int role);
+    enum {
+        EmuFrontFileObject_Id,
+        EmuFrontFileObject_Name,
+        EmuFrontFileObject_FileId,
+        EmuFrontFileObject_FileName,
+        EmuFrontFileObject_FileType,
+        EmuFrontFileObject_FileCheckSum,
+        EmuFrontFileObject_FileSize,
+        EmuFrontFileObject_FileUpdateTime
+    };
+
+protected:
+    void refresh();
+    QString constructSelect(QString where = "") const;
+    bool setName(int id, const QString &name);
+};
+
+#endif // EMUFRONTFILEOBJECTMODEL_H
index 328755b..66a2815 100644 (file)
 // You should have received a copy of the GNU General Public License
 // along with EmuFront.  If not, see <http://www.gnu.org/licenses/>.
 
-#include <QtSql>
 #include "platformmodel.h"
 
 PlatformModel::PlatformModel(QObject *parent) :
-    EmuFrontQueryModel(parent)
+    EmuFrontFileObjectModel(parent)
 {
     tableName = "platform";
     refresh();
 }
-
-Qt::ItemFlags PlatformModel::flags(const QModelIndex &index) const
-{
-    Qt::ItemFlags flags = QSqlQueryModel::flags(index);
-    if (index.column() == EmuFrontFileObject_Name) {
-        flags |= Qt::ItemIsEditable;
-    }
-    return flags;
-}
-
-bool PlatformModel::setData(const QModelIndex &index, const QVariant &value, int /*role*/)
-{
-    if(index.column() != EmuFrontFileObject_Name)
-        return false;
-
-    QModelIndex primaryKeyIndex
-        = QSqlQueryModel::index(index.row(), EmuFrontFileObject_Id);
-
-    int id = data(primaryKeyIndex).toInt();
-    clear();
-
-    bool ok;
-    if (index.column() == EmuFrontFileObject_Name) {
-        ok = setName(id, value.toString());
-    }
-
-    refresh();
-    return ok;
-}
-
-void PlatformModel::refresh()
- {
-     setQuery(constructSelect());
-     setHeaderData(EmuFrontFileObject_Id, Qt::Horizontal, tr("ID"));
-     setHeaderData(EmuFrontFileObject_Name, Qt::Horizontal, tr("Name"));
-     setHeaderData(EmuFrontFileObject_FileId, Qt::Horizontal, tr("FileID"));
-     setHeaderData(EmuFrontFileObject_FileName, Qt::Horizontal, tr("File Name"));
-     setHeaderData(EmuFrontFileObject_FileCheckSum, Qt::Horizontal, tr("File Checksum"));
-     setHeaderData(EmuFrontFileObject_FileSize, Qt::Horizontal, tr("File Size"));
-     setHeaderData(EmuFrontFileObject_FileType, Qt::Horizontal, tr("File Type"));
-     setHeaderData(EmuFrontFileObject_FileUpdateTime, Qt::Horizontal, tr("File Updated"));
- }
-
-QString PlatformModel::constructSelect(QString where) const
-{
-    return QString("SELECT maintbl.id AS FileObjectId, "
-            "maintbl.name AS Name, "
-            "file.id AS FileId, "
-            "file.name AS FileName, "
-            "file.type AS FileType, "
-            "file.checksum AS FileChecksum, "
-            "file.size AS FileSize, "
-            "file.updatetime AS FileUpdateTime "
-            "FROM %1 AS maintbl "
-            "LEFT OUTER JOIN file ON maintbl.fileid=file.id "
-            "%2 "
-            "ORDER BY Name").arg(tableName).arg(where);
-}
-
-bool PlatformModel::setName(int id, const QString &name)
-{
-    QSqlQuery query;
-    query.prepare("update platform set name = :name where id = :id");
-    query.bindValue(":name", name);
-    query.bindValue(":id", id);
-    return query.exec();
-}
-
index f7703e4..eef53ad 100644 (file)
 #ifndef PLATFORMMODEL_H
 #define PLATFORMMODEL_H
 
-#include "emufrontquerymodel.h"
+#include "emufrontfileobjectmodel.h"
 
-class PlatformModel : public EmuFrontQueryModel
+class PlatformModel : public EmuFrontFileObjectModel
 {
     Q_OBJECT
 public:
     PlatformModel(QObject *parent = 0);
-    Qt::ItemFlags flags(const QModelIndex &index) const;
-    bool setData(const QModelIndex &index, const QVariant &value, int role);
-    enum {
-        EmuFrontFileObject_Id,
-        EmuFrontFileObject_Name,
-        EmuFrontFileObject_FileId,
-        EmuFrontFileObject_FileName,
-        EmuFrontFileObject_FileType,
-        EmuFrontFileObject_FileCheckSum,
-        EmuFrontFileObject_FileSize,
-        EmuFrontFileObject_FileUpdateTime
-    };
-
-signals:
-
-public slots:
-
-private:
-
-    void refresh();
-    QString constructSelect(QString where = "") const;
-    bool setName(int id, const QString &name);
-
 };
 
 #endif // PLATFORMMODEL_H
index 5f3365f..36f2c9e 100644 (file)
@@ -74,7 +74,8 @@ HEADERS += mainwindow.h \
     db/platformmodel.h \
     dialogs/platformmaindialog.h \
     dialogs/emufrontdatadialog.h \
-    db/emufrontquerymodel.h
+    db/emufrontquerymodel.h \
+    db/emufrontfileobjectmodel.h
 SOURCES += main.cpp \
     mainwindow.cpp \
     db/databasemanager.cpp \
@@ -136,7 +137,8 @@ SOURCES += main.cpp \
     db/platformmodel.cpp \
     dialogs/platformmaindialog.cpp \
     dialogs/emufrontdatadialog.cpp \
-    db/emufrontquerymodel.cpp
+    db/emufrontquerymodel.cpp \
+    db/emufrontfileobjectmodel.cpp
 OTHER_FILES +=  
 
 CONFIG += mobility