-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscreenshot.cpp
More file actions
414 lines (354 loc) · 13.9 KB
/
Copy pathscreenshot.cpp
File metadata and controls
414 lines (354 loc) · 13.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
//屏幕截图
#include "screenshot.h" //插入头文件
#include "buttontool.h"
#include <QMouseEvent>
#include <QPaintEvent>
#include <QShowEvent>
#include <QPainter>
#include <QApplication>
#include <QDesktopWidget>
#include <QScreen>
#include <QFileDialog>
#include <QDateTime>
#include <QDebug>
#include "processobject.h"
ScreenShot::ScreenShot(QWidget *parent) //屏幕布局
: ImageToolBase(parent)
{
InitUi();
InitProperty();
}
ScreenShot::~ScreenShot() //析构函数
{
}
void ScreenShot::InitUi()
{
showFullScreen();
m_pButtonTool = new ButtonTool(this);
m_pButtonTool->InitButtons({"SaveButton", "CloseButton"});
m_pButtonTool->setVisible(false);
m_pZoom_Left_Top = new QWidget(this);
m_pZoom_Left_Top->setObjectName("Zoom");
m_pZoom_Top = new QWidget(this);
m_pZoom_Top->setObjectName("Zoom");
m_pZoom_Right_Top = new QWidget(this);
m_pZoom_Right_Top->setObjectName("Zoom");
m_pZoom_Left = new QWidget(this);
m_pZoom_Left->setObjectName("Zoom");
m_pZoom_Right = new QWidget(this);
m_pZoom_Right->setObjectName("Zoom");
m_pZoom_Left_Bottom = new QWidget(this);
m_pZoom_Left_Bottom->setObjectName("Zoom");
m_pZoom_Right_Bottom = new QWidget(this);
m_pZoom_Right_Bottom->setObjectName("Zoom");
m_pZoom_Bottom = new QWidget(this);
m_pZoom_Bottom->setObjectName("Zoom");
}
void ScreenShot::InitProperty()
{
this->setWindowFlags(Qt::FramelessWindowHint);
setAutoFillBackground(true);
QPalette pal = palette();
pal.setColor(QPalette::Background,Qt::transparent);
setPalette(pal);
setWindowOpacity(1);
setAttribute(Qt::WA_TranslucentBackground);
setAttribute(Qt::WA_TransparentForMouseEvents, true);
//添加自定义类控制
setAttribute(Qt::WA_StyledBackground,true);
//获取程序当前运行目录
//QString path = QCoreApplication::applicationDirPath();
QFile resourceqss(":/ImageTools/qss/ScreenShot.qss"); //读取这个文件
resourceqss.open(QFile::ReadOnly);
this->setStyleSheet(resourceqss.readAll());
resourceqss.close();
m_bLight = false;
SetLightState("OpenLight");
m_pButtonTool->SetLampToolTip(m_bLight);
connect(m_pButtonTool, &ButtonTool::clicked, this, [=](ButtonTool::STATE state){
switch (state) {
case ButtonTool::STATE::SAVE: {
Save();
break;
}
case ButtonTool::STATE::CLOSE: {
emit clicked(STATE::CLOSE);
this->close();
break;
}
}
});
// 初始化 处理屏幕的对象 并初始化 可活动范围
m_pScreen = new ProcessObject;
m_pScreen->SetMaxParentSize(QApplication::desktop()->size());
m_pScreen->SetMaxWidth(QApplication::desktop()->size().width());
m_pScreen->SetMaxHeight(QApplication::desktop()->size().height());
m_pScreen->SetMinWidth(0);
m_pScreen->SetMinHeight(0);
m_pFullScreen = new QPixmap;
m_bInit =true;
}
void ScreenShot::SetZoomVisible(bool visible) //使widget是否可见
{
m_pZoom_Top->setVisible(visible);
m_pZoom_Bottom->setVisible(visible);
m_pZoom_Left->setVisible(visible);
m_pZoom_Right->setVisible(visible);
m_pZoom_Left_Top->setVisible(visible);
m_pZoom_Right_Top->setVisible(visible);
m_pZoom_Left_Bottom->setVisible(visible);
m_pZoom_Right_Bottom->setVisible(visible);
}
void ScreenShot::SetZoomGeometry(int x, int y, int width, int height)
{
m_pZoom_Left_Top->move(x - (m_pZoom_Left_Top->width() - 1)/2,
y - (m_pZoom_Left_Top->height() - 1)/2);
m_pZoom_Top->move(x + width/2 - (m_pZoom_Top->width() - 1)/2,
y - (m_pZoom_Top->height() - 1)/2);
m_pZoom_Right_Top->move(x + width - (m_pZoom_Right_Top->width() - 1)/2,
y - (m_pZoom_Right_Top->height() - 1)/2);
m_pZoom_Left->move(x - (m_pZoom_Left->width() - 1)/2,
y + (height/2) - (m_pZoom_Left->height() - 1)/2);
m_pZoom_Right->move(x + width - (m_pZoom_Right->width() - 1)/2,
y + (height/2) - (m_pZoom_Right->height() - 1)/2);
m_pZoom_Left_Bottom->move(x - (m_pZoom_Left_Bottom->width() - 1)/2,
y + height - (m_pZoom_Left_Bottom->height() - 1)/2);
m_pZoom_Bottom->move(x + width/2 - (m_pZoom_Bottom->width() - 1)/2,
y + height - (m_pZoom_Bottom->height() - 1)/2);
m_pZoom_Right_Bottom->move(x + width - (m_pZoom_Right_Bottom->width() - 1)/2,
y + height - (m_pZoom_Right_Bottom->height() - 1)/2);
}
bool ScreenShot::ZoomIsInArea(QWidget *Zoom, QPoint pos)
{
if(pos.x() > Zoom->x()
&& pos.x() < Zoom->x() + Zoom->width()
&& pos.y() > Zoom->y()
&& pos.y() < Zoom->y() + Zoom->height())
{
return true;
}
return false;
}
void ScreenShot::Save() //保存图片的操作
{
QString fileName = QFileDialog::getSaveFileName(this, "保存图片", STRDATETIME + QString(".png"), "PNG (*.png);;JPEG (*.jpg *jpeg);;BMP (*.bmp)");
if(!fileName.isEmpty())
{
int x = m_pScreen->GetLeftTopPos().x();
int y = m_pScreen->GetLeftTopPos().y();
int w = m_pScreen->GetRightBottomPos().x() - x;
int h = m_pScreen->GetRightBottomPos().y() - y;
m_pFullScreen->copy(x, y, w, h).save(fileName);
close();
emit clicked(STATE::CLOSE);
}
}
void ScreenShot::mousePressEvent(QMouseEvent *event) //拖拽控件函数
{
if(ZoomIsInArea(m_pZoom_Left_Top, event->pos()))
{
// 左上角拖拽
m_pScreen->SetState(ProcessObject::STATE::ZOOM);
m_pScreen->SetStart(m_pScreen->GetRightBottomPos());
m_pButtonTool->setVisible(false);
this->setCursor(Qt::SizeFDiagCursor);
}
else if(ZoomIsInArea(m_pZoom_Right_Top, event->pos()))
{
// 右上角拖拽
m_pScreen->SetState(ProcessObject::STATE::ZOOM);
m_pScreen->SetStart(QPoint(m_pScreen->GetLeftTopPos().x(), m_pScreen->GetRightBottomPos().y()));
m_pButtonTool->setVisible(false);
this->setCursor(Qt::SizeBDiagCursor);
}
else if(ZoomIsInArea(m_pZoom_Left_Bottom, event->pos()))
{
// 左下角拖拽
m_pScreen->SetState(ProcessObject::STATE::ZOOM);
m_pScreen->SetStart(QPoint(m_pScreen->GetRightBottomPos().x(), m_pScreen->GetLeftTopPos().y()));
m_pButtonTool->setVisible(false);
this->setCursor(Qt::SizeBDiagCursor);
}
else if(ZoomIsInArea(m_pZoom_Right_Bottom, event->pos()))
{
// 右下角拖拽
m_pScreen->SetState(ProcessObject::STATE::ZOOM);
m_pScreen->SetStart(QPoint(m_pScreen->GetLeftTopPos().x(), m_pScreen->GetLeftTopPos().y()));
m_pButtonTool->setVisible(false);
this->setCursor(Qt::SizeFDiagCursor);
}
else if(ZoomIsInArea(m_pZoom_Top, event->pos()))
{
// 上边拖拽
m_pScreen->SetState(ProcessObject::STATE::TOP_ZOOM);
m_pScreen->SetStart(m_pScreen->GetRightBottomPos());
m_pButtonTool->setVisible(false);
this->setCursor(Qt::SizeVerCursor);
}
else if(ZoomIsInArea(m_pZoom_Bottom, event->pos()))
{
//下边拖拽
m_pScreen->SetState(ProcessObject::STATE::BOTTOM_ZOOM);
m_pScreen->SetStart(m_pScreen->GetLeftTopPos());
m_pButtonTool->setVisible(false);
this->setCursor(Qt::SizeVerCursor);
}
else if(ZoomIsInArea(m_pZoom_Left, event->pos()))
{
// 左边拖拽
m_pScreen->SetState(ProcessObject::STATE::LEFT_ZOOM);
m_pScreen->SetStart(m_pScreen->GetRightBottomPos());
m_pButtonTool->setVisible(false);
this->setCursor(Qt::SizeHorCursor);
}
else if(ZoomIsInArea(m_pZoom_Right, event->pos()))
{
// 右边拖拽
m_pScreen->SetState(ProcessObject::STATE::RIGHT_ZOOM);
m_pScreen->SetStart(m_pScreen->GetLeftTopPos());
m_pButtonTool->setVisible(false);
this->setCursor(Qt::SizeHorCursor);
}
else if(event->button() == Qt::LeftButton)
{
m_qMovePos = event->pos();
if(m_pScreen->GetStart() == QPoint(-1, -1))
{
m_pScreen->SetState(ProcessObject::STATE::ZOOM);
m_pScreen->SetStart(m_qMovePos);
m_pButtonTool->setVisible(false);
}
else if(m_pScreen->IsInArea(event->pos()))
{
m_pScreen->SetState(ProcessObject::STATE::MOVE);
m_pButtonTool->setVisible(false);
this->setCursor(Qt::SizeAllCursor);
}
}
else if(event->button() == Qt::RightButton)
{
if(m_pScreen->GetStart() == QPoint(-1, -1))
{
this->close();
emit clicked(STATE::CLOSE);
}
else {
m_pButtonTool->setVisible(false);
m_pScreen->SetStart(QPoint(-1, -1));
m_pScreen->SetGeometry(-1, -1, 0, 0);
SetZoomVisible(false);
update();
}
}
}
void ScreenShot::mouseMoveEvent(QMouseEvent *event) //应用鼠标决定裁剪大小
{
if(m_pScreen->GetState() == ProcessObject::STATE::MOVE)
{
QPoint pos(event->x() - m_qMovePos.x(), event->y() - m_qMovePos.y());
m_pScreen->Move(pos);
} else if(m_pScreen->GetState() == ProcessObject::STATE::ZOOM)
{
m_pScreen->SetEnd(event->pos());
} else if(m_pScreen->GetState() == ProcessObject::STATE::TOP_ZOOM) {
m_pScreen->SetEnd(QPoint(m_pScreen->GetLeftTopPos().x(), event->y()));
} else if(m_pScreen->GetState() == ProcessObject::STATE::BOTTOM_ZOOM) {
m_pScreen->SetEnd(QPoint(m_pScreen->GetRightBottomPos().x(), event->y()));
} else if(m_pScreen->GetState() == ProcessObject::STATE::LEFT_ZOOM) {
m_pScreen->SetEnd(QPoint(event->x(), m_pScreen->GetLeftTopPos().y()));
} else if(m_pScreen->GetState() == ProcessObject::STATE::RIGHT_ZOOM) {
m_pScreen->SetEnd(QPoint(event->x(), m_pScreen->GetRightBottomPos().y()));
} else {
this->setCursor(Qt::ArrowCursor);
}
m_qMovePos = event->pos();
update();
}
void ScreenShot::mouseReleaseEvent(QMouseEvent *event) //确定裁剪内容
{
Q_UNUSED(event)
if(m_pScreen->GetState() == ProcessObject::STATE::MOVE)
{
m_pButtonTool->setVisible(true);
} if(m_pScreen->GetState() == ProcessObject::STATE::ZOOM ||
m_pScreen->GetState() == ProcessObject::STATE::TOP_ZOOM ||
m_pScreen->GetState() == ProcessObject::STATE::BOTTOM_ZOOM ||
m_pScreen->GetState() == ProcessObject::STATE::RIGHT_ZOOM ||
m_pScreen->GetState() == ProcessObject::STATE::LEFT_ZOOM)
{
m_pButtonTool->setVisible(true);
}
m_pScreen->SetState(ProcessObject::STATE::SELECT);
this->setCursor(Qt::ArrowCursor);
}
void ScreenShot::paintEvent(QPaintEvent *event) //裁剪
{
Q_UNUSED(event)
QPainter painter(this);
painter.setRenderHints(QPainter::Antialiasing | QPainter::HighQualityAntialiasing);
int x = m_pScreen->x();
int y = m_pScreen->y();
int w = m_pScreen->width();
int h = m_pScreen->height();
QPen pen;
pen.setColor(Qt::green);
pen.setWidth(1);
painter.setPen(pen);
QPixmap backgroundscreen(m_pScreen->GetMaxParentWidget(), m_pScreen->GetMaxParentHeight());
//用给定的颜色填充像素图
backgroundscreen.fill(MaskLayerColor);
painter.drawPixmap(0,0, *m_pBackgroundScreen);
painter.drawPixmap(0,0, backgroundscreen);
if(w > 0 && h > 0)
{
painter.drawPixmap(x, y, m_pFullScreen->copy(x,y,w,h));
painter.drawRect(x, y, w, h);
// 动态变更按钮组位置
int maxheight = m_pScreen->GetMaxParentHeight();
int distance_y_bottom = maxheight - (y+h);
int distance_y_top = m_pScreen->y();
if(distance_y_bottom <= m_pButtonTool->height() + 10 && distance_y_top <= m_pButtonTool->height() + 10)
{
m_pButtonTool->move(x+w-m_pButtonTool->width() - 5, y+h-m_pButtonTool->height()-10);
} else if(distance_y_bottom<= m_pButtonTool->height() + 10)
{
m_pButtonTool->move(x+w-m_pButtonTool->width() , y - m_pButtonTool->height() - 10);
} else {
m_pButtonTool->move(x+w-m_pButtonTool->width() , y+h+10);
}
if(distance_y_top <= 20)
{
pen.setColor(Qt::white);
painter.setPen(pen);
painter.drawText(x + 2, y + 14, tr("( %1 , %2 ) - ( %3 x %4 )")
.arg(x).arg(y).arg(w).arg(h));
} else {
pen.setColor(Qt::white);
painter.setPen(pen);
painter.drawText(x + 2, y - 7, tr("( %1 , %2 ) - ( %3 x %4 )")
.arg(x).arg(y).arg(w).arg(h));
}
SetZoomGeometry(x, y, w, h);
SetZoomVisible(true);
}
}
void ScreenShot::showEvent(QShowEvent *event) //对裁剪后的内容进行处理
{
Q_UNUSED(event)
if(m_bInit)
{
//获取主屏幕大小 - 全屏大小
QScreen *screen = QApplication::primaryScreen();
*m_pFullScreen = screen->grabWindow(
QApplication::desktop()->winId(),
0,
0,
m_pScreen->GetMaxParentWidget(),
m_pScreen->GetMaxParentHeight()
);
m_pBackgroundScreen = new QPixmap(*m_pFullScreen);
update();
m_pButtonTool->setVisible(false);
SetZoomVisible(false);
}
}