QGIS API Documentation 3.30.0-'s-Hertogenbosch (f186b8efe0)
qgsmediawidget.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsmediawidget.cpp
3
4 ---------------------
5 begin : 2023.01.24
6 copyright : (C) 2023 by Mathieu Pellerin
7 email : mathieu at opengis dot ch
8 ***************************************************************************
9 * *
10 * This program is free software; you can redistribute it and/or modify *
11 * it under the terms of the GNU General Public License as published by *
12 * the Free Software Foundation; either version 2 of the License, or *
13 * (at your option) any later version. *
14 * *
15 ***************************************************************************/
16
17#include "qgsmediawidget.h"
18#include "qgsapplication.h"
19
20#include <QLabel>
21#include <QHBoxLayout>
22#include <QVBoxLayout>
23#include <QPushButton>
24#include <QSlider>
25#include <QVideoWidget>
26
27
29 : QWidget( parent )
30{
31 mLayout = new QVBoxLayout();
32 mLayout->setContentsMargins( 0, 0, 0, 0 );
33
34 mVideoWidget = new QVideoWidget( this );
35 mVideoWidget->setSizePolicy( QSizePolicy::Expanding, QSizePolicy::Expanding );
36 mVideoWidget->setMinimumHeight( 0 );
37 mVideoWidget->setMaximumHeight( 9999 );
38 mLayout->addWidget( mVideoWidget );
39
40 QHBoxLayout *controlsLayout = new QHBoxLayout();
41 controlsLayout->setContentsMargins( 0, 0, 0, 0 );
42
43 mPlayButton = new QPushButton( this );
44 mPlayButton->setSizePolicy( QSizePolicy::Minimum, QSizePolicy::Preferred );
45 mPlayButton->setIcon( QgsApplication::getThemeIcon( QStringLiteral( "/mActionPlay.svg" ) ) );
46 mPlayButton->setCheckable( true );
47 controlsLayout->addWidget( mPlayButton );
48
49 mPositionSlider = new QSlider( this );
50 mPositionSlider->setSizePolicy( QSizePolicy::Expanding, QSizePolicy::Preferred );
51 mPositionSlider->setOrientation( Qt::Horizontal );
52 controlsLayout->addWidget( mPositionSlider );
53
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 );
61
62 QWidget *controls = new QWidget();
63 controls->setLayout( controlsLayout );
64
65 mLayout->addWidget( controls );
66 setLayout( mLayout );
67
68 adjustControls();
69 setControlsEnabled( false );
70
71 mMediaPlayer.setVideoOutput( mVideoWidget );
72
73 connect( &mMediaPlayer, &QMediaPlayer::mediaStatusChanged, this, &QgsMediaWidget::mediaStatusChanged );
74 connect( &mMediaPlayer, &QMediaPlayer::positionChanged, this, [ = ]()
75 {
76 mPositionSlider->setValue( static_cast<int>( mMediaPlayer.position() / 1000 ) );
77 } );
78
79 connect( mPlayButton, &QAbstractButton::clicked, this, [ = ]()
80 {
81#if QT_VERSION >= QT_VERSION_CHECK( 6, 0, 0 )
82 if ( mMediaPlayer.playbackState() == QMediaPlayer::PlayingState )
83#else
84 if ( mMediaPlayer.state() == QMediaPlayer::PlayingState )
85#endif
86 {
87 mMediaPlayer.pause();
88 }
89 else
90 {
91 mMediaPlayer.play();
92 }
93 } );
94 connect( mPositionSlider, &QAbstractSlider::sliderReleased, this, [ = ]()
95 {
96 mMediaPlayer.setPosition( static_cast<qint64>( mPositionSlider->value() ) * 1000 );
97 } );
98}
99
100void QgsMediaWidget::setMediaPath( const QString &path )
101{
102 if ( mMediaPath == path )
103 return;
104
105 mMediaPath = path;
106#if QT_VERSION >= QT_VERSION_CHECK( 6, 0, 0 )
107 mMediaPlayer.setSource( QUrl::fromLocalFile( path ) );
108#else
109 mMediaPlayer.setMedia( QUrl::fromLocalFile( path ) );
110#endif
111}
112
114{
115 if ( mMode == mode )
116 return;
117
118 mMode = mode;
119 adjustControls();
120}
121
123{
124 return mVideoWidget->minimumHeight();
125}
126
128{
129 mVideoWidget->setMinimumHeight( height );
130 mVideoWidget->setMaximumHeight( height > 0 ? height : 9999 );
131}
132
133void QgsMediaWidget::adjustControls()
134{
135 switch ( mMode )
136 {
137 case Audio:
138 {
139 mVideoWidget->setVisible( false );
140 break;
141 }
142 case Video:
143 {
144 mVideoWidget->setVisible( true );
145 break;
146 }
147 }
148}
149
150void QgsMediaWidget::setControlsEnabled( bool enabled )
151{
152 mPlayButton->setEnabled( enabled );
153 mPositionSlider->setEnabled( enabled );
154}
155
156void QgsMediaWidget::mediaStatusChanged( QMediaPlayer::MediaStatus status )
157{
158 switch ( status )
159 {
160 case QMediaPlayer::LoadedMedia:
161 {
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' ) );
174 break;
175 }
176
177 case QMediaPlayer::EndOfMedia:
178 {
179 mPlayButton->setChecked( false );
180 break;
181 }
182
183 case QMediaPlayer::LoadingMedia:
184 case QMediaPlayer::StalledMedia:
185 case QMediaPlayer::BufferingMedia:
186 case QMediaPlayer::BufferedMedia:
187 {
188 break;
189 }
190
191 case QMediaPlayer::InvalidMedia:
192 {
193 setControlsEnabled( false );
194 mDurationLabel->setText( tr( "invalid" ) );
195 break;
196 }
197
198 case QMediaPlayer::NoMedia:
199#if QT_VERSION < QT_VERSION_CHECK( 6, 0, 0 )
200 case QMediaPlayer::UnknownMediaStatus:
201#endif
202 {
203 setControlsEnabled( false );
204 mDurationLabel->setText( QStringLiteral( "-" ) );
205 break;
206 }
207 }
208}
static QIcon getThemeIcon(const QString &name, const QColor &fillColor=QColor(), const QColor &strokeColor=QColor())
Helper to get a theme icon.
QgsMediaWidget(QWidget *parent=nullptr)
Constructor.
Mode mode() const
Returns the media widget mode.
int videoHeight() const
Returns the video frame height.
Mode
The mode determines the user interface elements visible within the widget.
@ Video
Video-centric user interface.
@ Audio
Audio-centric user interface.
void setMode(Mode mode)
Sets the media widget mode.
void setVideoHeight(int height)
Sets the video frame height.
void setMediaPath(const QString &path)
Sets the media path.