From 1ce65e8b2b25e3b18804914d367f76586bf3c42c Mon Sep 17 00:00:00 2001 From: Adrien Bertrand Date: Wed, 26 Jun 2024 17:30:12 +0200 Subject: [PATCH] LCDWidget: improve screen capture D'n'D. Fix #433. - Cleanup misc scoping issues. - Leave default png quality value insteead of using the minimal one. - ignore() in case the temp file saving failed. - Apparently, on macOS at least, drag->exec returns Qt::IgnoreAction in all cases (?) and the actual I/O action is async, which is why deleting the file then would end up with an error when dropping. --- gui/qt/lcdwidget.cpp | 39 +++++++++++++++++++++------------------ 1 file changed, 21 insertions(+), 18 deletions(-) diff --git a/gui/qt/lcdwidget.cpp b/gui/qt/lcdwidget.cpp index cde9b102..3549e835 100644 --- a/gui/qt/lcdwidget.cpp +++ b/gui/qt/lcdwidget.cpp @@ -147,27 +147,30 @@ void LCDWidget::mousePressEvent(QMouseEvent *e) { void LCDWidget::mouseMoveEvent(QMouseEvent *e) { if (m_screenshotDrag) { - QDrag *drag = new QDrag(this); - QMimeData *mimeData = new QMimeData; QImage image = getImage(); - QPixmap mymap = QPixmap::fromImage(image); - QString path = QDir::tempPath() + QDir::separator() + QStringLiteral("cemu_") + randomString(5) + QStringLiteral(".png"); - image.save(path, "PNG", 0); - mimeData->setImageData(image); - mimeData->setUrls(QList() << QUrl::fromLocalFile(path)); - drag->setMimeData(mimeData); - drag->setHotSpot(e->pos() * ((double)image.rect().width() / rect().width())); - drag->setPixmap(mymap); - switch (drag->exec(Qt::CopyAction | Qt::MoveAction, Qt::MoveAction)) { - case Qt::IgnoreAction: - case Qt::CopyAction: - QFile::remove(path); - break; - default: - break; + QString path = QDir::tempPath() + QDir::separator() + QStringLiteral("CEmu_screenshot_") + randomString(5) + QStringLiteral(".png"); + if (image.save(path, "PNG")) { + QDrag *drag = new QDrag(this); + QMimeData *mimeData = new QMimeData; + QPixmap mymap = QPixmap::fromImage(image); + mimeData->setImageData(image); + mimeData->setUrls(QList() << QUrl::fromLocalFile(path)); + drag->setMimeData(mimeData); + drag->setHotSpot(e->pos() * ((double)image.rect().width() / rect().width())); + drag->setPixmap(mymap); + switch (const auto tmp = drag->exec(Qt::CopyAction | Qt::MoveAction, Qt::MoveAction)) { + case Qt::IgnoreAction: + case Qt::CopyAction: + QTimer::singleShot(1000, this, [path] { QFile::remove(path); }); + break; + default: + break; + } + e->accept(); + } else { + e->ignore(); } m_screenshotDrag = false; - e->accept(); } else { e->ignore(); }