Some comments about heap-objects and cleaning up some heap-objects.
[emufront] / src / dialogs / mediaimagepathdialog.cpp
index e6849e3..facdf1a 100644 (file)
@@ -5,54 +5,49 @@
 //
 //
 // 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 "../db/dbplatform.h"
-#include "../db/dbmediatype.h"
 #include "mediaimagepathdialog.h"
-#include "../dataobjects/platform.h"
-#include "../dataobjects/mediatype.h"
+#include "../db/dbsetup.h"
 #include "../dataobjects/filepathobject.h"
+#include "../widgets/setupcombobox.h"
 
 MediaImagePathDialog::MediaImagePathDialog(QWidget *parent, EmuFrontObject *efObject)
-    : DataObjectEditDialog(parent, efObject, Qt::Horizontal)
+    : BrowseFilePathDialog(parent, efObject, Qt::Horizontal)
 {
-    qDebug() << "Creating MediaImagePathDialog";
     initWidgets();
-    populateMediaTypeComBox();
-    populatePlatformComBox();
+    dbPlatform = 0;
+    dbMediaType = 0;
+    dbSetup = 0;
     connectSignals();
     layout();
 }
 
 MediaImagePathDialog::~MediaImagePathDialog()
 {
-    qDebug() << "Destroying MediaImagePathDialog";
 }
 
-void MediaImagePathDialog::connectSignals()
+/*void MediaImagePathDialog::connectSignals()
 {
     DataObjectEditDialog::connectSignals();
     connect(filePathButton, SIGNAL(clicked()), this, SLOT(browseFilePath()));
-    qDebug() << "MediaImagePathDialog Connecting signals";
-}
+}*/
 
-void MediaImagePathDialog::browseFilePath()
+/*void MediaImagePathDialog::browseFilePath()
 {
-    qDebug() << "Browse file path";
     QString fpath = QFileDialog::getExistingDirectory(this, tr("Select a directory"), ".",
         QFileDialog::ShowDirsOnly | QFileDialog::DontResolveSymlinks);
     QDir d(fpath);
@@ -60,50 +55,28 @@ void MediaImagePathDialog::browseFilePath()
     {
         filePathLabel->setText(d.path());
     }
-    qDebug() << fpath << " selected.";
-}
+}*/
 
 void MediaImagePathDialog::initWidgets()
 {
-    qDebug() << "MediaImagePathDialog initializing widgets.";
+    BrowseFilePathDialog::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;
-}
-
-void MediaImagePathDialog::populateMediaTypeComBox()
-{
-    qDebug() << "MediaImagePathDialog populating media types combo box";
-    dbMediaType = new DbMediaType(this);
-    mediaTypeComBox->setModel(dbMediaType->getDataModel());
-    mediaTypeComBox->setModelColumn(DbMediaType::MediaType_Name);
-}
-
-void MediaImagePathDialog::populatePlatformComBox()
-{
-    qDebug() << "MediaImagePathDialog populating platform combo box";
-    dbPlatform = new DbPlatform(this);
-    platformComBox->setModel(dbPlatform->getDataModel());
-    platformComBox->setModelColumn(DbPlatform::Platform_Name);
+    //filePathLabel = new QLabel;
+    //filePathButton = new QPushButton(tr("&Browse filepath"));
+    dbSetup = new DbSetup(this);
+    setupComBox = new SetupComboBox(dbSetup, this);
 }
 
 void MediaImagePathDialog::layout()
 {
-    qDebug() << "MediaImagePathDialog setting layout";
-   QLabel *platformLabel = new QLabel(tr("&Platform"));
-   platformLabel->setBuddy(platformComBox);
-   QLabel *mediaTypeLabel = new QLabel(tr("Media&Type"));
-   mediaTypeLabel->setBuddy(mediaTypeComBox);
+   QLabel *setupLabel = new QLabel(tr("&Set up"));
+   setupLabel->setBuddy(setupComBox);
 
    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(filePathButton, 2, 0);
-   gridLayout->addWidget(filePathLabel, 2, 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,44 +92,67 @@ 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());
 }
 
-void MediaImagePathDialog::setSelectedPlatform(const Platform *plf)
+void MediaImagePathDialog::setSelectedSetup(const Setup *sup)
 {
-    setSelected(platformModel, platformComBox, plf, DbPlatform::Platform_Id);
+    setupComBox->setSelected(sup);
 }
 
-void MediaImagePathDialog::setSelectedMediaType(const MediaType *plf)
+/* Returns a pointer to Setup object which must be deleted by calling code */
+Setup* MediaImagePathDialog::getSelectedSetup()
 {
-    setSelected(mediaTypeModel, mediaTypeComBox, plf, DbMediaType::MediaType_Id);
+    EmuFrontObject *ob = 0;
+
+    try {
+        ob = setupComBox->getSelected();
+    }
+    catch(EmuFrontException &e){
+        errorMessage->showMessage(e.what());
+    }
+
+    if (!ob) return 0;
+    return dynamic_cast<Setup*>(ob);
 }
 
-// TODO: this might be useful to lever to upper classes
-void MediaImagePathDialog::setSelected(const QSqlTableModel *model, QComboBox *cbox, const EmuFrontObject *ob, int idIndex)
+void MediaImagePathDialog::acceptChanges()
 {
-    if (!ob) return;
-    for (int i = 0; i < model->rowCount(); ++i)
+    FilePathObject *fpo = dynamic_cast<FilePathObject*>(efObject);
+    Setup *sup = getSelectedSetup();
+    if (!sup)
     {
-        QSqlRecord rec = model->record(i);
-        if (rec.value(idIndex) == ob->getId())
-        {
-            cbox->setCurrentIndex(i);
-            break;
-        }
+        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())
+    {
+        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();
 
-void MediaImagePathDialog::acceptChanges()
-{
+    if (!tmp || *sup != *tmp)
+    {
+        delete tmp;
+        fpo->setSetup(sup);
+        change = true;
+    }
+    if (change) emit dataObjectUpdated();
+    efObject = 0;
     close();
 }
 
-void MediaImagePathDialog::rejectChanges()
+
+void MediaImagePathDialog::updateData()
 {
-    // we don't delete the data object here
-    efObject = 0;
-    close();
+    setupComBox->updateDataModel();
 }