Skip to content

Commit

Permalink
LCDWidget: improve screen capture D'n'D. Fix #433.
Browse files Browse the repository at this point in the history
- 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.
  • Loading branch information
adriweb committed Jun 26, 2024
1 parent f3c7ecf commit 1ce65e8
Showing 1 changed file with 21 additions and 18 deletions.
39 changes: 21 additions & 18 deletions gui/qt/lcdwidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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>() << 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>() << 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();
}
Expand Down

0 comments on commit 1ce65e8

Please sign in to comment.