-
-
Notifications
You must be signed in to change notification settings - Fork 78
Expand file tree
/
Copy pathScreenCapture.cpp
More file actions
280 lines (252 loc) · 8.11 KB
/
ScreenCapture.cpp
File metadata and controls
280 lines (252 loc) · 8.11 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
// Author: Kang Lin <kl222@126.com>
#include <QLoggingCategory>
#include <QApplication>
#include <QDesktopServices>
#include <QUrl>
#include <QFileInfo>
#include "ScreenCapture.h"
#include "DlgCapture.h"
#include "RabbitCommonTools.h"
#include "Plugin.h"
static Q_LOGGING_CATEGORY(log, "Screen.Capture")
CScreenCapture::CScreenCapture(CPlugin *plugin)
: COperate(plugin)
, m_pWidget(nullptr)
{
qDebug(log) << Q_FUNC_INFO;
}
CScreenCapture::~CScreenCapture()
{
qDebug(log) << Q_FUNC_INFO;
}
const qint16 CScreenCapture::Version() const
{
return 0;
}
int CScreenCapture::Initial()
{
bool check = false;
qDebug(log) << Q_FUNC_INFO;
Q_ASSERT(!m_pWidget);
m_pWidget = new QVideoWidget();
if(!m_pWidget) {
qCritical(log) << "new QVideoWidget() fail";
return -1;
}
m_pWidget->setFocusPolicy(Qt::WheelFocus);
m_pWidget->installEventFilter(this);
CPlugin* plugin = GetPlugin();
QString szTitle(plugin->DisplayName());
m_Menu.setTitle(szTitle);
m_Menu.setToolTip(szTitle);
m_Menu.setStatusTip(szTitle);
m_Menu.setWhatsThis(szTitle);
m_Menu.setIcon(plugin->Icon());
check = m_Menu.addAction(
QIcon::fromTheme("media-playback-start"), tr("Start"),
this, SLOT(slotStart()));
Q_ASSERT(check);
check = m_Menu.addAction(
QIcon::fromTheme("media-playback-stop"), tr("Stop"),
this, SLOT(slotStop()));
Q_ASSERT(check);
check = connect(
&m_ScreenCapture, &QScreenCapture::errorOccurred,
this, [&](QScreenCapture::Error error, const QString &errorString){
qDebug(log) << "Capture screen error occurred:" << error << errorString;
slotStop();
emit sigError(error, errorString);
});
Q_ASSERT(check);
check = connect(
&m_WindowCapture, &QWindowCapture::errorOccurred,
this, [&](QWindowCapture::Error error, const QString &errorString){
qDebug(log) << "Capture windows error occurred:" << error << errorString;
slotStop();
emit sigError(error, errorString);
});
Q_ASSERT(check);
check = connect(
&m_Recorder, &QMediaRecorder::errorOccurred,
this, [&](QMediaRecorder::Error error, const QString &errorString) {
qDebug(log) << "Recorder error occurred:" << error << errorString;
slotStop();
emit sigError(error, errorString);
});
Q_ASSERT(check);
check = connect(
&m_Recorder, &QMediaRecorder::recorderStateChanged,
this, [&](QMediaRecorder::RecorderState state){
qDebug(log) << "Recorder state changed:" << state;
if(QMediaRecorder::StoppedState == state)
switch(m_Parameter.m_Record.GetEndAction())
{
case CParameterRecord::ENDACTION::OpenFile:
QDesktopServices::openUrl(QUrl::fromLocalFile(m_szRecordFile));
break;
case CParameterRecord::ENDACTION::OpenFolder: {
QFileInfo fi(m_szRecordFile);
QDesktopServices::openUrl(QUrl::fromLocalFile(fi.absolutePath()));
break;
}
default:
break;
}
});
Q_ASSERT(check);
check = connect(
&m_Recorder, &QMediaRecorder::actualLocationChanged,
this, [&](const QUrl &location){
qInfo(log) << "Recorder actual location changed:" << location;
m_szRecordFile = location.toLocalFile();
});
Q_ASSERT(check);
check = connect(
&m_ImageCapture, &QImageCapture::errorOccurred,
this, [&](int id, QImageCapture::Error error, const QString &errorString) {
qDebug(log) << "Capture image error occurred:" << id << error << errorString;
slotStop();
emit sigError(error, errorString);
});
Q_ASSERT(check);
check = connect(
&m_ImageCapture, &QImageCapture::imageCaptured,
this, [&](int id, const QImage &image){
qDebug(log) << "Capture image:" << id << image;
QString szFile = m_Parameter.m_Record.GetImageFile(true);
if(!image.save(szFile, "PNG"))
{
qCritical(log) << "Capture image save to file fail." << szFile;
slotStop();
return;
}
qDebug(log) << "Capture image to file:" << szFile;
switch(m_Parameter.m_Record.GetEndAction())
{
case CParameterRecord::ENDACTION::OpenFile: {
bool bRet = QDesktopServices::openUrl(QUrl::fromLocalFile(szFile));
if(!bRet)
qCritical(log) << "Fail: Open capture image file" << szFile;
break;
}
case CParameterRecord::ENDACTION::OpenFolder: {
QFileInfo fi(szFile);
QDesktopServices::openUrl(QUrl::fromLocalFile(fi.absolutePath()));
break;
}
default:
break;
}
});
Q_ASSERT(check);
return 0;
}
int CScreenCapture::Clean()
{
if(m_pWidget)
delete m_pWidget;
return 0;
}
QWidget *CScreenCapture::GetViewer()
{
return m_pWidget;
}
QDialog *CScreenCapture::OnOpenDialogSettings(QWidget *parent)
{
return nullptr;
}
int CScreenCapture::Start()
{
int nRet = 0;
slotStart();
emit sigRunning();
return nRet;
}
int CScreenCapture::Stop()
{
int nRet = 0;
slotStop();
emit sigFinished();
return nRet;
}
int CScreenCapture::slotStart()
{
qDebug(log) << "CScreenCapture::slotStart()";
int nRet = 0;
CDlgCapture dlg(&m_Parameter);
nRet = RC_SHOW_WINDOW(&dlg);
if(QDialog::Accepted != nRet)
return 0;
int nIndex = m_Parameter.GetScreen();
if(m_Parameter.GetTarget() == CParameterScreenCapture::TARGET::Screen
&& -1 < nIndex && nIndex < QApplication::screens().size()) {
m_ScreenCapture.setScreen(QApplication::screens().at(nIndex));
m_CaptureSessioin.setScreenCapture(&m_ScreenCapture);
m_ScreenCapture.start();
}else
m_CaptureSessioin.setScreenCapture(nullptr);
if(m_Parameter.GetTarget() == CParameterScreenCapture::TARGET::Window) {
m_CaptureSessioin.setWindowCapture(&m_WindowCapture);
m_WindowCapture.setWindow(m_Parameter.GetWindow());
m_WindowCapture.start();
} else
m_CaptureSessioin.setWindowCapture(nullptr);
if(m_Parameter.GetOperate() == CParameterScreenCapture::OPERATE::Record) {
m_CaptureSessioin.setRecorder(&m_Recorder);
m_Parameter.m_Record >> m_Recorder;
m_Recorder.record();
qDebug(log) << "Record to file:" << m_Recorder.actualLocation();
}
else
m_CaptureSessioin.setRecorder(nullptr);
if(m_Parameter.GetOperate() == CParameterScreenCapture::OPERATE::Shot) {
m_CaptureSessioin.setImageCapture(&m_ImageCapture);
m_ImageCapture.capture();
} else
m_CaptureSessioin.setImageCapture(nullptr);
if(m_pWidget)
m_CaptureSessioin.setVideoOutput(m_pWidget);
return 0;
}
int CScreenCapture::slotStop()
{
qDebug(log) << "CScreenCapture::slotStop()";
if(m_Parameter.GetTarget() == CParameterScreenCapture::TARGET::Screen)
m_ScreenCapture.stop();
if(m_Parameter.GetTarget() == CParameterScreenCapture::TARGET::Window)
m_WindowCapture.stop();
if(m_Parameter.GetOperate() == CParameterScreenCapture::OPERATE::Record)
m_Recorder.stop();
return 0;
}
bool CScreenCapture::eventFilter(QObject *watched, QEvent *event)
{
//qDebug(log) << Q_FUNC_INFO << event;
if(m_pWidget == watched)
{
switch(event->type()){
case QEvent::FocusIn:
{
qDebug(log) << Q_FUNC_INFO << event;
emit sigViewerFocusIn(m_pWidget);
break;
}
default:
break;
}
}
return false;
}
int CScreenCapture::SetGlobalParameters(CParameterPlugin *pPara)
{
m_Parameter.SetGlobalParameters(pPara);
return 0;
}
int CScreenCapture::Load(QSettings &set)
{
return 0;
}
int CScreenCapture::Save(QSettings &set)
{
return 0;
}