<commit_before>#include "qniteline.h"
#include "qnitefillnode.h"
#include "qniteaxes.h"
#include "qniteaxis.h"

#include <QDebug>

#include <QSGGeometry>
#include <QSGGeometryNode>
#include <QSGFlatColorMaterial>


QniteLine::QniteLine(QQuickItem *parent):
  QniteXYArtist(parent),
  m_fillNode{nullptr},
  m_fill{false},
  m_selected{false}
{
  setFlag(ItemHasContents, true);
}

QniteLine::~QniteLine()
{
}

void QniteLine::setFill(bool fill)
{
  if (m_fill != fill) {
    m_fill = fill;
    emit fillChanged();
  }
}

void QniteLine::setFillColor(QColor fillColor)
{
  if (m_fillColor != fillColor) {
    m_fillColor = fillColor;
    emit fillColorChanged();
  }
}

QSGNode* QniteLine::updatePaintNode(QSGNode* oldNode, UpdatePaintNodeData*)
{
  QSGGeometryNode *node = 0;
  QSGGeometry *geometry = 0;

  // TODO: processdata should be triggered only when data changes
  processData();
  int dataSize = xMapped().size();

  // TODO: handle selection highlight in a proper way
  QSGFlatColorMaterial * material = new QSGFlatColorMaterial;
  if (selected()) {
    material->setColor(color().lighter(200));
  }
  else {
    material->setColor(color());
  }

  if (!oldNode) {
    node = new QSGGeometryNode;
    geometry = new QSGGeometry(QSGGeometry::defaultAttributes_Point2D(), dataSize);
    geometry->setLineWidth(2);
    geometry->setDrawingMode(GL_LINE_STRIP);
    node->setGeometry(geometry);
    node->setFlag(QSGNode::OwnsGeometry);
    node->setFlag(QSGNode::OwnsMaterial);
  } else {
    node = static_cast<QSGGeometryNode *>(oldNode);
    geometry = node->geometry();
    geometry->allocate(dataSize);
  }
  node->setMaterial(material);

  if (m_fill) {
    if (m_fillNode == nullptr) {
      m_fillNode = new QniteFillNode(QColor(m_fillColor));
      node->appendChildNode(m_fillNode);
    }

    qreal ya = axes()->leftAxis()->position();
    m_fillNode->updateGeometry(xMapped(), yMapped(), ya);
  }

  QSGGeometry::Point2D *vertices = geometry->vertexDataAsPoint2D();
  for(int i = 0; i < dataSize; ++i) {
    vertices[i].set(xMapped().at(i), yMapped().at(i));
  }

  node->markDirty(QSGNode::DirtyGeometry | QSGNode::DirtyMaterial);
  return node;
}

/*!
  Temporary implementation of the selection logic for lines.

  TODO: fix the implementation
  TODO: can we assume data is alwayws already mapped at this point?
 */
bool QniteLine::select(QPoint p)
{
  bool accepted = false;

  // get the distance from the first point on the path
  int dataSize = xMapped().size();
  for(int i = 0; i < dataSize; ++i) {
    QPoint cp(xMapped().at(i), yMapped().at(i));
    QPoint d = p - cp;
    if (d.manhattanLength() < 10) {
      m_selected = true;
      accepted = true;
      axes()->setOnTop(this);
      update();
      break;
    }
  }

  return accepted;
}

bool QniteLine::select(const QList<QPoint>& path)
{
  if (path.size()) {
    return select(path.first());
  }

  return false;
}

void QniteLine::clearSelection()<commit_msg>define tolerance for selection<commit_after>#include "qniteline.h"
#include "qnitefillnode.h"
#include "qniteaxes.h"
#include "qniteaxis.h"

#include <QDebug>

#include <QSGGeometry>
#include <QSGGeometryNode>
#include <QSGFlatColorMaterial>


#define SELECTION_TOLERANCE 25


QniteLine::QniteLine(QQuickItem *parent):
  QniteXYArtist(parent),
  m_fillNode{nullptr},
  m_fill{false},
  m_selected{false}
{
  setFlag(ItemHasContents, true);
}

QniteLine::~QniteLine()
{
}

void QniteLine::setFill(bool fill)
{
  if (m_fill != fill) {
    m_fill = fill;
    emit fillChanged();
  }
}

void QniteLine::setFillColor(QColor fillColor)
{
  if (m_fillColor != fillColor) {
    m_fillColor = fillColor;
    emit fillColorChanged();
  }
}

QSGNode* QniteLine::updatePaintNode(QSGNode* oldNode, UpdatePaintNodeData*)
{
  QSGGeometryNode *node = 0;
  QSGGeometry *geometry = 0;

  // TODO: processdata should be triggered only when data changes
  processData();
  int dataSize = xMapped().size();

  // TODO: handle selection highlight in a proper way
  QSGFlatColorMaterial * material = new QSGFlatColorMaterial;
  if (selected()) {
    material->setColor(color().lighter(200));
  }
  else {
    material->setColor(color());
  }

  if (!oldNode) {
    node = new QSGGeometryNode;
    geometry = new QSGGeometry(QSGGeometry::defaultAttributes_Point2D(), dataSize);
    geometry->setLineWidth(2);
    geometry->setDrawingMode(GL_LINE_STRIP);
    node->setGeometry(geometry);
    node->setFlag(QSGNode::OwnsGeometry);
    node->setFlag(QSGNode::OwnsMaterial);
  } else {
    node = static_cast<QSGGeometryNode *>(oldNode);
    geometry = node->geometry();
    geometry->allocate(dataSize);
  }
  node->setMaterial(material);

  if (m_fill) {
    if (m_fillNode == nullptr) {
      m_fillNode = new QniteFillNode(QColor(m_fillColor));
      node->appendChildNode(m_fillNode);
    }

    qreal ya = axes()->leftAxis()->position();
    m_fillNode->updateGeometry(xMapped(), yMapped(), ya);
  }

  QSGGeometry::Point2D *vertices = geometry->vertexDataAsPoint2D();
  for(int i = 0; i < dataSize; ++i) {
    vertices[i].set(xMapped().at(i), yMapped().at(i));
  }

  node->markDirty(QSGNode::DirtyGeometry | QSGNode::DirtyMaterial);
  return node;
}

/*!
  Temporary implementation of the selection logic for lines.

  TODO: fix the implementation
  TODO: can we assume data is alwayws already mapped at this point?
 */
bool QniteLine::select(QPoint p)
{
  bool accepted = false;

  // get the distance from the first point on the path
  int dataSize = xMapped().size();
  for(int i = 0; i < dataSize; ++i) {
    QPoint cp(xMapped().at(i), yMapped().at(i));
    QPoint d = p - cp;
    if (d.manhattanLength() < SELECTION_TOLERANCE) {
      m_selected = true;
      accepted = true;
      axes()->setOnTop(this);
      update();
      break;
    }
  }

  return accepted;
}

bool QniteLine::select(const QList<QPoint>& path)
{
  if (path.size()) {
    return select(path.first());
  }

  return false;
}

void QniteLine::clearSelection()
{'fraction_non_alphanumeric': 0.14098910310142498, 'fraction_numerical': 0.0043587594300083824, 'mean_word_length': 4.060220525869381, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 49, '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': '1061106', 'n_tokens_mistral': 2135, 'n_tokens_neox': 2109, 'n_words': 439}