Added new classes (DbMediaType, MediaTypeNameDialog) and did a bit
authorMikko Keinänen <mikko.keinanen@gmail.com>
Sun, 23 May 2010 22:14:26 +0000 (01:14 +0300)
committerMikko Keinänen <mikko.keinanen@gmail.com>
Sun, 23 May 2010 22:14:26 +0000 (01:14 +0300)
refactoring.

15 files changed:
src/db/databasemanager.cpp
src/db/databasemanager.h
src/db/dbmediatype.cpp [new file with mode: 0644]
src/db/dbmediatype.h [new file with mode: 0644]
src/db/dbplatform.cpp
src/db/dbplatform.h
src/dialogs/dbobjectdialog.cpp
src/dialogs/dbobjectdialog.h
src/dialogs/mediatypedialog.cpp
src/dialogs/mediatypedialog.h
src/dialogs/mediatypenamedialog.cpp [new file with mode: 0644]
src/dialogs/mediatypenamedialog.h [new file with mode: 0644]
src/dialogs/platformdialog.cpp
src/dialogs/platformdialog.h
src/emufront.pro

index af9071e..9b75560 100644 (file)
@@ -66,3 +66,15 @@ void DatabaseManager::resetModel() const
     sqlTableModel->setFilter("");
     sqlTableModel->select();
 }
+
+// sql must return a count(*) value
+int DatabaseManager::countRows(QString tableName, QString columnName, int id) const
+{
+    QString sql = QString("SELECT COUNT(*) FROM %1 WHERE %2 = %3")
+        .arg(tableName).arg(columnName).arg(id);
+    int numEntries = 0;
+    QSqlQuery query(sql);
+    if (query.next())
+        numEntries = query.value(0).toInt();
+    return numEntries;
+}
index 4323921..d6afc6c 100644 (file)
@@ -26,6 +26,8 @@
 class QSqlError;
 class QFile;
 class QSqlTableModel;
