Added new data class Setup to contain the setup information (platform,
authorMikko Keinänen <mikko.keinanen@gmail.com>
Sun, 6 Jun 2010 22:15:09 +0000 (01:15 +0300)
committerMikko Keinänen <mikko.keinanen@gmail.com>
Sun, 6 Jun 2010 22:15:09 +0000 (01:15 +0300)
media type, and supported file type extensions). Setup replaced
mediatype and platform in filepath class.

26 files changed:
doc/er.dia [new file with mode: 0644]
src/dataobjects/emufrontobject.cpp
src/dataobjects/emufrontobject.h
src/dataobjects/filepathobject.cpp
src/dataobjects/filepathobject.h
src/dataobjects/setup.cpp [new file with mode: 0644]
src/dataobjects/setup.h [new file with mode: 0644]
src/db/databasemanager.cpp
src/db/databasemanager.h
src/db/dbcreator.cpp
src/db/dbfilepath.cpp
src/db/dbfilepath.h
src/db/dbmediatype.cpp
src/db/dbmediatype.h
src/db/dbplatform.cpp
src/db/dbplatform.h
src/db/dbquerymodelmanager.cpp [new file with mode: 0644]
src/db/dbquerymodelmanager.h [new file with mode: 0644]
src/db/dbsetup.cpp [new file with mode: 0644]
src/db/dbsetup.h [new file with mode: 0644]
src/db/dbtablemodelmanager.cpp [new file with mode: 0644]
src/db/dbtablemodelmanager.h [new file with mode: 0644]
src/dialogs/dbobjectdialog.cpp
src/dialogs/mediaimagepathdialog.cpp
src/dialogs/mediaimagepathdialog.h
src/emufront.pro

diff --git a/doc/er.dia b/doc/er.dia
new file mode 100644 (file)
index 0000000..d78d7ed
Binary files /dev/null and b/doc/er.dia differ
index ac5b6d0..d3a1073 100644 (file)
@@ -23,18 +23,18 @@ EmuFrontObject::EmuFrontObject() : id(-1), name("")
 {
 }
 
-/*EmuFrontObject::EmuFrontObject(const EmuFrontObject &ob)
-    : id(ob.id), name(ob.name)
+EmuFrontObject::EmuFrontObject(const EmuFrontObject &ob)
+    : QObject(), id(ob.id), name(ob.name)
 {
     // no need to perform deep copy here, see:
     // http://doc.trolltech.com/4.0/shclass.html
-}*/
+}
 
 EmuFrontObject::EmuFrontObject(int id, QString name)
     : id(id), name(name)
 {}
 
-/*EmuFrontObject::~EmuFrontObject()
+EmuFrontObject::~EmuFrontObject()
 {
 }
 
@@ -44,5 +44,5 @@ EmuFrontObject& EmuFrontObject::operator =(const EmuFrontObject &ob)
     id = ob.id;
     name = ob.name;
     return (*this);
-}*/
+}
 
index 245bcfd..76a9583 100644 (file)
@@ -29,16 +29,16 @@ public:
     EmuFrontObject(int id, QString name);
 
     // No need for these as long we use QString (see Implicit Data Sharing)
-    //EmuFrontObject(const EmuFrontObject &);
-    //virtual ~EmuFrontObject();
-    //EmuFrontObject &operator=(const EmuFrontObject &);
+    EmuFrontObject(const EmuFrontObject &);
+    ~EmuFrontObject();
+    EmuFrontObject &operator=(const EmuFrontObject &);
 
     virtual const QString getName() const
     { return name; }
     virtual int getId() const
     { return id; }
     virtual void setName(QString name)
-    { this->name = name; };
+    { this->name = name; }
     virtual void setId(int id)
     { this->id = id; }
 
index 4caf49f..2d86d97 100644 (file)
@@ -1,33 +1,27 @@
 #include "filepathobject.h"
+#include "setup.h"
 
-FilePathObject::FilePathObject() : EmuFrontFileObject(), platform(0), mediaType(0)
+FilePathObject::FilePathObject() : EmuFrontFileObject(), setup(0)
 {
 }
 
 FilePathObject::FilePathObject(int id, QString name, QString filename, int filetype)
-    : EmuFrontFileObject(id, name, filename, filetype), platform(0), mediaType(0)
- {}
+    : EmuFrontFileObject(id, name, filename, filetype), setup(0) {}
 
  FilePathObject::FilePathObject(int id, QString name, QString filename,
-     int filetype, Platform *plf, MediaType *med)
-    : EmuFrontFileObject(id, name, filename, filetype), platform(plf), mediaType(med)
- {}
+     int filetype, Setup *setup)
+    : EmuFrontFileObject(id, name, filename, filetype), setup(setup) {}
 
 FilePathObject::~FilePathObject()
 {
-    if (platform) delete platform;
-    if (mediaType) delete mediaType;
+    if (setup) delete setup;
 }
 
 FilePathObject::FilePathObject(const FilePathObject &fpobj)
     : EmuFrontFileObject(fpobj.id, fpobj.name, fpobj.filename, fpobj.filetype)
 {
-    // Note: no need to deep copy members of type QString
-    // QString uses Implicit Data Sharing (http://doc.trolltech.com/4.0/shclass.html)
-    Platform *p = fpobj.platform;
-    platform = new Platform(p->getId(), p->getName(), p->getFilename());
-    MediaType *mt = fpobj.mediaType;
-    mediaType = new MediaType(mt->getId(), mt->getName(), mt->getFilename());
+    Setup *s = fpobj.setup;
+    setup = new Setup(*s);
 }
 
 FilePathObject& FilePathObject::operator =(const FilePathObject &fpobj)
@@ -37,11 +31,13 @@ FilePathObject& FilePathObject::operator =(const FilePathObject &fpobj)
     name = fpobj.name;
     filename = fpobj.filename;
     filetype = fpobj.filetype;
-    if (platform) delete platform;
-    Platform *p = fpobj.platform;
-    platform = new Platform(p->getId(), p->getName(), p->getFilename());
-    if (mediaType) delete mediaType;
-    MediaType *mt = fpobj.mediaType;
-    mediaType = new MediaType(mt->getId(), mt->getName(), mt->getFilename());
+    if (setup) delete setup;
+    Setup *sup = fpobj.setup;
+    setup = new Setup(*sup);
     return (*this);
 }
+
+Setup* FilePathObject::getSetup() const
+{ return setup; }
+void FilePathObject::setSetup(Setup *sup)
+{ setup = sup; }
index a1cf24a..41099a3 100644 (file)
@@ -21,8 +21,8 @@
 #define FILEPATHOBJECT_H
 
 #include "emufrontfileobject.h"
-#include "platform.h"
-#include "mediatype.h"
+
+class Setup;
 
 class FilePathObject : public EmuFrontFileObject
 {
@@ -30,23 +30,15 @@ public:
     FilePathObject();
     ~FilePathObject();
     FilePathObject(int id, QString name, QString filename, int filetype);
-    FilePathObject(int id, QString name, QString filename, int filetype, Platform*, MediaType*);
+    FilePathObject(int id, QString name, QString filename, int filetype, Setup*);
     FilePathObject(const FilePathObject &);
     FilePathObject& operator=(const FilePathObject &);
 
-    Platform* getPlatform() const
-    { return platform; }
-    MediaType* getMediaType() const
-    { return mediaType; }
-    void setPlatform(Platform *plf)
-    { platform = plf; }
-    void setMediaType(MediaType *med)
-    { mediaType = med; }
+    Setup* getSetup() const;
+    void setSetup(Setup *);
 
 private:
-    Platform *platform;
-    MediaType *mediaType;
-
+    Setup *setup;
 };
 
 #endif // FILEPATHOBJECT_H
