This commit is contained in:
朱子楚\zhuzi 2023-08-30 18:44:07 +08:00
parent 5fc7ae7e89
commit 94a96cf75e
3 changed files with 163 additions and 10 deletions

View File

@ -15,6 +15,7 @@ Item{
signal captrueCompleted(var captrue) signal captrueCompleted(var captrue)
QtObject{ QtObject{
id:d id:d
property bool isEdit: false
property int dotMouseSize: control.dotSize+10 property int dotMouseSize: control.dotSize+10
property int dotMargins: -(control.dotSize-control.borderSize)/2 property int dotMargins: -(control.dotSize-control.borderSize)/2
property bool enablePosition: false property bool enablePosition: false
@ -41,6 +42,7 @@ Item{
} }
} }
Component.onCompleted: { Component.onCompleted: {
d.isEdit = false
setGeometry(0,0,screenshot_background.width,screenshot_background.height) setGeometry(0,0,screenshot_background.width,screenshot_background.height)
} }
ScreenshotBackground{ ScreenshotBackground{
@ -108,6 +110,8 @@ Item{
} }
screenshot.start = Qt.point(0,0) screenshot.start = Qt.point(0,0)
screenshot.end = Qt.point(0,0) screenshot.end = Qt.point(0,0)
d.isEdit = false
screenshot_background.clear()
} }
} }
} }
@ -122,14 +126,55 @@ Item{
border.color: control.borderColor border.color: control.borderColor
MouseArea{ MouseArea{
property point clickPos: Qt.point(0,0) property point clickPos: Qt.point(0,0)
property var currentData
property bool enablePositionChanged : false
property var hitData
anchors.fill: parent anchors.fill: parent
cursorShape: Qt.SizeAllCursor hoverEnabled: true
cursorShape: d.isEdit ? Qt.ArrowCursor : Qt.SizeAllCursor
onPressed: onPressed:
(mouse)=>{ (mouse)=>{
if(hitData){
return
}
enablePositionChanged = true
if(d.isEdit){
clickPos = Qt.point(mouse.x, mouse.y) clickPos = Qt.point(mouse.x, mouse.y)
currentData = screenshot_background.appendDrawData(0,clickPos,clickPos)
}else{
clickPos = Qt.point(mouse.x, mouse.y)
}
}
onReleased: {
enablePositionChanged = false
}
onCanceled: {
enablePositionChanged = false
}
onClicked: {
if(hitData){
screenshot_background.hitDrawData = hitData
}
} }
onPositionChanged: onPositionChanged:
(mouse)=>{ (mouse)=>{
if(!enablePositionChanged){
hitData = screenshot_background.hit(Qt.point(rect_capture.x + mouse.x,rect_capture.y + mouse.y))
if(hitData){
FluTools.setOverrideCursor(Qt.SizeAllCursor)
}else{
FluTools.restoreOverrideCursor()
}
return
}
if(d.isEdit){
var start = Qt.point(rect_capture.x + clickPos.x,rect_capture.y + clickPos.y)
var end = Qt.point(Math.min(Math.max(rect_capture.x + mouse.x,rect_capture.x+borderSize),rect_capture.x+rect_capture.width-currentData.getLineWidth()),Math.min(Math.max(rect_capture.y + mouse.y,rect_capture.y+currentData.getLineWidth()+borderSize),rect_capture.y+rect_capture.height)-currentData.getLineWidth())
console.debug("start->"+start)
console.debug("end->"+end)
screenshot_background.updateDrawData(currentData,start,end)
}else{
var delta = Qt.point(mouse.x - clickPos.x,mouse.y - clickPos.y) var delta = Qt.point(mouse.x - clickPos.x,mouse.y - clickPos.y)
var w = Math.abs(screenshot.end.x - screenshot.start.x) var w = Math.abs(screenshot.end.x - screenshot.start.x)
var h = Math.abs(screenshot.end.y - screenshot.start.y) var h = Math.abs(screenshot.end.y - screenshot.start.y)
@ -140,6 +185,7 @@ Item{
} }
} }
} }
}
Rectangle{ Rectangle{
id:rect_top_left id:rect_top_left
width: control.dotSize width: control.dotSize
@ -474,7 +520,7 @@ Item{
} }
} }
Pane{ Pane{
width: 100 width: 140
height: 40 height: 40
visible: { visible: {
if(screenshot.start === Qt.point(0,0) && screenshot.end === Qt.point(0,0)){ if(screenshot.start === Qt.point(0,0) && screenshot.end === Qt.point(0,0)){
@ -498,6 +544,12 @@ Item{
RowLayout{ RowLayout{
anchors.verticalCenter: parent.verticalCenter anchors.verticalCenter: parent.verticalCenter
anchors.right: parent.right anchors.right: parent.right
FluIconButton{
iconSource: FluentIcons.Stop
onClicked: {
d.isEdit = true
}
}
FluIconButton{ FluIconButton{
iconSource: FluentIcons.Cancel iconSource: FluentIcons.Cancel
iconSize: 18 iconSize: 18

View File

@ -9,6 +9,7 @@
#include "Def.h" #include "Def.h"
Screenshot::Screenshot(QQuickItem* parent) : QQuickPaintedItem(parent) Screenshot::Screenshot(QQuickItem* parent) : QQuickPaintedItem(parent)
{ {
_desktopGeometry = qApp->primaryScreen()->virtualGeometry(); _desktopGeometry = qApp->primaryScreen()->virtualGeometry();
@ -42,17 +43,73 @@ ScreenshotBackground::ScreenshotBackground(QQuickItem* parent) : QQuickPaintedIt
} }
setWidth(w); setWidth(w);
setHeight(h); setHeight(h);
connect(this,&ScreenshotBackground::hitDrawDataChanged,this,[=]{update();});
} }
void ScreenshotBackground::paint(QPainter* painter) void ScreenshotBackground::paint(QPainter* painter)
{ {
painter->save(); painter->save();
_sourcePixmap = _desktopPixmap.copy(); _sourcePixmap = _desktopPixmap.copy();
foreach (auto item, _drawList) {
if(item->drawType == 0){
QPainter p(&_sourcePixmap);
QPen pen;
pen.setWidth(item->lineWidth);
pen.setColor(QColor(255,0,0));
pen.setStyle(Qt::SolidLine);
p.setPen(pen);
QRect rect(item->start.x(), item->start.y(), item->end.x()-item->start.x(), item->end.y()-item->start.y());
p.drawRect(rect);
}
}
painter->drawPixmap(_desktopGeometry,_sourcePixmap); painter->drawPixmap(_desktopGeometry,_sourcePixmap);
foreach (auto item, _drawList) {
if(item->drawType == 0){
if(item == _hitDrawData){
painter->setPen(QPen(QColor(255,0,0),3));
painter->setBrush(QColor(255,255,255));
painter->drawEllipse(QRect(item->start.x()-4,item->start.y()-4,8,8));
painter->drawEllipse(QRect(item->start.x()+item->getWidth()/2-4,item->start.y()-4,8,8));
painter->drawEllipse(QRect(item->start.x()+item->getWidth()-4,item->start.y()-4,8,8));
painter->drawEllipse(QRect(item->start.x()+item->getWidth()-4,item->start.y()+item->getHeight()/2-4,8,8));
painter->drawEllipse(QRect(item->start.x()+item->getWidth()-4,item->start.y()+item->getHeight()-4,8,8));
painter->drawEllipse(QRect(item->start.x()+item->getWidth()/2-4,item->start.y()+item->getHeight()-4,8,8));
painter->drawEllipse(QRect(item->start.x()-4,item->start.y()+item->getHeight()-4,8,8));
painter->drawEllipse(QRect(item->start.x()-4,item->start.y()+item->getHeight()/2-4,8,8));
}
}
}
painter->restore(); painter->restore();
} }
void ScreenshotBackground::clear(){
_drawList.clear();
update();
}
DrawData* ScreenshotBackground::hit(const QPoint& point){
foreach (auto item, _drawList) {
if(item->drawType == 0){
if(point.x()>=item->start.x()-mouseSpacing && point.x()<=item->start.x()+item->lineWidth+mouseSpacing && point.y()>=item->start.y()-mouseSpacing && point.y()<=item->end.y()+mouseSpacing){
return item;
}
if(point.x()>=item->start.x()-mouseSpacing && point.x()<=item->end.x()+mouseSpacing && point.y()>=item->start.y()-mouseSpacing && point.y()<=item->start.y()+item->lineWidth+mouseSpacing){
return item;
}
if(point.x()>=item->end.x()-item->lineWidth-mouseSpacing && point.x()<=item->end.x()+mouseSpacing && point.y()>=item->start.y()-mouseSpacing && point.y()<=item->end.y()+mouseSpacing){
return item;
}
if(point.x()>=item->start.x()-mouseSpacing && point.x()<=item->end.x()+mouseSpacing && point.y()>=item->end.y()-item->lineWidth-mouseSpacing && point.y()<=item->end.y()+mouseSpacing){
return item;
}
}
}
return nullptr;
}
void ScreenshotBackground::capture(const QPoint& start,const QPoint& end){ void ScreenshotBackground::capture(const QPoint& start,const QPoint& end){
hitDrawData(nullptr);
update();
auto pixelRatio = qApp->primaryScreen()->devicePixelRatio(); auto pixelRatio = qApp->primaryScreen()->devicePixelRatio();
auto x = qMin(start.x(),end.x()) * pixelRatio; auto x = qMin(start.x(),end.x()) * pixelRatio;
auto y = qMin(start.y(),end.y()) * pixelRatio; auto y = qMin(start.y(),end.y()) * pixelRatio;
@ -73,3 +130,20 @@ void ScreenshotBackground::capture(const QPoint& start,const QPoint& end){
} }
} }
DrawData* ScreenshotBackground::appendDrawData(int drawType,QPoint start,QPoint end){
DrawData *data = new DrawData(this);
data->drawType = drawType;
data->start = start;
data->end = end;
data->lineWidth = 3;
_drawList.append(data);
update();
return data;
}
void ScreenshotBackground::updateDrawData(DrawData* data,QPoint start,QPoint end){
data->start = start;
data->end = end;
update();
}

View File

@ -6,6 +6,26 @@
#include <QPainter> #include <QPainter>
#include <QQuickItemGrabResult> #include <QQuickItemGrabResult>
#include "stdafx.h" #include "stdafx.h"
#include <qmath.h>
class DrawData:public QObject{
Q_OBJECT;
public:
explicit DrawData(QObject *parent = nullptr): QObject{parent}{};
int drawType;
int lineWidth;
QPoint start;
QPoint end;
Q_INVOKABLE int getLineWidth(){
return lineWidth;
}
Q_INVOKABLE int getWidth(){
return end.x()-start.x();
}
Q_INVOKABLE int getHeight(){
return end.y()-start.y();
}
};
class ScreenshotBackground : public QQuickPaintedItem class ScreenshotBackground : public QQuickPaintedItem
{ {
@ -13,10 +33,15 @@ class ScreenshotBackground : public QQuickPaintedItem
QML_NAMED_ELEMENT(ScreenshotBackground) QML_NAMED_ELEMENT(ScreenshotBackground)
Q_PROPERTY_AUTO(QString,saveFolder); Q_PROPERTY_AUTO(QString,saveFolder);
Q_PROPERTY_AUTO(int,captureMode); Q_PROPERTY_AUTO(int,captureMode);
Q_PROPERTY_AUTO(DrawData*,hitDrawData);
public: public:
ScreenshotBackground(QQuickItem* parent = nullptr); ScreenshotBackground(QQuickItem* parent = nullptr);
void paint(QPainter* painter) override; void paint(QPainter* painter) override;
Q_INVOKABLE void capture(const QPoint& start,const QPoint& end); Q_INVOKABLE void capture(const QPoint& start,const QPoint& end);
Q_INVOKABLE DrawData* appendDrawData(int drawType,QPoint start,QPoint end);
Q_INVOKABLE void updateDrawData(DrawData* data,QPoint start,QPoint end);
Q_INVOKABLE void clear();
Q_INVOKABLE DrawData* hit(const QPoint& point);
Q_SIGNAL void captrueToPixmapCompleted(QPixmap captrue); Q_SIGNAL void captrueToPixmapCompleted(QPixmap captrue);
Q_SIGNAL void captrueToFileCompleted(QUrl captrue); Q_SIGNAL void captrueToFileCompleted(QUrl captrue);
private: private:
@ -26,6 +51,8 @@ private:
qreal _devicePixelRatio; qreal _devicePixelRatio;
QSharedPointer<QQuickItemGrabResult> _grabResult; QSharedPointer<QQuickItemGrabResult> _grabResult;
QRect _captureRect; QRect _captureRect;
QList<DrawData*> _drawList;
int mouseSpacing = 3;
}; };