Added new ComboBox child classes. Updated SetupEditDialog to use these.
authorMikko Keinänen <mikko.keinanen@gmail.com>
Fri, 1 Oct 2010 21:51:02 +0000 (00:51 +0300)
committerMikko Keinänen <mikko.keinanen@gmail.com>
Fri, 1 Oct 2010 21:51:02 +0000 (00:51 +0300)
Added data model updating to DatabaseManager. Added virtual UpdateData
to DataObjectEditDialog.

14 files changed:
doc/uml-db_layer.dia
src/db/databasemanager.cpp
src/db/databasemanager.h
src/dialogs/dataobjecteditdialog.cpp
src/dialogs/dataobjecteditdialog.h
src/dialogs/dbobjectdialog.cpp
src/dialogs/platformnamedialog.cpp
src/dialogs/setupeditdialog.cpp
src/dialogs/setupeditdialog.h
src/emufront.pro
src/widgets/efcombobox.cpp [new file with mode: 0644]
src/widgets/efcombobox.h [new file with mode: 0644]
src/widgets/effileobjectcombobox.cpp [new file with mode: 0644]
src/widgets/effileobjectcombobox.h [new file with mode: 0644]

index 7191859..b18718a 100644 (file)
Binary files a/doc/uml-db_layer.dia and b/doc/uml-db_layer.dia differ
index e28df50..b70f36f 100644 (file)
@@ -53,9 +53,9 @@ DatabaseManager::~DatabaseManager()
     // be destroyed when parent is destroyed
 }
 
-QSqlQueryModel* DatabaseManager::getDataModel()
+QSqlQueryModel* DatabaseManager::getDataModel(bool update)
 {
-    if (!sqlTableModel) sqlTableModel = getData();
+    if (!sqlTableModel || (sqlTableModel && !update)) sqlTableModel = getData();
     return sqlTableModel;
 }
 
index 7dd2855..44787c0 100644 (file)
@@ -36,7 +36,7 @@ public:
        DatabaseManager(QObject *parent = 0);
        ~DatabaseManager();
 
-    QSqlQueryModel* getDataModel();
+    QSqlQueryModel* getDataModel(bool update = false);
     EmuFrontObject* getDataObjectFromModel(QModelIndex*);
     EmuFrontObject* getDataObject(int id);
     EmuFrontObject* getDataObject(QString filter);
index cb5941d..67ef001 100644 (file)
@@ -37,6 +37,7 @@ void DataObjectEditDialog::connectSignals()
     connect(buttonBox, SIGNAL(rejected()), this, SLOT(rejectChanges()));
 }
 
+// TODO: remove this after implementing in EFComboBox
 void DataObjectEditDialog::setSelected(QComboBox *cbox, const EmuFrontObject *ob, int idIndex)
 {
     if (!ob) return;
@@ -60,3 +61,7 @@ void DataObjectEditDialog::rejectChanges()
     emit updateRejected();
     close();
 }
+
+void DataObjectEditDialog::updateData()
+{
+}
index 4c4f1a6..af8f93e 100644 (file)
@@ -33,6 +33,7 @@ class DataObjectEditDialog : public EmuFrontDialog
 public:
     DataObjectEditDialog(QWidget *parent, EmuFrontObject *ob, Qt::Orientation orientation = Qt::Horizontal);
     virtual void setDataObject(EmuFrontObject *) = 0;
+    virtual void updateData();
 
 protected:
     virtual void connectSignals();
index 9bcb91b..be16ef6 100644 (file)
@@ -212,6 +212,7 @@ void DbObjectDialog::activateNameDialog()
     if (!nameDialog) return;
     nameDialog->show();
     nameDialog->raise();
+    nameDialog->updateData();
     nameDialog->activateWindow();
 }
 
index ce8bb9a..208a480 100644 (file)
@@ -49,3 +49,4 @@ void PlatformNameDialog::setDataObject(EmuFrontObject *ob)
     qDebug() << "Setting name to " << efObject->getName();
     nameEdit->setText(efObject->getName());
 }