diff --git a/src/dataobjects/setup.cpp b/src/dataobjects/setup.cpp
new file mode 100644 (file)
index 0000000..3ebc820
--- /dev/null
@@ -0,0 +1,80 @@
+// 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 as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// Foobar 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 Foobar.  If not, see <http://www.gnu.org/licenses/>.
+
+#include "setup.h"
+#include "platform.h"
+#include "mediatype.h"
+
+Setup::Setup() : EmuFrontObject()
+{
+}
+
+Setup::Setup(int id, Platform *plf, MediaType *mt, QStringList fileTypeExtensions)
+    : EmuFrontObject(id,
+        QString("%1%2")
+            .arg(plf ? plf->getName() : "")
+            .arg(mt ? mt->getName() : "")),
+        fileTypeExtensions(fileTypeExtensions)
+{
+}
+
+Setup::~Setup()
+{
+    delete platform;
+    delete mediaType;
+}
+
+Setup::Setup(const Setup &s)
+        : EmuFrontObject(s.getId(), s.getName()),
+            fileTypeExtensions(s.fileTypeExtensions)
+{
+    Platform *p = s.platform;
+    MediaType *m = s.mediaType;
+    platform = new Platform(*p);
+    mediaType = new MediaType(*m);
+}
+
+Setup& Setup::operator =(const Setup &sup)
+{
+    if (this == &sup) return *this;
+    id = sup.id;
+    name = sup.name;
+    if (platform) delete platform;
+    Platform *p = sup.platform;
+    MediaType *m = sup.mediaType;
+    platform = new Platform(*p);
+    if (mediaType) delete mediaType;
+    mediaType = new MediaType(*m);
+    return (*this);
+}
+
+Platform* Setup::getPlatform() const
+{ return platform; }
+void Setup::setPlatform(Platform *plf)
+{ platform = plf; }
+
+MediaType* Setup::getMediaType() const
+{ return mediaType; }
+void Setup::setMediaType(MediaType *mt)
+{ mediaType = mt; }
+
+QStringList Setup::getSupportedFileTypeExtensions() const
+{ return fileTypeExtensions; }
+void Setup::setSupportedFileTypeExtensions(QStringList ftx)
+{ fileTypeExtensions = ftx; }
diff --git a/src/dataobjects/setup.h b/src/dataobjects/setup.h
new file mode 100644 (file)
index 0000000..0a4c854
--- /dev/null
@@ -0,0 +1,51 @@
+// 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 as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// Foobar 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 Foobar.  If not, see <http://www.gnu.org/licenses/>.
+
+#ifndef SETUP_H
+#define SETUP_H
+#include <QStringList>
+#include "emufrontobject.h"
+
+class Platform;
+class MediaType;
+
+class Setup : public EmuFrontObject
+{
+public:
+    Setup();
+    ~Setup();
+    Setup(int id, Platform *, MediaType *, QStringList fileTypeExtensions);
+    Setup(const Setup &);
+    Setup& operator=(const Setup &);
+    Platform* getPlatform() const;
+    MediaType* getMediaType() const;
+    QStringList getSupportedFileTypeExtensions() const;
+    void setPlatform(Platform *);
+    void setMediaType(MediaType *);
+    void setSupportedFileTypeExtensions(QStringList);
+
+
+private:
+    Platform *platform;
+    MediaType *mediaType;
+    // QStringList is implicitly shared.
+    QStringList fileTypeExtensions;
+};
+
+#endif // SETUP_H
index 33cdbba..1c78356 100644 (file)
@@ -33,6 +33,7 @@ const QString DatabaseManager::DATABASE = QString("QSQLITE");
 const QString DatabaseManager::DB_TABLE_NAME_MEDIATYPE = QString("mediatype");
 const QString DatabaseManager::DB_TABLE_NAME_PLATFORM = QString("platform");
 const QString DatabaseManager::DB_TABLE_NAME_FILEPATH = QString("filepath");
+const QString DatabaseManager::DB_TABLE_NAME_SETUP = QString("setup");
 
 DatabaseManager::DatabaseManager(QObject *parent)
        : QObject(parent)
@@ -45,6 +46,11 @@ DatabaseManager::~DatabaseManager()
     // be destroyed when parent is destroyed
 }
 
+QSqlQueryModel* DatabaseManager::getDataModel()
+{
+    return sqlTableModel;
+}
+
 bool DatabaseManager::openDB()
 {
     QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
@@ -64,11 +70,10 @@ QString DatabaseManager::getDbPath()
        return path;
 }
 
-void DatabaseManager::resetModel() const
+void DatabaseManager::resetModel()
 {
     if (!sqlTableModel) return;
-    sqlTableModel->setFilter("");
-    sqlTableModel->select();
+    clearFilters();
 }
 
 // sql must return a count(*) value
@@ -83,10 +88,9 @@ int DatabaseManager::countRows(QString tableName, QString columnName, int id) co
     return numEntries;
 }
 
