diff options
author | VaL Doroshchuk <valentyn.doroshchuk@qt.io> | 2017-09-14 09:56:30 +0200 |
---|---|---|
committer | VaL Doroshchuk <valentyn.doroshchuk@qt.io> | 2017-09-28 14:00:26 +0000 |
commit | 62d72daf70fed1d7fa1c38bd52468b6536ec6cad (patch) | |
tree | 867428c277ac8cc0fcfa5c15003d6ad5b43f03cd /examples/multimediawidgets/player/player.cpp | |
parent | 61b0771e832cad41c726995aafd7bea705909bdd (diff) |
Modernize the Player example
Fixed memory leak.
Renamed members, avoided passing args by copy.
Changed the style of member initialization.
Changed 0 to nullptr.
Changed to new signal/slot style if possible.
Removed Q_WS_SIMULATOR ifdef.
Changed signal/slot connection style.
Task-number: QTBUG-60627
Change-Id: I85bd6e56fe87f4e17f3c9964d6d1fdcb8202c519
Reviewed-by: Christian Stromme <christian.stromme@qt.io>
Diffstat (limited to 'examples/multimediawidgets/player/player.cpp')
-rw-r--r-- | examples/multimediawidgets/player/player.cpp | 282 |
1 files changed, 137 insertions, 145 deletions
diff --git a/examples/multimediawidgets/player/player.cpp b/examples/multimediawidgets/player/player.cpp index 085dff6a7..ce178d4a1 100644 --- a/examples/multimediawidgets/player/player.cpp +++ b/examples/multimediawidgets/player/player.cpp @@ -43,6 +43,7 @@ #include "playercontrols.h" #include "playlistmodel.h" #include "histogramwidget.h" +#include "videowidget.h" #include <QMediaService> #include <QMediaPlaylist> @@ -53,105 +54,98 @@ Player::Player(QWidget *parent) : QWidget(parent) - , videoWidget(0) - , coverLabel(0) - , slider(0) - , colorDialog(0) { //! [create-objs] - player = new QMediaPlayer(this); - player->setAudioRole(QAudio::VideoRole); + m_player = new QMediaPlayer(this); + m_player->setAudioRole(QAudio::VideoRole); qInfo() << "Supported audio roles:"; - for (QAudio::Role role : player->supportedAudioRoles()) + for (QAudio::Role role : m_player->supportedAudioRoles()) qInfo() << " " << role; // owned by PlaylistModel - playlist = new QMediaPlaylist(); - player->setPlaylist(playlist); + m_playlist = new QMediaPlaylist(); + m_player->setPlaylist(m_playlist); //! [create-objs] - connect(player, SIGNAL(durationChanged(qint64)), SLOT(durationChanged(qint64))); - connect(player, SIGNAL(positionChanged(qint64)), SLOT(positionChanged(qint64))); - connect(player, SIGNAL(metaDataChanged()), SLOT(metaDataChanged())); - connect(playlist, SIGNAL(currentIndexChanged(int)), SLOT(playlistPositionChanged(int))); - connect(player, SIGNAL(mediaStatusChanged(QMediaPlayer::MediaStatus)), - this, SLOT(statusChanged(QMediaPlayer::MediaStatus))); - connect(player, SIGNAL(bufferStatusChanged(int)), this, SLOT(bufferingProgress(int))); - connect(player, SIGNAL(videoAvailableChanged(bool)), this, SLOT(videoAvailableChanged(bool))); - connect(player, SIGNAL(error(QMediaPlayer::Error)), this, SLOT(displayErrorMessage())); - connect(player, &QMediaPlayer::stateChanged, this, &Player::stateChanged); + connect(m_player, &QMediaPlayer::durationChanged, this, &Player::durationChanged); + connect(m_player, &QMediaPlayer::positionChanged, this, &Player::positionChanged); + connect(m_player, QOverload<>::of(&QMediaPlayer::metaDataChanged), this, &Player::metaDataChanged); + connect(m_playlist, &QMediaPlaylist::currentIndexChanged, this, &Player::playlistPositionChanged); + connect(m_player, &QMediaPlayer::mediaStatusChanged, this, &Player::statusChanged); + connect(m_player, &QMediaPlayer::bufferStatusChanged, this, &Player::bufferingProgress); + connect(m_player, &QMediaPlayer::videoAvailableChanged, this, &Player::videoAvailableChanged); + connect(m_player, QOverload<QMediaPlayer::Error>::of(&QMediaPlayer::error), this, &Player::displayErrorMessage); + connect(m_player, &QMediaPlayer::stateChanged, this, &Player::stateChanged); //! [2] - videoWidget = new VideoWidget(this); - player->setVideoOutput(videoWidget); + m_videoWidget = new VideoWidget(this); + m_player->setVideoOutput(m_videoWidget); - playlistModel = new PlaylistModel(this); - playlistModel->setPlaylist(playlist); + m_playlistModel = new PlaylistModel(this); + m_playlistModel->setPlaylist(m_playlist); //! [2] - playlistView = new QListView(this); - playlistView->setModel(playlistModel); - playlistView->setCurrentIndex(playlistModel->index(playlist->currentIndex(), 0)); + m_playlistView = new QListView(this); + m_playlistView->setModel(m_playlistModel); + m_playlistView->setCurrentIndex(m_playlistModel->index(m_playlist->currentIndex(), 0)); - connect(playlistView, SIGNAL(activated(QModelIndex)), this, SLOT(jump(QModelIndex))); + connect(m_playlistView, &QAbstractItemView::activated, this, &Player::jump); - slider = new QSlider(Qt::Horizontal, this); - slider->setRange(0, player->duration() / 1000); + m_slider = new QSlider(Qt::Horizontal, this); + m_slider->setRange(0, m_player->duration() / 1000); - labelDuration = new QLabel(this); - connect(slider, SIGNAL(sliderMoved(int)), this, SLOT(seek(int))); + m_labelDuration = new QLabel(this); + connect(m_slider, &QSlider::sliderMoved, this, &Player::seek); - labelHistogram = new QLabel(this); - labelHistogram->setText("Histogram:"); - videoHistogram = new HistogramWidget(this); - audioHistogram = new HistogramWidget(this); + m_labelHistogram = new QLabel(this); + m_labelHistogram->setText("Histogram:"); + m_videoHistogram = new HistogramWidget(this); + m_audioHistogram = new HistogramWidget(this); QHBoxLayout *histogramLayout = new QHBoxLayout; - histogramLayout->addWidget(labelHistogram); - histogramLayout->addWidget(videoHistogram, 1); - histogramLayout->addWidget(audioHistogram, 2); + histogramLayout->addWidget(m_labelHistogram); + histogramLayout->addWidget(m_videoHistogram, 1); + histogramLayout->addWidget(m_audioHistogram, 2); - videoProbe = new QVideoProbe(this); - connect(videoProbe, SIGNAL(videoFrameProbed(QVideoFrame)), videoHistogram, SLOT(processFrame(QVideoFrame))); - videoProbe->setSource(player); + m_videoProbe = new QVideoProbe(this); + connect(m_videoProbe, &QVideoProbe::videoFrameProbed, m_videoHistogram, &HistogramWidget::processFrame); + m_videoProbe->setSource(m_player); - audioProbe = new QAudioProbe(this); - connect(audioProbe, SIGNAL(audioBufferProbed(QAudioBuffer)), audioHistogram, SLOT(processBuffer(QAudioBuffer))); - audioProbe->setSource(player); + m_audioProbe = new QAudioProbe(this); + connect(m_audioProbe, &QAudioProbe::audioBufferProbed, m_audioHistogram, &HistogramWidget::processBuffer); + m_audioProbe->setSource(m_player); QPushButton *openButton = new QPushButton(tr("Open"), this); - connect(openButton, SIGNAL(clicked()), this, SLOT(open())); + connect(openButton, &QPushButton::clicked, this, &Player::open); PlayerControls *controls = new PlayerControls(this); - controls->setState(player->state()); - controls->setVolume(player->volume()); + controls->setState(m_player->state()); + controls->setVolume(m_player->volume()); controls->setMuted(controls->isMuted()); - connect(controls, SIGNAL(play()), player, SLOT(play())); - connect(controls, SIGNAL(pause()), player, SLOT(pause())); - connect(controls, SIGNAL(stop()), player, SLOT(stop())); - connect(controls, SIGNAL(next()), playlist, SLOT(next())); - connect(controls, SIGNAL(previous()), this, SLOT(previousClicked())); - connect(controls, SIGNAL(changeVolume(int)), player, SLOT(setVolume(int))); - connect(controls, SIGNAL(changeMuting(bool)), player, SLOT(setMuted(bool))); - connect(controls, SIGNAL(changeRate(qreal)), player, SLOT(setPlaybackRate(qreal))); + connect(controls, &PlayerControls::play, m_player, &QMediaPlayer::play); + connect(controls, &PlayerControls::pause, m_player, &QMediaPlayer::pause); + connect(controls, &PlayerControls::stop, m_player, &QMediaPlayer::stop); + connect(controls, &PlayerControls::next, m_playlist, &QMediaPlaylist::next); + connect(controls, &PlayerControls::previous, this, &Player::previousClicked); + connect(controls, &PlayerControls::changeVolume, m_player, &QMediaPlayer::setVolume); + connect(controls, &PlayerControls::changeMuting, m_player, &QMediaPlayer::setMuted); + connect(controls, &PlayerControls::changeRate, m_player, &QMediaPlayer::setPlaybackRate); + connect(controls, &PlayerControls::stop, m_videoWidget, QOverload<>::of(&QVideoWidget::update)); - connect(controls, SIGNAL(stop()), videoWidget, SLOT(update())); + connect(m_player, &QMediaPlayer::stateChanged, controls, &PlayerControls::setState); + connect(m_player, &QMediaPlayer::volumeChanged, controls, &PlayerControls::setVolume); + connect(m_player, &QMediaPlayer::mutedChanged, controls, &PlayerControls::setMuted); - connect(player, SIGNAL(stateChanged(QMediaPlayer::State)), - controls, SLOT(setState(QMediaPlayer::State))); - connect(player, SIGNAL(volumeChanged(int)), controls, SLOT(setVolume(int))); - connect(player, SIGNAL(mutedChanged(bool)), controls, SLOT(setMuted(bool))); + m_fullScreenButton = new QPushButton(tr("FullScreen"), this); + m_fullScreenButton->setCheckable(true); - fullScreenButton = new QPushButton(tr("FullScreen"), this); - fullScreenButton->setCheckable(true); - - colorButton = new QPushButton(tr("Color Options..."), this); - colorButton->setEnabled(false); - connect(colorButton, SIGNAL(clicked()), this, SLOT(showColorDialog())); + m_colorButton = new QPushButton(tr("Color Options..."), this); + m_colorButton->setEnabled(false); + connect(m_colorButton, &QPushButton::clicked, this, &Player::showColorDialog); QBoxLayout *displayLayout = new QHBoxLayout; - displayLayout->addWidget(videoWidget, 2); - displayLayout->addWidget(playlistView); + displayLayout->addWidget(m_videoWidget, 2); + displayLayout->addWidget(m_playlistView); QBoxLayout *controlLayout = new QHBoxLayout; controlLayout->setMargin(0); @@ -159,14 +153,14 @@ Player::Player(QWidget *parent) controlLayout->addStretch(1); controlLayout->addWidget(controls); controlLayout->addStretch(1); - controlLayout->addWidget(fullScreenButton); - controlLayout->addWidget(colorButton); + controlLayout->addWidget(m_fullScreenButton); + controlLayout->addWidget(m_colorButton); QBoxLayout *layout = new QVBoxLayout; layout->addLayout(displayLayout); QHBoxLayout *hLayout = new QHBoxLayout; - hLayout->addWidget(slider); - hLayout->addWidget(labelDuration); + hLayout->addWidget(m_slider); + hLayout->addWidget(m_labelDuration); layout->addLayout(hLayout); layout->addLayout(controlLayout); layout->addLayout(histogramLayout); @@ -179,10 +173,10 @@ Player::Player(QWidget *parent) "Please check the media service plugins are installed.")); controls->setEnabled(false); - playlistView->setEnabled(false); + m_playlistView->setEnabled(false); openButton->setEnabled(false); - colorButton->setEnabled(false); - fullScreenButton->setEnabled(false); + m_colorButton->setEnabled(false); + m_fullScreenButton->setEnabled(false); } metaDataChanged(); @@ -194,7 +188,7 @@ Player::~Player() bool Player::isPlayerAvailable() const { - return player->isAvailable(); + return m_player->isAvailable(); } void Player::open() @@ -202,7 +196,7 @@ void Player::open() QFileDialog fileDialog(this); fileDialog.setAcceptMode(QFileDialog::AcceptOpen); fileDialog.setWindowTitle(tr("Open Files")); - QStringList supportedMimeTypes = player->supportedMimeTypes(); + QStringList supportedMimeTypes = m_player->supportedMimeTypes(); if (!supportedMimeTypes.isEmpty()) { supportedMimeTypes.append("audio/x-m3u"); // MP3 playlists fileDialog.setMimeTypeFilters(supportedMimeTypes); @@ -220,41 +214,41 @@ static bool isPlaylist(const QUrl &url) // Check for ".m3u" playlists. return fileInfo.exists() && !fileInfo.suffix().compare(QLatin1String("m3u"), Qt::CaseInsensitive); } -void Player::addToPlaylist(const QList<QUrl> urls) +void Player::addToPlaylist(const QList<QUrl> &urls) { - foreach (const QUrl &url, urls) { + for (auto &url: urls) { if (isPlaylist(url)) - playlist->load(url); + m_playlist->load(url); else - playlist->addMedia(url); + m_playlist->addMedia(url); } } void Player::durationChanged(qint64 duration) { - this->duration = duration/1000; - slider->setMaximum(duration / 1000); + m_duration = duration / 1000; + m_slider->setMaximum(m_duration); } void Player::positionChanged(qint64 progress) { - if (!slider->isSliderDown()) { - slider->setValue(progress / 1000); - } + if (!m_slider->isSliderDown()) + m_slider->setValue(progress / 1000); + updateDurationInfo(progress / 1000); } void Player::metaDataChanged() { - if (player->isMetaDataAvailable()) { + if (m_player->isMetaDataAvailable()) { setTrackInfo(QString("%1 - %2") - .arg(player->metaData(QMediaMetaData::AlbumArtist).toString()) - .arg(player->metaData(QMediaMetaData::Title).toString())); + .arg(m_player->metaData(QMediaMetaData::AlbumArtist).toString()) + .arg(m_player->metaData(QMediaMetaData::Title).toString())); - if (coverLabel) { - QUrl url = player->metaData(QMediaMetaData::CoverArtUrlLarge).value<QUrl>(); + if (m_coverLabel) { + QUrl url = m_player->metaData(QMediaMetaData::CoverArtUrlLarge).value<QUrl>(); - coverLabel->setPixmap(!url.isEmpty() + m_coverLabel->setPixmap(!url.isEmpty() ? QPixmap(url.toString()) : QPixmap()); } @@ -265,29 +259,29 @@ void Player::previousClicked() { // Go to previous track if we are within the first 5 seconds of playback // Otherwise, seek to the beginning. - if(player->position() <= 5000) - playlist->previous(); + if (m_player->position() <= 5000) + m_playlist->previous(); else - player->setPosition(0); + m_player->setPosition(0); } void Player::jump(const QModelIndex &index) { if (index.isValid()) { - playlist->setCurrentIndex(index.row()); - player->play(); + m_playlist->setCurrentIndex(index.row()); + m_player->play(); } } void Player::playlistPositionChanged(int currentItem) { clearHistogram(); - playlistView->setCurrentIndex(playlistModel->index(currentItem, 0)); + m_playlistView->setCurrentIndex(m_playlistModel->index(currentItem, 0)); } void Player::seek(int seconds) { - player->setPosition(seconds * 1000); + m_player->setPosition(seconds * 1000); } void Player::statusChanged(QMediaPlayer::MediaStatus status) @@ -344,86 +338,84 @@ void Player::bufferingProgress(int progress) void Player::videoAvailableChanged(bool available) { if (!available) { - disconnect(fullScreenButton, SIGNAL(clicked(bool)), - videoWidget, SLOT(setFullScreen(bool))); - disconnect(videoWidget, SIGNAL(fullScreenChanged(bool)), - fullScreenButton, SLOT(setChecked(bool))); - videoWidget->setFullScreen(false); + disconnect(m_fullScreenButton, &QPushButton::clicked, m_videoWidget, &QVideoWidget::setFullScreen); + disconnect(m_videoWidget, &QVideoWidget::fullScreenChanged, m_fullScreenButton, &QPushButton::setChecked); + m_videoWidget->setFullScreen(false); } else { - connect(fullScreenButton, SIGNAL(clicked(bool)), - videoWidget, SLOT(setFullScreen(bool))); - connect(videoWidget, SIGNAL(fullScreenChanged(bool)), - fullScreenButton, SLOT(setChecked(bool))); + connect(m_fullScreenButton, &QPushButton::clicked, m_videoWidget, &QVideoWidget::setFullScreen); + connect(m_videoWidget, &QVideoWidget::fullScreenChanged, m_fullScreenButton, &QPushButton::setChecked); - if (fullScreenButton->isChecked()) - videoWidget->setFullScreen(true); + if (m_fullScreenButton->isChecked()) + m_videoWidget->setFullScreen(true); } - colorButton->setEnabled(available); + m_colorButton->setEnabled(available); } void Player::setTrackInfo(const QString &info) { - trackInfo = info; - if (!statusInfo.isEmpty()) - setWindowTitle(QString("%1 | %2").arg(trackInfo).arg(statusInfo)); + m_trackInfo = info; + if (!m_statusInfo.isEmpty()) + setWindowTitle(QString("%1 | %2").arg(m_trackInfo).arg(m_statusInfo)); else - setWindowTitle(trackInfo); + setWindowTitle(m_trackInfo); } void Player::setStatusInfo(const QString &info) { - statusInfo = info; - if (!statusInfo.isEmpty()) - setWindowTitle(QString("%1 | %2").arg(trackInfo).arg(statusInfo)); + m_statusInfo = info; + if (!m_statusInfo.isEmpty()) + setWindowTitle(QString("%1 | %2").arg(m_trackInfo).arg(m_statusInfo)); else - setWindowTitle(trackInfo); + setWindowTitle(m_trackInfo); } void Player::displayErrorMessage() { - setStatusInfo(player->errorString()); + setStatusInfo(m_player->errorString()); } void Player::updateDurationInfo(qint64 currentInfo) { QString tStr; - if (currentInfo || duration) { - QTime currentTime((currentInfo/3600)%60, (currentInfo/60)%60, currentInfo%60, (currentInfo*1000)%1000); - QTime totalTime((duration/3600)%60, (duration/60)%60, duration%60, (duration*1000)%1000); + if (currentInfo || m_duration) { + QTime currentTime((currentInfo / 3600) % 60, (currentInfo / 60) % 60, + currentInfo % 60, (currentInfo * 1000) % 1000); + QTime totalTime((m_duration / 3600) % 60, (m_duration / 60) % 60, + m_duration % 60, (m_duration * 1000) % 1000); QString format = "mm:ss"; - if (duration > 3600) + if (m_duration > 3600) format = "hh:mm:ss"; tStr = currentTime.toString(format) + " / " + totalTime.toString(format); } - labelDuration->setText(tStr); + m_labelDuration->setText(tStr); } void Player::showColorDialog() { - if (!colorDialog) { + if (!m_colorDialog) { QSlider *brightnessSlider = new QSlider(Qt::Horizontal); brightnessSlider->setRange(-100, 100); - brightnessSlider->setValue(videoWidget->brightness()); - connect(brightnessSlider, SIGNAL(sliderMoved(int)), videoWidget, SLOT(setBrightness(int))); - connect(videoWidget, SIGNAL(brightnessChanged(int)), brightnessSlider, SLOT(setValue(int))); + brightnessSlider->setValue(m_videoWidget->brightness()); + connect(brightnessSlider, &QSlider::sliderMoved, m_videoWidget, &QVideoWidget::setBrightness); + connect(m_videoWidget, &QVideoWidget::brightnessChanged, brightnessSlider, &QSlider::setValue); QSlider *contrastSlider = new QSlider(Qt::Horizontal); contrastSlider->setRange(-100, 100); - contrastSlider->setValue(videoWidget->contrast()); - connect(contrastSlider, SIGNAL(sliderMoved(int)), videoWidget, SLOT(setContrast(int))); - connect(videoWidget, SIGNAL(contrastChanged(int)), contrastSlider, SLOT(setValue(int))); + contrastSlider->setValue(m_videoWidget->contrast()); + connect(contrastSlider, &QSlider::sliderMoved, m_videoWidget, &QVideoWidget::setContrast); + connect(m_videoWidget, &QVideoWidget::contrastChanged, contrastSlider, &QSlider::setValue); QSlider *hueSlider = new QSlider(Qt::Horizontal); hueSlider->setRange(-100, 100); - hueSlider->setValue(videoWidget->hue()); - connect(hueSlider, SIGNAL(sliderMoved(int)), videoWidget, SLOT(setHue(int))); - connect(videoWidget, SIGNAL(hueChanged(int)), hueSlider, SLOT(setValue(int))); + hueSlider->setValue(m_videoWidget->hue()); + connect(hueSlider, &QSlider::sliderMoved, m_videoWidget, &QVideoWidget::setHue); + connect(m_videoWidget, &QVideoWidget::hueChanged, hueSlider, &QSlider::setValue); QSlider *saturationSlider = new QSlider(Qt::Horizontal); saturationSlider->setRange(-100, 100); - saturationSlider->setValue(videoWidget->saturation()); - connect(saturationSlider, SIGNAL(sliderMoved(int)), videoWidget, SLOT(setSaturation(int))); - connect(videoWidget, SIGNAL(saturationChanged(int)), saturationSlider, SLOT(setValue(int))); + saturationSlider->setValue(m_videoWidget->saturation()); + connect(saturationSlider, &QSlider::sliderMoved, m_videoWidget, &QVideoWidget::setSaturation); + connect(m_videoWidget, &QVideoWidget::saturationChanged, saturationSlider, &QSlider::setValue); QFormLayout *layout = new QFormLayout; layout->addRow(tr("Brightness"), brightnessSlider); @@ -434,17 +426,17 @@ void Player::showColorDialog() QPushButton *button = new QPushButton(tr("Close")); layout->addRow(button); - colorDialog = new QDialog(this); - colorDialog->setWindowTitle(tr("Color Options")); - colorDialog->setLayout(layout); + m_colorDialog = new QDialog(this); + m_colorDialog->setWindowTitle(tr("Color Options")); + m_colorDialog->setLayout(layout); - connect(button, SIGNAL(clicked()), colorDialog, SLOT(close())); + connect(button, &QPushButton::clicked, m_colorDialog, &QDialog::close); } - colorDialog->show(); + m_colorDialog->show(); } void Player::clearHistogram() { - QMetaObject::invokeMethod(videoHistogram, "processFrame", Qt::QueuedConnection, Q_ARG(QVideoFrame, QVideoFrame())); - QMetaObject::invokeMethod(audioHistogram, "processBuffer", Qt::QueuedConnection, Q_ARG(QAudioBuffer, QAudioBuffer())); + QMetaObject::invokeMethod(m_videoHistogram, "processFrame", Qt::QueuedConnection, Q_ARG(QVideoFrame, QVideoFrame())); + QMetaObject::invokeMethod(m_audioHistogram, "processBuffer", Qt::QueuedConnection, Q_ARG(QAudioBuffer, QAudioBuffer())); } |