#include "VideoItem.h"

#include <QPainter>

VideoItem::VideoItem(QQuickItem *parent)
   : QQuickPaintedItem(parent)
{
   setAntialiasing(false);
   setOpaquePainting(true);
}

void VideoItem::paint(QPainter *painter)
{
   painter->fillRect(boundingRect(), Qt::black);

   if (m_frame.isNull()) {
      return;
   }

   QSizeF targetSize = m_frame.size();
   targetSize.scale(boundingRect().size(), Qt::KeepAspectRatio);

   const QRectF targetRect(
      (width() - targetSize.width()) / 2.0,
      (height() - targetSize.height()) / 2.0,
      targetSize.width(),
      targetSize.height()
   );

   painter->drawImage(targetRect, m_frame);
}

void VideoItem::setFrame(const QImage &frame)
{
   m_frame = frame;
   update();
}

void VideoItem::clearFrame()
{
   if (m_frame.isNull()) {
      return;
   }

   m_frame = QImage();
   update();
}
