25#include <QVideoWidget>
31 mLayout =
new QVBoxLayout();
32 mLayout->setContentsMargins( 0, 0, 0, 0 );
34 mVideoWidget =
new QVideoWidget(
this );
35 const int vHeight = QFontMetrics( font() ).height() * 12;
36 mVideoWidget->setMinimumHeight( vHeight );
37 mVideoWidget->setMaximumHeight( vHeight );
38 mLayout->addWidget( mVideoWidget );
40 QHBoxLayout *controlsLayout =
new QHBoxLayout();
41 controlsLayout->setContentsMargins( 0, 0, 0, 0 );
43 mPlayButton =
new QPushButton(
this );
44 mPlayButton->setSizePolicy( QSizePolicy::Minimum, QSizePolicy::Preferred );
46 mPlayButton->setCheckable(
true );
47 controlsLayout->addWidget( mPlayButton );
49 mPositionSlider =
new QSlider(
this );
50 mPositionSlider->setSizePolicy( QSizePolicy::Expanding, QSizePolicy::Preferred );
51 mPositionSlider->setOrientation( Qt::Horizontal );
52 controlsLayout->addWidget( mPositionSlider );
54 mDurationLabel =
new QLabel(
this );
55 mDurationLabel->setSizePolicy( QSizePolicy::Minimum, QSizePolicy::Preferred );
56 mDurationLabel->setAlignment( Qt::AlignHCenter );
57 mDurationLabel->setText( QStringLiteral(
"-" ) );
58 QFontMetrics fm( mDurationLabel->font() );
59 mDurationLabel->setMinimumWidth( fm.boundingRect( QStringLiteral(
"00:00:00" ) ).width() );
60 controlsLayout->addWidget( mDurationLabel );
62 QWidget *controls =
new QWidget();
63 controls->setLayout( controlsLayout );
65 mLayout->addWidget( controls );
69 setControlsEnabled(
false );
71 mMediaPlayer.setVideoOutput( mVideoWidget );
73 connect( &mMediaPlayer, &QMediaPlayer::mediaStatusChanged,
this, &QgsMediaWidget::mediaStatusChanged );
74 connect( &mMediaPlayer, &QMediaPlayer::positionChanged,
this, [ = ]()
76 mPositionSlider->setValue(
static_cast<int>( mMediaPlayer.position() / 1000 ) );
79 connect( mPlayButton, &QAbstractButton::clicked,
this, [ = ]()
81#if QT_VERSION >= QT_VERSION_CHECK( 6, 0, 0 )
82 if ( mMediaPlayer.playbackState() == QMediaPlayer::PlayingState )
84 if ( mMediaPlayer.state() == QMediaPlayer::PlayingState )
94 connect( mPositionSlider, &QAbstractSlider::sliderReleased,
this, [ = ]()
96 mMediaPlayer.setPosition(
static_cast<qint64
>( mPositionSlider->value() ) * 1000 );
102 if ( mMediaPath == path )
106#if QT_VERSION >= QT_VERSION_CHECK( 6, 0, 0 )
107 mMediaPlayer.setSource( QUrl::fromLocalFile( path ) );
109 mMediaPlayer.setMedia( QUrl::fromLocalFile( path ) );
124 return mVideoWidget->minimumHeight();
129 const int vHeight = height > 0 ? height : QFontMetrics( font() ).height() * 12;
130 mVideoWidget->setMinimumHeight( vHeight );
131 mVideoWidget->setMaximumHeight( vHeight );
134void QgsMediaWidget::adjustControls()
140 mVideoWidget->setVisible(
false );
145 mVideoWidget->setVisible(
true );
151void QgsMediaWidget::setControlsEnabled(
bool enabled )
153 mPlayButton->setEnabled( enabled );
154 mPositionSlider->setEnabled( enabled );
157void QgsMediaWidget::mediaStatusChanged( QMediaPlayer::MediaStatus status )
161 case QMediaPlayer::LoadedMedia:
163 setControlsEnabled(
true );
164 mPositionSlider->setMinimum( 0 );
165 mPositionSlider->setMaximum(
static_cast<int>( mMediaPlayer.duration() / 1000 ) );
166 mPositionSlider->setValue( 0 );
167 int seconds = std::floor( mMediaPlayer.duration() / 1000 );
168 const int hours = std::floor( seconds / 3600 );
169 seconds -= hours * 3600;
170 const int minutes = std::floor( seconds / 60 );
171 seconds -= minutes * 60;
172 mDurationLabel->setText( QStringLiteral(
"%1:%2:%3" ).arg( QString::number( hours ), 2,
'0' )
173 .arg( QString::number( minutes ), 2,
'0' )
174 .arg( QString::number( seconds ), 2,
'0' ) );
178 case QMediaPlayer::EndOfMedia:
180 mPlayButton->setChecked(
false );
184 case QMediaPlayer::LoadingMedia:
185 case QMediaPlayer::StalledMedia:
186 case QMediaPlayer::BufferingMedia:
187 case QMediaPlayer::BufferedMedia:
192 case QMediaPlayer::InvalidMedia:
194 setControlsEnabled(
false );
195 mDurationLabel->setText( tr(
"invalid" ) );
199 case QMediaPlayer::NoMedia:
200#if QT_VERSION < QT_VERSION_CHECK( 6, 0, 0 )
201 case QMediaPlayer::UnknownMediaStatus:
204 setControlsEnabled(
false );
205 mDurationLabel->setText( QStringLiteral(
"-" ) );
static QIcon getThemeIcon(const QString &name, const QColor &fillColor=QColor(), const QColor &strokeColor=QColor())
Helper to get a theme icon.