-EmuFrontObject* DatabaseManager::getDataObject(int id) const
+EmuFrontObject* DatabaseManager::getDataObject(int id)
 {
-    sqlTableModel->setFilter(QString("id = %1").arg(id));
-    sqlTableModel->select();
+    filterById(id);
     EmuFrontObject *plf = 0;
     if (sqlTableModel->rowCount() == 1)
     {
index 94ef8ba..858d1a9 100644 (file)
@@ -25,7 +25,7 @@
 
 class QSqlError;
 class QFile;
-class QSqlTableModel;
+class QSqlQueryModel;
 class QModelIndex;
 class EmuFrontObject;
 
@@ -35,15 +35,15 @@ public:
        DatabaseManager(QObject *parent = 0);
        ~DatabaseManager();
 
-    virtual QSqlTableModel* getDataModel() = 0;
+    QSqlQueryModel* getDataModel();
     EmuFrontObject* getDataObjectFromModel(QModelIndex*);
-    EmuFrontObject* getDataObject(int id) const;
+    EmuFrontObject* getDataObject(int id);
     virtual bool updateDataObjectToModel(const EmuFrontObject*) = 0;
     virtual bool insertDataObjectToModel(const EmuFrontObject*) = 0;
     virtual bool deleteDataObjectFromModel(QModelIndex*) = 0;
     virtual int countDataObjectRefs(int id) const = 0;
     static bool openDB();
-    void resetModel() const;
+    void resetModel();
     enum {
         Filetype_MediaImageContainer = 0,
         Filetype_Screenshot = 1,
@@ -51,18 +51,20 @@ public:
         Filetype_MediaTypeIcon = 3 };
 
 protected:
-    QSqlTableModel* sqlTableModel;
-    //virtual QSqlTableModel* getDataModel() = 0;
+    QSqlQueryModel* sqlTableModel;
     virtual EmuFrontObject* recordToDataObject(const QSqlRecord* ) const = 0;
+    virtual void filterById(int id) = 0;
+    virtual void clearFilters() = 0;
     int countRows(QString tableName, QString columnName, int id) const;
     static const QString DB_TABLE_NAME_FILEPATH;
     static const QString DB_TABLE_NAME_MEDIATYPE;
     static const QString DB_TABLE_NAME_PLATFORM;
+    static const QString DB_TABLE_NAME_SETUP;
 
 private:
        static const QString DB_FILENAME;
     static const QString DATABASE;
-    virtual QSqlTableModel* getData() = 0;
+    virtual QSqlQueryModel* getData() = 0;
     static QString getDbPath();
 };
 
index ab155dd..d004064 100644 (file)
@@ -45,6 +45,7 @@ bool DbCreator::createDB()
         query.exec("drop table if exists mediaimage");
         query.exec("drop table if exists mediaimagecontainer");
         query.exec("drop table if exists filepath");
+        query.exec("drop table if exists setup");
         query.exec("drop table if exists mediatype");
         query.exec("drop table if exists platform");
 
@@ -66,6 +67,16 @@ bool DbCreator::createDB()
 
         if (!ret) throw QString("mediatype.");
 
+        qDebug() << "Creating table setup";
+
+        ret = query.exec("create table if not exists setup "
+                        "(id integer primary key, "
+                        "platformid integer, "
+                        "mediatypeid integer, "
+                        "filetypeextensions text, "
+                        "foreign key (platformid) references platform(id), "
+                        "foreign key (mediatypeid) references mediatype(id))");
+
         /*qDebug() << "Creating table filetype";
             ret = query.exec("create table filetype if not exists"
                              "(id integer primary key, "
@@ -82,11 +93,9 @@ bool DbCreator::createDB()
                          "(id integer primary key, "
                          "name text, "
                          "filetypeid integer, "
-                         "platformid integer, "
-                         "mediatypeid integer, "
+                         "setupid integer, "
                          "lastscanned numeric, "
-                         "foreign key (platformid) references platform(id), "
-                         "foreign key (mediatypeid) references mediatype(id))");
+                         "foreign key (setupid) references setup(id))");
 
         if (!ret) throw QString("filepath");
 
index cfd89f0..caf6c03 100644 (file)
 
 #include <QSqlRelationalTableModel>
 #include <QSqlRecord>
+#include <QSqlQuery>
 #include "../dataobjects/filepathobject.h"
 #include "dbfilepath.h"
-#include "dbplatform.h"
-#include "dbmediatype.h"
+#include "dbsetup.h"
 
-DbFilePath::DbFilePath(QObject *parent) : DatabaseManager(parent)
+DbFilePath::DbFilePath(QObject *parent) : DbQueryModelManager(parent)
 {
-    dbPlatform = new DbPlatform(this);
-    dbMediaType = new DbMediaType(this);
+    dbSetup = new DbSetup(this);
     sqlTableModel = getData();
 }
 
-QSqlTableModel* DbFilePath::getDataModel()
-{
-    return sqlTableModel;
-}
-
 EmuFrontObject* DbFilePath::recordToDataObject(const QSqlRecord *rec) const
 {
     int id = rec->value(FilePath_Id).toInt();
     QString fpath = rec->value(FilePath_Name).toString();
-    int plfId = rec->value(FilePath_PlatformId).toInt();
-    int mtId = rec->value(FilePath_MediaTypeId).toInt();
+    int setupId = rec->value(FilePath_SetupId).toInt();
     int fileType = rec->value(FilePath_FileTypeId).toInt();
-    Platform *plf = dynamic_cast<Platform*>(dbPlatform->getDataObject(plfId));
-    MediaType *mt = dynamic_cast<MediaType*>(dbMediaType->getDataObject(mtId));
+    Setup *sup = dynamic_cast<Setup*>(dbSetup->getDataObject(setupId));
        // TODO
     //int lastScanned = 0;
-    return new FilePathObject(id, fpath, fpath, fileType, plf, mt);
+    return new FilePathObject(id, fpath, fpath, fileType, sup);
 }
 
 bool DbFilePath::updateDataObjectToModel(const EmuFrontObject *ob)
 {
     const FilePathObject *fpo = dynamic_cast<const FilePathObject*>(ob);
     bool ret = false;
-    sqlTableModel->setFilter(QString("id = %1").arg(fpo->getId()));
+    QSqlQuery query;
+    query.prepare(QString("UPDATE filepath SET "
+        "name=:name, "
+        "filetypeid=:filetypeid, "
+        "setupid=:setupid, "
+        "lastscanned=:lastscanned "
+        "WHERE id=:id"));
+    query.bindValue(":name", fpo->getName());
+    query.bindValue(":filetypeid", fpo->getFiletype());
+    query.bindValue(":lastscanned", 0); // TODO
+    query.bindValue(":id", fpo->getId());
+    ret = query.exec();
+    if (ret) resetModel();
+    /*sqlTableModel->setFilter(QString("id = %1").arg(fpo->getId()));
     sqlTableModel->select();
     if (sqlTableModel->rowCount() == 1)
     {
         QSqlRecord rec = sqlTableModel->record(0);
         rec.setValue("name", fpo->getName());
-        rec.setValue("filetypeid", fpo->getFiletype());
+        rec.setValue("filetypeid", fpo->getFilet.bype());
 
-        Platform *pl = fpo->getPlatform();
-        MediaType *mt = fpo->getMediaType();
-        if (pl) rec.setValue("platformid", pl->getId());
-        if (mt) rec.setValue("mediatypeid", mt->getId());
+        Setup *sup = fpo->getSetup();
+        if (sup) rec.setValue("setupid", sup->getId());
 
         // TODO
         //rec.setValue("lastscanned", 0);
-    }
+    }*/
     return ret;
 }
 
 bool DbFilePath::insertDataObjectToModel(const EmuFrontObject *ob)
 {
     const FilePathObject *fpo = dynamic_cast<const FilePathObject*>(ob);
-    int row = 0;
+    QSqlQuery query;
+    query.prepare("INSERT INTO filepath (id, name, filetypeid, setupid, lastscanned) "
+        "VALUES (NULL, :name, :filetypeid, :setupid, :lastscanned) ");
+    query.bindValue(":name", fpo->getName());
+    query.bindValue(":filetypeid", fpo->getFiletype());
+    if (fpo->getSetup())
+        query.bindValue(":setupid", fpo->getSetup()->getId());
+    query.bindValue(":lastscanned", 0); // TODO
+    return query.exec();
+    /*int row = 0;
+
     sqlTableModel->insertRows(row, 1);
 
-    Platform *pl = fpo->getPlatform();
-    MediaType *mt = fpo->getMediaType();
+
+    Setup *sup = fpo->getSetup();
+    //Platform *pl = fpo->getPlatform();
+    //MediaType *mt = fpo->getMediaType();
 
     //sqlTableModel->setData(sqlTableModel->index(row, FilePath_Id), NULL);
     sqlTableModel->setData(sqlTableModel->index(row, FilePath_Name), fpo->getName());
     sqlTableModel->setData(sqlTableModel->index(row, FilePath_FileTypeId), fpo->getFiletype());
     // not all the file path types have platform and/or media type
-    if (pl) sqlTableModel->setData(sqlTableModel->index(row, FilePath_PlatformId), pl->getId());
-    if (mt) sqlTableModel->setData(sqlTableModel->index(row, FilePath_MediaTypeId), mt->getId());
+    //if (pl) sqlTableModel->setData(sqlTableModel->index(row, FilePath_PlatformId), pl->getId());
+    //if (mt) sqlTableModel->setData(sqlTableModel->index(row, FilePath_MediaTypeId), mt->getId());
+    if (sup) sqlTableModel->setData(sqlTableModel->index(row, FilePath_SetupId), sup->getId());
     // TODO:
     sqlTableModel->setData(sqlTableModel->index(row, FilePath_LastScanned), 0);
-    return sqlTableModel->submitAll();
+    return sqlTableModel->submitAll();*/
 }
 
 int DbFilePath::countDataObjectRefs(int id) const
@@ -104,17 +120,53 @@ bool DbFilePath::deleteDataObjectFromModel(QModelIndex *index)
     return false;
 }
 
