videoitem.cpp Example File
multimedia/videographicsitem/videoitem.cpp
#include "videoitem.h"
#include <QtMultimedia>
VideoItem::VideoItem(QGraphicsItem *parent)
: QGraphicsItem(parent)
, imageFormat(QImage::Format_Invalid)
, framePainted(false)
{
}
VideoItem::~VideoItem()
{
}
QRectF VideoItem::boundingRect() const
{
return QRectF(QPointF(0,0), surfaceFormat().sizeHint());
}
void VideoItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
Q_UNUSED(option);
Q_UNUSED(widget);
if (currentFrame.map(QAbstractVideoBuffer::ReadOnly)) {
const QTransform oldTransform = painter->transform();
if (surfaceFormat().scanLineDirection() == QVideoSurfaceFormat::BottomToTop) {
painter->scale(1, -1);
painter->translate(0, -boundingRect().height());
}
painter->drawImage(boundingRect(), QImage(
currentFrame.bits(),
imageSize.width(),
imageSize.height(),
imageFormat));
painter->setTransform(oldTransform);
framePainted = true;
currentFrame.unmap();
}
}
QList<QVideoFrame::PixelFormat> VideoItem::supportedPixelFormats(
QAbstractVideoBuffer::HandleType handleType) const
{
if (handleType == QAbstractVideoBuffer::NoHandle) {
return QList<QVideoFrame::PixelFormat>()
<< QVideoFrame::Format_RGB32
<< QVideoFrame::Format_ARGB32
<< QVideoFrame::Format_ARGB32_Premultiplied
<< QVideoFrame::Format_RGB565
<< QVideoFrame::Format_RGB555;
} else {
return QList<QVideoFrame::PixelFormat>();
}
}
bool VideoItem::start(const QVideoSurfaceFormat &format)
{
if (isFormatSupported(format)) {
imageFormat = QVideoFrame::imageFormatFromPixelFormat(format.pixelFormat());
imageSize = format.frameSize();
framePainted = true;
QAbstractVideoSurface::start(format);
prepareGeometryChange();
return true;
} else {
return false;
}
}
void VideoItem::stop()
{
currentFrame = QVideoFrame();
framePainted = false;
QAbstractVideoSurface::stop();
}
bool VideoItem::present(const QVideoFrame &frame)
{
if (!framePainted) {
if (!QAbstractVideoSurface::isActive())
setError(StoppedError);
return false;
} else {
currentFrame = frame;
framePainted = false;
update();
return true;
}
}