File indexing completed on 2024-05-19 05:51:04

0001 /* Atelier KDE Printer Host for 3D Printing
0002     Copyright (C) <2017>
0003     Author: Lays Rodrigues - lays.rodrigues@kde.org
0004 
0005     This program is free software: you can redistribute it and/or modify
0006     it under the terms of the GNU General Public License as published by
0007     the Free Software Foundation, either version 3 of the License, or
0008     (at your option) any later version.
0009 
0010     This program is distributed in the hope that it will be useful,
0011     but WITHOUT ANY WARRANTY; without even the implied warranty of
0012     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0013     GNU General Public License for more details.
0014 
0015     You should have received a copy of the GNU General Public License
0016     along with this program.  If not, see <http://www.gnu.org/licenses/>.
0017 */
0018 #include "videomonitorwidget.h"
0019 #include <KLocalizedString>
0020 #include <QComboBox>
0021 #include <QDir>
0022 #include <QGridLayout>
0023 #include <QPushButton>
0024 #include <QVideoWidget>
0025 
0026 VideoMonitorWidget::VideoMonitorWidget(QWidget *parent)
0027     : QWidget(parent)
0028     , _errorlabel(new QLabel(this))
0029     , _mediaplayer(this, QMediaPlayer::StreamPlayback)
0030 {
0031     auto _layout = new QGridLayout();
0032     auto _label = new QLabel(i18n("Source url:"), this);
0033     _layout->addWidget(_label, 0, 0);
0034 
0035     auto _sourceCB = new QComboBox(this);
0036     _sourceCB->setEditable(true);
0037     _sourceCB->setToolTip(
0038         i18n("Valid Urls:\n\
0039         http://www.example.com/stream.avi\n\
0040         rtp://@:1234\n\
0041         mms://mms.examples.com/stream.asx\n\
0042         rtsp://server.example.org:8080/test.sdp"));
0043     _layout->addWidget(_sourceCB, 0, 1);
0044 
0045     auto _playPB = new QPushButton(this);
0046     _playPB->setCheckable(true);
0047     _playPB->setIcon(QIcon::fromTheme("media-playback-start", style()->standardIcon(QStyle::SP_MediaPlay)));
0048     _layout->addWidget(_playPB, 0, 2);
0049 
0050     auto _videoWidget = new QVideoWidget(this);
0051     _layout->addWidget(_videoWidget, 1, 0, -1, -1);
0052 
0053     _layout->addWidget(_errorlabel, 2, 0, 0, -1);
0054 
0055     this->setLayout(_layout);
0056 
0057     _mediaplayer.setVideoOutput(_videoWidget);
0058 
0059 #ifdef Q_OS_LINUX
0060     QStringList sources;
0061     sources << QString("video*");
0062     _sourceCB->addItems(QDir("/dev/").entryList(sources, QDir::System).replaceInStrings(QRegExp("^"), "v4l2:///dev/"));
0063 #endif
0064 
0065     connect(_playPB, &QPushButton::clicked, this, [this, _playPB, _sourceCB, _videoWidget](bool b) {
0066         if (b) {
0067             if (_mediaplayer.state() != QMediaPlayer::PausedState) {
0068                 QString source = _sourceCB->currentText();
0069                 _mediaplayer.setMedia(QUrl(source));
0070             }
0071             _playPB->setIcon(QIcon::fromTheme("media-playback-pause", style()->standardIcon(QStyle::SP_MediaPause)));
0072             _mediaplayer.play();
0073         } else {
0074             _mediaplayer.pause();
0075             _playPB->setIcon(QIcon::fromTheme("media-playback-start", style()->standardIcon(QStyle::SP_MediaPlay)));
0076         }
0077         _videoWidget->setVisible(b);
0078     });
0079     using ErrorSignal = void (QMediaPlayer::*)(QMediaPlayer::Error);
0080     connect(&_mediaplayer, static_cast<ErrorSignal>(&QMediaPlayer::error), this, &VideoMonitorWidget::handleError);
0081 }
0082 
0083 void VideoMonitorWidget::handleError()
0084 {
0085     const QString errorString = _mediaplayer.errorString();
0086     QString message = "Error: ";
0087     if (errorString.isEmpty()) {
0088         message += " #" + QString::number(int(_mediaplayer.error()));
0089     } else {
0090         message += errorString;
0091     }
0092     _errorlabel->setText(message);
0093 }