-QSqlTableModel* DbFilePath::getData()
+QString DbFilePath::constructSelect(QString whereClause) const
+{
+    QString where = whereClause.isEmpty()
+        ? "" : QString("WHERE ").append(whereClause);
+
+    return QString("SELECT filepath.id AS FilePathId, "
+            "filepath.name AS Name, "
+            "filepath.lastscanned AS LastScanned, "
+            "setup.id AS SetupId, "
+            "platform.name || ' ' || mediatype.name AS SetupName "
+            "FROM filepath "
+            "INNER JOIN setup ON filepath.setupid=setup.id  "
+            "INNER JOIN platform ON setup.platformid=platform.id "
+            "INNER JOIN mediatype ON setup.mediatypeid=mediatype.id "
+            "%1 "
+            "ORDER BY SetupName").arg(where);
+}
+
+QString DbFilePath::constructSelectById(int id) const
 {
-   QSqlRelationalTableModel *model = new QSqlRelationalTableModel(this);
-   model->setTable(DB_TABLE_NAME_FILEPATH);
-   model->setRelation(FilePath_PlatformId,
+    return constructSelect(QString("filepath.id = %1").arg(id));
+}
+
+QSqlQueryModel* DbFilePath::getData()
+{
+    QSqlQueryModel *model = new QSqlQueryModel;
+    model->setQuery(constructSelect());
+    model->setHeaderData(FilePath_Id, Qt::Horizontal, tr("Id"));
+    model->setHeaderData(FilePath_Name, Qt::Horizontal, tr("Name"));
+    model->setHeaderData(FilePath_LastScanned, Qt::Horizontal, tr("Last scanned"));
+    model->setHeaderData(FilePath_SetupId, Qt::Horizontal, tr("Set up id"));
+    model->setHeaderData(FilePath_SetupName, Qt::Horizontal, tr("Set up"));
+    return model;
+
+            //"platform.name, mediatype.name
+   /*QSqlRelationalTableModel *model = new QSqlRelationalTableModel(this);
+   model->setTable(DB_TABLE_NAME_FILEPATH);*/
+   /*model->setRelation(FilePath_PlatformId,
        QSqlRelation(DB_TABLE_NAME_PLATFORM, "id", "name"));
    model->setRelation(FilePath_MediaTypeId,
-       QSqlRelation(DB_TABLE_NAME_MEDIATYPE, "id", "name"));
-   model->setSort(FilePath_Name, Qt::AscendingOrder);
-   model->setHeaderData(FilePath_MediaTypeId, Qt::Horizontal, tr("Media type"));
-   model->setHeaderData(FilePath_PlatformId, Qt::Horizontal, tr("Platform"));
-   model->select();
-   return model;
+       QSqlRelation(DB_TABLE_NAME_MEDIATYPE, "id", "name"e));*/
+    /*model->setRelation(FilePath_SetupId,
+        QSqlRelation(DB_TABLE_NAME_SETUP, "id", ""));
+           model->setSort(FilePath_Name, Qt::AscendingOrder);*/
+
+   //model->setHeaderData(FilePath_MediaTypeId, Qt::Horizontal, tr("Media type"));
+   //model->setHeaderData(FilePath_PlatformId, Qt::Horizontal, tr("Platform"));
+   /*model->select();
+   return model;*/
 }
index 5eca5fe..605da93 100644 (file)
 #ifndef DBFILEPATH_H
 #define DBFILEPATH_H
 
-#include "databasemanager.h"
+#include "dbquerymodelmanager.h"
 
-class DbPlatform;
-class DbMediaType;
+class DbSetup;
 
-class DbFilePath : public DatabaseManager
+class DbFilePath : public DbQueryModelManager
 {
 public:
     DbFilePath(QObject *);    
-    virtual QSqlTableModel* getDataModel();
     virtual bool updateDataObjectToModel(const EmuFrontObject*);
     bool insertDataObjectToModel(const EmuFrontObject*);
     bool deleteDataObjectFromModel(QModelIndex*);
@@ -37,18 +35,19 @@ public:
 
 protected:
     virtual EmuFrontObject* recordToDataObject(const QSqlRecord* ) const;
+    virtual QString constructSelectById(int id) const;
+    virtual QString constructSelect(QString whereClause = "") const;
 
 private:
-    virtual QSqlTableModel* getData();
-    DbPlatform *dbPlatform;
-    DbMediaType *dbMediaType;
+    virtual QSqlQueryModel* getData();
+    DbSetup *dbSetup;
     // TODO: add last scanned column
     enum { FilePath_Id = 0,
            FilePath_Name,
            FilePath_FileTypeId,
-           FilePath_PlatformId,
-           FilePath_MediaTypeId,
-           FilePath_LastScanned  };
+           FilePath_LastScanned,
+           FilePath_SetupId,
+           FilePath_SetupName };
 };
 
 #endif // DBFILEPATH_H
index bc77ca6..a66de94 100644 (file)
 #include "../dataobjects/mediatype.h"
 
 
-DbMediaType::DbMediaType(QObject *parent) : DatabaseManager(parent)
+DbMediaType::DbMediaType(QObject *parent) : DbTableModelManager(parent)
 {
     sqlTableModel = getData();
 }
 
-QSqlTableModel* DbMediaType::getDataModel()
-{
-    return sqlTableModel;
-}
-
 EmuFrontObject* DbMediaType::recordToDataObject(const QSqlRecord *record) const
 {
     int id = record->value(MediaType_Id).toInt();
@@ -47,15 +42,16 @@ bool DbMediaType::updateDataObjectToModel(const EmuFrontObject *ob)
 {
     const MediaType *plf = dynamic_cast<const MediaType*>(ob);
     bool ret = false;
-    sqlTableModel->setFilter(QString("id = %1").arg(plf->getId()));
-    sqlTableModel->select();
-    if (sqlTableModel->rowCount() == 1)
+    QSqlTableModel *tmodel = dynamic_cast<QSqlTableModel*>(sqlTableModel);
+    tmodel->setFilter(QString("id = %1").arg(plf->getId()));
+    tmodel->select();
+    if (tmodel->rowCount() == 1)
     {
         QSqlRecord record = sqlTableModel->record(0);
         record.setValue("name", plf->getName());
         record.setValue("filename", plf->getFilename());
-        sqlTableModel->setRecord(0, record);
-        ret = sqlTableModel->submitAll();
+        tmodel->setRecord(0, record);
+        ret = tmodel->submitAll();
     }
     resetModel();
     return ret;
@@ -65,13 +61,14 @@ bool DbMediaType::insertDataObjectToModel(const EmuFrontObject *ob)
 {
     const MediaType *plf = dynamic_cast<const MediaType*>(ob);
     int row = 0;
-    sqlTableModel->insertRows(row, 1);
+     QSqlTableModel *tmodel = dynamic_cast<QSqlTableModel*>(sqlTableModel);
+    tmodel->insertRows(row, 1);
     // the null value for index will be set implicitily
     // when we don't assign any value to cell 0 in the sql table model
     //sqlTableModel->setData(sqlTableModel->index(row, 0), NULL);
-    sqlTableModel->setData(sqlTableModel->index(row, MediaType_Name), plf->getName());
-    sqlTableModel->setData(sqlTableModel->index(row, MediaType_Filename), plf->getFilename());
-    return sqlTableModel->submitAll();
+    tmodel->setData(sqlTableModel->index(row, MediaType_Name), plf->getName());
+    tmodel->setData(sqlTableModel->index(row, MediaType_Filename), plf->getFilename());
+    return tmodel->submitAll();
 }
 
 int DbMediaType::countDataObjectRefs(int id) const
@@ -83,7 +80,8 @@ int DbMediaType::countDataObjectRefs(int id) const
 bool DbMediaType::deleteDataObjectFromModel(QModelIndex *index)
 {
     QSqlDatabase::database().transaction();
-    QSqlRecord record = sqlTableModel->record(index->row());
+    QSqlTableModel *tmodel = dynamic_cast<QSqlTableModel*>(sqlTableModel);
+    QSqlRecord record = tmodel->record(index->row());
     int id = record.value(MediaType_Id).toInt();
     qDebug() << "Deleting mediatype " << id;
     int count = countDataObjectRefs(id);
@@ -97,12 +95,12 @@ bool DbMediaType::deleteDataObjectFromModel(QModelIndex *index)
             return false;
         }
     }
-    sqlTableModel->removeRow(index->row());
-    sqlTableModel->submitAll();
+    tmodel->removeRow(index->row());
+    tmodel->submitAll();
     return QSqlDatabase::database().commit();
 }
 
-QSqlTableModel* DbMediaType::getData()
+QSqlQueryModel* DbMediaType::getData()
 {
     QSqlTableModel *model = new QSqlTableModel(this);
     model->setTable(DB_TABLE_NAME_MEDIATYPE);
@@ -111,3 +109,4 @@ QSqlTableModel* DbMediaType::getData()
     model->select();
     return model;
 }
+
index 430bf4b..856ebbd 100644 (file)
 #ifndef DBMEDIATYPE_H
 #define DBMEDIATYPE_H
 
-#include "databasemanager.h"
+#include "dbtablemodelmanager.h"
+#include "../dataobjects/mediatype.h"
 
