#include "VideoWidget.h" VideoWidget::VideoWidget(QWidget* parent) : QWidget(parent) , m_bIsWindMax(false) , m_iWgtWidth(800) , m_iWgtHeight(600) , m_iMaxWidth(1920) , m_iMaxHeight(1080) , m_iCurWidth(0) // 当前宽度 , m_iCurHeight(0) // 当前高度 , m_bMoving(false) , m_bEmbedded(false) , m_pFfmpegDecodeNew(nullptr) , m_uiIndex(0) , m_uiDataSize(0) , m_bIsPlay(false) , m_iTimerId(-1) , m_uiParseStatus(0) , m_pPixmapItem(nullptr) { ui.setupUi(this); setWindowFlags(Qt::FramelessWindowHint); connect(ui.pb_popup, &QPushButton::clicked, this, &VideoWidget::popupWidget); connect(ui.pb_minimize, &QPushButton::clicked, this, &VideoWidget::showMinimized); connect(ui.pb_resize, &QPushButton::clicked, this, &VideoWidget::resizeWidget); connect(ui.pb_hide, &QPushButton::clicked, this, &VideoWidget::hideWidget); connect(ui.pb_videoPlayer, &QPushButton::clicked, this, &VideoWidget::videoPlayer); connect(ui.cb_videoSpeed, &QComboBox::currentTextChanged, this, &VideoWidget::selectVideoSpeed); m_wgtRect.m_iMaxWidth = 1920; m_wgtRect.m_iMaxHeight = 1080; m_wgtRect.m_iWgtWidth = 800; m_wgtRect.m_iWgtHeight = 600; // FFmpeg播放器 ui.gv_videoPlayer->setScene(new QGraphicsScene(this)); ui.gv_videoPlayer->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff); // 关闭水平滚动条 ui.gv_videoPlayer->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff); // 关闭垂直滚动条 m_pFfmpegDecodeNew = new FfmpegDecodeNew(); // 进度条 ui.hs_videoPlayer->setMaximum(100); ui.hs_videoPlayer->setValue(0); connect(ui.hs_videoPlayer, &QSlider::sliderPressed, this, &VideoWidget::videoSilderPressed); // 按下 connect(ui.hs_videoPlayer, &QSlider::sliderReleased, this, &VideoWidget::videoSilderReleased); // 释放 } VideoWidget::~VideoWidget() { if (m_pFfmpegDecodeNew) { m_pFfmpegDecodeNew->stop(); m_pFfmpegDecodeNew->quit(); m_pFfmpegDecodeNew->wait(); delete m_pFfmpegDecodeNew; m_pFfmpegDecodeNew = nullptr; } } void VideoWidget::setParam(QString title, int count) { m_strTitle = title; m_iCount = count; ui.lb_videoTitle->setText(title); } void VideoWidget::resizeEvent(QResizeEvent* event) { QSize size = event->size(); m_iCurWidth = size.width(); m_iCurHeight = size.height(); resizeSubControl(m_iCurWidth, m_iCurHeight); } void VideoWidget::mouseMoveEvent(QMouseEvent* event) { if (m_bEmbedded == true) { return; } if (event->buttons().testFlag(Qt::LeftButton) && m_bMoving) { this->setCursor(Qt::OpenHandCursor); this->move(this->pos() + (event->globalPos() - m_lastMousePos)); m_lastMousePos = event->globalPos(); } } void VideoWidget::mouseReleaseEvent(QMouseEvent* event) { if (m_bEmbedded == true) { return; } if (event->button() == Qt::LeftButton) { m_bMoving = false; this->setCursor(Qt::ArrowCursor); } } void VideoWidget::mousePressEvent(QMouseEvent* event) { if (m_bEmbedded == true) { return; } if (event->button() == Qt::LeftButton) { m_bMoving = true; m_lastMousePos = event->globalPos(); } } void VideoWidget::resizeWidget() { if (false == m_bIsWindMax) { // 窗口最大化 this->setGeometry(0, 0, m_iMaxWidth, m_iMaxHeight); // resizeSubControl(m_iMaxWidth, m_iMaxHeight); } else if (true == m_bIsWindMax) { // 窗口正常化 int iWgtPosX = (m_iMaxWidth - m_iWgtWidth) / 2; int iWgtPosY = (m_iMaxHeight - m_iWgtHeight) / 2; this->setGeometry(iWgtPosX, iWgtPosY, m_iWgtWidth, m_iWgtHeight); // resizeSubControl(m_iWgtWidth,m_iWgtHeight); } m_bIsWindMax = !m_bIsWindMax; } void VideoWidget::resizeSubControl(int width, int height) { // 背景窗口 QRect rect = ui.wgt_video->geometry(); ui.wgt_video->setGeometry(rect.x(), rect.y(), width, height); // 菜单窗口 QRect rectMenu = ui.wgt_menu->geometry(); ui.wgt_menu->setGeometry(rectMenu.x(), rectMenu.y(), width, rectMenu.height()); rect = ui.wgt_resize->geometry(); ui.wgt_resize->setGeometry(width - 1 - rect.width(), rect.y(), rect.width(), rect.height()); rect = ui.pb_popup->geometry(); ui.pb_popup->setGeometry(width - 100 - rect.width(), rect.y(), rect.width(), rect.height()); // 控制窗口 QRect rectCtrl = ui.wgt_control->geometry(); ui.wgt_control->setGeometry(rectCtrl.x(), height - rectCtrl.height(), width, rectCtrl.height()); // 显示窗口 // FFmpeg播放器 rect = ui.gv_videoPlayer->geometry(); ui.gv_videoPlayer->setGeometry(rect.x(), rect.y(), width, height - rectMenu.height() - rectCtrl.height()); fitInView(); } void VideoWidget::fitInView() { if (m_pPixmapItem) { ui.gv_videoPlayer->fitInView(m_pPixmapItem, Qt::KeepAspectRatio); } } void VideoWidget::popupWidget() { emit popupVideoWidget(m_strTitle, m_iCount, m_bEmbedded); } void VideoWidget::setWidgetEmbedded(bool embedded) { m_bEmbedded = embedded; if (embedded == true) { ui.pb_minimize->setEnabled(false); ui.pb_resize->setEnabled(false); ui.pb_popup->setText("弹出"); } else if (embedded == false) { ui.pb_minimize->setEnabled(true); ui.pb_resize->setEnabled(true); ui.pb_popup->setText("内嵌"); } } void VideoWidget::setVideoData(QByteArray array) { m_videoArray = array; } void VideoWidget::videoPlayer() { if (m_uiParseStatus == 0) { // 没有解析任务 return; } if (m_bIsPlay == false) { m_iTimerId = startTimer(20); ui.pb_videoPlayer->setText("停止"); m_bIsPlay = true; // 播放 } else if (m_bIsPlay == true) { killTimer(m_iTimerId); ui.pb_videoPlayer->setText("播放"); m_bIsPlay = false; // 停止 } emit startPlayer(m_bIsPlay); } void VideoWidget::timerEvent(QTimerEvent* event) { if (m_pFfmpegDecodeNew->getImage(m_image)) { m_pixmap = QPixmap::fromImage(m_image); m_pPixmapItem->setPixmap(m_pixmap); ui.gv_videoPlayer->fitInView(m_pPixmapItem, Qt::KeepAspectRatio); } } void VideoWidget::playedCount(unsigned int num, unsigned int count) { int value = (count * 1.0) / (num * 1.0) * 100; // 用于显示播放进度 ui.hs_videoPlayer->setValue(value); if (ui.hs_videoPlayer->value() == 100) { ui.pb_videoPlayer->setText("播放"); ui.hs_videoPlayer->setValue(0); } } void VideoWidget::stopPlay() { m_bIsPlay = true; videoPlayer(); // 清空缓存 m_pFfmpegDecodeNew->getConstBuffer()->clear(); } void VideoWidget::setParseStatus(unsigned int status) { m_uiParseStatus = status; if (m_uiParseStatus != 0) { m_pPixmapItem = new QGraphicsPixmapItem(); ui.gv_videoPlayer->scene()->addItem(m_pPixmapItem); } if (m_uiParseStatus == 1) { // 离线解析 ui.hs_videoPlayer->show(); ui.lb_videoSpeed->show(); ui.cb_videoSpeed->show(); } else if (m_uiParseStatus == 2) { // 在线解析 ui.hs_videoPlayer->hide(); ui.lb_videoSpeed->hide(); ui.cb_videoSpeed->hide(); } } void VideoWidget::hideWidget() { emit hideVideoWidget(m_iCount); } // 拖动进度条 void VideoWidget::videoSilderPressed() { // 先暂停播放,然后拖动进度条 killTimer(m_iTimerId); ui.pb_videoPlayer->setText("播放"); m_bIsPlay = false; // 停止 emit startPlayer(m_bIsPlay); } // 释放进度条 void VideoWidget::videoSilderReleased() { // 从指定位置开始回放 int iPos = ui.hs_videoPlayer->value(); emit setPlayPos(iPos); m_iTimerId = startTimer(20); ui.pb_videoPlayer->setText("停止"); m_bIsPlay = true; // 播放 emit startPlayer(m_bIsPlay); } void VideoWidget::clearWidget() { if (m_pPixmapItem) { ui.gv_videoPlayer->scene()->removeItem(m_pPixmapItem); delete m_pPixmapItem; m_pPixmapItem = nullptr; } ui.pb_videoPlayer->setText("播放"); } void VideoWidget::selectVideoSpeed() { QString strSpeed = ui.cb_videoSpeed->currentText(); emit videoSpeed(strSpeed); }