QGIS API Documentation 3.99.0-Master (d270888f95f)
Loading...
Searching...
No Matches
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
19#include "qgsapplication.h"
20
21#include <QHBoxLayout>
22#include <QLabel>
23#include <QPushButton>
24#include <QSlider>
25#include <QString>
26#include <QVBoxLayout>
27#include <QVideoWidget>
28
29#include "moc_qgsmediawidget.cpp"
30
31using namespace Qt::StringLiterals;
32
34 : QWidget( parent )
35{
36 mLayout = new QVBoxLayout();
37 mLayout->setContentsMargins( 0, 0, 0, 0 );
38
39 mVideoWidget = new QVideoWidget( this );
40 const int vHeight = QFontMetrics( font() ).height() * 12;
41 mVideoWidget->setMinimumHeight( vHeight );
42 mVideoWidget->setMaximumHeight( vHeight );
43 mLayout->addWidget( mVideoWidget );
44
45 QHBoxLayout *controlsLayout = new QHBoxLayout();
46 controlsLayout->setContentsMargins( 0, 0, 0, 0 );
47
48 mPlayButton = new QPushButton( this );
49 mPlayButton->setSizePolicy( QSizePolicy::Minimum, QSizePolicy::Preferred );
50 mPlayButton->setIcon( QgsApplication::getThemeIcon( u"/mActionPlay.svg"_s ) );
51 mPlayButton->setCheckable( true );
52 controlsLayout->addWidget( mPlayButton );
53
54 mPositionSlider = new QSlider( this );
55 mPositionSlider->setSizePolicy( QSizePolicy::Expanding, QSizePolicy::Preferred );
56 mPositionSlider->setOrientation( Qt::Horizontal );
57 controlsLayout->addWidget( mPositionSlider );
58
59 mDurationLabel = new QLabel( this );
60 mDurationLabel->setSizePolicy( QSizePolicy::Minimum, QSizePolicy::Preferred );
61 mDurationLabel->setAlignment( Qt::AlignHCenter );
62 mDurationLabel->setText( u"-"_s );
63 QFontMetrics fm( mDurationLabel->font() );
64 mDurationLabel->setMinimumWidth( fm.boundingRect( u"00:00:00"_s ).width() );
65 controlsLayout->addWidget( mDurationLabel );
66
67 QWidget *controls = new QWidget();
68 controls->setLayout( controlsLayout );
69
70 mLayout->addWidget( controls );
71 setLayout( mLayout );
72
73 adjustControls();
74 setControlsEnabled( false );
75
76 mMediaPlayer.setVideoOutput( mVideoWidget );
77
78 connect( &mMediaPlayer, &QMediaPlayer::mediaStatusChanged, this, &QgsMediaWidget::mediaStatusChanged );
79 connect( &mMediaPlayer, &QMediaPlayer::positionChanged, this, [this]() {
80 mPositionSlider->setValue( static_cast<int>( mMediaPlayer.position() / 1000 ) );
81 } );
82
83 connect( mPlayButton, &QAbstractButton::clicked, this, [this]() {
84 if ( mMediaPlayer.playbackState() == QMediaPlayer::PlayingState )
85 {
86 mMediaPlayer.pause();
87 }
88 else
89 {
90 mMediaPlayer.play();
91 }
92 } );
93 connect( mPositionSlider, &QAbstractSlider::sliderReleased, this, [this]() {
94 mMediaPlayer.setPosition( static_cast<qint64>( mPositionSlider->value() ) * 1000 );
95 } );
96}
97
98void QgsMediaWidget::setMediaPath( const QString &path )
99{
100 if ( mMediaPath == path )
101 return;
102
103 mMediaPath = path;
104 mMediaPlayer.setSource( QUrl::fromLocalFile( path ) );
105}
106
108{
109 if ( mMode == mode )
110 return;
111
112 mMode = mode;
113 adjustControls();
114}
115
117{
118 return mVideoWidget->minimumHeight();
119}
120
122{
123 const int vHeight = height > 0 ? height : QFontMetrics( font() ).height() * 12;
124 mVideoWidget->setMinimumHeight( vHeight );
125 mVideoWidget->setMaximumHeight( vHeight );
126}
127
128void QgsMediaWidget::adjustControls()
129{
130 switch ( mMode )
131 {
132 case Audio:
133 {
134 mVideoWidget->setVisible( false );
135 break;
136 }
137 case Video:
138 {
139 mVideoWidget->setVisible( true );
140 break;
141 }
142 }
143}
144
145void QgsMediaWidget::setControlsEnabled( bool enabled )
146{
147 mPlayButton->setEnabled( enabled );
148 mPositionSlider->setEnabled( enabled );
149}
150
151void QgsMediaWidget::mediaStatusChanged( QMediaPlayer::MediaStatus status )
152{
153 switch ( status )
154 {
155 case QMediaPlayer::LoadedMedia:
156 {
157 setControlsEnabled( true );
158 mPositionSlider->setMinimum( 0 );
159 mPositionSlider->setMaximum( static_cast<int>( mMediaPlayer.duration() / 1000 ) );
160 mPositionSlider->setValue( 0 );
161 int seconds = std::floor( mMediaPlayer.duration() / 1000 );
162 const int hours = std::floor( seconds / 3600 );
163 seconds -= hours * 3600;
164 const int minutes = std::floor( seconds / 60 );
165 seconds -= minutes * 60;
166 mDurationLabel->setText( u"%1:%2:%3"_s.arg( QString::number( hours ), 2, '0' ).arg( QString::number( minutes ), 2, '0' ).arg( QString::number( seconds ), 2, '0' ) );
167 break;
168 }
169
170 case QMediaPlayer::EndOfMedia:
171 {
172 mPlayButton->setChecked( false );
173 break;
174 }
175
176 case QMediaPlayer::LoadingMedia:
177 case QMediaPlayer::StalledMedia:
178 case QMediaPlayer::BufferingMedia:
179 case QMediaPlayer::BufferedMedia:
180 {
181 break;
182 }
183
184 case QMediaPlayer::InvalidMedia:
185 {
186 setControlsEnabled( false );
187 mDurationLabel->setText( tr( "invalid" ) );
188 break;
189 }
190
191 case QMediaPlayer::NoMedia:
192 {
193 setControlsEnabled( false );
194 mDurationLabel->setText( u"-"_s );
195 break;
196 }
197 }
198}
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.