+class QModelIndex;
+class EmuFrontObject;
 
 class DatabaseManager : public QObject
 {
@@ -34,6 +36,11 @@ public:
        ~DatabaseManager();
 
     virtual QSqlTableModel* getDataModel() = 0;
+    virtual EmuFrontObject* getDataObjectFromModel(QModelIndex*) = 0;
+    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;
     enum {
@@ -43,7 +50,9 @@ public:
         Filetype_MediaTypeIcon = 3 };
 
 protected:
-    QSqlTableModel *sqlTableModel;
+    QSqlTableModel* sqlTableModel;
+    //virtual QSqlTableModel* getDataModel() = 0;
+    int countRows(QString tableName, QString columnName, int id) const;
 
 private:
        static const QString DB_FILENAME;
diff --git a/src/db/dbmediatype.cpp b/src/db/dbmediatype.cpp
new file mode 100644 (file)
index 0000000..f12be25
--- /dev/null
@@ -0,0 +1,116 @@
+// 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 <QSqlRecord>
+#include <QSqlQuery>
+#include <QSqlTableModel>
+#include <QDebug>
+#include "dbmediatype.h"
+
+const QString DbMediaType::DB_TABLE_NAME_MEDIATYPE = QString("mediatype");
+
+
+DbMediaType::DbMediaType(QObject *parent) : DatabaseManager(parent)
+{
+    sqlTableModel = getData();
+}
+
+QSqlTableModel* DbMediaType::getDataModel()
+{
+    return sqlTableModel;
+}
+
+EmuFrontObject* DbMediaType::getDataObjectFromModel(QModelIndex *index)
+{
+    QSqlRecord record = sqlTableModel->record(index->row());
+    int id = record.value(MediaType_Id).toInt();
+    QString name = record.value(MediaType_Name).toString();
+    QString fileName = record.value(MediaType_Filename).toString();
+    //qDebug() << "Got platform Name " << name << " id " << id;
+    return new MediaType(id, name, fileName);
+}
+
+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)
+    {
+        QSqlRecord record = sqlTableModel->record(0);
+        record.setValue("name", plf->getName());
+        record.setValue("filename", plf->getFilename());
+        sqlTableModel->setRecord(0, record);
+        ret = sqlTableModel->submitAll();
+    }
+    resetModel();
+    return ret;
+}
+
+bool DbMediaType::insertDataObjectToModel(const EmuFrontObject *ob)
+{
+    const MediaType *plf = dynamic_cast<const MediaType*>(ob);
+    int row = 0;
+    sqlTableModel->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();
+}
+
+int DbMediaType::countDataObjectRefs(int id) const
+{
+    return countRows("imagecontainer", "mediatypeid", id);
+}
+
+// WARNING: this will delete also all the databindings to selected platform
+bool DbMediaType::deleteDataObjectFromModel(QModelIndex *index)
+{
+    QSqlDatabase::database().transaction();
+    QSqlRecord record = sqlTableModel->record(index->row());
+    int id = record.value(MediaType_Id).toInt();
+    qDebug() << "Deleting mediatype " << id;
+    int count = countDataObjectRefs(id);
+    if (count > 0)
+    {
+        QSqlQuery query;
+        if (!query.exec(QString("DELETE FROM imagecontainer WHERE mediatypeid = %1").arg(id)))
+        {
+            qDebug() << "Deleting data bindings failed!";
+            QSqlDatabase::database().rollback();
+            return false;
+        }
+    }
+    sqlTableModel->removeRow(index->row());
+    sqlTableModel->submitAll();
+    return QSqlDatabase::database().commit();
+}
+
+QSqlTableModel* DbMediaType::getData()
+{
+    QSqlTableModel *model = new QSqlTableModel(this);
+    model->setTable(DB_TABLE_NAME_MEDIATYPE);
+    model->setSort(MediaType_Name, Qt::AscendingOrder);
+    model->setHeaderData(MediaType_Name, Qt::Horizontal, tr("Name"));
+    model->select();
+    return model;
+}
diff --git a/src/db/dbmediatype.h b/src/db/dbmediatype.h
new file mode 100644 (file)
index 0000000..5552f8d
--- /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 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 DBMEDIATYPE_H
+#define DBMEDIATYPE_H
+
+#include "databasemanager.h"
+#include "../dataobjects/mediatype.h"
+
+class QModelIndex;
+
+class DbMediaType : public DatabaseManager
+{
+public:
+    DbMediaType(QObject*);
+    virtual QSqlTableModel* getDataModel();
+    virtual EmuFrontObject* getDataObjectFromModel(QModelIndex*);
+    virtual bool updateDataObjectToModel(const EmuFrontObject*);
+    virtual bool insertDataObjectToModel(const EmuFrontObject*);
+    virtual bool deleteDataObjectFromModel(QModelIndex*);
+    virtual int countDataObjectRefs(int) const;
+
+private:
+    enum {
+        MediaType_Id = 0,
+        MediaType_Name = 1,
+        MediaType_Filename = 2 };
+    static const QString DB_TABLE_NAME_MEDIATYPE;
+    virtual QSqlTableModel* getData();
+
+};
+
+#endif // DBMEDIATYPE_H
index 7d4db6b..f708544 100644 (file)
@@ -35,7 +35,7 @@ QSqlTableModel* DbPlatform::getDataModel()
     return sqlTableModel;
 }
 
-Platform* DbPlatform::getPlatformFromModel(QModelIndex *index)
+EmuFrontObject* DbPlatform::getDataObjectFromModel(QModelIndex *index)
 {
     QSqlRecord record = sqlTableModel->record(index->row());
     int id = record.value(Platform_Id).toInt();
@@ -45,16 +45,17 @@ Platform* DbPlatform::getPlatformFromModel(QModelIndex *index)
     return new Platform(id, name, fileName);
 }
 
-bool DbPlatform::updatePlatformToModel(const Platform *ob)
+bool DbPlatform::updateDataObjectToModel(const EmuFrontObject *ob)
 {
+    const Platform *plf = dynamic_cast<const Platform*>(ob);
     bool ret = false;
-    sqlTableModel->setFilter(QString("id = %1").arg(ob->getId()));
+    sqlTableModel->setFilter(QString("id = %1").arg(plf->getId()));
     sqlTableModel->select();
     if (sqlTableModel->rowCount() == 1)
     {
         QSqlRecord record = sqlTableModel->record(0);
-        record.setValue("name", ob->getName());
-        record.setValue("filename", ob->getFilename());
+        record.setValue("name", plf->getName());
+        record.setValue("filename", plf->getFilename());
         sqlTableModel->setRecord(0, record);
         ret = sqlTableModel->submitAll();
     }
@@ -62,35 +63,32 @@ bool DbPlatform::updatePlatformToModel(const Platform *ob)
     return ret;
 }
 
-bool DbPlatform::insertPlatformToModel(const Platform *ob)
+bool DbPlatform::insertDataObjectToModel(const EmuFrontObject *ob)
 {
+    const Platform *plf = dynamic_cast<const Platform*>(ob);
     int row = 0;
     sqlTableModel->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), ob->getName());
