Fixed bugs: dialogs remained disabled (signals were not attached to
[emufront] / src / dialogs / setupeditdialog.cpp
index 5c82703..c6fa561 100644 (file)
@@ -5,23 +5,24 @@
 //
 //
 // 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.
+// it under the terms of the GNU General Public License version 2 as published by
+// the Free Software Foundation and appearing in the file gpl.txt included in the
+// packaging of this file.
 //
-// Foobar is distributed in the hope that it will be useful,
+// 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 Foobar.  If not, see <http://www.gnu.org/licenses/>.
+// along with EmuFront.  If not, see <http://www.gnu.org/licenses/>.
 
 #include <QtGui>
 #include <QSqlTableModel>
 #include <QSqlRecord>
 #include "setupeditdialog.h"
-#include "../widgets/stringlistwidget.h"
+#include "../widgets/fileextensionwidget.h"
+#include "../widgets/effileobjectcombobox.h"
 #include "../db/dbmediatype.h"
 #include "../db/dbplatform.h"
 #include "../db/dbsetup.h"
 SetupEditDialog::SetupEditDialog(QWidget *parent, EmuFrontObject* obj)
     : DataObjectEditDialog(parent, obj)
 {
-    dbSetup = 0; //new DbSetup(this);
-    dbPlatform = 0; //new DbPlatform(this);
-    dbMediaType = 0; //new DbMediaType(this);
+    dbSetup = 0;
+    dbPlatform = new DbPlatform(this);
+    dbMediaType = new DbMediaType(this);
     initWidgets();
-    qDebug() << "Connecting signals";
     connectSignals();
+    emit test();
     layout();
 }
 
 void SetupEditDialog::initWidgets()
 {
-    mediaTypeComBox = new QComboBox;
-    platformComBox = new QComboBox;
-    supportedFileTypesList = new StringListWidget;
-    populateMediaTypeComBox();
-    populatePlatformComBox();
-}
-
-void SetupEditDialog::populateMediaTypeComBox()
-{
-    qDebug() << "populating media types combo box";
-    if (!dbMediaType) dbMediaType = new DbMediaType(this);
-    qDebug() << "media type database manager created";
-    QSqlQueryModel *model = dbMediaType->getDataModel();
-    if (!model) return;
-    qDebug() << "We have a media type data model";
-    mediaTypeComBox->setModel(model);
-    mediaTypeComBox->setModelColumn(DbMediaType::EmuFrontFileObject_Name);
-}
-
-void SetupEditDialog::populatePlatformComBox()
-{
-    qDebug() << "populating platform combo box";
-    if (!dbPlatform) dbPlatform = new DbPlatform(this);
-    QSqlQueryModel *model = dbPlatform->getDataModel();
-    if (!model) return;
-    platformComBox->setModel(model);
-    platformComBox->setModelColumn(DbPlatform::EmuFrontFileObject_Name);
-    qDebug() << "platform combo box populated";
+    mediaTypeComBox = new EFFileObjectComboBox(dbMediaType, this);
+    platformComBox = new EFFileObjectComboBox(dbPlatform, this);
+    supportedFileTypesList = new FileExtensionWidget;
+    supportedFileTypesList->setToolTip(tr("Set the supported file types by entering the file extension(s) (no wildcards or dots!)."));
 }
 
 void SetupEditDialog::layout()
 {
-    qDebug() << "SetupEditDialog::layout";
-   QLabel *platformLabel = new QLabel(tr("&Platform"));
+   QLabel *platformLabel = new QLabel(tr("&Platform:"));
    platformLabel->setBuddy(platformComBox);
-   QLabel *mediaTypeLabel = new QLabel(tr("Media&Type"));
+   QLabel *mediaTypeLabel = new QLabel(tr("Media&Type:"));
    mediaTypeLabel->setBuddy(mediaTypeComBox);
+   QLabel *fileTypesLabel = new QLabel(tr("Supported file types:"));
    QGridLayout *gridLayout = new QGridLayout;
    gridLayout->addWidget(platformLabel, 0, 0);
    gridLayout->addWidget(platformComBox, 0, 1);
    gridLayout->addWidget(mediaTypeLabel, 1, 0);
    gridLayout->addWidget(mediaTypeComBox, 1, 1);
-   gridLayout->addWidget(supportedFileTypesList, 2, 0, 2, 2);
+   gridLayout->addWidget(fileTypesLabel, 2, 0, 1, 2);
+   gridLayout->addWidget(supportedFileTypesList, 3, 0, 2, 2);
    QVBoxLayout *mainLayout = new QVBoxLayout;
    mainLayout->addLayout(gridLayout);
    mainLayout->addWidget(buttonBox);
    setLayout(mainLayout);
-    qDebug() << "SetupEditDialog::layout done";
-
    setWindowTitle(tr("Edit setup"));
 }
 
@@ -98,7 +74,8 @@ void SetupEditDialog::acceptChanges()
     Platform *plf = getSelectedPlatform();
     if (!plf)
     {
-        QMessageBox::information(this, tr("Platform"), tr("Platform not selected"), QMessageBox::Ok);
+        QMessageBox::information(this, tr("Platform"),
+            tr("Platform not selected"), QMessageBox::Ok);
         return;
     }
     qDebug() << "Platform selected " << plf->getName();
@@ -111,30 +88,46 @@ void SetupEditDialog::acceptChanges()
     qDebug() << "Media type selected " << mt->getName();
 
 
+    bool change = false;
     Platform *ptmp = sup->getPlatform();
-    if (plf != ptmp)
+    if (!ptmp || *plf != *ptmp)
     {
         delete ptmp;
         sup->setPlatform(plf);
+        change = true;
     }
     MediaType *mtmp = sup->getMediaType();
-    if (mt != mtmp)
+    if (!mtmp || *mt != *mtmp)
     {
         delete mtmp;
         sup->setMediaType(mt);
+        change = true;
+    }
+
+    // "Two lists are considered equal if they contain the same values in the same order."
+    if (supportedFileTypesList->getItems() != sup->getSupportedFileTypeExtensions()) {
+        sup->setSupportedFileTypeExtensions(supportedFileTypesList->getItems());
+        change = true;
     }
-    sup->setSupportedFileTypeExtensions(supportedFileTypesList->getItems());
-    emit dataObjectUpdated();
+    // TODO: the second time around this signal is not received!
+    if (change) emit dataObjectUpdated();
     efObject = 0;
-    qDebug() << "Closing setup edit dialog";
     close();
 }
 
 void SetupEditDialog::setDataObject(EmuFrontObject *ob)
 {
     if (!ob) return;
+    qDebug() << "Updating Setup edit dialog data object to "
+        << ob->getName() << ".";
+    if (efObject)
+        delete dynamic_cast<Setup*>(efObject); // TODO: caused crash if another instance of setupeditdialog was created and new instance destroyed object being referenced in another existing dialog.
     efObject = ob;
     Setup *sup= dynamic_cast<Setup*>(ob);
+    if (!sup) {
+        qDebug() << "Failed casting to Setup";
+        return;
+    }
     if (sup->getPlatform()) setSelectedPlatform(sup->getPlatform());
     if (sup->getMediaType()) setSelectedMediaType(sup->getMediaType());
     supportedFileTypesList->setItems(sup->getSupportedFileTypeExtensions());
@@ -142,49 +135,32 @@ void SetupEditDialog::setDataObject(EmuFrontObject *ob)
 
 void SetupEditDialog::setSelectedPlatform(const Platform *plf)
 {
-    setSelected(platformComBox, plf, DbPlatform::EmuFrontFileObject_Id);
+    platformComBox->setSelected(plf);
 }
 
 void SetupEditDialog::setSelectedMediaType(const MediaType *plf)
 {
-    setSelected(mediaTypeComBox, plf, DbMediaType::EmuFrontFileObject_Id);
+    mediaTypeComBox->setSelected(plf);
 }
 
 Platform* SetupEditDialog::getSelectedPlatform() const
 {
-    Platform *plf = 0;
-    int index = platformComBox->currentIndex();
-    if (index < 0) return plf;
-    QSqlTableModel* platformModel = dynamic_cast<QSqlTableModel*>(platformComBox->model());
-    if (!platformModel)
-    {
-        return plf;
-    }
-    QSqlRecord rec = platformModel->record(index);
-    if (!rec.isEmpty())
-    {
-        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;
-    int index = mediaTypeComBox->currentIndex();
-    if (index < 0) return mt;
-    QSqlTableModel* mediaTypeModel = dynamic_cast<QSqlTableModel*>(mediaTypeComBox->model());
-    if (!mediaTypeModel)
-    {
-        qDebug("Media type data model missing");
-        return mt;
-    }
-    QSqlRecord rec = mediaTypeModel->record(index);
-    if (!rec.isEmpty())
-    {
-        EmuFrontObject *o = dbMediaType->getDataObject(rec.value(DbMediaType::EmuFrontFileObject_Id).toInt());
-        mt = dynamic_cast<MediaType*>(o);
-    }
+    EmuFrontObject *o = mediaTypeComBox->getSelected();
+    if (!o) return 0;
+    MediaType *mt = dynamic_cast<MediaType*>(o);
     return mt;
 }
+
+void SetupEditDialog::updateData()
+{
+    platformComBox->updateDataModel();
+    mediaTypeComBox->updateDataModel();
+}