From 9c036fe39393d8a5c5843ac673267dd2b398fe7f Mon Sep 17 00:00:00 2001 From: Alessandro Portale Date: Tue, 12 Sep 2023 18:11:21 +0200 Subject: [PATCH] ScreenRecorder: Offer saving of the current cropped frame The "crop and trim" dialog gets two new buttons. The first offers to save the current, cropped frame as png image. The second one copies the image into the clipboard. Copyuing into clipboard is also triggered by the QKeySequence::Copy shortcut. The location where the last image was saved gets remembered in the settings. Change-Id: Id4ac93838d59f6a6fa801456fe8d9c17d1e74154 Reviewed-by: Marcus Tillmanns --- src/plugins/screenrecorder/cropandtrim.cpp | 44 +++++++++++++++++++ src/plugins/screenrecorder/cropandtrim.h | 1 + .../screenrecorder/screenrecordersettings.cpp | 4 ++ .../screenrecorder/screenrecordersettings.h | 1 + 4 files changed, 50 insertions(+) diff --git a/src/plugins/screenrecorder/cropandtrim.cpp b/src/plugins/screenrecorder/cropandtrim.cpp index 16cdb3ec3c3..c6c35560ea1 100644 --- a/src/plugins/screenrecorder/cropandtrim.cpp +++ b/src/plugins/screenrecorder/cropandtrim.cpp @@ -7,6 +7,7 @@ #include "screenrecordersettings.h" #include "screenrecordertr.h" +#include #include #include #include @@ -16,8 +17,10 @@ #include +#include #include #include +#include #include #include #include @@ -164,6 +167,14 @@ void CropScene::setImage(const QImage &image) resize(sceneSize); } +QImage CropScene::croppedImage() const +{ + if (!m_image) + return {}; + + return m_image->copy(m_cropRect); +} + void CropScene::mouseMoveEvent(QMouseEvent *event) { const QPoint imagePos = toImagePos(event->pos()); @@ -267,6 +278,18 @@ CropWidget::CropWidget(QWidget *parent) m_warningIcon = new CropSizeWarningIcon(CropSizeWarningIcon::StandardVariant); + auto saveImageButton = new QToolButton; + saveImageButton->setToolTip(Tr::tr("Save current, cropped frame as image file...")); + saveImageButton->setIcon(Icons::SAVEFILE.icon()); + + auto copyImageToClipboardAction = new QAction(Tr::tr("Copy current, cropped frame as image " + "into the Clipboard"), this); + copyImageToClipboardAction->setIcon(Icons::COPY.icon()); + copyImageToClipboardAction->setShortcut(QKeySequence::Copy); + + auto copyImageToClipboardButton = new QToolButton; + copyImageToClipboardButton->setDefaultAction(copyImageToClipboardAction); + using namespace Layouting; Column { scrollArea, @@ -278,6 +301,8 @@ CropWidget::CropWidget(QWidget *parent) m_resetButton, m_warningIcon, st, + saveImageButton, + copyImageToClipboardButton, }, noMargin(), }.attachTo(this); @@ -290,6 +315,25 @@ CropWidget::CropWidget(QWidget *parent) connect(m_resetButton, &QToolButton::pressed, this, [this] { m_cropScene->setFullySelected(); }); + connect(saveImageButton, &QToolButton::clicked, this, [this] { + FilePathAspect &lastDir = Internal::settings().lastSaveImageDirectory; + const QString ext(".png"); + FilePath file = FileUtils::getSaveFilePath(nullptr, Tr::tr("Save Current Frame As"), + lastDir(), "*" + ext); + if (!file.isEmpty()) { + if (!file.endsWith(ext)) + file = file.stringAppended(ext); + lastDir.setValue(file.parentDir()); + lastDir.writeToSettingsImmediatly(); + const QImage image = m_cropScene->croppedImage(); + image.save(file.toString()); + } + }); + connect(copyImageToClipboardAction, &QAction::triggered, this, [this] { + const QImage image = m_cropScene->croppedImage(); + QClipboard *clipboard = QGuiApplication::clipboard(); + clipboard->setImage(image); + }); updateWidgets(); } diff --git a/src/plugins/screenrecorder/cropandtrim.h b/src/plugins/screenrecorder/cropandtrim.h index 164f0dbbd20..1ac2cbae453 100644 --- a/src/plugins/screenrecorder/cropandtrim.h +++ b/src/plugins/screenrecorder/cropandtrim.h @@ -26,6 +26,7 @@ public: void setFullySelected(); QRect fullRect() const; void setImage(const QImage &image); + QImage croppedImage() const; const static int lineWidth = 1; diff --git a/src/plugins/screenrecorder/screenrecordersettings.cpp b/src/plugins/screenrecorder/screenrecordersettings.cpp index 98dfefdcf63..ae01d321175 100644 --- a/src/plugins/screenrecorder/screenrecordersettings.cpp +++ b/src/plugins/screenrecorder/screenrecordersettings.cpp @@ -107,6 +107,10 @@ ScreenRecorderSettings::ScreenRecorderSettings() exportLastDirectory.setExpectedKind(PathChooser::ExistingDirectory); exportLastDirectory.setDefaultValue(FileUtils::homePath().toString()); + lastSaveImageDirectory.setSettingsKey("LastSaveImageDir"); + lastSaveImageDirectory.setExpectedKind(PathChooser::ExistingDirectory); + lastSaveImageDirectory.setDefaultValue(FileUtils::homePath().toString()); + recordFrameRate.setSettingsKey("RecordFrameRate"); recordFrameRate.setDefaultValue(24); recordFrameRate.setLabelText(Tr::tr("Recording frame rate:")); diff --git a/src/plugins/screenrecorder/screenrecordersettings.h b/src/plugins/screenrecorder/screenrecordersettings.h index ac05169bc40..75527da6538 100644 --- a/src/plugins/screenrecorder/screenrecordersettings.h +++ b/src/plugins/screenrecorder/screenrecordersettings.h @@ -40,6 +40,7 @@ public: // Used in other places Utils::FilePathAspect lastOpenDirectory{this}; Utils::FilePathAspect exportLastDirectory{this}; + Utils::FilePathAspect lastSaveImageDirectory{this}; Utils::IntegerAspect recordFrameRate{this}; Utils::IntegerAspect recordScreenId{this}; Utils::StringListAspect recordScreenCropRect{this};