-    sqlTableModel->setData(sqlTableModel->index(row, 2), ob->getFilename());
+    sqlTableModel->setData(sqlTableModel->index(row, 1), plf->getName());
+    sqlTableModel->setData(sqlTableModel->index(row, 2), plf->getFilename());
     return sqlTableModel->submitAll();
 }
 
-int DbPlatform::countPlatformBindings(int id) const
+int DbPlatform::countDataObjectRefs(int id) const
 {
-    int numEntries = 0;
-    QSqlQuery query(QString("SELECT COUNT(*) FROM imagecontainer WHERE platformid = %1").arg(id));
-    if (query.next())
-        numEntries = query.value(0).toInt();
-    return numEntries;
+    return countRows("imagecontainer", "platformid", id);
 }
 
 // WARNING: this will delete also all the databindings to selected platform
-bool DbPlatform::deletePlatformFromModel(QModelIndex *index)
+bool DbPlatform::deleteDataObjectFromModel(QModelIndex *index)
 {
     QSqlDatabase::database().transaction();
     QSqlRecord record = sqlTableModel->record(index->row());
     int id = record.value(Platform_Id).toInt();
     qDebug() << "Deleting platform " << id;
-    int count = countPlatformBindings(id);
+    int count = countDataObjectRefs(id);
     if (count > 0)
     {
         QSqlQuery query;
index ce58f23..859c444 100644 (file)
@@ -30,11 +30,11 @@ class DbPlatform : public DatabaseManager
 public:
     DbPlatform(QObject *);
     virtual QSqlTableModel* getDataModel();
-    Platform* getPlatformFromModel(QModelIndex*);
-    bool updatePlatformToModel(const Platform *);
-    bool insertPlatformToModel(const Platform *);
-    bool deletePlatformFromModel(QModelIndex*);
-    int countPlatformBindings(int) const;
+    virtual EmuFrontObject* getDataObjectFromModel(QModelIndex*);
+    virtual bool updateDataObjectToModel(const EmuFrontObject*);
+    bool insertDataObjectToModel(const EmuFrontObject*);
+    bool deleteDataObjectFromModel(QModelIndex*);
+    int countDataObjectRefs(int) const;
 
 private:
     enum {
index 28732bd..b34f595 100644 (file)
@@ -129,12 +129,6 @@ void DbObjectDialog::disableSelection()
     setButtonsEnabled(false);
 }
 
-void DbObjectDialog::updateData()
-{
-    qDebug() << "DbObjectDialog::updateData()";
-    updateList();
-}
-
 void DbObjectDialog::activateNameDialog()
 {
     if (!nameDialog) return;
@@ -143,3 +137,46 @@ void DbObjectDialog::activateNameDialog()
     nameDialog->activateWindow();
 }
 
+void DbObjectDialog::initDataTable()
+{
+   objectList->setModel(dbManager->getDataModel());
+   objectList->setSelectionMode(QAbstractItemView::SingleSelection);
+   objectList->resizeColumnsToContents();
+}
+void DbObjectDialog::updateData()
+{
+    qDebug() << "Update data";
+    // update data model
+    if (!dbObject) return;
+
+    qDebug() << "dbObject is not 0";
+
+    qDebug() << "We have a " + dbObject->getName();
+
+    qDebug() << "Data will be inserted/updated...";
+
+    // if data object id > -1 we are updating the data otherwise we are inserting new data
+    if (dbObject->getId() > -1) updateDb(dbObject);
+    else insertDb(dbObject);
+
+    // we don't need dbObject anymore
+    deleteCurrentObject();
+    dbObject = 0;
+    updateList();
+}
+
+void DbObjectDialog::deleteCurrentObject()
+{
+    delete dbObject;
+}
+
+bool DbObjectDialog::confirmDelete(QString name, int numRefs)
+{
+    int r = QMessageBox::warning(this, tr("Confirm delete"),
+                                 QString("Do you really want to delete %1 with %2 data bindings?")
+                                 .arg(name).arg(numRefs),
+                                 QMessageBox::Yes | QMessageBox::No);
+    if ( r == QMessageBox::No )
+        return false;
+    return true;
+}
index 024256d..9a08ee3 100644 (file)
@@ -43,11 +43,14 @@ protected slots:
        void addButtonClicked();
        void deleteButtonClicked();
        void listObjectClicked(const QModelIndex &);
-    virtual void updateData();
+    void updateData();
     void updateList() const;
+    private slots:
 
 protected:
        virtual int deleteObject() =0;
+    // implementation specific, deletes current data object from memory
+    virtual void deleteCurrentObject();
        virtual void addObject() =0;
        virtual void editObject() =0;
     virtual bool deleteItem() = 0;
@@ -55,6 +58,8 @@ protected:
     virtual void insertDb(const EmuFrontObject*) const = 0;
     void connectSignals();
     void activateNameDialog();
+    void initDataTable();
+    bool confirmDelete(QString name, int numRefs);
     NameDialog *nameDialog;
     DatabaseManager *dbManager;
     QTableView *objectList;
index cac8e02..d97dd15 100644 (file)
@@ -1,5 +1,126 @@
+// 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 <QtGui>
+#include <QAbstractItemView>
+#include <QSqlTableModel>
+#include <QTextStream>
+#include "../db/dbmediatype.h"
+#include "../dataobjects/mediatype.h"
 #include "mediatypedialog.h"
+#include "mediatypenamedialog.h"
+
+MediaTypeDialog::MediaTypeDialog(QWidget* parent)
+    :DbObjectDialog(parent)
+{
+    setWindowTitle(tr("Set media types"));
+    //nameDialog = 0;
+    nameDialog = new MediaTypeNameDialog(this, dynamic_cast<MediaType*>(dbObject));
+    dbManager = new DbMediaType(this);
+    initDataTable();
+
+    // do not move to parent class:
+    connectSignals();
+
+}
+
+MediaTypeDialog::~MediaTypeDialog()
+{
+    delete dynamic_cast<MediaType*>(dbObject);
+}
 
-MediaTypeDialog::MediaTypeDialog()
+void MediaTypeDialog::addObject()
 {
+    delete dynamic_cast<MediaType*>(dbObject);
+    dbObject = new MediaType;
+    nameDialog->setDataObject(dbObject);
+    activateNameDialog();
+}
+
+void MediaTypeDialog::editObject()
+{
+    qDebug() << "editObject called";
+    QModelIndex index = objectList->currentIndex();
+    if (!index.isValid())
+        return;
+    qDebug() << "we have a valid index";
+    delete dbObject;
+    dbObject = (dynamic_cast<DbMediaType*>(dbManager))->getDataObjectFromModel(&index);
+    nameDialog->setDataObject(dbObject);
+    activateNameDialog();
+}
+
+void MediaTypeDialog::deleteCurrentObject()
+{
+    delete dynamic_cast<MediaType*>(dbObject);
+}
+
+int MediaTypeDialog::deleteObject()
+{
+    return 0;
+}
+
+void MediaTypeDialog::updateDb(const EmuFrontObject *ob) const
+{
+    if (!ob) return;
+    qDebug() << "Updating media type" << ob->getName();
+    (dynamic_cast<DbMediaType*>(dbManager))->updateDataObjectToModel(ob);
+}
+
+void MediaTypeDialog::insertDb(const EmuFrontObject *ob) const
+{
+    (dynamic_cast<DbMediaType*>(dbManager))->insertDataObjectToModel(ob);
+}
+
+bool MediaTypeDialog::deleteItem()
+{
+    qDebug() << "MediaTypeDialog::deleteItem()";
+    QModelIndex index = objectList->currentIndex();
+    if (!index.isValid()) return false;
+
+    qDebug() << "Index is valid";
+
+    // TODO: when implementing data bindings to platform
+    // we need to check if platform being removed has bindings
+    // and a) ask user if this platform should be removed
+    // b) remove all the data associated to this platform
+
+    EmuFrontObject *ob = dynamic_cast<DbMediaType*>(dbManager)->getDataObjectFromModel(&index);
+    if (!ob) return false;
+
+    MediaType *plf = dynamic_cast<MediaType*>(ob);
+
+    qDebug() << "Got platform" << plf->getName();
+
+    int numBindings = dynamic_cast<DbMediaType*>(dbManager)->countDataObjectRefs(plf->getId());
+    if (numBindings > 0 && !confirmDelete(plf->getName(), numBindings))
+    {
+        return false;
+    }
+    delete plf;
+    bool delOk = (dynamic_cast<DbMediaType *>(dbManager))->deleteDataObjectFromModel(&index);
+    if (!delOk)
+    {
+        qDebug() << "delete failed";
+        return false;
+    }
+    updateList();
+    objectList->setFocus();
+    return false;
 }
index 1f345db..07bde37 100644 (file)
@@ -32,14 +32,12 @@ public:
 
 protected:
     virtual int deleteObject();
+    virtual void deleteCurrentObject();
     virtual void addObject();
     virtual void editObject();
     virtual bool deleteItem();
     virtual void updateDb(const EmuFrontObject*) const;
     virtual void insertDb(const EmuFrontObject*) const;
-
-private slots:
-    virtual void updateData();
 };
 
 #endif // MEDIATYPEDIALOG_H
diff --git a/src/dialogs/mediatypenamedialog.cpp b/src/dialogs/mediatypenamedialog.cpp
new file mode 100644 (file)
index 0000000..b852359
--- /dev/null
@@ -0,0 +1,50 @@
+// 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 <QtGui>
+#include "mediatypenamedialog.h"
+
+MediaTypeNameDialog::MediaTypeNameDialog(QWidget *parent, MediaType *efObj)
+    : NameDialog(parent, efObj)
+{
+    setWindowTitle(tr("Set media type"));
+}
+
+MediaTypeNameDialog::~MediaTypeNameDialog()
+{
+    // no need to delete efObject here, the calling widget will take care of it
+    // delete dynamic_cast<Platform*>(efObject);
+}
+
+void MediaTypeNameDialog::setDataObject(QString name)
+{
+    efObject->setName(name);
+    (dynamic_cast<MediaType*>(efObject))->setFilename("");
+}
+
+void MediaTypeNameDialog::setDataObject(EmuFrontObject *ob)
+{
+    if (!ob) return;
+    efObject = dynamic_cast<MediaType*>(ob);
+    QString name = efObject->getName();
+    nameEdit->setText(name);
+
+    qDebug() << "Setting name to " << efObject->getName();
+    nameEdit->setText(efObject->getName());
+}
diff --git a/src/dialogs/mediatypenamedialog.h b/src/dialogs/mediatypenamedialog.h
new file mode 100644 (file)
index 0000000..2b8a9e8
--- /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/>.
+
+#ifndef MEDIATYPENAMEDIALOG_H
+#define MEDIATYPENAMEDIALOG_H
+
+#include "namedialog.h"
+#include "../dataobjects/mediatype.h"
+
+class MediaTypeNameDialog : public NameDialog
+{
+    Q_OBJECT
+public:
+    MediaTypeNameDialog(QWidget *parent, MediaType*);
+    ~MediaTypeNameDialog();
+    virtual void setDataObject(EmuFrontObject*);
+
+
+protected:
+    virtual void setDataObject(QString name);
+
+};
+
+#endif // MEDIATYPENAMEDIALOG_H
index e600164..1c13d5c 100644 (file)
@@ -27,7 +27,7 @@
 #include "platformnamedialog.h"
 
 
-QTextStream cout(stdout, QIODevice::WriteOnly);
+//QTextStream cout(stdout, QIODevice::WriteOnly);
 
 PlatformDialog::PlatformDialog(QWidget *parent)
     : DbObjectDialog(parent)
@@ -36,14 +36,8 @@ PlatformDialog::PlatformDialog(QWidget *parent)
     //nameDialog = 0;
     nameDialog = new PlatformNameDialog(this, dynamic_cast<Platform*>(dbObject));
     dbManager = new DbPlatform(this);
-
-    // let's create a table model for platforms
+    initDataTable();
     
-    objectList->setModel(dbManager->getDataModel());
-    objectList->setSelectionMode(QAbstractItemView::SingleSelection);
-    //objectList->setColumnHidden(Platform_Id, true);
-    objectList->resizeColumnsToContents();
-
     // do not move to parent class:
     connectSignals();
 }
