Fixed memory loss cases (parent was missing from objects).
[emufront] / src / emulauncher.cpp
index a190248..d876634 100644 (file)
@@ -39,7 +39,6 @@ EmuLauncher::EmuLauncher(QWidget *parent) :
     dbMediaType = new DbMediaType(this);
     dbExec = new DbExecutable(this);
     dbMic = 0;
-    proc = 0;
     emuHelper = new EmuHelper(this);
     initWidgets();
     layout();
@@ -48,10 +47,12 @@ EmuLauncher::EmuLauncher(QWidget *parent) :
 
 EmuLauncher::~EmuLauncher()
 {
-    if (proc) {
-        proc->kill(); // TODO: do this in a more sophisticated way
-        delete proc;
-    }
+    if (emuHelper) {
+        qDebug() << "EmuLauncher destructor";
+        if (emuHelper->state() == EmuHelper::Running)
+            qDebug() << "EmuHelper process is running, killing...";
+            emuHelper->kill();
+        }
 }
 
 void EmuLauncher::updateData()
@@ -68,8 +69,8 @@ void EmuLauncher::initWidgets()
     mediaTypeSelectBox = new EFFileObjectComboBox(dbMediaType, this);
     platformSelectBox = new EFFileObjectComboBox(dbPlatform, this);
     execSelectBox = new ExecutableComboBox(dbExec, this);
-    selectButton = new QPushButton(tr("&Update"));
-    launchButton = new QPushButton(tr("&Launch"));
+    selectButton = new QPushButton(tr("&Update"), this);
+    launchButton = new QPushButton(tr("&Launch"), this);
 }
 
 void EmuLauncher::layout()
@@ -81,6 +82,7 @@ void EmuLauncher::layout()
     grid->addWidget(micTable, 2, 0, 1, 2);
     grid->addWidget(execSelectBox, 3, 0);
     grid->addWidget(launchButton, 3, 1);
+    // grid will be implicitily parented to this
     setLayout(grid);
 }
 
@@ -105,6 +107,16 @@ void EmuLauncher::updateMediaImageContainers()
     if (!dbMic) dbMic = new DbMediaImageContainer(this);
     dbMic->filter(mtid, plfid);
     micTable->setModel(dbMic->getDataModel());
+    micTable->hideColumn(DbMediaImageContainer::MIC_FileId);
+    micTable->hideColumn(DbMediaImageContainer::MIC_FileSize);
+    micTable->hideColumn(DbMediaImageContainer::MIC_FileCheckSum);
+    micTable->hideColumn(DbMediaImageContainer::MIC_FilePathId);
+    micTable->hideColumn(DbMediaImageContainer::MIC_FilePathName);
+    micTable->hideColumn(DbMediaImageContainer::MIC_SetupId);
+    micTable->hideColumn(DbMediaImageContainer::MIC_PlatformName);
+    micTable->hideColumn(DbMediaImageContainer::MIC_PlatformId);
+    micTable->hideColumn(DbMediaImageContainer::MIC_MediaTypeName);
+    micTable->hideColumn(DbMediaImageContainer::MIC_MediaTypeId);
     micTable->resizeColumnsToContents();
     platformSelectBox->updateDataModel();
     mediaTypeSelectBox->updateDataModel();
@@ -122,12 +134,8 @@ void EmuLauncher::launchEmu()
         if (!execSelectBox || execSelectBox->currentIndex() == -1) {
             throw EmuFrontException(tr("Emulator not selected!"));
         }
-        //QModelIndex mindex = micTable->currentIndex();
         QItemSelectionModel *selModel = micTable->selectionModel();
         QModelIndexList listMIndex =  selModel->selectedIndexes();
-        /*if (!mindex.isValid()) {
-            throw EmuFrontException(tr("Media image container not selected!"));
-        }*/
         if (listMIndex.count() < 1) {
             throw EmuFrontException(tr("Media image container not selected!"));
         }
@@ -142,10 +150,8 @@ void EmuLauncher::launchEmu()
             throw EmuFrontException(tr("Failed creating Emulator object!"));
         }
 
