<commit_before>#include "videodrawhelper.h"

#include "common.h"

#include <QDebug>
#include <QWidget>
#include <QKeyEvent>

const uint16_t VIEWBUFFERSIZE = 5;

VideoDrawHelper::VideoDrawHelper(uint32_t sessionID, uint32_t index, uint8_t borderSize):
  sessionID_(sessionID),
  index_(index),
  tmpParent_(nullptr),
  firstImageReceived_(false),
  previousSize_(QSize(0,0)),
  borderSize_(borderSize)
{}

VideoDrawHelper::~VideoDrawHelper()
{
  viewBuffer_.clear();
  dataBuffer_.clear();
}

void VideoDrawHelper::initWidget(QWidget* widget)
{
  widget->setAutoFillBackground(false);
  widget->setAttribute(Qt::WA_NoSystemBackground, true);

  QPalette palette = widget->palette();
  palette.setColor(QPalette::Background, Qt::black);
  widget->setPalette(palette);

  widget->setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::MinimumExpanding);
  //widget->showFullScreen();
  widget->setWindowState(Qt::WindowFullScreen);
  widget->setUpdatesEnabled(true);
}

bool VideoDrawHelper::readyToDraw()
{
  return firstImageReceived_;
}

void VideoDrawHelper::inputImage(QWidget* widget, std::unique_ptr<uchar[]> data, QImage &image)
{
  if(!firstImageReceived_)
  {
    lastImage_ = image;
    lastImageData_ = std::move(data);
    firstImageReceived_ = true;
    updateTargetRect(widget);
  }
  else
  {
    if(previousSize_ != image.size())
    {
      qDebug() << "Drawing," << metaObject()->className() << ": Video widget needs to update its target rectangle because of resolution change.";
      viewBuffer_.clear();
      dataBuffer_.clear();

      viewBuffer_.push_front(image);
      dataBuffer_.push_front(std::move(data));

      updateTargetRect(widget);
    }
    else
    {
      viewBuffer_.push_front(image);
      dataBuffer_.push_front(std::move(data));
    }

    // delete oldes image if there is too much buffer
    if(viewBuffer_.size() > VIEWBUFFERSIZE)
    {
      printWarning(this, "Buffer full when inputting image",
                  {"SessionID"}, QString::number(sessionID_));

      viewBuffer_.pop_back();
      dataBuffer_.pop_back();

      //setUpdatesEnabled(true);

      //stats_->packetDropped("view" + QString::number(sessionID_));
    }
  }
}

bool VideoDrawHelper::getRecentImage(QImage& image)
{
  Q_ASSERT(readyToDraw());
  if(readyToDraw())
  {
    if(!viewBuffer_.empty())
    {
      image = viewBuffer_.back();

      lastImage_ = viewBuffer_.back();
      lastImageData_ = std::move(dataBuffer_.back());<commit_msg>refactor(GUI): Disable debug warning for buffer if not active.<commit_after>#include "videodrawhelper.h"

#include "common.h"

#include <QDebug>
#include <QWidget>
#include <QKeyEvent>

const uint16_t VIEWBUFFERSIZE = 5;

VideoDrawHelper::VideoDrawHelper(uint32_t sessionID, uint32_t index, uint8_t borderSize):
  sessionID_(sessionID),
  index_(index),
  tmpParent_(nullptr),
  firstImageReceived_(false),
  previousSize_(QSize(0,0)),
  borderSize_(borderSize)
{}

VideoDrawHelper::~VideoDrawHelper()
{
  viewBuffer_.clear();
  dataBuffer_.clear();
}

void VideoDrawHelper::initWidget(QWidget* widget)
{
  widget->setAutoFillBackground(false);
  widget->setAttribute(Qt::WA_NoSystemBackground, true);

  QPalette palette = widget->palette();
  palette.setColor(QPalette::Background, Qt::black);
  widget->setPalette(palette);

  widget->setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::MinimumExpanding);
  //widget->showFullScreen();
  widget->setWindowState(Qt::WindowFullScreen);
  widget->setUpdatesEnabled(true);
}

bool VideoDrawHelper::readyToDraw()
{
  return firstImageReceived_;
}

void VideoDrawHelper::inputImage(QWidget* widget, std::unique_ptr<uchar[]> data, QImage &image)
{
  if(!firstImageReceived_)
  {
    lastImage_ = image;
    lastImageData_ = std::move(data);
    firstImageReceived_ = true;
    updateTargetRect(widget);
  }
  else
  {
    if(previousSize_ != image.size())
    {
      qDebug() << "Drawing," << metaObject()->className() << ": Video widget needs to update its target rectangle because of resolution change.";
      viewBuffer_.clear();
      dataBuffer_.clear();

      viewBuffer_.push_front(image);
      dataBuffer_.push_front(std::move(data));

      updateTargetRect(widget);
    }
    else
    {
      viewBuffer_.push_front(image);
      dataBuffer_.push_front(std::move(data));
    }

    // delete oldes image if there is too much buffer
    if(viewBuffer_.size() > VIEWBUFFERSIZE)
    {
      if ( widget->isVisible() &&
           widget->isActiveWindow() &&
          !widget->isHidden() &&
          !widget->isMinimized())
      {
        printWarning(this, "Buffer full when inputting image",
                   {"Buffer"}, QString::number(viewBuffer_.size()) + "/"
                     + QString::number(VIEWBUFFERSIZE));
      }

      viewBuffer_.pop_back();
      dataBuffer_.pop_back();

      //setUpdatesEnabled(true);

      //stats_->packetDropped("view" + QString::number(sessionID_));
    }
  }
}

bool VideoDrawHelper::getRecentImage(QImage& image)
{
  Q_ASSERT(readyToDraw());
  if(readyToDraw())
  {
    if(!viewBuffer_.empty())
    {
      image = viewBuffer_.back();

      lastImage_ = viewBuffer_.back();
      lastImageData_ = std::move(dataBuffer_.back());
{'fraction_non_alphanumeric': 0.13934740882917465, 'fraction_numerical': 0.003838771593090211, 'mean_word_length': 4.169642857142857, 'pattern_counts': {'":': 2, '<': 23, '<?xml version=': 0, '>': 37, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4015920', 'n_tokens_mistral': 1647, 'n_tokens_neox': 1611, 'n_words': 267}