Some comments about heap-objects and cleaning up some heap-objects.
[emufront] / src / dialogs / mediaimagepathdialog.cpp
index ebeefae..facdf1a 100644 (file)
@@ -5,9 +5,9 @@
 //
 //
 // 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.
 //
 // EmuFront is distributed in the hope that it will be useful,
 // but WITHOUT ANY WARRANTY; without even the implied warranty of
@@ -26,7 +26,7 @@
 #include "../widgets/setupcombobox.h"
 
 MediaImagePathDialog::MediaImagePathDialog(QWidget *parent, EmuFrontObject *efObject)
-    : DataObjectEditDialog(parent, efObject, Qt::Horizontal)
+    : BrowseFilePathDialog(parent, efObject, Qt::Horizontal)
 {
     initWidgets();
     dbPlatform = 0;
@@ -40,13 +40,13 @@ MediaImagePathDialog::~MediaImagePathDialog()
 {
 }
 
-void MediaImagePathDialog::connectSignals()
+/*void MediaImagePathDialog::connectSignals()
 {
     DataObjectEditDialog::connectSignals();
     connect(filePathButton, SIGNAL(clicked()), this, SLOT(browseFilePath()));
-}
+}*/
 
-void MediaImagePathDialog::browseFilePath()
+/*void MediaImagePathDialog::browseFilePath()
 {
     QString fpath = QFileDialog::getExistingDirectory(this, tr("Select a directory"), ".",
         QFileDialog::ShowDirsOnly | QFileDialog::DontResolveSymlinks);
@@ -55,25 +55,18 @@ void MediaImagePathDialog::browseFilePath()
     {
         filePathLabel->setText(d.path());
     }
-}
+}*/
 
 void MediaImagePathDialog::initWidgets()
 {
+    BrowseFilePathDialog::initWidgets();
     // these widgets will be automatically parented using layout components
-    filePathLabel = new QLabel;
-    filePathButton = new QPushButton(tr("&Browse filepath"));
+    //filePathLabel = new QLabel;
+    //filePathButton = new QPushButton(tr("&Browse filepath"));
     dbSetup = new DbSetup(this);
-    setupComBox = new SetupComboBox(dbSetup, this); //QComboBox;
-    //populateSetupComBox();
+    setupComBox = new SetupComboBox(dbSetup, this);
 }
 
-/*void MediaImagePathDialog::populateSetupComBox()
-{
-    dbSetup = new DbSetup(this);
-    setupComBox->setModel(dbSetup->getDataModel());
-    setupComBox->setModelColumn(DbSetup::Setup_Name);
-}*/
-
 void MediaImagePathDialog::layout()
 {
    QLabel *setupLabel = new QLabel(tr("&Set up"));
@@ -104,43 +97,23 @@ void MediaImagePathDialog::setDataObject(EmuFrontObject *ob)
 
 void MediaImagePathDialog::setSelectedSetup(const Setup *sup)
 {
-
     setupComBox->setSelected(sup);
-    // setSelected(setupComBox, sup, DbSetup::Setup_Id);
 }
 
+/* Returns a pointer to Setup object which must be deleted by calling code */
 Setup* MediaImagePathDialog::getSelectedSetup()
 {
-    EmuFrontObject *ob = setupComBox->getSelected();
-    if (!ob) return 0;
-    return dynamic_cast<Setup*>(ob);
-    /*if (!dbPlatform) dbPlatform => new DbPlatform(this);
-    if (!dbMediaType) dbMediaType = new DbMediaType(this);
-    Setup *sup = 0;
-    int index = setupComBox->currentIndex();
-    if (index < 0) return sup;
-    QSqlQueryModel *model
-        = dynamic_cast<QSqlQueryModel*>(setupComBox->model());
-    if (!model)
-    {
-        errorMessage->showMessage(tr("Data model missing."));
-        return sup;
-    }
-    QSqlRecord rec = model->record(index);
-    if (!rec.isEmpty())
-    {
-        EmuFrontObject *efPlf = dbPlatform->getDataObject(rec.value(DbSetup::Setup_PlatformId).toInt());
-        EmuFrontObject *efMt = dbMediaType->getDataObject(rec.value(DbSetup::Setup_MediaTypeId).toInt());
+    EmuFrontObject *ob = 0;
 
-        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));
+    try {
+        ob = setupComBox->getSelected();
+    }
+    catch(EmuFrontException &e){
+        errorMessage->showMessage(e.what());
     }
-    else errorMessage->showMessage("Record missing");
-    return sup;*/
+
+    if (!ob) return 0;
+    return dynamic_cast<Setup*>(ob);
 }
 
 void MediaImagePathDialog::acceptChanges()
@@ -152,6 +125,7 @@ void MediaImagePathDialog::acceptChanges()
         QMessageBox::information(this, tr("Set up"), tr("Set up not selected"), QMessageBox::Ok);
         return;
     }
+    bool change = false;
     qDebug() << "Setup selected " << sup->getName();
     QString filePath = filePathLabel->text();
     if (filePath.isEmpty())
@@ -159,14 +133,20 @@ void MediaImagePathDialog::acceptChanges()
         QMessageBox::information(this, tr("File path"), tr("File path was not selected"), QMessageBox::Ok);
         return;
     }
+    if (filePath != fpo->getName()) {
+        fpo->setName(filePath);
+        change = true;
+    }
+
     Setup *tmp = fpo->getSetup();
-    if (sup != tmp)
+
+    if (!tmp || *sup != *tmp)
     {
         delete tmp;
         fpo->setSetup(sup);
+        change = true;
     }
-    fpo->setName(filePath);
-    emit dataObjectUpdated();
+    if (change) emit dataObjectUpdated();
     efObject = 0;
     close();
 }