From 464b2fa383e80e5c09489cc9044e490a7c1e3bb7 Mon Sep 17 00:00:00 2001 From: renbin Date: Tue, 23 Jan 2024 18:19:07 +0800 Subject: [PATCH] fix: Copy may cause program lag MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 拷贝较大图片向剪贴板写入数据时,可能造成卡顿, 缩放图片,且仅在 wayland 环境下调用 Log: 修复一处拷贝可能卡顿的问题 --- libimageviewer/unionimage/baseutils.cpp | 27 ++++++++++++++++++++----- libimageviewer/unionimage/baseutils.h | 5 ++++- libimageviewer/viewpanel/viewpanel.cpp | 4 ++-- 3 files changed, 28 insertions(+), 8 deletions(-) diff --git a/libimageviewer/unionimage/baseutils.cpp b/libimageviewer/unionimage/baseutils.cpp index 4e07fc0f..2619a42a 100755 --- a/libimageviewer/unionimage/baseutils.cpp +++ b/libimageviewer/unionimage/baseutils.cpp @@ -180,7 +180,10 @@ void showInFileManager(const QString &path) // } } -void copyImageToClipboard(const QStringList &paths) +/** + @brief 拷贝图片信息到剪贴板,若 `sourceImage` 不为空,则在 wayland 下设置缩略图数据 + */ +void copyImageToClipboard(const QStringList &paths, const QImage &sourceImage) { if (paths.isEmpty()) { return; @@ -214,10 +217,8 @@ void copyImageToClipboard(const QStringList &paths) newMimeData->setData("x-special/gnome-copied-files", gnomeFormat); // Copy Image Data - QImage img; - QString errMsg; - if (LibUnionImage_NameSpace::loadStaticImageFromFile(paths.first(), img, errMsg)) { - newMimeData->setImageData(img); + if (!sourceImage.isNull() && checkWayland()) { + newMimeData->setImageData(sourceImage.scaled(200, 200, Qt::KeepAspectRatioByExpanding, Qt::SmoothTransformation)); } // Set the mimedata @@ -486,6 +487,22 @@ bool checkCommandExist(const QString &command) } } +/** + @brief 判断当前是否为 wayland 环境 + */ +bool checkWayland() +{ + auto e = QProcessEnvironment::systemEnvironment(); + QString XDG_SESSION_TYPE = e.value(QStringLiteral("XDG_SESSION_TYPE")); + QString WAYLAND_DISPLAY = e.value(QStringLiteral("WAYLAND_DISPLAY")); + + if (XDG_SESSION_TYPE == QLatin1String("wayland") || WAYLAND_DISPLAY.contains(QLatin1String("wayland"), Qt::CaseInsensitive)) + return true; + else { + return false; + } +} + } // namespace base } // namespace utils diff --git a/libimageviewer/unionimage/baseutils.h b/libimageviewer/unionimage/baseutils.h index c0513adc..126a684c 100755 --- a/libimageviewer/unionimage/baseutils.h +++ b/libimageviewer/unionimage/baseutils.h @@ -8,6 +8,7 @@ #include #include #include +#include #if QT_VERSION >= 0x050500 #define TIMER_SINGLESHOT(Time, Code, captured...){ \ @@ -123,7 +124,7 @@ namespace widgets { } namespace base { void copyOneImageToClipboard(const QString &path); -void copyImageToClipboard(const QStringList &paths); +void copyImageToClipboard(const QStringList &paths, const QImage &sourceImage = QImage()); void showInFileManager(const QString &path); int stringWidth(const QFont &f, const QString &str); int stringHeight(const QFont &f, const QString &str); @@ -146,6 +147,8 @@ bool onMountDevice(const QString &path); bool mountDeviceExist(const QString &path); bool checkCommandExist(const QString &command); //bool isCommandExist(const QString &command); + +bool checkWayland(); } // namespace base } // namespace utils diff --git a/libimageviewer/viewpanel/viewpanel.cpp b/libimageviewer/viewpanel/viewpanel.cpp index 35d986b7..784628e6 100644 --- a/libimageviewer/viewpanel/viewpanel.cpp +++ b/libimageviewer/viewpanel/viewpanel.cpp @@ -1838,9 +1838,9 @@ void LibViewPanel::onMenuItemClicked(QAction *action) // 判断当前是否为AI增强图片,若为设置增强后的图片 if (AIModelService::instance()->isTemporaryFile(m_currentPath)) { - Libutils::base::copyImageToClipboard({m_currentPath}); + Libutils::base::copyImageToClipboard(QStringList(m_currentPath), m_view->image()); } else { - Libutils::base::copyImageToClipboard(QStringList(m_bottomToolbar->getCurrentItemInfo().path)); + Libutils::base::copyImageToClipboard(QStringList(m_bottomToolbar->getCurrentItemInfo().path), m_view->image()); } PermissionConfig::instance()->triggerAction(PermissionConfig::TidCopy, m_bottomToolbar->getCurrentItemInfo().path);