EmuFront ... NOT Foobar :D
[emufront] / src / dialogs / mediaimagepathdialog.cpp
index 588222a..28b6f3f 100644 (file)
@@ -9,87 +9,78 @@
 // the Free Software Foundation, either version 3 of the License, or
 // (at your option) any later version.
 //
-// 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"
 
 MediaImagePathDialog::MediaImagePathDialog(QWidget *parent, EmuFrontObject *efObject)
     : DataObjectEditDialog(parent, efObject, Qt::Horizontal)
 {
-    qDebug() << "Creating MediaImagePathDialog";
     initWidgets();
-    populateMediaTypeComBox();
-    populatePlatformComBox();
+    dbPlatform = 0;
+    dbMediaType = 0;
     connectSignals();
     layout();
 }
 
 MediaImagePathDialog::~MediaImagePathDialog()
 {
-    qDebug() << "Destroying MediaImagePathDialog";
 }
 
 void MediaImagePathDialog::connectSignals()
 {
     DataObjectEditDialog::connectSignals();
-    qDebug() << "MediaImagePathDialog Connecting signals";
+    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()
 {
-    qDebug() << "MediaImagePathDialog initializing widgets.";
     // these widgets will be automatically parented using layout components
     filePathLabel = new QLabel;
     filePathButton = new QPushButton(tr("&Browse filepath"));
-    mediaTypeComBox = new QComboBox;
-    platformComBox = new QComboBox;
+    setupComBox = new QComboBox;
+    populateSetupComBox();
 }
 
-void MediaImagePathDialog::populateMediaTypeComBox()
+void MediaImagePathDialog::populateSetupComBox()
 {
-    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);
+    dbSetup = new DbSetup(this);
+    setupComBox->setModel(dbSetup->getDataModel());
+    setupComBox->setModelColumn(DbSetup::Setup_Name);
 }
 
 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);
@@ -105,44 +96,69 @@ 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());
-}
-
-void MediaImagePathDialog::setSelectedPlatform(const Platform *plf)
-{
-    setSelected(platformModel, platformComBox, plf, DbPlatform::Platform_Id);
+    if (fpo->getSetup()) setSelectedSetup(fpo->getSetup());
 }
 
-void MediaImagePathDialog::setSelectedMediaType(const MediaType *plf)
+void MediaImagePathDialog::setSelectedSetup(const Setup *sup)
 {
-    setSelected(mediaTypeModel, mediaTypeComBox, plf, DbMediaType::MediaType_Id);
+    setSelected(setupComBox, sup, DbSetup::Setup_Id);
 }
 
-// TODO: this might be useful to lever to upper classes
-void MediaImagePathDialog::setSelected(const QSqlTableModel *model, QComboBox *cbox, const EmuFrontObject *ob, int idIndex)
+Setup* MediaImagePathDialog::getSelectedSetup()
 {
-    if (!ob) return;
-    for (int i = 0; i < model->rowCount(); ++i)
+    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)
     {
-        QSqlRecord rec = model->record(i);
-        if (rec.value(idIndex) == ob->getId())
-        {
-            cbox->setCurrentIndex(i);
-            break;
-        }
+        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());
 
+        Platform *plf = dynamic_cast<Platform*>(efPlf);
+        MediaType *mt = dynamic_cast<MediaType*>(efMt);
+        QString exts = rec.value(DbSetup::Setup_FileTypeExtensions).toString();
 
-void MediaImagePathDialog::acceptChanges()
-{
-    close();
+        sup = new Setup(rec.value(DbSetup::Setup_Id).toInt(), plf, mt,
+            exts.split(DbSetup::FILE_TYPE_EXTENSION_SEPARATOR));
+    }
+    else errorMessage->showMessage("Record missing");
+    return sup;
 }
 
-void MediaImagePathDialog::rejectChanges()
+void MediaImagePathDialog::acceptChanges()
 {
-    // we don't delete the data object here
+    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;
+    }
+    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;
+    }
+    Setup *tmp = fpo->getSetup();
+    if (sup != tmp)
+    {
+        delete tmp;
+        fpo->setSetup(sup);
+    }
+    fpo->setName(filePath);
+    emit dataObjectUpdated();
     efObject = 0;
     close();
 }