@@ -80,45 +74,27 @@ void PlatformDialog::editObject()
         return;
     qDebug() << "we have a valid index";
     delete dbObject;
-    dbObject = (dynamic_cast<DbPlatform*>(dbManager))->getPlatformFromModel(&index);
+    dbObject = (dynamic_cast<DbPlatform*>(dbManager))->getDataObjectFromModel(&index);
     nameDialog->setDataObject(dbObject);
     activateNameDialog();
 }
 
-void PlatformDialog::updateData()
+void PlatformDialog::deleteCurrentObject()
 {
-    qDebug() << "Update data";
-    // update data model
-    if (!dbObject) return;
-
-    qDebug() << "dbObject is not 0";
-
-    qDebug() << "We have a " + dbObject->getName();
-
-    qDebug() << "Data will be inserted/updated...";
-
-    // if data object id > -1 we are updating the data otherwise we are inserting new data
-    if (dbObject->getId() > -1) updateDb(dbObject);
-    else insertDb(dbObject);
-
-    // we don't need dbObject anymore
     delete dynamic_cast<Platform*>(dbObject);
-    dbObject = 0;
-
-    // refresh...
-    DbObjectDialog::updateData();
 }
 
+
 void PlatformDialog::updateDb(const EmuFrontObject *ob) const
 {
     if (!ob) return;
     qDebug() << "Updating platform " << ob->getName();
-    (dynamic_cast<DbPlatform*>(dbManager))->updatePlatformToModel(dynamic_cast<const Platform*>(ob));
+    (dynamic_cast<DbPlatform*>(dbManager))->updateDataObjectToModel(ob);
 }
 
 void PlatformDialog::insertDb(const EmuFrontObject *ob) const
 {
-    (dynamic_cast<DbPlatform*>(dbManager))->insertPlatformToModel(dynamic_cast<const Platform*>(ob));
+    (dynamic_cast<DbPlatform*>(dbManager))->insertDataObjectToModel(ob);
 }
 
 bool PlatformDialog::deleteItem()
