5#include <QLoggingCategory>
12#ifndef HAVE_QVideoWidget
13#include <QGraphicsVideoItem>
14#include <QGraphicsScene>
15#include <QGraphicsView>
18static Q_LOGGING_CATEGORY(log,
"FrmPlayer")
20#ifndef HAVE_QVideoWidget
21class CVideoGraphicsView :
public QGraphicsView {
23 using QGraphicsView::QGraphicsView;
25 void resizeEvent(QResizeEvent *e)
override {
26 QGraphicsView::resizeEvent(e);
27 if (scene() && !scene()->items().isEmpty()) {
28 auto *item = scene()->items().first();
29 fitInView(item, Qt::KeepAspectRatio);
35CFrmPlayer::CFrmPlayer(QWidget *parent) : QWidget(parent)
40 , m_paRecordPause(nullptr)
44#ifdef HAVE_QVideoWidget
45 , m_pVideoWidget(nullptr)
47 , m_pGraphicsView(nullptr)
48 , m_pVideoItem(nullptr)
52 , m_pbVideo(Qt::Horizontal, this)
53 , m_pbVolume(Qt::Horizontal, this)
55 , m_pParameter(nullptr)
57 , m_bFullScreen(false)
61 qDebug(log) << Q_FUNC_INFO;
63 setFocusPolicy(Qt::WheelFocus);
65 QVBoxLayout* pLayout =
new QVBoxLayout(
this);
68#ifdef HAVE_QVideoWidget
69 m_pVideoWidget =
new QVideoWidget(
this);
70 m_pVideoWidget->setFocusPolicy(Qt::WheelFocus);
71 m_pVideoWidget->installEventFilter(
this);
72 m_pVideoWidget->setMouseTracking(
true);
74 for (QObject *o : m_pVideoWidget->findChildren<QObject*>()) {
75 o->installEventFilter(
this);
76 if (
auto *w = qobject_cast<QWidget*>(o))
77 w->setMouseTracking(
true);
79 pLayout->addWidget(m_pVideoWidget);
82 QGraphicsScene *pScene =
new QGraphicsScene(
this);
83 m_pVideoItem =
new QGraphicsVideoItem;
84 pScene->addItem(m_pVideoItem);
86 m_pGraphicsView =
new CVideoGraphicsView(pScene,
this);
87 m_pGraphicsView->setRenderHint(QPainter::SmoothPixmapTransform);
88 m_pGraphicsView->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
89 m_pGraphicsView->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
90 m_pGraphicsView->setFrameStyle(0);
91 m_pGraphicsView->setBackgroundBrush(Qt::black);
92 m_pGraphicsView->setMouseTracking(
true);
93 m_pGraphicsView->viewport()->setMouseTracking(
true);
94 m_pGraphicsView->viewport()->installEventFilter(
this);
96 connect(m_pVideoItem, &QGraphicsVideoItem::nativeSizeChanged,
97 this, [
this](
const QSizeF &size) {
100 if(!m_pGraphicsView || !m_pVideoItem)
103 m_pVideoItem->setSize(size);
104 UpdateGraphicsVideoGeometry();
106 pLayout->addWidget((m_pGraphicsView));
109 m_pToolBar =
new QToolBar(
this);
110 m_paStart = m_pToolBar->addAction(
111 QIcon::fromTheme(
"media-playback-start"), tr(
"Start"));
112 m_paStart->setCheckable(
true);
113 check = connect(m_paStart, SIGNAL(toggled(
bool)),
114 this, SLOT(slotStart(
bool)));
117 m_paPause = m_pToolBar->addAction(
118 QIcon::fromTheme(
"media-playback-pause"), tr(
"pause"));
119 m_paPause->setCheckable(
true);
120 m_paPause->setEnabled(
false);
122 m_pToolBar->addSeparator();
123 m_pToolBar->addAction(QIcon::fromTheme(
"media-seek-backward"), tr(
"Backward"),
125 qDebug(log) <<
"Backward action";
126 emit sigChangePosition(m_pbVideo.value() - 1000);
129 m_pbVideo.setRange(0, 0);
130 m_pbVideo.setValue(0);
131 m_pbVideo.setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred);
132 check = connect(&m_pbVideo, &QSlider::sliderPressed,
137 check = connect(&m_pbVideo, &QSlider::sliderReleased,
139 m_bMoveVideo =
false;
140 emit sigChangePosition(m_pbVideo.value());
143 m_pToolBar->addWidget(&m_pbVideo);
145 m_pToolBar->addAction(QIcon::fromTheme(
"media-seek-forward"), tr(
"Forward"),
147 qDebug(log) <<
"Forward action";
148 emit sigChangePosition(m_pbVideo.value() + 1000);
151 m_pToolBar->addSeparator();
152 m_pLabel =
new QLabel(m_pToolBar);
153 m_pLabel->setText(
"00:00:00 / 00:00:00");
154 m_pToolBar->addWidget(m_pLabel);
155 m_pToolBar->addSeparator();
157 m_paScreenShot = m_pToolBar->addAction(
158 QIcon::fromTheme(
"camera-photo"), tr(
"ScreenShot"));
159 m_paScreenShot->setEnabled(
false);
162 m_paRecordPause = m_pToolBar->addAction(
163 QIcon::fromTheme(
"media-playback-pause"), tr(
"Record pause"));
164 m_paRecordPause->setCheckable(
true);
165 m_paRecordPause->setEnabled(
false);
167 m_paRecord = m_pToolBar->addAction(
168 QIcon::fromTheme(
"media-record"), tr(
"Record"));
169 m_paRecord->setCheckable(
true);
170 m_paRecord->setEnabled(
false);
171 check = connect(m_paRecord, &QAction::toggled,
this, [&](
bool checked){
172 m_paRecordPause->setEnabled(checked);
173 m_paRecordPause->setChecked(
false);
178 m_paSettings = m_pToolBar->addAction(
179 QIcon::fromTheme(
"system-settings"), tr(
"Settings"));
181 m_paMuted = m_pToolBar->addAction(
182 QIcon::fromTheme(
"audio-volume-medium"), tr(
"Audio"));
183 m_paMuted->setCheckable(
true);
184 check = connect(m_paMuted, SIGNAL(toggled(
bool)),
185 this, SLOT(slotAudioMuted(
bool)));
188 m_pbVolume.setRange(0, 100);
189 m_pbVolume.setValue(0);
190 m_pbVolume.setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
191 m_paVolume = m_pToolBar->addWidget(&m_pbVolume);
192 check = connect(&m_pbVolume, SIGNAL(sliderMoved(
int)),
193 this, SLOT(slotAduioVolume(
int)));
196 pLayout->addWidget(m_pToolBar);
199CFrmPlayer::~CFrmPlayer()
201 qDebug(log) << Q_FUNC_INFO;
204QVideoSink *CFrmPlayer::videoSink()
206#ifdef HAVE_QVideoWidget
207 return m_pVideoWidget->videoSink();
209 return m_pVideoItem->videoSink();
215 if(!pParameter || m_pParameter == pParameter)
218 m_pParameter = pParameter;
220 m_paMuted->setChecked(m_pParameter->GetAudioOutputMuted());
221 m_pbVolume.setValue(m_pParameter->GetAudioOutputVolume());
225void CFrmPlayer::slotAudioMuted(
bool bMuted)
231 m_paMuted->setIcon(QIcon::fromTheme(
"audio-volume-muted"));
232 m_paMuted->setText(tr(
"Muted"));
234 m_paMuted->setIcon(QIcon::fromTheme(
"audio-volume-medium"));
235 m_paMuted->setText(tr(
"Audio"));
237 m_pParameter->SetAudioOutputMuted(bMuted);
238 m_paVolume->setEnabled(!bMuted);
241void CFrmPlayer::slotAduioVolume(
int volume)
243 qDebug(log) <<
"Volume:" << volume;
246 m_pParameter->SetAudioOutputVolume(volume);
249void CFrmPlayer::slotPositionChanged(qint64 pos, qint64 duration)
251 qint64 currentInfo = pos / 1000;
252 qint64 dur = duration / 1000;
255 if (currentInfo || dur) {
256 QTime currentTime((currentInfo / 3600) % 60,
257 (currentInfo / 60) % 60,
259 (currentInfo * 1000) % 1000);
260 QTime totalTime((dur / 3600) % 60,
263 (dur * 1000) % 1000);
264 QString format =
"mm:ss";
267 szStr = currentTime.toString(format)
268 +
" / " + totalTime.toString(format);
269 m_pLabel->setText(szStr);
270 m_pbVideo.setRange(0, duration);
272 m_pbVideo.setValue(pos);
280 m_bFullScreen = bFull;
282 m_Margins = layout()->contentsMargins();
283 layout()->setContentsMargins(QMargins());
285 layout()->setContentsMargins(m_Margins);
287 if(m_pToolBar && m_bFullScreen)
295 qDebug(log) << Q_FUNC_INFO <<
event <<
this;
300void CFrmPlayer::focusOutEvent(QFocusEvent *event)
302 qDebug(log) << Q_FUNC_INFO <<
event <<
this;
306void CFrmPlayer::mouseMoveEvent(QMouseEvent *event)
310 if(m_pToolBar && m_pToolBar->isHidden()) {
316void CFrmPlayer::slotStart(
bool bStart)
318 QAction* p = qobject_cast<QAction*>(sender());
321 p->setIcon(QIcon::fromTheme(
"media-playback-stop"));
322 p->setText(tr(
"Stop"));
323 m_paPause->setEnabled(
true);
324 m_paPause->setChecked(
false);
325 m_paScreenShot->setEnabled(
true);
327 m_paRecord->setEnabled(
true);
328 m_paRecord->setChecked(
false);
329 m_paRecordPause->setEnabled(
true);
330 m_paRecordPause->setChecked(
false);
334 p->setIcon(QIcon::fromTheme(
"media-playback-start"));
335 p->setText(tr(
"Start"));
336 m_paPause->setEnabled(
false);
337 m_paPause->setChecked(
false);
338 m_paScreenShot->setEnabled(
false);
340 m_paRecord->setEnabled(
false);
341 m_paRecord->setChecked(
false);
342 m_paRecordPause->setEnabled(
false);
343 m_paRecordPause->setChecked(
false);
350void CFrmPlayer::slotTimeOut()
357void CFrmPlayer::StartTimer()
359 if(m_pParameter && m_pParameter->GetHideToolBar() <= 0
362 if(m_paStart->isChecked()) {
364 tm_ToolBar.singleShot(m_pParameter->GetHideToolBar() * 1000,
365 this, SLOT(slotTimeOut()));
369bool CFrmPlayer::eventFilter(QObject *watched, QEvent *event)
372#ifdef HAVE_QVideoWidget
373 if(m_pVideoWidget == watched
374 || m_pVideoWidget->isAncestorOf(qobject_cast<QWidget*>(watched)))
376 switch(event->type()){
377 case QEvent::MouseMove: {
378 qDebug(log) <<
"Mouse move";
379 QMouseEvent* e = (QMouseEvent*)event;
383 case QEvent::MouseButtonRelease:
384 m_paPause->trigger();
386 case QEvent::MouseButtonDblClick: {
387 m_pVideoWidget->setFullScreen(!m_pVideoWidget->isFullScreen());
390 case QEvent::KeyRelease:
392 QKeyEvent* k = (QKeyEvent*)(event);
396 if(m_pVideoWidget->isFullScreen()) {
397 m_pVideoWidget->setFullScreen(
false);
402 m_paPause->trigger();
409 case QEvent::FocusIn:
421 if(m_pGraphicsView->viewport() == watched) {
422 switch(event->type()){
423 case QEvent::MouseMove: {
424 qDebug(log) <<
"Mouse move";
425 QMouseEvent* e = (QMouseEvent*)event;
437#ifndef HAVE_QVideoWidget
438void CFrmPlayer::UpdateGraphicsVideoGeometry()
440 if (!m_pGraphicsView || !m_pVideoItem)
442 if (m_pVideoItem->boundingRect().isEmpty())
446 m_pGraphicsView->setSceneRect(m_pVideoItem->boundingRect());
447 m_pGraphicsView->fitInView(m_pVideoItem, Qt::KeepAspectRatio);
virtual void focusInEvent(QFocusEvent *event) override
[Full Screen]
Q_INVOKABLE int OnFullScreen(bool bFull)
Full Screen.
void sigViewerFocusIn(QWidget *pView)
The view is focus.