-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathEventFilter.cpp
More file actions
430 lines (407 loc) · 14.5 KB
/
EventFilter.cpp
File metadata and controls
430 lines (407 loc) · 14.5 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
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
/******************************************************************************
QtAV Player Demo: this file is part of QtAV examples
Copyright (C) 2012-2015 Wang Bin <wbsecg1@gmail.com>
* This file is part of QtAV
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
******************************************************************************/
#include "EventFilter.h"
//#include <QtAVWidgets>
#include <QApplication>
#include <QtCore/QUrl>
#include <QEvent>
#include <QFileDialog>
#include <QGraphicsObject>
#include <QGraphicsSceneContextMenuEvent>
#include <QInputDialog>
#include <QKeyEvent>
#include <QMenu>
#include <QMessageBox>
#include <QMimeData>
#include <QMouseEvent>
#include <QtGui/QWindowStateChangeEvent>
#include <QDebug>
//#include <QtAV/AVPlayer.h>
//#include <QtAV/AudioOutput.h>
//#include <QtAV/VideoCapture.h>
//#include <QtAV/VideoRenderer.h>
using namespace QtAV;
// TODO: watch main window
EventFilter::EventFilter(/*AVPlayer *player*/) :
//QObject(player),
menu(0)
{
}
EventFilter::~EventFilter()
{
if (menu) {
delete menu;
menu = 0;
}
}
void EventFilter::openLocalFile()
{
QString file = QFileDialog::getOpenFileName(0, tr("Open a video"));
if (file.isEmpty())
return;
// AVPlayer *player = static_cast<AVPlayer*>(parent());
// player->play(file);
}
void EventFilter::openUrl()
{
QString url = QInputDialog::getText(0, tr("Open an url"), tr("Url"));
if (url.isEmpty())
return;
// AVPlayer *player = static_cast<AVPlayer*>(parent());
// player->play(url);
}
void EventFilter::about()
{
// QtAV::about();
}
void EventFilter::aboutFFmpeg()
{
// QtAV::aboutFFmpeg();
}
void EventFilter::help()
{
emit helpRequested();
return;
static QString help = QString::fromLatin1("<h4>") +tr("Drag and drop a file to player\n") + QString::fromLatin1("</h4>"
"<p>") + tr("A: switch aspect ratio") + QString::fromLatin1("</p>"
"<p>") + tr("Double click to switch fullscreen") + QString::fromLatin1("</p>"
"<p>") + tr("Shortcut:\n") + QString::fromLatin1("</p>"
"<p>") + tr("Space: pause/continue\n") + QString::fromLatin1("</p>"
"<p>") + tr("F: fullscreen on/off\n") + QString::fromLatin1("</p>"
"<p>") + tr("T: stays on top on/off\n") + QString::fromLatin1("</p>"
"<p>") + tr("N: show next frame. Continue the playing by pressing 'Space'\n") + QString::fromLatin1("</p>"
"<p>") + tr("Ctrl+O: open a file\n") + QString::fromLatin1("</p>"
"<p>") + tr("O: OSD\n") + QString::fromLatin1("</p>"
"<p>") + tr("P: replay\n") + QString::fromLatin1("</p>"
"<p>") + tr("Q/ESC: quit\n") + QString::fromLatin1("</p>"
"<p>") + tr("S: stop\n") + QString::fromLatin1("</p>"
"<p>") + tr("R: rotate 90") + QString::fromLatin1("</p>"
"<p>") + tr("M: mute on/off\n") + QString::fromLatin1("</p>"
"<p>") + tr("C: capture video") + QString::fromLatin1("</p>"
"<p>") + tr("Up/Down: volume +/-\n") + QString::fromLatin1("</p>"
"<p>") + tr("Ctrl+Up/Down: speed +/-\n") + QString::fromLatin1("</p>"
"<p>") + tr("->/<-: seek forward/backward\n");
QMessageBox::about(0, tr("Help"), help);
}
bool EventFilter::eventFilter(QObject *watched, QEvent *event)
{
Q_UNUSED(watched);
qDebug()<<"EventFilter::eventFilter";
// AVPlayer *player = static_cast<AVPlayer*>(parent());
// if (!player || !player->renderer() || !player->renderer()->widget())
// return false;
// if (qobject_cast<QWidget*>(watched) != player->renderer()->widget()) {
// return false;
// }
//#ifndef QT_NO_DYNAMIC_CAST //dynamic_cast is defined as a macro to force a compile error
// if (player->renderer() != dynamic_cast<VideoRenderer*>(watched)) {
// // return false;
// }
//#endif
QEvent::Type type = event->type();
switch (type) {
case QEvent::KeyPress: {
QKeyEvent *key_event = static_cast<QKeyEvent*>(event);
int key = key_event->key();
Qt::KeyboardModifiers modifiers = key_event->modifiers();
switch (key) {
case Qt::Key_0:
//player->seek(0LL);
break;
case Qt::Key_C: //capture
//player->videoCapture()->capture();
break;
case Qt::Key_N: //check playing?
//player->stepForward();
break;
case Qt::Key_B:
//player->stepBackward();
break;
case Qt::Key_P:
//player->stop();
// player->play();
break;
case Qt::Key_Q:
case Qt::Key_Escape:
//qApp->quit();
//Xuno
{ QWidget *w = qApp->activeWindow();
if (!w)
return false;
if (w->isFullScreen())
w->showNormal();
}
break;
break;
case Qt::Key_S:
//player->stop(); //check playing?
break;
case Qt::Key_Space: //check playing?
//qDebug("isPaused = %d", player->isPaused());
//player->pause(!player->isPaused());
break;
case Qt::Key_F: { //TODO: move to gui
QWidget *w = qApp->activeWindow();
if (!w)
return false;
w->setWindowState(w->windowState() ^ Qt::WindowFullScreen);
}
break;
case Qt::Key_U:
//player->setNotifyInterval(player->notifyInterval() + 100);
break;
case Qt::Key_D:
//player->setNotifyInterval(player->notifyInterval() - 100);
break;
case Qt::Key_Up: /*{
//AudioOutput *ao = player->audio();
if (modifiers == Qt::ControlModifier) {
// qreal s = player->speed();
if (s < 1.4)
s += 0.02;
else
s += 0.05;
if (qAbs<qreal>(s-1.0) <= 0.01)
s = 1.0;
// player->setSpeed(s);
return true;
}
if (ao && ao->isAvailable()) {
// qreal v = player->audio()->volume();
if (v > 0.5)
v += 0.1;
else if (v > 0.1)
v += 0.05;
else
v += 0.025;
// player->audio()->setVolume(v);
qDebug("vol = %.3f", player->audio()->volume());
}
}*/
break;
case Qt::Key_Down: /*{
// AudioOutput *ao = player->audio();
if (modifiers == Qt::ControlModifier) {
qreal s ;//= player->speed();
if (s < 1.4)
s -= 0.02;
else
s -= 0.05;
if (qAbs<qreal>(s-1.0) <= 0.01)
s = 1.0;
s = qMax<qreal>(s, 0.0);
// player->setSpeed(s);
return true;
}
if (ao && ao->isAvailable()) {
qreal v ;//= player->audio()->volume();
if (v > 0.5)
v -= 0.1;
else if (v > 0.1)
v -= 0.05;
else
v -= 0.025;
//player->audio()->setVolume(v);
//qDebug("vol = %.3f", player->audio()->volume());
}
}*/
break;
case Qt::Key_O: {
if (modifiers == Qt::ControlModifier) {
//TODO: emit a signal so we can use custome dialogs?
openLocalFile();
} else/* if (m == Qt::NoModifier) */{
emit showNextOSD();
}
}
break;
case Qt::Key_Left:
qDebug("<-");
//player->setSeekType(key_event->isAutoRepeat() ? KeyFrameSeek : AccurateSeek);
//player->seekBackward();
break;
case Qt::Key_Right:
qDebug("->");
//player->setSeekType(key_event->isAutoRepeat() ? KeyFrameSeek : AccurateSeek);
// player->seekForward();
break;
case Qt::Key_M:
// if (player->audio()) {
// player->audio()->setMute(!player->audio()->isMute());
// }
break;
case Qt::Key_A: {
// VideoRenderer* renderer = player->renderer();
// VideoRenderer::OutAspectRatioMode r = renderer->outAspectRatioMode();
// renderer->setOutAspectRatioMode(VideoRenderer::OutAspectRatioMode(((int)r+1)%2));
}
break;
case Qt::Key_R: {
// VideoRenderer* renderer = player->renderer();
// renderer->setOrientation(renderer->orientation() + 90);
// qDebug("orientation: %d", renderer->orientation());
}
break;
//Xuno
case Qt::Key_X: {
// if (xunoGLSLFilter!=Q_NULLPTR){
// //bool state=!xunoGLSLFilter->getNeedSuperScale();
// //xunoGLSLFilter->setNeedSuperScale(state);
// bool state=!xunoGLSLFilter->getNeedToUseFXAAFiltering();
// xunoGLSLFilter->setNeedToUseFXAAFiltering(state);
// qDebug("toggle FXAA: %d", state);
// }
}
case Qt::Key_T: {
QWidget *w = qApp->activeWindow();
if (!w)
return false;
w->setWindowFlags(w->window()->windowFlags() ^ Qt::WindowStaysOnTopHint);
//call setParent() when changing the flags, causing the widget to be hidden
w->show();
}
break;
case Qt::Key_F1:
help();
break;
default:
return false;
}
break;
}
case QEvent::DragEnter:
case QEvent::DragMove:{
QDropEvent *e = static_cast<QDropEvent*>(event);
if (e->mimeData()->hasUrls())
e->acceptProposedAction();
else
e->ignore();
break;
}
case QEvent::Drop: {
QDropEvent *e = static_cast<QDropEvent*>(event);
if (e->mimeData()->hasUrls()) {
QString path = e->mimeData()->urls().first().toLocalFile();
// player->stop();
// player->play(path);
e->acceptProposedAction();
} else {
e->ignore();
}
}
break;
case QEvent::MouseButtonDblClick: {
QMouseEvent *me = static_cast<QMouseEvent*>(event);
Qt::MouseButton mbt = me->button();
qDebug("MouseButtonDblClick");
//QWidget *mpWindow ;//= static_cast<QWidget*>(player->parent());
//QWidget *mpWindow ;//= static_cast<QWidget*>(player->parent());
QWidget *mpWindow = qApp->activeWindow();
if (mbt == Qt::LeftButton) {
if (Qt::WindowFullScreen == mpWindow->windowState()){
mpWindow->setWindowState(mpWindow->windowState() ^ Qt::WindowFullScreen);
}else{
mpWindow->showFullScreen();
}
}
break;
}
case QEvent::GraphicsSceneContextMenu: {
QGraphicsSceneContextMenuEvent *e = static_cast<QGraphicsSceneContextMenuEvent*>(event);
showMenu(e->screenPos());
}
break;
case QEvent::ContextMenu: {
QContextMenuEvent *e = static_cast<QContextMenuEvent*>(event);
showMenu(e->globalPos());
}
break;
default:
return false;
}
return true; //false: for text input
}
void EventFilter::showMenu(const QPoint &p)
{
if (!menu) {
menu = new QMenu();
menu->addAction(tr("Open"), this, SLOT(openLocalFile()));
menu->addAction(tr("Open Url"), this, SLOT(openUrl()));
menu->addSeparator();
menu->addAction(tr("About"), this, SLOT(about()));
menu->addAction(tr("Help"), this, SLOT(help()));
menu->addSeparator();
}
menu->exec(p);
}
//void EventFilter::setXunoGLSLFilter(XunoGLSLFilter *value)
//{
// xunoGLSLFilter = value;
//}
WindowEventFilter::WindowEventFilter(QWidget *window)
: QObject(window)
, mpWindow(window)
, enableMovingWindow(false)
{
}
bool WindowEventFilter::eventFilter(QObject *watched, QEvent *event)
{
if (watched != mpWindow)
return false;
if (event->type() == QEvent::WindowStateChange) {
QWindowStateChangeEvent *e = static_cast<QWindowStateChangeEvent*>(event);
mpWindow->updateGeometry();
if (mpWindow->windowState().testFlag(Qt::WindowFullScreen) || e->oldState().testFlag(Qt::WindowFullScreen)) {
emit fullscreenChanged();
}
return false;
}
if (event->type() == QEvent::MouseButtonPress) {
QMouseEvent *me = static_cast<QMouseEvent*>(event);
Qt::MouseButton mbt = me->button();
if (mbt == Qt::LeftButton) {
gMousePos = me->globalPos();
iMousePos = me->pos();
}
return false;
}
if (event->type() == QEvent::MouseButtonRelease) {
QMouseEvent *me = static_cast<QMouseEvent*>(event);
Qt::MouseButton mbt = me->button();
if (mbt != Qt::LeftButton)
return false;
iMousePos = QPoint();
gMousePos = QPoint();
return false;
}
if (event->type() == QEvent::MouseMove) {
if (iMousePos.isNull() || gMousePos.isNull() || !enableMovingWindow)
return false;
QMouseEvent *me = static_cast<QMouseEvent*>(event);
int x = mpWindow->pos().x();
int y = mpWindow->pos().y();
int dx = me->globalPos().x() - gMousePos.x();
int dy = me->globalPos().y() - gMousePos.y();
gMousePos = me->globalPos();
mpWindow->move(x + dx, y + dy);
return false;
}
return false;
}
void WindowEventFilter::setEnableMovingWindow(bool s)
{
enableMovingWindow = s;
}