@@ -134,24 +110,20 @@ bool PlatformDialog::deleteItem()
     // and a) ask user if this platform should be removed
     // b) remove all the data associated to this platform
 
-    Platform *plf = dynamic_cast<DbPlatform*>(dbManager)->getPlatformFromModel(&index);
-    if (!plf) return false;
+    EmuFrontObject *ob = dynamic_cast<DbPlatform*>(dbManager)->getDataObjectFromModel(&index);
+    if (!ob) return false;
+
+    Platform *plf = dynamic_cast<Platform*>(ob);
 
     qDebug() << "Got platform" << plf->getName();
 
-    int numBindings = dynamic_cast<DbPlatform*>(dbManager)->countPlatformBindings(plf->getId());
-    if (numBindings > 0)
+    int numBindings = dynamic_cast<DbPlatform*>(dbManager)->countDataObjectRefs(plf->getId());
+    if (numBindings > 0 && !confirmDelete(plf->getName(), numBindings))
     {
-        qDebug() << "Got " << numBindings << " bindings";
-        int r = QMessageBox::warning(this, tr("Delete platform"),
-                                     QString("Do you really want to delete platform %1 with %2 data bindings?")
-                                     .arg(plf->getName()).arg(numBindings),
-                                     QMessageBox::Yes | QMessageBox::No);
-        if ( r == QMessageBox::No )
             return false;
     }
     delete plf;