-class DbMediaType : public DatabaseManager
+class DbMediaType : public DbTableModelManager
 {
 public:
     DbMediaType(QObject *);
-    virtual QSqlTableModel* getDataModel();
     virtual bool updateDataObjectToModel(const EmuFrontObject*);
     virtual bool insertDataObjectToModel(const EmuFrontObject*);
     virtual bool deleteDataObjectFromModel(QModelIndex*);
@@ -40,7 +40,7 @@ protected:
     virtual EmuFrontObject* recordToDataObject(const QSqlRecord* ) const;
 
 private:
-      virtual QSqlTableModel* getData();
+      virtual QSqlQueryModel* getData();
 
 };
 
index 62ef6ed..753acd8 100644 (file)
 #include "dbplatform.h"
 
 
-DbPlatform::DbPlatform(QObject *parent) : DatabaseManager(parent)
+DbPlatform::DbPlatform(QObject *parent) : DbTableModelManager(parent)
 {
     sqlTableModel = getData();
 }
 
-QSqlTableModel* DbPlatform::getDataModel()
-{
-    return sqlTableModel;
-}
-
 EmuFrontObject* DbPlatform::recordToDataObject(const QSqlRecord *record) const
 {
     int id = record->value(Platform_Id).toInt();
@@ -47,15 +42,16 @@ bool DbPlatform::updateDataObjectToModel(const EmuFrontObject *ob)
 {
     const Platform *plf = dynamic_cast<const Platform*>(ob);
     bool ret = false;
-    sqlTableModel->setFilter(QString("id = %1").arg(plf->getId()));
-    sqlTableModel->select();
-    if (sqlTableModel->rowCount() == 1)
+    QSqlTableModel *tmodel = dynamic_cast<QSqlTableModel*>(sqlTableModel);
+    tmodel->setFilter(QString("id = %1").arg(plf->getId()));
+    tmodel->select();
+    if (tmodel->rowCount() == 1)
     {
-        QSqlRecord record = sqlTableModel->record(0);
+        QSqlRecord record = tmodel->record(0);
         record.setValue("name", plf->getName());
         record.setValue("filename", plf->getFilename());
-        sqlTableModel->setRecord(0, record);
-        ret = sqlTableModel->submitAll();
+        tmodel->setRecord(0, record);
+        ret = tmodel->submitAll();
     }
     resetModel();
     return ret;
@@ -65,13 +61,14 @@ bool DbPlatform::insertDataObjectToModel(const EmuFrontObject *ob)
 {
     const Platform *plf = dynamic_cast<const Platform*>(ob);
     int row = 0;
-    sqlTableModel->insertRows(row, 1);
+    QSqlTableModel *tmodel = dynamic_cast<QSqlTableModel*>(sqlTableModel);
+    tmodel->insertRows(row, 1);
     // the null value for index will be set implicitily
     // when we don't assign any value to cell 0 in the sql table model
     //sqlTableModel->setData(sqlTableModel->index(row, 0), NULL);
-    sqlTableModel->setData(sqlTableModel->index(row, 1), plf->getName());
-    sqlTableModel->setData(sqlTableModel->index(row, 2), plf->getFilename());
-    return sqlTableModel->submitAll();
+    tmodel->setData(sqlTableModel->index(row, 1), plf->getName());
+    tmodel->setData(sqlTableModel->index(row, 2), plf->getFilename());
+    return tmodel->submitAll();
 }
 
 int DbPlatform::countDataObjectRefs(int id) const
@@ -83,7 +80,8 @@ int DbPlatform::countDataObjectRefs(int id) const
 bool DbPlatform::deleteDataObjectFromModel(QModelIndex *index)
 {
     QSqlDatabase::database().transaction();
-    QSqlRecord record = sqlTableModel->record(index->row());
+    QSqlTableModel *tmodel = dynamic_cast<QSqlTableModel*>(sqlTableModel);
+    QSqlRecord record = tmodel->record(index->row());
     int id = record.value(Platform_Id).toInt();
     qDebug() << "Deleting platform " << id;
     int count = countDataObjectRefs(id);
@@ -97,12 +95,12 @@ bool DbPlatform::deleteDataObjectFromModel(QModelIndex *index)
             return false;
         }
     }
-    sqlTableModel->removeRow(index->row());
-    sqlTableModel->submitAll();
+    tmodel->removeRow(index->row());
+    tmodel->submitAll();
     return QSqlDatabase::database().commit();
 }
 
-QSqlTableModel* DbPlatform::getData()
+QSqlQueryModel* DbPlatform::getData()
 {
     QSqlTableModel *model = new QSqlTableModel(this);
     model->setTable(DB_TABLE_NAME_PLATFORM);
@@ -111,3 +109,11 @@ QSqlTableModel* DbPlatform::getData()
     model->select();
     return model;
 }
+
+/*void DbPlatform::filterById(int id)
+{
+    QSqlTableModel *tmodel = dynamic_cast<QSqlTableModel*>(sqlTableModel);
+    tmodel->setFilter(QString("id = %1").arg(id));
+    tmodel->setQuery(constructSelectById(id));
+    tmodel->select();
+}*/
index 6b57646..5327422 100644 (file)
 #ifndef DBPLATFORM_H
 #define DBPLATFORM_H
 
-#include "databasemanager.h"
+#include "dbtablemodelmanager.h"
 #include "../dataobjects/platform.h"
 
 class QModelIndex;
 
-class DbPlatform : public DatabaseManager
+class DbPlatform : public DbTableModelManager
 {
 public:
     DbPlatform(QObject *);
-    virtual QSqlTableModel* getDataModel();
     virtual bool updateDataObjectToModel(const EmuFrontObject*);
     bool insertDataObjectToModel(const EmuFrontObject*);
     bool deleteDataObjectFromModel(QModelIndex*);
@@ -43,7 +42,7 @@ protected:
     virtual EmuFrontObject* recordToDataObject(const QSqlRecord* ) const;
 
 private:
-    virtual QSqlTableModel* getData();
+    virtual QSqlQueryModel* getData();
 };
 
 #endif // DBPLATFORM_H
