-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwidget.cpp
More file actions
356 lines (320 loc) · 9.87 KB
/
widget.cpp
File metadata and controls
356 lines (320 loc) · 9.87 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
#include "ui_widget.h"
#include"widget.h"
#include<QMessageBox>
#include<QApplication>
#include<QDesktopWidget>
#include<QColor>
#include<QBrush>
#include<QFileDialog>
#include<QTime>
Widget::Widget(QWidget *parent) :
QWidget(parent), ui(new Ui::Widget), box(this)
{
ui->setupUi(this);
input = new InputDialog(this);
//No window title
this->setWindowIcon(QIcon(":/x.ico"));
this->setWindowFlags(Qt::FramelessWindowHint);
m_colorCursor = QCursor(QPixmap(":/103.ico"),0,0);
setCursor(m_colorCursor);
// 右键菜单
m_clearAction = new QAction(tr("重新选择区域"),this);
m_saveAction = new QAction(tr("保存"),this);
m_finishAction = new QAction(tr("完成截图"),this);
m_saveFullAction = new QAction(tr("选择全屏"),this);
m_cancelAction = new QAction(tr("最小化"),this);
m_aboutAction = new QAction(tr("关于..."),this);
m_quitAction = new QAction(tr("退出"),this);
m_menu = new QMenu(this);
m_menu->addAction(m_clearAction);
m_menu->addSeparator();
m_menu->addAction(m_saveAction);
m_menu->addAction(m_finishAction);
m_menu->addAction(m_saveFullAction);
m_menu->addSeparator();
m_menu->addAction(m_cancelAction);
m_menu->addSeparator();
m_menu->addAction(m_quitAction);
//系统托盘
m_systemTray = new QSystemTrayIcon(this);
m_systemTray->setIcon(QIcon(":/x.ico"));
m_systemTray->setToolTip(tr("cutScreen"));
m_systemTray->setContextMenu(m_menu);
m_systemTray->show();
//取得屏幕大小,初始化 cutScreen
cutScreen = new RCutScreen(QApplication::desktop()->size());
resize(cutScreen->width(),cutScreen->height());
// resize(600,400);
//截图信息显示区域背景
infoPix = new QPixmap(200,32);
QPainter infoP(infoPix);
infoP.setBrush(QBrush(QColor(Qt::black),Qt::SolidPattern));
infoP.drawRect(0,0,200,32);
//保存全屏
fullScreen = new QPixmap();
bgScreen = new QPixmap();
grabFullScreen();
connect(m_clearAction,SIGNAL(triggered()),this,SLOT(clearScreen()));
connect(m_saveAction,SIGNAL(triggered()),this,SLOT(saveScreen()));
connect(m_finishAction,SIGNAL(triggered()),this,SLOT(clipScreen()));
connect(m_saveFullAction,SIGNAL(triggered()),this,SLOT(saveFullScreen()));
connect(m_cancelAction,SIGNAL(triggered()),this,SLOT(startCutScreen()));
connect(m_quitAction,SIGNAL(triggered()),qApp,SLOT(quit()));
//点击托盘图标
connect(m_systemTray , SIGNAL(activated(QSystemTrayIcon::ActivationReason)),
this, SLOT(activeSystemTray(QSystemTrayIcon::ActivationReason)));
//全局快捷键
m_shortcut = new QxtGlobalShortcut(QKeySequence("Ctrl+Alt+a"), this);
connect(m_shortcut, SIGNAL(activated()),this,SLOT(startCutScreen()));
}
Widget::~Widget()
{
}
void Widget::paintEvent(QPaintEvent *)
{
int x = cutScreen->getLeftUp().x();
int y = cutScreen->getLeftUp().y();
int w = cutScreen->getRightDown().x()-x;
int h = cutScreen->getRightDown().y()-y;
qDebug("paintEvent %d-%d %d-%d\n",x,y,w,h);
QPainter painter(this);
painter.setPen(QPen(Qt::green,2,Qt::SolidLine));
painter.drawPixmap(0,0,*bgScreen); //画模糊背景
if( x == -1 || y == -1 )
{
return;
}
if( w!=0 && h!=0 )
{
painter.drawPixmap(x,y,fullScreen->copy(x,y,w,h)); //画截取区域
}
painter.drawRect(x,y,w,h); //截取区域边框
//显示截取区域信息 width height
int offsetY = 0;
if( y < 32 )
{
offsetY = 32;
}
painter.drawPixmap(x,y-32 + offsetY,*infoPix);
painter.drawText(x+2,y-20 + offsetY,QString("%5: (%1 x %2)-(%3 x %4)").arg(x).arg(y).arg(x+w).arg(y+h).arg(tr("区域坐标")));
painter.drawText(x+2,y-6 + offsetY,QString("%5: (%1 x %2)").arg(w).arg(h).arg(tr("区域大小")));
}
void Widget::grabFullScreen()
{
*fullScreen = QPixmap::grabWindow(QApplication::desktop()->winId(),0,0,cutScreen->width(),cutScreen->height());
//设置透明度实现模糊背景
QPixmap pix(cutScreen->width(),cutScreen->height());
pix.fill((QColor(160,160,165,192)));
*bgScreen = *fullScreen;
QPainter p(bgScreen);
p.drawPixmap(0,0,pix);
}
void Widget::clipScreen()
{
int x = cutScreen->getLeftUp().x();
int y = cutScreen->getLeftUp().y();
int w = cutScreen->getRightDown().x()-x;
int h = cutScreen->getRightDown().y()-y;
QClipboard *clipboard = QApplication::clipboard();
clipboard->setPixmap(fullScreen->copy(x,y,w,h));
qApp->quit();
}
/*****************************************
*
*截取全屏
*
****************************************/
void Widget::saveFullScreen()
{
if( this->isVisible() == false )
{
startCutScreen();
}
cutScreen->setStart(QPoint(0,0));
cutScreen->setEnd(QPoint(this->width(),this->height()));
update();
}
void Widget::clearScreen()
{
cutScreen->clearArea();
update();
}
/****************************************
*
*保存截取区域
*
****************************************/
void Widget::saveScreen()
{
QFileDialog::Options options;
QString selectedFilter;
QString str = QTime().currentTime().toString();
//默认文件名:hour-minute-second.jpg
QString fileName = str.mid(0,2) + "-" + str.mid(3,2) + "-" + str.mid(6,2);
fileName = QFileDialog::getSaveFileName(this,
tr("Save picture"),
fileName,
tr("JPEG Files (*.jpg);;JPEG (*.jpg)"),
&selectedFilter,
options);
if( !fileName.isEmpty() )
{
int x = cutScreen->getLeftUp().x();
int y = cutScreen->getLeftUp().y();
int w = cutScreen->getRightDown().x()-x;
int h = cutScreen->getRightDown().y()-y;
fullScreen->copy(x,y,w,h).save(fileName,"JPG");
//保存成功后退出
qApp->quit();
}
}
void Widget::startCutScreen()
{
if( this->isVisible() )
{
m_cancelAction->setText(tr("截屏 Ctrl+Alt+a"));
setVisible(false);
this->setCursor(Qt::ArrowCursor);
}
else
{
m_cancelAction->setText(tr("最小化"));
cutScreen->clearArea();
grabFullScreen();
setVisible(true);
this->activateWindow();
this->setCursor(m_colorCursor);
}
m_clearAction->setVisible(isVisible());
m_saveAction->setVisible(isVisible());
}
void Widget::about()
{
box.setWindowTitle(tr("cutScreen v1.0"));
box.setText(tr("cutScreen v1.0 小截图工具\n快捷键 Ctrl + Alt + a"));
box.exec();
//???
}
void Widget::activeSystemTray(QSystemTrayIcon::ActivationReason reason)
{
switch(reason)
{
//点击托盘显示窗口
case QSystemTrayIcon::Trigger:
startCutScreen();
break;
default:
break;
}
}
void Widget::contextMenuEvent(QContextMenuEvent *)
{
QPoint pos = this->cursor().pos();
m_menu->exec(pos);
}
/****************************************
*
*按键功能
*
****************************************/
void Widget::keyReleaseEvent(QKeyEvent* )
{
}
/****************************************
*
*鼠标移动在 SELECT MOV 两种状态下
*
****************************************/
void Widget::mouseMoveEvent(QMouseEvent *e)
{
if( e->buttons() & Qt::RightButton )
{
return;
}
if( cutScreen->getStatus()==SELECT ) // 选择区域
{
qDebug("mouseMoveEvent SELECT");
cutScreen->setEnd( e->pos() );
}
else if( cutScreen->getStatus()==MOV ) //移动所选区域
{
QPoint p(e->x()-movPos.x(),e->y()-movPos.y());
cutScreen->move(p);
movPos = e->pos();
}
update();
}
/****************************************
*
*按下鼠标,记录初值
*
****************************************/
void Widget::mousePressEvent(QMouseEvent *e)
{
if( e->button() == Qt::RightButton )
{
return;
}
int status = cutScreen->getStatus();
if( status==SELECT ) // 记录鼠标
{
cutScreen->setStart( e->pos() );
}
else if( status==MOV ) //
{
// 不在截图区域内,重新选择,
if( cutScreen->isInArea(e->pos())==false )
{
cutScreen->setStart( e->pos() );
cutScreen->setStatus(SELECT);
}
// 在截图区域,移动截图 鼠标指针成十字
else
{
movPos = e->pos();
this->setCursor(Qt::SizeAllCursor);
}
}
update();
}
/****************************************
*
*任何情况下,双击弹出 width height设置框
*
****************************************/
void Widget::mouseDoubleClickEvent(QMouseEvent *e)
{
if( e->button() == Qt::RightButton )
{
return;
}
if( cutScreen->getStatus()!= SET_W_H )
{
cutScreen->setStatus(SET_W_H);
input->show();
input->exec();
if( input->isOk()==true ) //数据有效,改变区域大小
{
QPoint pos = cutScreen->getLeftUp();
cutScreen->setStart(pos);
cutScreen->setEnd(QPoint(pos.x()+input->getWidth(),pos.y()+input->getHeight()));
}
// 回到 MOV 状态
cutScreen->setStatus(MOV);
}
}
void Widget::mouseReleaseEvent(QMouseEvent *e)
{
if( e->button() == Qt::RightButton )
{
return;
}
if( cutScreen->getStatus()==SELECT ) // SELECT状态下 释放鼠标
{
cutScreen->setStatus(MOV); //移动、撤销
}
else if( cutScreen->getStatus()==MOV ) // 鼠标成正常状态
{
this->setCursor(m_colorCursor);
}
}