+
index 4508a28..ebe54d2 100644 (file)
@@ -22,6 +22,7 @@
 #include <QSqlRecord>
 #include "setupeditdialog.h"
 #include "../widgets/stringlistwidget.h"
+#include "../widgets/effileobjectcombobox.h"
 #include "../db/dbmediatype.h"
 #include "../db/dbplatform.h"
 #include "../db/dbsetup.h"
@@ -30,8 +31,8 @@ SetupEditDialog::SetupEditDialog(QWidget *parent, EmuFrontObject* obj)
     : DataObjectEditDialog(parent, obj)
 {
     dbSetup = 0; //new DbSetup(this);
-    dbPlatform = 0; //new DbPlatform(this);
-    dbMediaType = 0; //new DbMediaType(this);
+    dbPlatform = new DbPlatform(this);
+    dbMediaType = new DbMediaType(this);
     initWidgets();
     qDebug() << "Connecting signals";
     connectSignals();
@@ -40,14 +41,14 @@ SetupEditDialog::SetupEditDialog(QWidget *parent, EmuFrontObject* obj)
 
 void SetupEditDialog::initWidgets()
 {
-    mediaTypeComBox = new QComboBox;
-    platformComBox = new QComboBox;
+    mediaTypeComBox = new EFFileObjectComboBox(dbMediaType, this); //new QComboBox;
+    platformComBox = new EFFileObjectComboBox(dbPlatform, this); //new QComboBox;
     supportedFileTypesList = new StringListWidget;
-    populateMediaTypeComBox();
-    populatePlatformComBox();
+    //populateMediaTypeComBox();
+    //populatePlatformComBox();
 }
 
-void SetupEditDialog::populateMediaTypeComBox()
+/*void SetupEditDialog::populateMediaTypeComBox()
 {
     qDebug() << "populating media types combo box";
     if (!dbMediaType) dbMediaType = new DbMediaType(this);
@@ -68,7 +69,7 @@ void SetupEditDialog::populatePlatformComBox()
     platformComBox->setModel(model);
     platformComBox->setModelColumn(DbPlatform::EmuFrontFileObject_Name);
     qDebug() << "platform combo box populated";
-}
+}*/
 
 void SetupEditDialog::layout()
 {
@@ -142,17 +143,19 @@ void SetupEditDialog::setDataObject(EmuFrontObject *ob)
 
 void SetupEditDialog::setSelectedPlatform(const Platform *plf)
 {
-    setSelected(platformComBox, plf, DbPlatform::EmuFrontFileObject_Id);
+    //setSelected(platformComBox, plf, DbPlatform::EmuFrontFileObject_Id);
+    platformComBox->setSelected(plf);
 }
 
 void SetupEditDialog::setSelectedMediaType(const MediaType *plf)
 {
-    setSelected(mediaTypeComBox, plf, DbMediaType::EmuFrontFileObject_Id);
+    //setSelected(mediaTypeComBox, plf, DbMediaType::EmuFrontFileObject_Id);
+    mediaTypeComBox->setSelected(plf);
 }
 
 Platform* SetupEditDialog::getSelectedPlatform() const
 {
-    Platform *plf = 0;
+    /*Platform *plf = 0;
     int index = platformComBox->currentIndex();
     qDebug() << "Platform from index " << index << " selected";
     if (index < 0)
@@ -172,13 +175,17 @@ Platform* SetupEditDialog::getSelectedPlatform() const
         EmuFrontObject *o
             = dbPlatform->getDataObject(rec.value(DbPlatform::EmuFrontFileObject_Id).toInt());
         plf = dynamic_cast<Platform*>(o);
-    }
+    }*/
+
+    EmuFrontObject *o = platformComBox->getSelected();
+    if (!o) return 0;
+    Platform *plf = dynamic_cast<Platform*>(o);
     return plf;
 }
 
 MediaType* SetupEditDialog::getSelectedMediaType() const
 {
-    MediaType *mt = 0;
+    /*MediaType *mt = 0;
     int index = mediaTypeComBox->currentIndex();
     if (index < 0) return mt;
     QSqlQueryModel* mediaTypeModel = dynamic_cast<QSqlQueryModel*>(mediaTypeComBox->model());
@@ -193,5 +200,16 @@ MediaType* SetupEditDialog::getSelectedMediaType() const
         EmuFrontObject *o = dbMediaType->getDataObject(rec.value(DbMediaType::EmuFrontFileObject_Id).toInt());
         mt = dynamic_cast<MediaType*>(o);
     }
+    return mt;*/
+    EmuFrontObject *o = mediaTypeComBox->getSelected();
+    if (!o) return 0;
+    MediaType *mt = dynamic_cast<MediaType*>(o);
     return mt;
 }
+
+void SetupEditDialog::updateData()
+{
+    qDebug() << "Updating combobox data";
+    platformComBox->updateDataModel();
+    mediaTypeComBox->updateDataModel();
+}
index 9955831..eb21559 100644 (file)
@@ -22,7 +22,8 @@
 
 #include "dataobjecteditdialog.h"
 
-class QComboBox;
+//class QComboBox;
+class EFFileObjectComboBox;
 class DbSetup;
 class DbMediaType;
 class DbPlatform;
@@ -38,13 +39,16 @@ public:
     SetupEditDialog(QWidget *parent, EmuFrontObject*);
     //~SetupEditDialog();
     virtual void setDataObject(EmuFrontObject *);
+    virtual void updateData();
 
 protected slots:
     virtual void acceptChanges();
 
 private:
-    QComboBox *mediaTypeComBox;
-    QComboBox *platformComBox;
+    //QComboBox *mediaTypeComBox;
+    EFFileObjectComboBox *mediaTypeComBox;
+    //QComboBox *platformComBox;
+    EFFileObjectComboBox *platformComBox;
     DbSetup *dbSetup;
     DbPlatform *dbPlatform;
     DbMediaType *dbMediaType;
index 6c9f988..dc74408 100644 (file)
@@ -53,7 +53,9 @@ HEADERS += mainwindow.h \
     db/dbmediaimage.h \
     utils/OSDaB-Zip/zipentry_p.h \
     utils/OSDaB-Zip/unzip_p.h \
-    emulauncher.h
+    emulauncher.h \
+    widgets/efcombobox.h \
+    widgets/effileobjectcombobox.h
 SOURCES += main.cpp \
     mainwindow.cpp \
     db/databasemanager.cpp \
@@ -94,5 +96,7 @@ SOURCES += main.cpp \
     db/dbemufrontfileobject.cpp \
     db/dbmediaimagecontainer.cpp \
     db/dbmediaimage.cpp \
-    emulauncher.cpp
+    emulauncher.cpp \
+    widgets/efcombobox.cpp \
+    widgets/effileobjectcombobox.cpp
 OTHER_FILES += 
diff --git a/src/widgets/efcombobox.cpp b/src/widgets/efcombobox.cpp
new file mode 100644 (file)
index 0000000..9d5440f
--- /dev/null
@@ -0,0 +1,85 @@
+// 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.
+//
+// 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 "efcombobox.h"
+#include "../exceptions/emufrontexception.h"
+#include "../dataobjects/emufrontobject.h"
+#include <QSqlQueryModel>
+#include <QSqlRecord>
+#include <QDebug>
+
+EFComboBox::EFComboBox(DatabaseManager *dbMan, QWidget *parent)
+    : QComboBox(parent), dbManager(dbMan)
+{
+    if (!dbManager)
+        throw new EmuFrontException("Database manager is not available!");
+    updateDataModel();
+}
+
+EFComboBox::~EFComboBox()
+{
+}
+
+void EFComboBox::updateDataModel()
+{
+    QSqlQueryModel *model = dbManager->getDataModel();
+    if (model)
+        setModel(model);
+}
+
+EmuFrontObject* EFComboBox::getSelected() const
+{
+    EmuFrontObject *efo = 0;
+    int index = currentIndex();
+    qDebug() << "Selected index " << index << ".";
+    if (index < 0)
+        return 0;
+    QSqlQueryModel *qmodel
+        = dynamic_cast<QSqlQueryModel*>(model());
+    if (!qmodel) {
+        qDebug() << "No data model available!";
+        return 0;
+    }
+    QSqlRecord rec = qmodel->record(index);
+    if (rec.isEmpty()) {
+        qDebug() << "No record available!";
+        return efo;
+    }
+    qDebug() << "Fetching a data object with id "
+        << rec.value(dataModelIndex_id).toInt();
+    EmuFrontObject *o
+        = dbManager->getDataObject(rec.value(dataModelIndex_id).toInt());
+    return o;
+}
+
+void EFComboBox::setSelected(const EmuFrontObject *efo)
+{
+    if (!efo)
+        return;
+    QSqlQueryModel *qmodel
+        = dynamic_cast<QSqlQueryModel*>(model());
+    for (int i = 0; i < qmodel->rowCount(); i++){
+        QSqlRecord rec = qmodel->record(i);
+        if (rec.value(dataModelIndex_id) == efo->getId()){
+            setCurrentIndex(i);
+            break;
+        }
+    }
+
+}
diff --git a/src/widgets/efcombobox.h b/src/widgets/efcombobox.h
new file mode 100644 (file)
index 0000000..780273b
--- /dev/null
@@ -0,0 +1,41 @@
+// 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.
+//
+// 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 EFCOMBOBOX_H
+#define EFCOMBOBOX_H
+
+#include <QComboBox>
+#include "../db/databasemanager.h"
+
+class EFComboBox : public QComboBox
+{
+public:
+    EFComboBox(DatabaseManager *dbMan, QWidget *parent = 0);
+    ~EFComboBox();
+    EmuFrontObject* getSelected() const;
+    // This will replace DataObjectEditDialog::setSelected!
+    void setSelected(const EmuFrontObject*);
+    void updateDataModel();
+protected:
+    DatabaseManager *dbManager;
+    int dataModelIndex_id;
+    int dataModelIndex_name;
+};
+
+#endif // EFCOMBOBOX_H
diff --git a/src/widgets/effileobjectcombobox.cpp b/src/widgets/effileobjectcombobox.cpp
new file mode 100644 (file)
index 0000000..b969e0c
--- /dev/null
@@ -0,0 +1,31 @@
+// 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.
+//
+// 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 "effileobjectcombobox.h"
+#include "../db/dbemufrontfileobject.h"
+
+EFFileObjectComboBox::EFFileObjectComboBox(DatabaseManager *dbMan, QWidget *parent)
+    : EFComboBox(dbMan, parent)
+{
+    dataModelIndex_id
+        = DbEmuFrontFileObject::EmuFrontFileObject_Id;
+    dataModelIndex_name
+        = DbEmuFrontFileObject::EmuFrontFileObject_Name;
+    setModelColumn(dataModelIndex_name);
+}
diff --git a/src/widgets/effileobjectcombobox.h b/src/widgets/effileobjectcombobox.h
new file mode 100644 (file)
index 0000000..f82422e
--- /dev/null
@@ -0,0 +1,31 @@
+// 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.
+//
+// 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 EFFILEOBJECTCOMBOBOX_H
+#define EFFILEOBJECTCOMBOBOX_H
+
+#include "efcombobox.h"
+
+class EFFileObjectComboBox : public EFComboBox
+{
+public:
+    EFFileObjectComboBox(DatabaseManager *dbMan, QWidget *parent = 0);
+};
+
+#endif // EFFILEOBJECTCOMBOBOX_H