Some comments about heap-objects and cleaning up some heap-objects.
[emufront] / src / dialogs / mediaimagepathdialog.cpp
index a0605c7..facdf1a 100644 (file)
 //
 //
 // 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 "../db/dbplatform.h"
-#include "../db/dbmediatype.h"
+#include <QSqlRecord>
 #include "mediaimagepathdialog.h"
+#include "../db/dbsetup.h"
+#include "../dataobjects/filepathobject.h"
+#include "../widgets/setupcombobox.h"
 
 MediaImagePathDialog::MediaImagePathDialog(QWidget *parent, EmuFrontObject *efObject)
-    : DataObjectEditDialog(parent, efObject)
+    : BrowseFilePathDialog(parent, efObject, Qt::Horizontal)
 {
     initWidgets();
-    populateMediaTypeComBox();
-    populatePlatformComBox();
-    layout();
+    dbPlatform = 0;
+    dbMediaType = 0;
+    dbSetup = 0;
     connectSignals();
+    layout();
 }
 
-void MediaImagePathDialog::connectSignals()
+MediaImagePathDialog::~MediaImagePathDialog()
 {
 }
 
+/*void MediaImagePathDialog::connectSignals()
+{
+    DataObjectEditDialog::connectSignals();
+    connect(filePathButton, SIGNAL(clicked()), this, SLOT(browseFilePath()));
+}*/
+
+/*void MediaImagePathDialog::browseFilePath()
+{
+    QString fpath = QFileDialog::getExistingDirectory(this, tr("Select a directory"), ".",
+        QFileDialog::ShowDirsOnly | QFileDialog::DontResolveSymlinks);
+    QDir d(fpath);
+    if (d.exists() && d.isReadable())
+    {
+        filePathLabel->setText(d.path());
+    }
+}*/
+
 void MediaImagePathDialog::initWidgets()
 {
+    BrowseFilePathDialog::initWidgets();
     // these widgets will be automatically parented using layout components
-    buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel, Qt::Horizontal);
-    filePathLabel = new QLabel;
-    filePathButton = new QPushButton(tr("&Browse filepath"));
-    mediaTypeComBox = new QComboBox;
-    platformComBox = new QComboBox;
+    //filePathLabel = new QLabel;
+    //filePathButton = new QPushButton(tr("&Browse filepath"));
+    dbSetup = new DbSetup(this);
+    setupComBox = new SetupComboBox(dbSetup, this);
 }
 
-void MediaImagePathDialog::populateMediaTypeComBox()
+void MediaImagePathDialog::layout()
 {
+   QLabel *setupLabel = new QLabel(tr("&Set up"));
+   setupLabel->setBuddy(setupComBox);
+
+   QGridLayout *gridLayout = new QGridLayout;
+   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);
+   setLayout(mainLayout);
+
+   setWindowTitle(tr("Set media image paths"));
 }
 
-void MediaImagePathDialog::populatePlatformComBox()
+void MediaImagePathDialog::setDataObject(EmuFrontObject *ob)
 {
+    if (!ob) return;
+    efObject = ob;
+    FilePathObject *fpo = dynamic_cast<FilePathObject*>(ob);
+    QString fpath = fpo->getName();
+    filePathLabel->setText(fpath);
+    if (fpo->getSetup()) setSelectedSetup(fpo->getSetup());
 }
 
-void MediaImagePathDialog::layout()
+void MediaImagePathDialog::setSelectedSetup(const Setup *sup)
 {
-   QLabel *platformLabel = new QLabel(tr("&Platform"));
-   platformLabel->setBuddy(platformComBox);
-   QLabel *mediaTypeLabel = new QLabel(tr("Media&Type"));
-   mediaTypeLabel->setBuddy(mediaTypeComBox);
+    setupComBox->setSelected(sup);
+}
 
-   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(buttonBox, 3, 0, 1, 2);
-   setLayout(gridLayout);
+/* Returns a pointer to Setup object which must be deleted by calling code */
+Setup* MediaImagePathDialog::getSelectedSetup()
+{
+    EmuFrontObject *ob = 0;
 
-   setWindowTitle(tr("Set media image paths"));
+    try {
+        ob = setupComBox->getSelected();
+    }
+    catch(EmuFrontException &e){
+        errorMessage->showMessage(e.what());
+    }
+
+    if (!ob) return 0;
+    return dynamic_cast<Setup*>(ob);
 }
 
-void MediaImagePathDialog::setDataObject(EmuFrontObject *)
+void MediaImagePathDialog::acceptChanges()
+{
+    FilePathObject *fpo = dynamic_cast<FilePathObject*>(efObject);
+    Setup *sup = getSelectedSetup();
+    if (!sup)
+    {
+        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();
+
+    if (!tmp || *sup != *tmp)
+    {
+        delete tmp;
+        fpo->setSetup(sup);
+        change = true;
+    }
+    if (change) emit dataObjectUpdated();
+    efObject = 0;
+    close();
+}
+
+
+void MediaImagePathDialog::updateData()
 {
+    setupComBox->updateDataModel();
 }