This commit is contained in:
zhuzichu 2023-08-18 12:45:16 +08:00
parent c0efd71d31
commit 153090637b
3 changed files with 9 additions and 15 deletions

View File

@ -32,6 +32,7 @@ FluScrollablePage{
Layout.preferredHeight: 400 Layout.preferredHeight: 400
Layout.preferredWidth: 400 Layout.preferredWidth: 400
fillMode: Image.PreserveAspectFit fillMode: Image.PreserveAspectFit
asynchronous: true
} }
FluScreenshot{ FluScreenshot{

View File

@ -5,6 +5,7 @@
#include <QDir> #include <QDir>
#include <Def.h> #include <Def.h>
#include <QtMath> #include <QtMath>
#include <QThreadPool>
Screenshot::Screenshot(QQuickItem* parent) : QQuickPaintedItem(parent) Screenshot::Screenshot(QQuickItem* parent) : QQuickPaintedItem(parent)
{ {
@ -44,24 +45,18 @@ ScreenshotBackground::ScreenshotBackground(QQuickItem* parent) : QQuickPaintedIt
void ScreenshotBackground::paint(QPainter* painter) void ScreenshotBackground::paint(QPainter* painter)
{ {
painter->save(); painter->save();
_sourcePixmap = QPixmap(QSize(_desktopPixmap.width(),_desktopPixmap.height())); _sourcePixmap = _desktopPixmap.copy();
QPainter p(&_sourcePixmap); painter->drawPixmap(_desktopGeometry,_sourcePixmap);
p.drawPixmap(_desktopGeometry,_desktopPixmap);
painter->drawPixmap(_desktopGeometry,_desktopPixmap);
painter->restore(); painter->restore();
} }
void ScreenshotBackground::capture(const QPoint& start,const QPoint& end){ void ScreenshotBackground::capture(const QPoint& start,const QPoint& end){
_grabResult = grabToImage(); auto pixelRatio = qApp->primaryScreen()->devicePixelRatio();
auto x = qMin(start.x(),end.x()); auto x = qMin(start.x(),end.x()) * pixelRatio;
auto y = qMin(start.y(),end.y()); auto y = qMin(start.y(),end.y()) * pixelRatio;
auto w = qAbs(end.x()-start.x()); auto w = qAbs(end.x()-start.x()) * pixelRatio;
auto h = qAbs(end.y()-start.y()); auto h = qAbs(end.y()-start.y()) * pixelRatio;
_captureRect = QRect(x,y,w,h); _captureRect = QRect(x,y,w,h);
connect(_grabResult.data(), &QQuickItemGrabResult::ready, this, &ScreenshotBackground::handleGrabResult);
}
void ScreenshotBackground::handleGrabResult(){
if(_captureMode == FluScreenshotType::CaptrueMode::Pixmap){ if(_captureMode == FluScreenshotType::CaptrueMode::Pixmap){
Q_EMIT captrueToPixmapCompleted(_sourcePixmap.copy(_captureRect)); Q_EMIT captrueToPixmapCompleted(_sourcePixmap.copy(_captureRect));
} }
@ -71,7 +66,6 @@ void ScreenshotBackground::handleGrabResult(){
dir.mkpath(_saveFolder); dir.mkpath(_saveFolder);
} }
auto filePath = _saveFolder.append("/").append(QString::number(QDateTime::currentDateTime().toMSecsSinceEpoch())).append(".png"); auto filePath = _saveFolder.append("/").append(QString::number(QDateTime::currentDateTime().toMSecsSinceEpoch())).append(".png");
qDebug()<<filePath;
_sourcePixmap.copy(_captureRect).save(filePath); _sourcePixmap.copy(_captureRect).save(filePath);
Q_EMIT captrueToFileCompleted(QUrl::fromLocalFile(filePath)); Q_EMIT captrueToFileCompleted(QUrl::fromLocalFile(filePath));
} }

View File

@ -16,7 +16,6 @@ class ScreenshotBackground : public QQuickPaintedItem
public: public:
ScreenshotBackground(QQuickItem* parent = nullptr); ScreenshotBackground(QQuickItem* parent = nullptr);
void paint(QPainter* painter) override; void paint(QPainter* painter) override;
Q_SLOT void handleGrabResult();
Q_INVOKABLE void capture(const QPoint& start,const QPoint& end); Q_INVOKABLE void capture(const QPoint& start,const QPoint& end);
Q_SIGNAL void captrueToPixmapCompleted(QPixmap captrue); Q_SIGNAL void captrueToPixmapCompleted(QPixmap captrue);
Q_SIGNAL void captrueToFileCompleted(QUrl captrue); Q_SIGNAL void captrueToFileCompleted(QUrl captrue);