File indexing completed on 2024-12-22 04:40:20
0001 /* 0002 SPDX-FileCopyrightText: 2007-2009 Sergio Pistone <sergio_pistone@yahoo.com.ar> 0003 SPDX-FileCopyrightText: 2010-2022 Mladen Milinkovic <max@smoothware.net> 0004 0005 SPDX-License-Identifier: GPL-2.0-or-later 0006 */ 0007 0008 #include "videowidget.h" 0009 0010 #include <QApplication> 0011 #include <QPainter> 0012 #include <QPaintEvent> 0013 #include <QResizeEvent> 0014 #include <QMoveEvent> 0015 #include <QMouseEvent> 0016 #include <QScreen> 0017 0018 #include <QDebug> 0019 0020 #if QT_VERSION < QT_VERSION_CHECK(5, 14, 0) 0021 #include <QDesktopWidget> 0022 #endif 0023 0024 0025 using namespace SubtitleComposer; 0026 0027 VideoWidget::VideoWidget(QWidget *parent) 0028 : QWidget(parent), 0029 m_videoLayer(nullptr), 0030 m_videoWidth(0), 0031 m_videoHeight(0), 0032 m_videoDAR(0) 0033 { 0034 setFocusPolicy(Qt::NoFocus); 0035 QPalette pal(palette()); 0036 pal.setColor(QPalette::Window, Qt::black); 0037 setAutoFillBackground(true); 0038 setPalette(pal); 0039 } 0040 0041 VideoWidget::~VideoWidget() 0042 {} 0043 0044 void 0045 VideoWidget::setVideoLayer(QWidget *videoLayer) 0046 { 0047 m_videoLayer = videoLayer; 0048 m_videoLayer->setParent(this); 0049 m_videoLayer->hide(); 0050 } 0051 0052 QWidget * 0053 VideoWidget::videoLayer() const 0054 { 0055 return m_videoLayer; 0056 } 0057 0058 int 0059 VideoWidget::videoWidth() const 0060 { 0061 return m_videoWidth; 0062 } 0063 0064 int 0065 VideoWidget::videoHeight() const 0066 { 0067 return m_videoHeight; 0068 } 0069 0070 double 0071 VideoWidget::videoDAR() const 0072 { 0073 return m_videoDAR; 0074 } 0075 0076 void 0077 VideoWidget::setVideoResolution(int width, int height, double dar) 0078 { 0079 if(width <= 0 || height <= 0) { 0080 qWarning() << "invalid video width or height reported"; 0081 return; 0082 } 0083 0084 m_videoWidth = width; 0085 m_videoHeight = height; 0086 m_videoDAR = dar > 0.0 ? dar : (double)width / height; 0087 0088 updateVideoLayerGeometry(); 0089 } 0090 0091 void 0092 VideoWidget::setMouseTracking(bool enable) 0093 { 0094 QWidget::setMouseTracking(enable); 0095 m_videoLayer->setMouseTracking(enable); 0096 } 0097 0098 void 0099 VideoWidget::resizeEvent(QResizeEvent *) 0100 { 0101 updateVideoLayerGeometry(); 0102 } 0103 0104 #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0) 0105 #define globalPosition globalPos 0106 #endif 0107 0108 void 0109 VideoWidget::mouseReleaseEvent(QMouseEvent *e) 0110 { 0111 // FIXME: ignore doubleclicks 0112 Q_ASSERT(e->type() != QEvent::MouseButtonDblClick); 0113 if(e->button() == Qt::LeftButton) { 0114 e->accept(); 0115 emit leftClicked(e->globalPosition()); 0116 } else if(e->button() == Qt::RightButton) { 0117 e->accept(); 0118 emit rightClicked(e->globalPosition()); 0119 } else { 0120 e->ignore(); 0121 } 0122 } 0123 0124 void 0125 VideoWidget::mouseDoubleClickEvent(QMouseEvent *e) 0126 { 0127 e->accept(); 0128 emit doubleClicked(e->globalPosition()); 0129 } 0130 0131 void 0132 VideoWidget::wheelEvent(QWheelEvent *e) 0133 { 0134 e->accept(); 0135 if(e->angleDelta().y() >= 0) 0136 emit wheelUp(); 0137 else 0138 emit wheelDown(); 0139 } 0140 0141 QSize 0142 VideoWidget::desktopSize() 0143 { 0144 #if QT_VERSION < QT_VERSION_CHECK(5, 14, 0) 0145 QRect rect = QApplication::desktop()->screenGeometry(); 0146 #else 0147 QRect rect = screen()->geometry(); 0148 #endif 0149 return QSize(rect.width(), rect.height()); 0150 } 0151 0152 double 0153 VideoWidget::desktopAspectRatio() 0154 { 0155 QSize size = desktopSize(); 0156 return (double)size.width() / size.height(); 0157 } 0158 0159 void 0160 VideoWidget::updateVideoLayerGeometry() 0161 { 0162 QSize frameSize = size(); 0163 0164 int videoLayerX = 0; 0165 int videoLayerY = 0; 0166 int videoLayerWidth = frameSize.width(); 0167 int videoLayerHeight = frameSize.height(); 0168 0169 if(m_videoDAR > 0.0) { 0170 if(m_videoDAR >= ((double)videoLayerWidth / videoLayerHeight)) { 0171 videoLayerHeight = (int)(videoLayerWidth / m_videoDAR); 0172 videoLayerY = (frameSize.height() - videoLayerHeight) / 2; 0173 } else { 0174 videoLayerWidth = (int)(videoLayerHeight * m_videoDAR); 0175 videoLayerX = (frameSize.width() - videoLayerWidth) / 2; 0176 } 0177 } 0178 0179 m_videoLayer->setGeometry(videoLayerX, videoLayerY, videoLayerWidth, videoLayerHeight); 0180 0181 m_videoLayer->update(); 0182 update(); 0183 } 0184 0185 QSize 0186 VideoWidget::sizeHint() const 0187 { 0188 return QSize(m_videoWidth, m_videoHeight); 0189 } 0190 0191 QSize 0192 VideoWidget::minimumSizeHint() const 0193 { 0194 return QSize(0, 0); 0195 } 0196 0197