-    bool delOk = (dynamic_cast<DbPlatform *>(dbManager))->deletePlatformFromModel(&index);
+    bool delOk = (dynamic_cast<DbPlatform *>(dbManager))->deleteDataObjectFromModel(&index);
     if (!delOk)
     {
         qDebug() << "delete failed";
index 8eed1ae..b5b7db9 100644 (file)
@@ -26,20 +26,19 @@ class PlatformDialog : public DbObjectDialog
 {
     Q_OBJECT
 
-    public:
+public:
        PlatformDialog(QWidget *parent = 0);
     ~PlatformDialog();
 
-    protected:
-       virtual int deleteObject();
+protected:
+    virtual int deleteObject();
+    virtual void deleteCurrentObject();
        virtual void addObject();
        virtual void editObject();
     virtual bool deleteItem();
     virtual void updateDb(const EmuFrontObject*) const;
     virtual void insertDb(const EmuFrontObject*) const;
 
-    private slots:
-    virtual void updateData();
 };
 
 #endif
index 3bd76b2..23af652 100644 (file)
@@ -25,7 +25,9 @@ HEADERS += mainwindow.h \
     db/dbcreator.h \
     dialogs/mediatypedialog.h \
     dataobjects/emufrontfileobject.h \
-    dataobjects/mediatype.h
+    dataobjects/mediatype.h \
+    db/dbmediatype.h \
+    dialogs/mediatypenamedialog.h
 SOURCES += main.cpp \
     mainwindow.cpp \
     db/databasemanager.cpp \
@@ -40,4 +42,6 @@ SOURCES += main.cpp \
     db/dbcreator.cpp \
     dialogs/mediatypedialog.cpp \
     dataobjects/emufrontfileobject.cpp \
-    dataobjects/mediatype.cpp
+    dataobjects/mediatype.cpp \
+    db/dbmediatype.cpp \
+    dialogs/mediatypenamedialog.cpp