-        // TODO: multiple media image container selection
-        //          - build a list of selected media image objects
-        //          - check that the platform and media type (setup) matches
         foreach(QModelIndex mind, listMIndex) {
+            if (!mind.isValid()) continue;
             EmuFrontObject *obImg = dbMic->getDataObjectFromModel(&mind);
             if (!obImg) {
                 qDebug() << "Failed creating media image container at row " << mind.row();
@@ -160,10 +166,6 @@ void EmuLauncher::launchEmu()
             mediaImageContainers << mic;
             QMap<QString, EmuFrontObject*> contained = mic->getMediaImages();
             mediaImages.unite(contained);
-            /*QMapIterator<QString, MediaImage*> it(contained);
-            while (it.hasNext()){
-                mediaImages << it.value();
-            }*/
         }
 
         if (mediaImages.count() < 1) {
@@ -172,7 +174,7 @@ void EmuLauncher::launchEmu()
 
         // check if command options have slots for nr media images > 1 e.g. "-diska $1 -diskb $2 ..."
         QString opts = exe->getOptions();
-        QRegExp rx("(\\s\\$\\d+\\s)");
+        QRegExp rx("(\\$\\d+)");
         QStringList list;
         int pos = 0;
         while ((pos = rx.indexIn(opts, pos)) != -1) {
@@ -182,8 +184,12 @@ void EmuLauncher::launchEmu()
 
         bool ok;
         QList<EmuFrontObject*> selectedImages;
+        if (list.count() > mediaImages.count()) {
+            throw EmuFrontException(tr("Select %1 media images for this emulator configuration").arg(list.count()));
+        }
         if (list.count() > 1) {
-            for(int i = 0; i < list.count(); i++) {
+            int lim = list.count() == mediaImages.count() ? list.count() - 1 : list.count();
+            for(int i = 0; i < lim; i++) {
                 EmuFrontObject *efo = EmuFrontInputDialog::getItem(
                         this, tr("Select image no. %1").arg(i+1), tr("Select"), mediaImages.values(), 0, false, &ok);
                 if (!ok)  {
@@ -191,7 +197,12 @@ void EmuLauncher::launchEmu()
                 }
                 selectedImages << efo;
                 MediaImage *mi = dynamic_cast<MediaImage*>(efo);
-                mediaImages.remove(mi->getCheckSum());
+                QString key = mi->getCheckSum();
+                mediaImages.remove(key);
+            }
+            if (mediaImages.count() == 1) {
+                // there should be at least one media image left in mediaImages map
+                selectedImages << mediaImages.values().first();
             }
         }
         else if (mediaImages.count() > 1) {
@@ -210,7 +221,8 @@ void EmuLauncher::launchEmu()
         if (selectedImages.count() < 1)
             throw EmuFrontException(tr("No media images selected"));
 
-        emuHelper->launch(exe, mediaImageContainers, selectedImages);
+        emuHelper->launch(exe, mediaImageContainers, selectedImages, list.count());
+        micTable->clearSelection();
     } catch (EmuFrontException efe) {
         delete exe;
         qDeleteAll(mediaImageContainers);
@@ -227,7 +239,7 @@ void EmuLauncher::processError(QProcess::ProcessError e)
     QString stdErr = emuHelper->readAllStandardError();
     QMessageBox::warning(this, tr("Emulator"),
         tr("Launching emulator failed with: %1.\n").arg(e)
-        .append(";\n").append(proc->errorString().append(";\n")
+        .append(";\n").append(emuHelper->errorString().append(";\n")
         .append(stdErr)), QMessageBox::Ok );
 }
 
@@ -238,7 +250,7 @@ void EmuLauncher::processFinished(int a)
     QString stdErr = emuHelper->readAllStandardError();
     QString stdMsg = emuHelper->readAllStandardOutput();
     QString msg = tr("Emulator has finished with: %1.\n").arg(a).append(stdMsg);
-    if (a) msg.append("; ").append(proc->errorString()).append(";\n").append(stdErr);
+    if (a) msg.append("; ").append(emuHelper->errorString()).append(";\n").append(stdErr);
     QMessageBox::information(this, tr("Emulator finished"), msg, QMessageBox::Ok);
 }