diff --git a/src/db/dbquerymodelmanager.cpp b/src/db/dbquerymodelmanager.cpp
new file mode 100644 (file)
index 0000000..8ec50bb
--- /dev/null
@@ -0,0 +1,38 @@
+// 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 as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// Foobar 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 Foobar.  If not, see <http://www.gnu.org/licenses/>.
+
+#include <QSqlQueryModel>
+#include "dbquerymodelmanager.h"
+
+
+DbQueryModelManager::DbQueryModelManager(QObject *parent)
+    : DatabaseManager(parent)
+{
+}
+
+void DbQueryModelManager::filterById(int id)
+{
+    sqlTableModel->setQuery(constructSelectById(id));
+}
+
+void DbQueryModelManager::clearFilters()
+{
+    sqlTableModel->setQuery(constructSelect());
+}
+
diff --git a/src/db/dbquerymodelmanager.h b/src/db/dbquerymodelmanager.h
new file mode 100644 (file)
index 0000000..97777d1
--- /dev/null
@@ -0,0 +1,36 @@
+// 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 as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// Foobar 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 Foobar.  If not, see <http://www.gnu.org/licenses/>.
+
+#ifndef DBQUERYMODELMANAGER_H
+#define DBQUERYMODELMANAGER_H
+
+#include "databasemanager.h"
+
+class DbQueryModelManager : public DatabaseManager
+{
+public:
+    DbQueryModelManager(QObject *parent);
+protected:
+    void filterById(int id);
+    void clearFilters();
+    virtual QString constructSelectById(int id) const = 0;
+    virtual QString constructSelect(QString whereClause = "") const = 0;
+};
+
+#endif // DBQUERYMODELMANAGER_H
diff --git a/src/db/dbsetup.cpp b/src/db/dbsetup.cpp
new file mode 100644 (file)
index 0000000..6aa238a
--- /dev/null
@@ -0,0 +1,175 @@
+// 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 as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// Foobar 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 Foobar.  If not, see <http://www.gnu.org/licenses/>.
+
+#include <QStringList>
+#include <QSqlRecord>
+#include <QSqlQuery>
+#include <QSqlRelationalTableModel>
+#include "dbsetup.h"
+#include "dbplatform.h"
+#include "dbmediatype.h"
+
+const QString DbSetup::FILE_TYPE_EXTENSION_SEPARATOR = QString("|");
+
+DbSetup::DbSetup(QObject *parent) : DbQueryModelManager(parent)
+{
+    dbPlatform = new DbPlatform(this);
+    dbMediaType = new DbMediaType(this);
+    sqlTableModel = getData();
+}
+
+EmuFrontObject* DbSetup::recordToDataObject(const QSqlRecord *rec) const
+{
+    int id = rec->value(Setup_Id).toInt();
+    QString extensions = rec->value(Setup_FileTypeExtensions).toString();
+    QStringList list = extensions.split(FILE_TYPE_EXTENSION_SEPARATOR);
+    int plfId = rec->value(Setup_PlatformId).toInt();
+    int mtId = rec->value(Setup_MediaTypeId).toInt();
+    Platform *plf = dynamic_cast<Platform*>(dbPlatform->getDataObject(plfId));
+    MediaType *mt = dynamic_cast<MediaType*>(dbMediaType->getDataObject(mtId));
+    return new Setup(id, plf, mt, list);
+}
+
+bool DbSetup::updateDataObjectToModel(const EmuFrontObject *ob)
+{
+    const Setup *fpo = dynamic_cast<const Setup*>(ob);
+    bool ret = false;
+    QSqlQuery query;
+    query.prepare(QString("UPDATE setup SET "
+        "platformid=:platformid, "
+        "mediatypeid=:mediatypeid, "
+        "filetypeextensions:=filetypeextensions "
+        "WHERE id = :id"));
+    if (fpo->getPlatform())
+        query.bindValue(":platformid", fpo->getPlatform()->getId());
+    if (fpo->getMediaType())
+        query.bindValue(":mediatypeid", fpo->getMediaType()->getId());
+    query.bindValue(":filetypeextensions", fpo->getSupportedFileTypeExtensions().join(FILE_TYPE_EXTENSION_SEPARATOR));
+    query.bindValue(":id", fpo->getId());
+    query.exec();
+
+    /*sqlTableModel->setFilter(QString("id = %1").arg(fpo->getId()));
+    sqlTableModel->select();
+    if (sqlTableModel->rowCount() == 1)
+    {
+        QSqlRecord rec = sqlTableModel->record(0);
+        rec.setValue("filetypeid", fpo->getFiletype());
+
+        Platform *pl = fpo->getPlatform();
+        MediaType *mt = fpo->getMediaType();
+        if (pl) rec.setValue("platformid", pl->getId());
+        if (mt) rec.setValue("mediatypeid", mt->getId());
+
+        QStringList list = fpo->getSupportedFileTypeExtensions();
+        if (list.count() > 0)
+            rec.setValue("filetypeextensions", list.join(FILE_TYPE_EXTENSION_SEPARATOR));
+    }*/
+    return ret;
+}
+
+bool DbSetup::insertDataObjectToModel(const EmuFrontObject *ob)
+{
+    const Setup *fpo = dynamic_cast<const Setup*>(ob);
+    QSqlQuery query;
+    query.prepare("INSERT INTO setup (id, platformid, mediatypeid, fileextensions)"
+        "VALUES (NULL, :platformid, :mediatypeid, :fileextensions)");
+    if (fpo->getPlatform())
+        query.bindValue(":platformid", fpo->getPlatform()->getId());
+    if (fpo->getMediaType())
+        query.bindValue(":mediatypeid", fpo->getMediaType()->getId());
+    query.bindValue(":filetypeextensions", fpo->getSupportedFileTypeExtensions().join(FILE_TYPE_EXTENSION_SEPARATOR));
+    return query.exec();
+
+    /*int row = 0;
+    sqlTableModel->insertRows(row, 1);
+
+    Platform *pl = fpo->getPlatform();
+    MediaType *mt = fpo->getMediaType();
+
+    //sqlTableModel->setData(sqlTableModel->index(row, FilePath_Id), NULL);
+    // not all the file path types have platform and/or media type
+    if (pl) sqlTableModel->setData(sqlTableModel->index(row, Setup_PlatformId), pl->getId());
+    if (mt) sqlTableModel->setData(sqlTableModel->index(row, Setup_MediaTypeId), mt->getId());
+    QStringList list = fpo->getSupportedFileTypeExtensions();
+    if (list.count() > 0)
+        sqlTableModel->setData(sqlTableModel->index(row, Setup_FileTypeExtensions), list.join(FILE_TYPE_EXTENSION_SEPARATOR));
+    return sqlTableModel->submitAll();*/
+}
+
+int DbSetup::countDataObjectRefs(int ) const
+{
+    return 0;
+}
+
+QString DbSetup::constructSelect(QString whereClause) const
+{
+    QString where = whereClause.isEmpty()
+        ? "" : QString("WHERE ").append(whereClause);
+    return QString(
+        "SELECT setup.id AS SetupId, "
+        "setup.platformid AS PlatformId, "
+        "setup.mediatypeid AS MediaTypeId, "
+        "setup.filetypeextensions AS SupportedFileTypeExtensions, "
+        "platform.name || ' ' || mediatype.name AS SetupName, "
+        "FROM setup %1"
+        "INNER JOIN platform ON setup.platformid=platform.id "
+        "INNER JOIN mediatype ON setup.mediatypeid=mediatype.id "
+        "ORDER BY SetupName"
+        ).arg(where);
+}
+
+QString DbSetup::constructSelectById(int id) const
+{
+     return constructSelect(QString("setup.id = %1").arg(id));
+}
+
+// WARNING: this will delete also all the databindings to selected media image path
+bool DbSetup::deleteDataObjectFromModel(QModelIndex */*index*/)
+{
+    return false;
+}
+
+QSqlQueryModel* DbSetup::getData()
+{
+    QSqlQueryModel *model = new QSqlQueryModel;
+    model->setQuery(constructSelect());
+    model->setHeaderData(Setup_Id, Qt::Horizontal, tr("Id"));
+    model->setHeaderData(Setup_PlatformId, Qt::Horizontal, tr("Platform id"));
+    model->setHeaderData(Setup_MediaTypeId, Qt::Horizontal, tr("Media type id"));
+    model->setHeaderData(Setup_FileTypeExtensions, Qt::Horizontal, tr("File types"));
+    model->setHeaderData(Setup_Name, Qt::Horizontal, tr("Name"));
+    return model;
+
+   /*QSqlRelationalTableModel *model = new QSqlRelationalTableModel(this);
+   model->setTable(DB_TABLE_NAME_FILEPATH);
+   model->setRelation(FilePath_PlatformId,
+       QSqlRelation(DB_TABLE_NAME_PLATFORM, "id", "name"));
+   model->setRelation(FilePath_MediaTypeId,
+       QSqlRelation(DB_TABLE_NAME_MEDIATYPE, "id", "name"));
+   model->setSort(FilePath_Name, Qt::AscendingOrder);
+   model->setHeaderData(FilePath_MediaTypeId, Qt::Horizontal, tr("Media type"));
+   model->setHeaderData(FilePath_PlatformId, Qt::Horizontal, tr("Platform"));
+   model->select();
+   return model;*/
+}
+
+/*void DbMediaType::filterById(int id)
+{
+    sqlTableModel->setQuery(constructSelectById(id));
+}*/
diff --git a/src/db/dbsetup.h b/src/db/dbsetup.h
new file mode 100644 (file)
index 0000000..fdc8b85
--- /dev/null
@@ -0,0 +1,54 @@
+// 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 as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// Foobar 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 Foobar.  If not, see <http://www.gnu.org/licenses/>.
+
+#ifndef DBSETUP_H
+#define DBSETUP_H
+
+#include "dbquerymodelmanager.h"
+#include "../dataobjects/setup.h"
+
+class DbPlatform;
+class DbMediaType;
+
+class DbSetup : public DbQueryModelManager
+{
+public:
+    DbSetup(QObject *);
+    virtual bool updateDataObjectToModel(const EmuFrontObject*);
+    bool insertDataObjectToModel(const EmuFrontObject*);
+    bool deleteDataObjectFromModel(QModelIndex*);
+    int countDataObjectRefs(int) const;
+    enum { Setup_Id = 0,
+           Setup_PlatformId,
+           Setup_MediaTypeId,
+           Setup_FileTypeExtensions,
+           Setup_Name };
+    static const QString FILE_TYPE_EXTENSION_SEPARATOR;
+
+protected:
+    virtual EmuFrontObject* recordToDataObject(const QSqlRecord* ) const;
+    virtual QString constructSelectById(int id) const;
+    virtual QString constructSelect(QString whereClause = "") const;
+
+private:
+    virtual QSqlQueryModel* getData();
+    DbPlatform *dbPlatform;
+    DbMediaType *dbMediaType;
+};
+#endif // DBSETUP_H
diff --git a/src/db/dbtablemodelmanager.cpp b/src/db/dbtablemodelmanager.cpp
new file mode 100644 (file)
index 0000000..4245459
--- /dev/null
@@ -0,0 +1,40 @@
+// 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 as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// Foobar 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 Foobar.  If not, see <http://www.gnu.org/licenses/>.
+
+#include <QSqlTableModel>
+#include "dbtablemodelmanager.h"
+
+DbTableModelManager::DbTableModelManager(QObject *parent)
+    : DatabaseManager(parent)
+{
+}
+
+void DbTableModelManager::filterById(int id)
+{
+    QSqlTableModel *tmodel = dynamic_cast<QSqlTableModel*>(sqlTableModel);
+    tmodel->setFilter(QString("id = %1").arg(id));
+    tmodel->select();
+}
+
+void DbTableModelManager::clearFilters()
+{
+    QSqlTableModel *tmodel = dynamic_cast<QSqlTableModel*>(sqlTableModel);
+    tmodel->setFilter("");
+    tmodel->select();
+}
diff --git a/src/db/dbtablemodelmanager.h b/src/db/dbtablemodelmanager.h
new file mode 100644 (file)
index 0000000..71bdb95
--- /dev/null
@@ -0,0 +1,34 @@
+// 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 as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// Foobar 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 Foobar.  If not, see <http://www.gnu.org/licenses/>.
+
+#ifndef DBTABLEMODELMANAGER_H
+#define DBTABLEMODELMANAGER_H
+
+#include "databasemanager.h"
+
+class DbTableModelManager : public DatabaseManager
+{
+public:
+    DbTableModelManager(QObject *parent);
+protected:
+    void filterById(int id);
+    void clearFilters();
+};
+
+#endif // DBTABLEMODELMANAGER_H
index 1d6771d..1dfc9da 100644 (file)
@@ -125,6 +125,7 @@ void DbObjectDialog::updateDb(const EmuFrontObject *ob) const
 void DbObjectDialog::updateList() const
 {
     if (!dbManager) return;
+    qDebug() << "Going to reset the data model";
     dbManager->resetModel();
 }
 
