From ec9a9a507495cddaf04d4c19f9356ea1298f29d6 Mon Sep 17 00:00:00 2001 From: zhuzichu Date: Wed, 16 Aug 2023 18:05:49 +0800 Subject: [PATCH] update --- example/qml/global/ItemsOriginal.qml | 8 +- example/qml/page/T_Screenshot.qml | 33 +++++++ example/qml/window/MainWindow.qml | 12 +++ src/Screenshot.cpp | 25 +++++ src/Screenshot.h | 25 +++++ .../FluentUI/Controls/FluScreenshot.qml | 99 +++++++++++++++++++ src/imports/FluentUI/qmldir | 2 + 7 files changed, 203 insertions(+), 1 deletion(-) create mode 100644 example/qml/page/T_Screenshot.qml create mode 100644 src/Screenshot.cpp create mode 100644 src/Screenshot.h create mode 100644 src/imports/FluentUI/Controls/FluScreenshot.qml diff --git a/example/qml/global/ItemsOriginal.qml b/example/qml/global/ItemsOriginal.qml index 437eb28a..f36572eb 100644 --- a/example/qml/global/ItemsOriginal.qml +++ b/example/qml/global/ItemsOriginal.qml @@ -349,6 +349,12 @@ FluObject{ navigationView.push("qrc:/example/qml/page/T_Timeline.qml") } } + FluPaneItem{ + title:"Screenshot" + onTap:{ + navigationView.push("qrc:/example/qml/page/T_Screenshot.qml") + } + } FluPaneItem{ title:"Chart" onTap:{ @@ -377,7 +383,7 @@ FluObject{ FluPaneItem{ title:"HotLoader" tapFunc:function(){ - FluApp.navigate("/hotload") + FluApp.navigate("/hotload") } } } diff --git a/example/qml/page/T_Screenshot.qml b/example/qml/page/T_Screenshot.qml new file mode 100644 index 00000000..8e566a8c --- /dev/null +++ b/example/qml/page/T_Screenshot.qml @@ -0,0 +1,33 @@ +import QtQuick +import QtQuick.Layouts +import QtQuick.Window +import QtQuick.Controls +import FluentUI +import "qrc:///example/qml/component" + +FluScrollablePage{ + + title:"Screenshot" + + FluArea{ + Layout.fillWidth: true + height: 100 + paddings: 10 + Layout.topMargin: 20 + + FluFilledButton{ + anchors{ + top: parent.top + topMargin: 14 + } + text:"Open Screenshot" + onClicked: { + screenshot.open() + } + } + } + + FluScreenshot{ + id:screenshot + } +} diff --git a/example/qml/window/MainWindow.qml b/example/qml/window/MainWindow.qml index be1553d6..02efb33c 100644 --- a/example/qml/window/MainWindow.qml +++ b/example/qml/window/MainWindow.qml @@ -269,6 +269,18 @@ CustomWindow { } } + Shortcut { + sequence: "F7" + context: Qt.WindowShortcut + onActivated: { + screenshot.open() + } + } + + FluScreenshot{ + id:screenshot + } + FluTour{ id:tour steps:[ diff --git a/src/Screenshot.cpp b/src/Screenshot.cpp new file mode 100644 index 00000000..9fac0c66 --- /dev/null +++ b/src/Screenshot.cpp @@ -0,0 +1,25 @@ +#include "Screenshot.h" +#include +#include +#include + +Screenshot::Screenshot(QQuickItem* parent) : QQuickPaintedItem(parent) +{ + start(QPoint(0,0)); + end(QPoint(0,0)); + _desktopGeometry = qApp->primaryScreen()->virtualGeometry(); + _desktopPixmap = qApp->primaryScreen()->grabWindow(0, _desktopGeometry.x(), _desktopGeometry.y(), _desktopGeometry.width(), _desktopGeometry.height()); + connect(this,&Screenshot::startChanged,this,[=]{update();}); + connect(this,&Screenshot::endChanged,this,[=]{update();}); +} + +void Screenshot::paint(QPainter* painter) +{ + painter->save(); + painter->eraseRect(boundingRect()); + painter->drawPixmap(_desktopGeometry,_desktopPixmap); + painter->fillRect(_desktopGeometry,QColor(0,0,0,60)); + painter->setCompositionMode(QPainter::CompositionMode_Clear); + painter->fillRect(QRect(_start.x(),_start.y(),_end.x()-_start.x(),_end.y()-_start.y()), Qt::black); + painter->restore(); +} diff --git a/src/Screenshot.h b/src/Screenshot.h new file mode 100644 index 00000000..2768545a --- /dev/null +++ b/src/Screenshot.h @@ -0,0 +1,25 @@ +#ifndef SCREENSHOT_H +#define SCREENSHOT_H + +#include +#include +#include +#include "stdafx.h" + +class Screenshot : public QQuickPaintedItem +{ + Q_OBJECT + QML_NAMED_ELEMENT(Screenshot) + Q_PROPERTY_AUTO(QPoint,start); + Q_PROPERTY_AUTO(QPoint,end); +public: + Screenshot(QQuickItem* parent = nullptr); + void paint(QPainter* painter) override; + +private: + QRect _desktopGeometry; + QPixmap _desktopPixmap; + bool _isFirst = true; +}; + +#endif // SCREENSHOT_H diff --git a/src/imports/FluentUI/Controls/FluScreenshot.qml b/src/imports/FluentUI/Controls/FluScreenshot.qml new file mode 100644 index 00000000..813b2965 --- /dev/null +++ b/src/imports/FluentUI/Controls/FluScreenshot.qml @@ -0,0 +1,99 @@ +import QtQuick +import QtQuick.Controls +import QtQuick.Controls.Basic +import QtQuick.Layouts +import FluentUI + +Loader { + id:control + Component{ + id:com_screen + Window{ + id:window_screen + flags: Qt.FramelessWindowHint | Qt.WindowStaysOnTopHint + width: Screen.desktopAvailableWidth + height: Screen.height + visible: true + color: "#00000000" + onVisibleChanged: { + if(!window_screen.visible){ + control.sourceComponent = undefined + } + } + Screenshot{ + id:screenshot + anchors.fill: parent + } + MouseArea{ + property bool enablePosition: false + anchors.fill: parent + acceptedButtons: Qt.RightButton | Qt.LeftButton + onPressed: + (mouse)=>{ + if(mouse.button === Qt.LeftButton){ + enablePosition = true + screenshot.start = Qt.point(mouse.x,mouse.y) + screenshot.end = Qt.point(mouse.x,mouse.y) + } + } + onPositionChanged: + (mouse)=>{ + if(enablePosition){ + screenshot.end = Qt.point(mouse.x,mouse.y) + } + } + onReleased: + (mouse)=>{ + if(mouse.button === Qt.LeftButton){ + enablePosition = false + screenshot.end = Qt.point(mouse.x,mouse.y) + } + } + onClicked: + (mouse)=>{ + if (mouse.button === Qt.RightButton){ + if(screenshot.start === Qt.point(0,0) && screenshot.end === Qt.point(0,0)){ + control.sourceComponent = undefined + return + } + screenshot.start = Qt.point(0,0) + screenshot.end = Qt.point(0,0) + } + } + } + Rectangle{ + id:rect_capture + x:Math.min(screenshot.start.x,screenshot.end.x) + y:Math.min(screenshot.start.y,screenshot.end.y) + width: Math.abs(screenshot.end.x - screenshot.start.x) + height: Math.abs(screenshot.end.y - screenshot.start.y) + color:"#00000000" + border.width: 1 + border.color: FluTheme.primaryColor.dark + MouseArea{ + property point clickPos: Qt.point(0,0) + anchors.fill: parent + cursorShape: Qt.SizeAllCursor + onPressed: + (mouse)=>{ + clickPos = Qt.point(mouse.x, mouse.y) + } + onPositionChanged: + (mouse)=>{ + var delta = Qt.point(mouse.x - clickPos.x,mouse.y - clickPos.y) + var w = Math.abs(screenshot.end.x - screenshot.start.x) + var h = Math.abs(screenshot.end.y - screenshot.start.y) + var x = Math.min(Math.max(rect_capture.x + delta.x,0),window_screen.width-w) + var y =Math.min(Math.max(rect_capture.y + delta.y,0),window_screen.height-h) + screenshot.start = Qt.point(x,y) + screenshot.end = Qt.point(x+w,y+h) + } + } + } + } + } + + function open(){ + control.sourceComponent = com_screen + } +} diff --git a/src/imports/FluentUI/qmldir b/src/imports/FluentUI/qmldir index 70f7e096..23fd917e 100644 --- a/src/imports/FluentUI/qmldir +++ b/src/imports/FluentUI/qmldir @@ -84,4 +84,6 @@ FluWindow 1.0 Controls/FluWindow.qml FluTimeline 1.0 Controls/FluTimeline.qml FluChart 1.0 Controls/FluChart.qml FluQRCode 1.0 Controls/FluQRCode.qml +FluScreenshot 1.0 Controls/FluScreenshot.qml + plugin fluentuiplugin