/
screenshots.cpp
216 lines (168 loc) · 5.68 KB
/
screenshots.cpp
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
#include "widget.h"
#include "ui_widget.h"
#include <QtGui>
Widget::Widget(QWidget *parent) :
QWidget(parent),
ui(new Ui::Widget)
{
ui->setupUi(this);
createWidgets();
createConnects();
createEventFilter();
}
Widget::~Widget()
{
delete ui;
delete quit;
delete mini;
delete restore;
delete menu;
delete trayIcon;
delete fullScreenLabel;
delete shotScreenLabel;
}
bool Widget::eventFilter(QObject *o, QEvent *e)
{
if (o != fullScreenLabel)
{
return Widget::eventFilter(o, e);
}
QMouseEvent *mouseEvent = static_cast<QMouseEvent*> (e);
//true 鼠标左键按下且按键还未弹起
if ((mouseEvent->button() == Qt::LeftButton)
&& (mouseEvent->type() == QEvent::MouseButtonPress))
{
//鼠标左键标志位按下
leftMousePress = true;
//获取鼠标点
origin = mouseEvent->pos();
if (!rubberBand)
{
rubberBand = new QRubberBand(QRubberBand::Rectangle, fullScreenLabel);
}
rubberBand->setGeometry(QRect(origin,QSize()));
rubberBand->show();
return true;
}
//true 鼠标左键按下并拖动
if ((mouseEvent->type() == QEvent::MouseMove)
&& (leftMousePress))
{
if (rubberBand)
{
rubberBand->setGeometry(QRect(origin, mouseEvent->pos()).normalized());
}
return true;
}
//鼠标左键松开
if ((mouseEvent->button() == Qt::LeftButton)
&& (mouseEvent->type() == QEvent::MouseButtonRelease))
{
//鼠标标志位弹起
leftMousePress = false;
if (rubberBand)
{
//获取橡皮筋框的终止坐标
termination = mouseEvent->pos();
QRect rect = QRect(origin, termination);
//根据橡皮筋框截取全屏上的信息,并将其放入shotScreenLabel
shotScreenLabel->setPixmap(fullScreenPixmap.grabWidget(fullScreenLabel,
rect.x(),
rect.y(),
rect.width(),
rect.height()));
//将shotScreenLabel的用户区大小固定为所截图片大小
shotScreenLabel->setFixedSize(rect.width(), rect.height());
shotScreenLabel->show();
rubberBand->hide();
fullScreenLabel->hide();
}
return true;
}
return false;
}
/**
descr:实例化控件
*/
void Widget::createWidgets()
{
//两个QLabel的父控件不能为this,否则截图信息会现在是主窗口中,无法正确显示
fullScreenLabel = new QLabel();
shotScreenLabel = new QLabel();
rubberBand = new QRubberBand(QRubberBand::Rectangle, fullScreenLabel);
leftMousePress = false;
//初始化托盘控件并组装**************************************************************
trayIcon = new QSystemTrayIcon(QIcon(tr(":/images/heart.svg")), this);
menu = new QMenu(this);
restore = new QAction(tr("Restore"), this);
mini = new QAction(tr("Mini"), this);
quit = new QAction(tr("Quit"), this);
menu->addAction(restore);
menu->addAction(mini);
menu->addAction(quit);
trayIcon->setContextMenu(menu);
//将托盘显示
trayIcon->show();
//初始化托盘控件并组装**************************************************************
savePixmap = new QAction(tr("save"), shotScreenLabel);
shotScreenLabel->addAction(savePixmap);
shotScreenLabel->setContextMenuPolicy(Qt::ActionsContextMenu);
}
void Widget::createConnects()
{
//主窗口信号槽*****************************************************************
connect(ui->pbtnShot, SIGNAL(clicked()), this, SLOT(grapWindowScreen()));
connect(ui->pbtnShotAndMin, SIGNAL(clicked()), this, SLOT(miniWindows()));
connect(ui->pbtnMin, SIGNAL(clicked()), this, SLOT(miniWindows()));
connect(savePixmap, SIGNAL(triggered()), this, SLOT(saveShotPixmap()));
//主窗口信号槽*****************************************************************
//托盘信号槽*******************************************************************
connect(restore, SIGNAL(triggered()), this, SLOT(restoreWindows()));
connect(mini, SIGNAL(triggered()), this, SLOT(miniWindows()));
connect(quit, SIGNAL(triggered()), this, SLOT(quitApplication()));
//托盘信号槽*******************************************************************
}
void Widget::createEventFilter()
{
fullScreenLabel->installEventFilter(this);
}
QString Widget::getSaveShotPixmap()
{
return QFileDialog::getSaveFileName(shotScreenLabel,
tr("Open Image"),
".",
tr("Image Files(*.JPG *.PNG)"));
}
void Widget::grapWindowScreen()
{
if (!fullScreenLabel)
{
fullScreenLabel = new QLabel();
}
//获取全屏截图fullScreenPixmap,并将其放入fullScreenLabel
fullScreenPixmap = QPixmap::grabWindow(QApplication::desktop()->winId());
fullScreenLabel->setPixmap(fullScreenPixmap);
//label全屏显示
fullScreenLabel->showFullScreen();
}
void Widget::miniWindows()
{
showMinimized();
grapWindowScreen();
}
void Widget::restoreWindows()
{
showNormal();
}
void Widget::quitApplication()
{
qApp->quit();
}
void Widget::saveShotPixmap()
{
QString fileName = getSaveShotPixmap();
if (!fileName.isNull())
{
fullScreenPixmap.save(fileName);
}
}