index 9ad5c80..4e2e84d 100644 (file)
 #include <QSqlRecord>
 #include "../db/dbplatform.h"
 #include "../db/dbmediatype.h"
+#include "../db/dbsetup.h"
 #include "mediaimagepathdialog.h"
-#include "../dataobjects/platform.h"
-#include "../dataobjects/mediatype.h"
+//#include "../dataobjects/platform.h"
+//#include "../dataobjects/mediatype.h"
 #include "../dataobjects/filepathobject.h"
 
 MediaImagePathDialog::MediaImagePathDialog(QWidget *parent, EmuFrontObject *efObject)
@@ -32,8 +33,10 @@ MediaImagePathDialog::MediaImagePathDialog(QWidget *parent, EmuFrontObject *efOb
 {
     qDebug() << "Creating MediaImagePathDialog";
     initWidgets();
-    populateMediaTypeComBox();
-    populatePlatformComBox();
+    //populateMediaTypeComBox();
+    //populatePlatformComBox();
+    dbPlatform = 0;
+    dbMediaType = 0;
     connectSignals();
     layout();
 }
@@ -69,11 +72,20 @@ void MediaImagePathDialog::initWidgets()
     // these widgets will be automatically parented using layout components
     filePathLabel = new QLabel;
     filePathButton = new QPushButton(tr("&Browse filepath"));
-    mediaTypeComBox = new QComboBox;
-    platformComBox = new QComboBox;
+    /*mediaTypeComBox = new QComboBox;
+    platformComBox = new QComboBox;*/
+    setupComBox = new QComboBox;
 }
 
-void MediaImagePathDialog::populateMediaTypeComBox()
+void MediaImagePathDialog::populateSetupComBox()
+{
+    qDebug() << "MediaImagePathDialog populating media types combo box";
+    dbSetup = new DbSetup(this);
+    setupComBox->setModel(dbSetup->getDataModel());
+    setupComBox->setModelColumn(DbSetup::Setup_Name);
+}
+
+/*void MediaImagePathDialog::populateMediaTypeComBox()
 {
     qDebug() << "MediaImagePathDialog populating media types combo box";
     dbMediaType = new DbMediaType(this);
@@ -87,23 +99,27 @@ void MediaImagePathDialog::populatePlatformComBox()
     dbPlatform = new DbPlatform(this);
     platformComBox->setModel(dbPlatform->getDataModel());
     platformComBox->setModelColumn(DbPlatform::Platform_Name);
-}
+}*/
 
 void MediaImagePathDialog::layout()
 {
     qDebug() << "MediaImagePathDialog setting layout";
-   QLabel *platformLabel = new QLabel(tr("&Platform"));
+   /*QLabel *platformLabel = new QLabel(tr("&Platform"));
    platformLabel->setBuddy(platformComBox);
    QLabel *mediaTypeLabel = new QLabel(tr("Media&Type"));
-   mediaTypeLabel->setBuddy(mediaTypeComBox);
+   mediaTypeLabel->setBuddy(mediaTypeComBox);*/
+   QLabel *setupLabel = new QLabel(tr("&Set up"));
+   setupLabel->setBuddy(setupComBox);
 
    QGridLayout *gridLayout = new QGridLayout;
-   gridLayout->addWidget(platformLabel, 0, 0);
+   /*gridLayout->addWidget(platformLabel, 0, 0);
    gridLayout->addWidget(platformComBox, 0, 1);
    gridLayout->addWidget(mediaTypeLabel, 1, 0);
-   gridLayout->addWidget(mediaTypeComBox, 1, 1);
-   gridLayout->addWidget(filePathButton, 2, 0);
-   gridLayout->addWidget(filePathLabel, 2, 1);
+   gridLayout->addWidget(mediaTypeComBox, 1, 1);*/
+   gridLayout->addWidget(setupLabel, 0, 0);
+   gridLayout->addWidget(setupComBox, 0, 1);
+   gridLayout->addWidget(filePathButton, 1, 0);
+   gridLayout->addWidget(filePathLabel, 1, 1);
    QVBoxLayout *mainLayout = new QVBoxLayout;
    mainLayout->addLayout(gridLayout);
    mainLayout->addWidget(buttonBox);
@@ -119,11 +135,17 @@ void MediaImagePathDialog::setDataObject(EmuFrontObject *ob)
     FilePathObject *fpo = dynamic_cast<FilePathObject*>(ob);
     QString fpath = fpo->getName();
     filePathLabel->setText(fpath);
-    if (fpo->getPlatform()) setSelectedPlatform(fpo->getPlatform());
-    if (fpo->getMediaType()) setSelectedMediaType(fpo->getMediaType());
+    if (fpo->getSetup()) setSelectedSetup(fpo->getSetup());
+    /*if (fpo->getPlatform()) setSelectedPlatform(fpo->getPlatform());
+    if (fpo->getMediaType()) setSelectedMediaType(fpo->getMediaType());*/
+}
+
+void MediaImagePathDialog::setSelectedSetup(const Setup *sup)
+{
+    setSelected(setupComBox, sup, DbSetup::Setup_Id);
 }
 
