From 387500f01af5708dd33f9524a00b9e89210a135e Mon Sep 17 00:00:00 2001 From: Thomas Harte Date: Fri, 26 Jun 2020 18:25:56 -0400 Subject: [PATCH] Implements 'Insert...' menu item. --- OSBindings/Qt/mainwindow.cpp | 34 ++++++++++++++++++++++++++++------ OSBindings/Qt/mainwindow.h | 3 +++ 2 files changed, 31 insertions(+), 6 deletions(-) diff --git a/OSBindings/Qt/mainwindow.cpp b/OSBindings/Qt/mainwindow.cpp index 74c51218a..2e6087cee 100644 --- a/OSBindings/Qt/mainwindow.cpp +++ b/OSBindings/Qt/mainwindow.cpp @@ -139,12 +139,16 @@ void MainWindow::createActions() { // Add a separator and then an 'Insert...'. fileMenu->addSeparator(); - QAction *const insertAct = new QAction(tr("&Insert..."), this); - insertAct->setStatusTip(tr("Open an existing file")); - connect(insertAct, &QAction::triggered, this, [] { - // TODO. + insertAction = new QAction(tr("&Insert..."), this); + insertAction->setStatusTip(tr("Open an existing file")); + insertAction->setEnabled(false); + connect(insertAction, &QAction::triggered, this, [this] { + const QString fileName = getFilename("Insert..."); + if(!fileName.isEmpty()) { + insertFile(fileName); + } }); - fileMenu->addAction(insertAct); + fileMenu->addAction(insertAction); // Add Help menu, with an 'About...' option. QMenu *helpMenu = menuBar()->addMenu(tr("&Help")); @@ -178,6 +182,16 @@ QString MainWindow::getFilename(const char *title) { return fileName; } +void MainWindow::insertFile(const QString &fileName) { + if(!machine) return; + + auto mediaTarget = machine->media_target(); + if(!mediaTarget) return; + + Analyser::Static::Media media = Analyser::Static::GetMedia(fileName.toStdString()); + mediaTarget->insert_media(media); +} + void MainWindow::launchFile(const QString &fileName) { targets = Analyser::Static::GetTargets(fileName.toStdString()); if(!targets.empty()) { @@ -299,6 +313,11 @@ void MainWindow::launchMachine() { timer->startWithMachine(timedMachine, &machineMutex); } + // If the machine can accept new media while running, enable + // the inert action. + if(machine->media_target()) { + insertAction->setEnabled(true); + } } break; case Machine::Error::MissingROM: { @@ -347,15 +366,18 @@ void MainWindow::dropEvent(QDropEvent* event) { switch(uiPhase) { case UIPhase::NoFileSelected: { // Treat exactly as a File -> Open... . - // TODO: permit multiple files dropped at once. const auto fileName = event->mimeData()->urls()[0].toLocalFile(); launchFile(fileName); } break; case UIPhase::RunningMachine: { // Attempt to insert into the running machine. + const auto fileName = event->mimeData()->urls()[0].toLocalFile(); + insertFile(fileName); } break; + // TODO: permit multiple files dropped at once in both of the above cases. + case UIPhase::RequestingROMs: { // Attempt to match up the dragged files to the requested ROM list; // if and when they match, copy to a writeable QStandardPaths::AppDataLocation diff --git a/OSBindings/Qt/mainwindow.h b/OSBindings/Qt/mainwindow.h index 5536026f7..bb0bd5ab6 100644 --- a/OSBindings/Qt/mainwindow.h +++ b/OSBindings/Qt/mainwindow.h @@ -82,6 +82,9 @@ class MainWindow : public QMainWindow, public Outputs::Speaker::Speaker::Delegat void start_zx80(); void start_zx81(); + QAction *insertAction = nullptr; + void insertFile(const QString &fileName); + void launchFile(const QString &fileName); void launchTarget(std::unique_ptr &&);