-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
26 changed files
with
2,038 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
105 changes: 105 additions & 0 deletions
105
SmartScreenSnapper/src/freehandsnap/freehandsnapgraphicsscene.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
#include "freehandsnapgraphicsscene.h" | ||
|
||
#include <QGraphicsSceneMouseEvent> | ||
#include <QGraphicsPolygonItem> | ||
#include <QRegion> | ||
#include <QDebug> | ||
|
||
FreeHandSnapGraphicsScene::FreeHandSnapGraphicsScene(QWidget *parent) : | ||
QGraphicsScene(parent), | ||
mousePressed(false), | ||
unsuredPath(nullptr), | ||
hasNewUnsuredPath(false), | ||
path(), | ||
pathItem(nullptr), | ||
grayItem(nullptr), | ||
grayColor(0, 0, 0, 180) | ||
{ | ||
connect(this, &QGraphicsScene::sceneRectChanged, this, [=](){ | ||
refreshGrayArea(); | ||
}); | ||
} | ||
|
||
FreeHandSnapGraphicsScene::~FreeHandSnapGraphicsScene() | ||
{ | ||
if (unsuredPath) delete unsuredPath; | ||
if (path) delete path; | ||
} | ||
|
||
QPolygonF FreeHandSnapGraphicsScene::getPathPolygonF() | ||
{ | ||
auto p = QPainterPath(*path); | ||
QPointF point = pathItem->pos(); | ||
p.translate(-point.x(), -point.y()); | ||
return p.toFillPolygon(); | ||
} | ||
|
||
QRectF FreeHandSnapGraphicsScene::getPathRect() | ||
{ | ||
return path->boundingRect(); | ||
} | ||
|
||
void FreeHandSnapGraphicsScene::mousePressEvent(QGraphicsSceneMouseEvent *event) | ||
{ | ||
mousePressed = true; | ||
hasNewUnsuredPath = false; | ||
if (!path) { | ||
path = new QPainterPath(event->scenePos()); | ||
} | ||
} | ||
|
||
void FreeHandSnapGraphicsScene::mouseReleaseEvent(QGraphicsSceneMouseEvent *event) | ||
{ | ||
mousePressed = false; | ||
|
||
if (hasNewUnsuredPath) { | ||
path = new QPainterPath(*unsuredPath); | ||
hasNewUnsuredPath = false; | ||
refreshPathItem(*path); | ||
} | ||
} | ||
|
||
void FreeHandSnapGraphicsScene::mouseMoveEvent(QGraphicsSceneMouseEvent *event) | ||
{ | ||
if (mousePressed) { | ||
path->lineTo(event->scenePos()); | ||
refreshPathItem(*path); | ||
} else if (path) { | ||
unsuredPath = new QPainterPath(*path); | ||
unsuredPath->lineTo(event->scenePos()); | ||
hasNewUnsuredPath = true; | ||
refreshPathItem(*unsuredPath); | ||
} | ||
} | ||
|
||
// 更新区域和阴影 | ||
void FreeHandSnapGraphicsScene::refreshPathItem(QPainterPath path) | ||
{ | ||
refreshGrayArea(); | ||
if (pathItem) { | ||
removeItem(pathItem); | ||
delete pathItem; | ||
} | ||
pathItem = addPath(path, QPen(QBrush(Qt::yellow), 5)); | ||
} | ||
|
||
|
||
// 绘制灰色阴影 | ||
void FreeHandSnapGraphicsScene::refreshGrayArea() | ||
{ | ||
if (grayItem) { | ||
removeItem(grayItem); | ||
delete grayItem; | ||
} | ||
|
||
if (!path) { | ||
grayItem = addRect(sceneRect(), | ||
QPen(Qt::transparent), | ||
QBrush(grayColor)); | ||
return; | ||
} | ||
|
||
grayItem = addPolygon(QPolygonF(sceneRect()).subtracted(path->toFillPolygon()), | ||
QPen(Qt::transparent), | ||
QBrush(grayColor)); | ||
} |
35 changes: 35 additions & 0 deletions
35
SmartScreenSnapper/src/freehandsnap/freehandsnapgraphicsscene.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
#ifndef FREEHANDSNAPGRAPHICSVIEW_H | ||
#define FREEHANDSNAPGRAPHICSVIEW_H | ||
|
||
#include <QGraphicsView> | ||
|
||
class FreeHandSnapGraphicsScene : public QGraphicsScene | ||
{ | ||
Q_OBJECT | ||
public: | ||
FreeHandSnapGraphicsScene(QWidget *parent = nullptr); | ||
|
||
~FreeHandSnapGraphicsScene(); | ||
|
||
QPolygonF getPathPolygonF(); | ||
|
||
QRectF getPathRect(); | ||
|
||
protected: | ||
void mousePressEvent(QGraphicsSceneMouseEvent *event); | ||
void mouseMoveEvent(QGraphicsSceneMouseEvent *event); | ||
void mouseReleaseEvent(QGraphicsSceneMouseEvent *event); | ||
void refreshPathItem(QPainterPath path); | ||
void refreshGrayArea(); | ||
private: | ||
bool mousePressed; | ||
|
||
QPainterPath* unsuredPath; | ||
bool hasNewUnsuredPath; | ||
QPainterPath* path; | ||
QGraphicsItem* pathItem; | ||
QGraphicsItem* grayItem; | ||
QColor grayColor; | ||
}; | ||
|
||
#endif // FREEHANDSNAPGRAPHICSVIEW_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
#include "freehandsnapdialog.h" | ||
#include "ui_freehandsnapdialog.h" | ||
#include "windows.h" | ||
#include "screenshothelper.h" | ||
#include "freehandsnap/freehandsnapgraphicsscene.h" | ||
|
||
#include <QMouseEvent> | ||
#include <QPainter> | ||
#include <QDebug> | ||
|
||
FreeHandSnapDialog::FreeHandSnapDialog(QWidget *parent) : | ||
QDialog(parent), | ||
ui(new Ui::FreeHandSnapDialog) | ||
{ | ||
ui->setupUi(this); | ||
|
||
setWindowFlags(Qt::Window | Qt::FramelessWindowHint); | ||
setWindowState(Qt::WindowActive); | ||
//多显示器支持 | ||
move(GetSystemMetrics(SM_XVIRTUALSCREEN), GetSystemMetrics(SM_YVIRTUALSCREEN)); | ||
resize(GetSystemMetrics(SM_CXVIRTUALSCREEN), GetSystemMetrics(SM_CYVIRTUALSCREEN)); //信息Widget不接收鼠标事件 | ||
|
||
// 可在不点击鼠标的情况下捕获移动事件 | ||
setMouseTracking(true); | ||
ui->graphicsView->setMouseTracking(true); | ||
|
||
fullScreenPixmap = ScreenShotHelper::getFullScreen(); | ||
scene = new FreeHandSnapGraphicsScene(this); | ||
scene->setSceneRect(QRectF(0, 0, width(), height())); | ||
scene->setBackgroundBrush(QBrush(fullScreenPixmap)); | ||
ui->graphicsView->setRenderHint(QPainter::Antialiasing); | ||
ui->graphicsView->setScene(scene); | ||
ui->graphicsView->setGeometry(0, 0, width(), height()); | ||
} | ||
|
||
FreeHandSnapDialog::~FreeHandSnapDialog() | ||
{ | ||
delete ui; | ||
} | ||
|
||
void FreeHandSnapDialog::keyPressEvent(QKeyEvent *event) | ||
{ | ||
if (event->key() == Qt::Key_Escape) { | ||
close(); | ||
return; | ||
} else if (event->key() == Qt::Key_Return || event->key() == Qt::Key_Enter) { | ||
QRectF r = scene->getPathRect(); | ||
// 先用QImage设置透明度,直接用QPixmap是黑色背景,设置透明无效 | ||
QImage image(r.width(), r.height(), QImage::Format_ARGB32_Premultiplied); | ||
QPainter painter(&image); | ||
image.fill(Qt::transparent); | ||
painter.setBrush(QBrush(fullScreenPixmap)); | ||
painter.setPen(QPen(Qt::transparent)); | ||
painter.translate(-r.x(), -r.y()); | ||
painter.drawConvexPolygon(scene->getPathPolygonF()); | ||
emit captured(QPixmap::fromImage(image)); | ||
close(); | ||
return; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
#ifndef FREEHANDSNAPDIALOG_H | ||
#define FREEHANDSNAPDIALOG_H | ||
|
||
#include <QDialog> | ||
#include <QGraphicsScene> | ||
|
||
#include <src/freehandsnap/freehandsnapgraphicsscene.h> | ||
|
||
namespace Ui { | ||
class FreeHandSnapDialog; | ||
} | ||
|
||
class FreeHandSnapDialog : public QDialog | ||
{ | ||
Q_OBJECT | ||
|
||
signals: | ||
void captured(QPixmap); | ||
|
||
public: | ||
explicit FreeHandSnapDialog(QWidget *parent = nullptr); | ||
~FreeHandSnapDialog(); | ||
|
||
private: | ||
Ui::FreeHandSnapDialog *ui; | ||
|
||
FreeHandSnapGraphicsScene *scene; | ||
|
||
QPixmap fullScreenPixmap; | ||
|
||
protected: | ||
void keyPressEvent(QKeyEvent *event); | ||
}; | ||
|
||
#endif // FREEHANDSNAPDIALOG_H |
Oops, something went wrong.