-void MediaImagePathDialog::setSelectedPlatform(const Platform *plf)
+/*void MediaImagePathDialog::setSelectedPlatform(const Platform *plf)
 {
     setSelected(platformComBox, plf, DbPlatform::Platform_Id);
 }
@@ -131,7 +153,7 @@ void MediaImagePathDialog::setSelectedPlatform(const Platform *plf)
 void MediaImagePathDialog::setSelectedMediaType(const MediaType *plf)
 {
     setSelected(mediaTypeComBox, plf, DbMediaType::MediaType_Id);
-}
+}*/
 
 // TODO: this might be useful to lever to upper classes
 void MediaImagePathDialog::setSelected(QComboBox *cbox, const EmuFrontObject *ob, int idIndex)
@@ -149,7 +171,40 @@ void MediaImagePathDialog::setSelected(QComboBox *cbox, const EmuFrontObject *ob
     }
 }
 
-Platform* MediaImagePathDialog::getSelectedPlatform() const
+Setup* MediaImagePathDialog::getSelectedSetup()
+{
+    if (!dbPlatform) dbPlatform = new DbPlatform(this);
+    if (!dbMediaType) dbMediaType = new DbMediaType(this);
+    qDebug() << "MediaImagePathDialog Selecting setup";
+    Setup *sup = 0;
+    int index = setupComBox->currentIndex();
+    qDebug() << "Current index " << index;
+    if (index < 0) return sup;
+    QSqlQueryModel *model
+        = dynamic_cast<QSqlQueryModel*>(setupComBox->model());
+    if (!model)
+    {
+        qDebug() << "Data model missing";
+        return sup;
+    }
+    QSqlRecord rec = model->record(index);
+    if (!rec.isEmpty())
+    {
+        qDebug() << "We have a record";
+        EmuFrontObject *efPlf = dbPlatform->getDataObject(rec.value(DbSetup::Setup_PlatformId).toInt());
+        EmuFrontObject *efMt = dbMediaType->getDataObject(rec.value(DbSetup::Setup_MediaTypeId).toInt());
+
+        Platform *plf = dynamic_cast<Platform*>(efPlf);
+        MediaType *mt = dynamic_cast<MediaType*>(efMt);
+        QString exts = rec.value(DbSetup::Setup_FileTypeExtensions).toString();
+
+        sup = new Setup(rec.value(DbSetup::Setup_Id).toInt(), plf, mt,
+            exts.split(DbSetup::FILE_TYPE_EXTENSION_SEPARATOR));
+    }
+    else qDebug() << "Record missing";
+    return sup;
+}
+/*Platform* MediaImagePathDialog::getSelectedPlatform() const
 {
     qDebug() << "MediaImagePathDialog Selecting platform";
     Platform *plf = 0;
@@ -191,13 +246,20 @@ MediaType* MediaImagePathDialog::getSelectedMediaType() const
                            rec.value(DbMediaType::MediaType_Name).toString(),
                            rec.value(DbMediaType::MediaType_Filename).toString());
     return mt;
-}
+}*/
 
 void MediaImagePathDialog::acceptChanges()
 {
     qDebug() << "Changes accepted";
     FilePathObject *fpo = dynamic_cast<FilePathObject*>(efObject);
-    Platform *plf = getSelectedPlatform();
+    Setup *sup = getSelectedSetup();
+    if (!sup)
+    {
+        QMessageBox::information(this, tr("Set up"), tr("Set up not selected"), QMessageBox::Ok);
+        return;
+    }
+    qDebug() << "Setup selected " << sup->getName();
+    /*Platform *plf = getSelectedPlatform();
     if (!plf)
     {
         QMessageBox::information(this, tr("Platform"), tr("Platform not selected"), QMessageBox::Ok);
@@ -210,7 +272,7 @@ void MediaImagePathDialog::acceptChanges()
         QMessageBox::information(this, tr("Media type"), tr("Media type was not selected"), QMessageBox::Ok);
         return;
     }
-    qDebug() << "Media type selected " << mt->getName();
+    qDebug() << "Media type selected " << mt->getName();*/
     QString filePath = filePathLabel->text();
     if (filePath.isEmpty())
     {
@@ -218,7 +280,13 @@ void MediaImagePathDialog::acceptChanges()
         return;
     }
     qDebug() << "File path selected " << filePath;
-    Platform *ptmp = fpo->getPlatform();
+    Setup *tmp = fpo->getSetup();
+    if (sup != tmp)
+    {
+        delete tmp;
+        fpo->setSetup(sup);
+    }
+    /*Platform *ptmp = fpo->getPlatform();
     if (plf != ptmp)
     {
         delete ptmp;
@@ -229,7 +297,7 @@ void MediaImagePathDialog::acceptChanges()
     {
         delete mtmp;
         fpo->setMediaType(mt);
-    }
+    }*/
     fpo->setName(filePath);
     emit dataObjectUpdated();
     efObject = 0;
index 5114071..7c07cec 100644 (file)
@@ -27,10 +27,12 @@ class QComboBox;
 class QLabel;
 class QPushButton;
 class QSqlTableModel;
+class DbSetup;
+class Setup;
 class DbMediaType;
 class DbPlatform;
-class MediaType;
-class Platform;
+/* class MediaType;
+class Platform;*/
 
 class MediaImagePathDialog : public DataObjectEditDialog
 {
@@ -47,23 +49,28 @@ protected slots:
     void browseFilePath();
 
 private:
-    QComboBox *mediaTypeComBox;
-    QComboBox *platformComBox;
+    //QComboBox *mediaTypeComBox;
+    //QComboBox *platformComBox;
+    QComboBox *setupComBox;
     QLabel *filePathLabel;
     QPushButton *filePathButton;
+    DbSetup *dbSetup;
     DbMediaType *dbMediaType;
     DbPlatform *dbPlatform;
 
     void initWidgets();
     void layout();
     void connectSignals();
-    void populateMediaTypeComBox();
-    void populatePlatformComBox();
-    void setSelectedMediaType(const MediaType *);
-    void setSelectedPlatform(const Platform *);
+    void populateSetupComBox();
+    //void populateMediaTypeComBox();
+    //void populatePlatformComBox();
+    void setSelectedSetup(const Setup *);
+    //void setSelectedMediaType(const MediaType *);
+    //void setSelectedPlatform(const Platform *);
     void setSelected(QComboBox*, const EmuFrontObject*, int idIndex);
-    Platform* getSelectedPlatform() const;
-    MediaType* getSelectedMediaType() const;
+    Setup* getSelectedSetup();
+    //Platform* getSelectedPlatform() const;
+    //MediaType* getSelectedMediaType() const;
 
 };
 
index 11d9382..8169cd1 100644 (file)
@@ -35,7 +35,11 @@ HEADERS += mainwindow.h \
     db/dbfilepath.h \
     utils/ziputil.h \
     utils/fileutil.h \
-    utils/OSDaB-Zip/unzip.h
+    utils/OSDaB-Zip/unzip.h \
+    dataobjects/setup.h \
+    db/dbsetup.h \
+    db/dbtablemodelmanager.h \
+    db/dbquerymodelmanager.h
 SOURCES += main.cpp \
     mainwindow.cpp \
     db/databasemanager.cpp \
@@ -60,4 +64,10 @@ SOURCES += main.cpp \
     db/dbfilepath.cpp \
     utils/ziputil.cpp \
     utils/fileutil.cpp \
-    utils/OSDaB-Zip/unzip.cpp
+    utils/OSDaB-Zip/unzip.cpp \
+    dataobjects/setup.cpp \
+    db/dbsetup.cpp \
+    db/dbtablemodelmanager.cpp \
+    db/dbquerymodelmanager.cpp
+
+OTHER_FILES +=