This repository has been archived by the owner on Oct 20, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 41
/
screenshottool.cpp
298 lines (248 loc) · 7.41 KB
/
screenshottool.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
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
#include "screenshottool.h"
#include "ui_screenshottool.h"
#include <QDebug>
#include <QSettings>
#include "operateSet.h"
#include <QTranslator>
/*
* Author:qiuzhiqian
* Email:[email protected]
* Github:https://github.com/qiuzhiqian
* Date:2017.07.23
* Description:这个类主要用来进行截图工具的设置
**/
/*
* 画图原理
* 1\首先在全屏幕设置一个透明画布
* 2\然后在透明画布上绘制矩形
* 3\对画布上的矩形区域截图
* 4\保存截图到文件夹或剪切板
* 修正
* 1\全屏截屏,同时生成一个暗色图片
* 2\设置全屏画布,然后将暗色全屏图片显示到画布
* 3\在暗色全屏图片上画矩形,矩形内显示亮色图片
* 4\对矩形区域截图
* 5\保存截图到文件夹或剪切板
* */
/*
* 截图区域调整策略
* 左边--pointLT.x
* 右边--pointRB.x
* 上边--pointLT.y
* 下边--pointRB.y
*
* 为简化操作,将角调整转化为同时两个边的调整
* 左上--同时作用左边、上边
* 右下--同时作用右边、下边
* 左下--同时作用左边、下边
* 右上--同时作用右边、上边
* */
ScreenShotTool::ScreenShotTool(QWidget *parent) :
QWidget(parent),
ui(new Ui::ScreenShotTool)
{
ui->setupUi(this);
QWidget * xparent = new QWidget; //设置一个空的父对象,从而达到掩藏任务栏图标的目的
setParent(xparent);
this->setWindowFlags(Qt::Dialog|Qt::WindowCloseButtonHint|Qt::WindowStaysOnTopHint);
this->setWindowTitle(tr("Setting"));
this->setWindowIcon(QIcon(":/ss.ico"));
keystring=new KeyString();
sc_set=new HotKeyBar();
sc_set->setKeyString(keystring); //传递keystring指针
sc_set->setReadOnly(true);
ui->gridLayout->addWidget(sc_set,0,1);
connect(sc_set,SIGNAL(sgn_hotKeyChanged(Qt::Key,Qt::KeyboardModifiers)),this,SLOT(slt_changeHotKey(Qt::Key,Qt::KeyboardModifiers)));
connect(ui->checkBox,SIGNAL(stateChanged(int)),this,SLOT(slt_auto_run(int)));
connect(ui->comboBox,SIGNAL(currentIndexChanged(int)),this,SLOT(slt_language_set(int)));
initTray(); //托盘显示
QString keystr=OperateSet::readSetting("HotKey","ScreenShot","Ctrl+F1").toString();
sc_set->setShowText(keystr); //设置快捷键显示条内容
isAutoRun=bool(OperateSet::readSetting("Setting","AutoRun",0).toInt());
ui->checkBox->setChecked(isAutoRun);
setAutoRun(isAutoRun);
QString language=OperateSet::readSetting("Setting","Language","en").toString();
changeLanguage(language);
if(language=="en")
{
ui->comboBox->setCurrentIndex(0);
}
else
{
ui->comboBox->setCurrentIndex(1);
}
this->setFixedSize(this->size());
//设置快捷键
shortcut = new QxtGlobalShortcut(this);
shortcut->setShortcut(QKeySequence(keystr));
connect(shortcut, SIGNAL(activated()),this,SLOT(slt_doShot()));
}
ScreenShotTool::~ScreenShotTool()
{
//unregisterHotKey(key,mods);
delete ui;
}
void ScreenShotTool::ss_start() //开始截屏
{
QScreen *screen = QGuiApplication::primaryScreen();
fullPixmap = screen->grabWindow(0);
screenCanvas=new Canvas(0); //创建画布
screenCanvas->setbgPixmap(fullPixmap); //传递全屏背景图片
if(ui->comboBox->currentIndex()==0)
{
screenCanvas->changeLanguage("en");
}
else if(ui->comboBox->currentIndex()==1)
{
screenCanvas->changeLanguage("zh_cn");
}
}
void ScreenShotTool::initTray() //初始化托盘
{
m_systemTray = new QSystemTrayIcon(this);
m_systemTray->setIcon(QIcon(":/ss.ico"));
m_systemTray->setToolTip("ScreenShot");
setAction=new QAction(tr("Setting"),this);
aboutAction=new QAction(tr("About"),this);
exitAction=new QAction(tr("Quit"),this);
QMenu *trayMenu=new QMenu(this);
trayMenu->addAction(setAction);
trayMenu->addAction(aboutAction);
trayMenu->addSeparator();
trayMenu->addAction(exitAction);
m_systemTray->setContextMenu(trayMenu);
m_systemTray->show();
connect(m_systemTray,SIGNAL(activated(QSystemTrayIcon::ActivationReason)),this,SLOT(slt_clickTray(QSystemTrayIcon::ActivationReason)));
connect(setAction,SIGNAL(triggered(bool)),this,SLOT(slt_setAction()));
connect(aboutAction,SIGNAL(triggered(bool)),this,SLOT(slt_aboutAction()));
connect(exitAction,SIGNAL(triggered(bool)),this,SLOT(slt_exitAction()));
}
void ScreenShotTool::slt_clickTray(QSystemTrayIcon::ActivationReason reason)
{
switch(reason)
{
case QSystemTrayIcon::Trigger:
//单击托盘图标,开始截图
ss_start();
break;
case QSystemTrayIcon::DoubleClick:
//双击托盘图标
//双击后显示设置窗口
this->show();
break;
default:
break;
}
}
void ScreenShotTool::slt_setAction()
{
this->show();
}
void ScreenShotTool::slt_aboutAction()
{
}
void ScreenShotTool::slt_exitAction()
{
closeFlag=true;
this->close();
QApplication::quit(); //程序退出
}
void ScreenShotTool::closeEvent(QCloseEvent *event)
{
if(closeFlag==true)
{
event->accept();
}
else
{
this->hide();
event->ignore();
}
}
void ScreenShotTool::setHotKey()
{
key=Qt::Key_F1;
mods=Qt::ControlModifier;
//registerHotKey(key,mods);
}
void ScreenShotTool::slt_changeHotKey(Qt::Key t_key, Qt::KeyboardModifiers t_mod) //快捷键修改
{
//unregisterHotKey(key,mods);
key=t_key;
mods=t_mod;
//registerHotKey(key,mods);
QString strval=keystring->Key2String(key,mods);
OperateSet::writeSetting("HotKey","ScreenShot",strval);
qDebug()<<strval;
shortcut->setShortcut(QKeySequence(strval));
}
void ScreenShotTool::slt_auto_run(int states)
{
if(states == Qt::Checked)
{
isAutoRun=true;
OperateSet::writeSetting("Setting","AutoRun",1);
}
else
{
isAutoRun=false;
OperateSet::writeSetting("Setting","AutoRun",0);
}
setAutoRun(isAutoRun);
}
void ScreenShotTool::slt_language_set(int index)
{
if(index==0) //en
{
changeLanguage("en");
}
else if(index==1) //zh_cn
{
changeLanguage("zh_cn");
}
}
void ScreenShotTool::setAutoRun(bool sta)
{
QSettings *reg=new QSettings("HKEY_CURRENT_USER\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run",QSettings::NativeFormat);
if(sta == true)
{
reg->setValue("ScreenShotTool",QApplication::applicationFilePath().replace("/","\\"));
}
else
{
reg->remove("ScreenShotTool");
}
}
void ScreenShotTool::changeLanguage(QString lan)
{
QTranslator translator;
bool sta=false;
if(lan=="zh_cn") //简体中文
{
sta=translator.load("zh_cn.qm");
OperateSet::writeSetting("Setting","Language","zh_cn");
}
else if(lan=="en")
{
sta=translator.load("en.qm");
OperateSet::writeSetting("Setting","Language","en");
}
if(sta)
{
QApplication::installTranslator(&translator);
ui->retranslateUi(this); //刷新ui语言翻译
reFrash(); //刷新自定义语言翻译
}
}
void ScreenShotTool::slt_doShot(void)
{
qDebug()<<"doShot";
ss_start();
}
void ScreenShotTool::reFrash()
{
this->setWindowTitle(tr("Setting"));
setAction->setText(tr("Setting"));
aboutAction->setText(tr("About"));
exitAction->setText(tr("Quit"));
}