This commit is contained in:
zhuzichu 2023-08-17 17:14:31 +08:00
parent 26669f76e0
commit 06c9b4e382
5 changed files with 83 additions and 9 deletions

View File

@ -3,6 +3,7 @@
#include <QClipboard> #include <QClipboard>
#include <QUuid> #include <QUuid>
#include <QCursor> #include <QCursor>
#include <QScreen>
#include <QFileInfo> #include <QFileInfo>
#include <QTextDocument> #include <QTextDocument>
@ -110,3 +111,7 @@ QString FluTools::html2PlantText(const QString& html){
textDocument.setHtml(html); textDocument.setHtml(html);
return textDocument.toPlainText(); return textDocument.toPlainText();
} }
QRect FluTools::getVirtualGeometry(){
return qApp->primaryScreen()->virtualGeometry();
}

View File

@ -103,14 +103,25 @@ public:
*/ */
Q_INVOKABLE QString toLocalPath(const QUrl& url); Q_INVOKABLE QString toLocalPath(const QUrl& url);
Q_INVOKABLE QString getFileNameByUrl(const QUrl& url);
/** /**
* @brief deleteItem Item对象 * @brief deleteItem Item对象
* @param p * @param p
*/ */
Q_INVOKABLE void deleteItem(QObject *p); Q_INVOKABLE void deleteItem(QObject *p);
/**
* @brief getFileNameByUrl
* @param url
* @return
*/
Q_INVOKABLE QString getFileNameByUrl(const QUrl& url);
/**
* @brief getVirtualGeometry
* @return
*/
Q_INVOKABLE QRect getVirtualGeometry();
}; };

View File

@ -2,13 +2,14 @@
#include <QGuiApplication> #include <QGuiApplication>
#include <QScreen> #include <QScreen>
#include <QQuickWindow> #include <QQuickWindow>
#include <QtMath>
Screenshot::Screenshot(QQuickItem* parent) : QQuickPaintedItem(parent) Screenshot::Screenshot(QQuickItem* parent) : QQuickPaintedItem(parent)
{ {
_desktopGeometry = qApp->primaryScreen()->virtualGeometry();
maskColor(QColor(0,0,0,80)); maskColor(QColor(0,0,0,80));
start(QPoint(0,0)); start(QPoint(0,0));
end(QPoint(0,0)); end(QPoint(0,0));
_desktopGeometry = qApp->primaryScreen()->virtualGeometry();
connect(this,&Screenshot::startChanged,this,[=]{update();}); connect(this,&Screenshot::startChanged,this,[=]{update();});
connect(this,&Screenshot::endChanged,this,[=]{update();}); connect(this,&Screenshot::endChanged,this,[=]{update();});
} }
@ -24,14 +25,45 @@ void Screenshot::paint(QPainter* painter)
ScreenshotBackground::ScreenshotBackground(QQuickItem* parent) : QQuickPaintedItem(parent) ScreenshotBackground::ScreenshotBackground(QQuickItem* parent) : QQuickPaintedItem(parent)
{ {
_devicePixelRatio = qApp->primaryScreen()->devicePixelRatio();
_desktopGeometry = qApp->primaryScreen()->virtualGeometry(); _desktopGeometry = qApp->primaryScreen()->virtualGeometry();
_desktopPixmap = qApp->primaryScreen()->grabWindow(0, _desktopGeometry.x(), _desktopGeometry.y(), _desktopGeometry.width(), _desktopGeometry.height()); _desktopPixmap = qApp->primaryScreen()->grabWindow(0, _desktopGeometry.x(), _desktopGeometry.y(), _desktopGeometry.width(), _desktopGeometry.height());
int w = qApp->primaryScreen()->geometry().width();
int h = qApp->primaryScreen()->geometry().height();
foreach (auto item, qApp->screens()) {
if(item != qApp->primaryScreen()){
w = w + item->geometry().width()/qApp->primaryScreen()->devicePixelRatio();
}
}
setWidth(w);
setHeight(h);
} }
void ScreenshotBackground::paint(QPainter* painter) void ScreenshotBackground::paint(QPainter* painter)
{ {
painter->save(); painter->save();
painter->drawPixmap(_desktopGeometry,_desktopPixmap); QPixmap sourcePixmap;
QPainter p(&sourcePixmap);
p.drawPixmap(_desktopGeometry,_desktopPixmap);
painter->drawPixmap(_desktopGeometry,sourcePixmap);
painter->restore(); painter->restore();
} }
void ScreenshotBackground::capture(const QPoint& start,const QPoint& end){
qDebug()<<start;
qDebug()<<end;
_grabResult = grabToImage();
auto x = qMin(start.x(),end.x());
auto y = qMin(start.y(),end.y());
auto w = qAbs(end.x()-start.x());
auto h = qAbs(end.y()-start.y());
_captureRect = QRect(x,y,w,h);
qDebug()<<_captureRect;
connect(_grabResult.data(), &QQuickItemGrabResult::ready, this, &ScreenshotBackground::handleGrabResult);
}
void ScreenshotBackground::handleGrabResult(){
_grabResult.data()->image().copy(_captureRect).save("aaa.png");
}

View File

@ -4,6 +4,7 @@
#include <QQuickItem> #include <QQuickItem>
#include <QQuickPaintedItem> #include <QQuickPaintedItem>
#include <QPainter> #include <QPainter>
#include <QQuickItemGrabResult>
#include "stdafx.h" #include "stdafx.h"
class ScreenshotBackground : public QQuickPaintedItem class ScreenshotBackground : public QQuickPaintedItem
@ -13,10 +14,14 @@ 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);
private: private:
QRect _desktopGeometry; QRect _desktopGeometry;
QPixmap _desktopPixmap; QPixmap _desktopPixmap;
qreal _devicePixelRatio;
QSharedPointer<QQuickItemGrabResult> _grabResult;
QRect _captureRect;
}; };

View File

@ -33,10 +33,12 @@ Loader {
} }
} }
Component.onCompleted: { Component.onCompleted: {
setGeometry(0,0,Screen.desktopAvailableWidth,Screen.height) // setGeometry(0,0,FluTools.getVirtualGeometry().width/2,FluTools.getVirtualGeometry().height)
setGeometry(0,0,screenshot_background.width,screenshot_background.height)
console.debug(width+";"+height)
} }
ScreenshotBackground{ ScreenshotBackground{
anchors.fill: parent id:screenshot_background
} }
Screenshot{ Screenshot{
id:screenshot id:screenshot
@ -431,7 +433,7 @@ Loader {
FluTools.restoreOverrideCursor() FluTools.restoreOverrideCursor()
} }
} }
Rectangle{ Pane{
width: 100 width: 100
height: 40 height: 40
visible: { visible: {
@ -453,6 +455,25 @@ Loader {
screenshot.height - height - d.menuMargins screenshot.height - height - d.menuMargins
} }
} }
RowLayout{
anchors.verticalCenter: parent.verticalCenter
anchors.right: parent.right
FluIconButton{
iconSource: FluentIcons.Cancel
iconSize: 18
iconColor: Qt.rgba(247/255,75/255,77/255,1)
onClicked: {
control.sourceComponent = undefined
}
}
FluIconButton{
iconSource: FluentIcons.AcceptMedium
iconColor: FluTheme.primaryColor.dark
onClicked: {
screenshot_background.capture(screenshot.start,screenshot.end)
}
}
}
} }
} }
} }