Imported needed functionality from (soon) deprecated database
[emufront] / src / emulauncher.cpp
index 100fcdb..0e15d46 100644 (file)
@@ -1,43 +1,40 @@
-// EmuFront
-// Copyright 2010 Mikko Keinänen
-//
-// This file is part of EmuFront.
-//
-//
-// EmuFront is free software: you can redistribute it and/or modify
-// 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
-// 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 EmuFront.  If not, see <http://www.gnu.org/licenses/>.
-
+/*
+** EmuFront
+** Copyright 2010 Mikko Keinänen
+**
+** This file is part of EmuFront.
+**
+**
+** EmuFront is free software: you can redistribute it and/or modify
+** 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
+** 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 EmuFront.  If not, see <http://www.gnu.org/licenses/>.
+*/
 #include <QtGui>
 #include <QProcess>
 #include <QSqlTableModel>
 #include <QItemSelectionModel>
 #include "emulauncher.h"
-#include "db/dbmediatype.h"
-#include "db/dbplatform.h"
-#include "db/dbexecutable.h"
-#include "db/dbmediaimagecontainer.h"
-#include "widgets/effileobjectcombobox.h"
-#include "widgets/executablecombobox.h"
-#include "dataobjects/executable.h"
-#include "utils/emuhelper.h"
-#include "dialogs/emufrontinputdialog.h"
+#include "setupmodel.h"
+#include "externalexecutablemodel.h"
+#include "dbmediaimagecontainer.h"
+#include "effileobjectcombobox.h"
+#include "executablecombobox.h"
+#include "executable.h"
+#include "emuhelper.h"
+#include "emufrontinputdialog.h"
 
 EmuLauncher::EmuLauncher(QErrorMessage *errorMessage, QWidget *parent, QString tmp) :
     QWidget(parent), tmpDirPath(tmp), errorMessage(errorMessage)
 {
-    dbPlatform = new DbPlatform(this);
-    dbMediaType = new DbMediaType(this);
-    dbExec = new DbExecutable(this);
     dbMic = 0;
     emuHelper = new EmuHelper(this);
     initWidgets();
@@ -57,9 +54,6 @@ EmuLauncher::~EmuLauncher()
 
 void EmuLauncher::updateData()
 {
-    platformSelectBox->updateDataModel();
-    mediaTypeSelectBox->updateDataModel();
-    //execSelectBox->updateDataModel();
 }
 
 void EmuLauncher::initWidgets()
@@ -68,11 +62,18 @@ void EmuLauncher::initWidgets()
     micTable->setSelectionMode(QAbstractItemView::ExtendedSelection);
     micTable->setCornerButtonEnabled(false);
     micTable->verticalHeader()->setVisible(false);
-    //micTable->horizontalHeader()->setDisabled(true);
     micTable->horizontalHeader()->setClickable(false);
-    mediaTypeSelectBox = new EFFileObjectComboBox(dbMediaType, this);
-    platformSelectBox = new EFFileObjectComboBox(dbPlatform, this);
-    execSelectBox = new ExecutableComboBox(dbExec, this);
+
+    SetupModel *supModel = new SetupModel(this);
+    setupSelectBox = new QComboBox(this);
+    setupSelectBox->setModel(supModel);
+    setupSelectBox->setModelColumn(SetupModel::Setup_Name);
+
+    ExternalExecutableModel *emuModel = new ExternalExecutableModel(this);
+    execSelectBox = new QComboBox(this);
+    execSelectBox->setModel(emuModel);
+    execSelectBox->setModelColumn(ExternalExecutableModel::Executable_Name);
+
     selectButton = new QPushButton(tr("&Update"), this);
     launchButton = new QPushButton(tr("&Launch"), this);
 }
@@ -80,8 +81,7 @@ void EmuLauncher::initWidgets()
 void EmuLauncher::layout()
 {
     QGridLayout *grid = new QGridLayout;
-    grid->addWidget(platformSelectBox, 0, 0);
-    grid->addWidget(mediaTypeSelectBox, 0, 1);
+    grid->addWidget(setupSelectBox, 0, 0, 1, 2);
     grid->addWidget(selectButton, 0, 2);
     grid->setColumnStretch(3, 1);
 
@@ -102,22 +102,22 @@ void EmuLauncher::connectSignals()
 
 void EmuLauncher::updateMediaImageContainers()
 {
-    int mtid, plfid = -1;
-    MediaType *mt = 0;
-    Platform *plf = 0;
-    try {
-        mt = dynamic_cast<MediaType*>(mediaTypeSelectBox->getSelected());
-        plf = dynamic_cast<Platform*>(platformSelectBox->getSelected());
-    }
-    catch(EmuFrontException &e){
-        errorMessage->showMessage(e.what());
-        return;
-    }
-    mtid = mt ? mt->getId() : -1;
-    plfid = plf ? plf->getId() : -1;
-    if (mt) delete mt;
-    if (plf) delete plf;
+    if (setupSelectBox->currentIndex() == -1) return;
+
+    // 1. get selected platform and media type id
+    QAbstractItemModel *setupAbsModel = setupSelectBox->model();
+    SetupModel *supModel = qobject_cast<SetupModel *>(setupAbsModel);
+    if (!supModel) return;
+    QModelIndex plfInd =
+        supModel->index(setupSelectBox->currentIndex(), SetupModel::Setup_PlatformId);
+    int plfid = supModel->data(plfInd).toInt();
+    QModelIndex mtInd =
+        supModel->index(setupSelectBox->currentIndex(), SetupModel::Setup_MediaTypeId);
+    int mtid = supModel->data(mtInd).toInt();
+
+    if (mtid < 0 || plfid < 0) return;
 
+    // 2. fetch available media image containers
     if (!dbMic) dbMic = new DbMediaImageContainer(this);
     dbMic->filter(mtid, plfid);
     micTable->setModel(dbMic->getDataModel());
@@ -132,7 +132,15 @@ void EmuLauncher::updateMediaImageContainers()
     micTable->hideColumn(DbMediaImageContainer::MIC_MediaTypeName);
     micTable->hideColumn(DbMediaImageContainer::MIC_MediaTypeId);
     micTable->resizeColumnsToContents();
-    execSelectBox->updateToSetup(plfid, mtid);
+
+    // 3. filter available emulators
+    QModelIndex supInd =
+            supModel->index(setupSelectBox->currentIndex(), SetupModel::Setup_Id);
+    int supid = supModel->data(supInd).toInt();
+    QAbstractItemModel *execAbsModel = execSelectBox->model();
+    ExternalExecutableModel *execModel = qobject_cast<ExternalExecutableModel*>(execAbsModel);
+    if (!execModel) return;
+    execModel->filterBySetup(supid);
 }
 
 void EmuLauncher::launchEmu()
@@ -156,14 +164,16 @@ void EmuLauncher::launchEmu()
         }
         qDebug() << listMIndex.count() << " items selected.";
 
-        EmuFrontObject *obExe = execSelectBox->getSelected();
+        // TODO: write a method to ExternalExecutable to return an Executable object of a selected row.
+        // TODO2: rewrite ExecutableComboBox and reimplement getSelected?
+        /*EmuFrontObject *obExe = execSelectBox->getSelected();
         if (!obExe) {
             throw EmuFrontException(tr("Failed fetching selected emulator!"));
         }
         exe = dynamic_cast<Executable*>(obExe);
         if (!exe) {
             throw EmuFrontException(tr("Failed creating Emulator object!"));
-        }
+        }*/
 
         qDebug() << "File types; " << exe->getSetup()->getSupportedFileTypeExtensions().count();