25#include <QVideoWidget>
31 mLayout =
new QVBoxLayout();
32 mLayout->setContentsMargins( 0, 0, 0, 0 );
34 mVideoWidget =
new QVideoWidget(
this );
35 mVideoWidget->setSizePolicy( QSizePolicy::Expanding, QSizePolicy::Expanding );
36 mVideoWidget->setMinimumHeight( 0 );
37 mVideoWidget->setMaximumHeight( 9999 );
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 mVideoWidget->setMinimumHeight( height );
130 mVideoWidget->setMaximumHeight( height > 0 ? height : 9999 );
133void QgsMediaWidget::adjustControls()
139 mVideoWidget->setVisible(
false );
144 mVideoWidget->setVisible(
true );
150void QgsMediaWidget::setControlsEnabled(
bool enabled )
152 mPlayButton->setEnabled( enabled );
153 mPositionSlider->setEnabled( enabled );
156void QgsMediaWidget::mediaStatusChanged( QMediaPlayer::MediaStatus status )
160 case QMediaPlayer::LoadedMedia:
162 setControlsEnabled(
true );
163 mPositionSlider->setMinimum( 0 );
164 mPositionSlider->setMaximum(
static_cast<int>( mMediaPlayer.duration() / 1000 ) );
165 mPositionSlider->setValue( 0 );
166 int seconds = std::floor( mMediaPlayer.duration() / 1000 );
167 const int hours = std::floor( seconds / 3600 );
168 seconds -= hours * 3600;
169 const int minutes = std::floor( seconds / 60 );
170 seconds -= minutes * 60;
171 mDurationLabel->setText( QStringLiteral(
"%1:%2:%3" ).arg( QString::number( hours ), 2,
'0' )
172 .arg( QString::number( minutes ), 2,
'0' )
173 .arg( QString::number( seconds ), 2,
'0' ) );
177 case QMediaPlayer::EndOfMedia:
179 mPlayButton->setChecked(
false );
183 case QMediaPlayer::LoadingMedia:
184 case QMediaPlayer::StalledMedia:
185 case QMediaPlayer::BufferingMedia:
186 case QMediaPlayer::BufferedMedia:
191 case QMediaPlayer::InvalidMedia:
193 setControlsEnabled(
false );
194 mDurationLabel->setText( tr(
"invalid" ) );
198 case QMediaPlayer::NoMedia:
199#if QT_VERSION < QT_VERSION_CHECK( 6, 0, 0 )
200 case QMediaPlayer::UnknownMediaStatus:
203 setControlsEnabled(
false );
204 mDurationLabel->setText( QStringLiteral(
"-" ) );
static QIcon getThemeIcon(const QString &name, const QColor &fillColor=QColor(), const QColor &strokeColor=QColor())
Helper to get a theme icon.