QGIS API Documentation 4.1.0-Master (5bf3c20f3c9)
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]() { mPositionSlider->setValue( static_cast<int>( mMediaPlayer.position() / 1000 ) ); } );
80
81 connect( mPlayButton, &QAbstractButton::clicked, this, [this]() {
82 if ( mMediaPlayer.playbackState() == QMediaPlayer::PlayingState )
83 {
84 mMediaPlayer.pause();
85 }
86 else
87 {
88 mMediaPlayer.play();
89 }
90 } );
91 connect( mPositionSlider, &QAbstractSlider::sliderReleased, this, [this]() { mMediaPlayer.setPosition( static_cast<qint64>( mPositionSlider->value() ) * 1000 ); } );
92}
93
94void QgsMediaWidget::setMediaPath( const QString &path )
95{
96 if ( mMediaPath == path )
97 return;
98
99 mMediaPath = path;
100 mMediaPlayer.setSource( QUrl::fromLocalFile( path ) );
101}
102
104{
105 if ( mMode == mode )
106 return;
107
108 mMode = mode;
109 adjustControls();
110}
111
113{
114 return mVideoWidget->minimumHeight();
115}
116
118{
119 const int vHeight = height > 0 ? height : QFontMetrics( font() ).height() * 12;
120 mVideoWidget->setMinimumHeight( vHeight );
121 mVideoWidget->setMaximumHeight( vHeight );
122}
123
124void QgsMediaWidget::adjustControls()
125{
126 switch ( mMode )
127 {
128 case Audio:
129 {
130 mVideoWidget->setVisible( false );
131 break;
132 }
133 case Video:
134 {
135 mVideoWidget->setVisible( true );
136 break;
137 }
138 }
139}
140
141void QgsMediaWidget::setControlsEnabled( bool enabled )
142{
143 mPlayButton->setEnabled( enabled );
144 mPositionSlider->setEnabled( enabled );
145}
146
147void QgsMediaWidget::mediaStatusChanged( QMediaPlayer::MediaStatus status )
148{
149 switch ( status )
150 {
151 case QMediaPlayer::LoadedMedia:
152 {
153 setControlsEnabled( true );
154 mPositionSlider->setMinimum( 0 );
155 mPositionSlider->setMaximum( static_cast<int>( mMediaPlayer.duration() / 1000 ) );
156 mPositionSlider->setValue( 0 );
157 int seconds = std::floor( mMediaPlayer.duration() / 1000 );
158 const int hours = std::floor( seconds / 3600 );
159 seconds -= hours * 3600;
160 const int minutes = std::floor( seconds / 60 );
161 seconds -= minutes * 60;
162 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' ) );
163 break;
164 }
165
166 case QMediaPlayer::EndOfMedia:
167 {
168 mPlayButton->setChecked( false );
169 break;
170 }
171
172 case QMediaPlayer::LoadingMedia:
173 case QMediaPlayer::StalledMedia:
174 case QMediaPlayer::BufferingMedia:
175 case QMediaPlayer::BufferedMedia:
176 {
177 break;
178 }
179
180 case QMediaPlayer::InvalidMedia:
181 {
182 setControlsEnabled( false );
183 mDurationLabel->setText( tr( "invalid" ) );
184 break;
185 }
186
187 case QMediaPlayer::NoMedia:
188 {
189 setControlsEnabled( false );
190 mDurationLabel->setText( u"-"_s );
191 break;
192 }
193 }
194}
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.