<commit_before>#include "circlepalette.h"

#include <QEvent>
#include <QPainter>
#include <QWidget>
#include <QLabel>
#include <QMouseEvent>
#include <QDebug>
#include <QtMath>
#include <QFileDialog>

#include <algorithm>

#define LIGHTING_ICON_MIDDLE_TO_TOP 7

CirclePalette::CirclePalette(QWidget *parent) : QWidget(parent)
{
	lightingPic = QPixmap(QString::fromUtf8(":/main/graphics/LightingIcon.png"));

	circlePic = QPixmap(QString::fromUtf8(":/main/graphics/PrimaryHandle.png"));
	primaryRadius = (double)circlePic.width() / 2.0;

	gamutShape = PrestoPalette::GamutShapeNone;
	wheelShape = PrestoPalette::WheelShapeCourse;

	backgroundWheel = new QLabel(parent);
	backgroundWheel->setGeometry(QRect(44, 37, 549, 549));
	backgroundWheel->setPixmap(QPixmap(QString::fromUtf8(":/main/graphics/Wheel_BG.png")));

	colorWheel = new QLabel(parent);
	colorWheel->setGeometry(QRect(70, 62, 499, 499));
	colorWheel->setPixmap(QPixmap(QString::fromUtf8(":/main/graphics/YWheel_Course.png")));
	colorWheel->raise();
	//colorWheel->installEventFilter(this);
	//colorWheel->setMouseTracking(true);

	lighting = new QPoint(325, 40);

	drawnElements = new QWidget(parent);
	drawnElements->setGeometry(colorWheel->geometry());
	drawnElements->raise();
	drawnElements->installEventFilter(this);

	//drawnElements->setMouseTracking(true);

	setMouseTracking(true);

	installEventFilter(this);

	QMetaObject::connectSlotsByName(this);
}

void CirclePalette::_draw_primary_imp(QPainter &painter, QVector<QColor> *colors, QLabel *colorWheel, const QPoint &p, int circleRadius)
{
	QPoint p_center(p.x() - circleRadius, p.y() - circleRadius);

	painter.drawPixmap(p_center, circlePic);

	QColor color = colorWheel->pixmap()->toImage().pixelColor(p.x(), p.y());
	colors->append(color);
}

void CirclePalette::_draw_line_imp(QPainter &painter, QVector<QColor> *colors, QLabel *colorWheel, const QPoint &p1, const QPoint &p2, int circleRadius)
{
	// TODO Trim the lines, so they don't go into the circle

	QPen linePen(Qt::white);
	linePen.setWidth(3);
	painter.setPen(linePen);

	//TODO use the radius (9px) of the circles, then compute

	painter.drawLine(p1, p2);

	QPoint midpoint((p1.x() + p2.x()) / 2, (p1.y() + p2.y()) / 2);

	/* TODO use the image for these... */
	painter.setPen(QPen(Qt::white, 3));
	painter.drawEllipse(midpoint, circleRadius, circleRadius);

	QColor color = colorWheel->pixmap()->toImage().pixelColor(midpoint.x(), midpoint.y());
	colors->append(color);
}

void CirclePalette::_draw_centroid(QPainter &painter, QVector<QColor> *colors, QLabel *colorWheel, std::vector<QPoint*> &points, int circleRadius)
{
	QPoint centroid;

	for (auto p : points)
	{
		centroid += *p;
	}
	centroid /= points.size();

	// shift over the centroid (because the above is using top-left)
	centroid = QPoint(centroid.x() - circleRadius, centroid.y() - circleRadius);
	_draw_primary_imp(painter, colors, colorWheel, centroid, circleRadius);
}

struct tup
{
	QPoint *point;
	double angle; // in radians
};

bool sort_angles (struct tup i, struct tup j) { return (i.angle <= j.angle); }

bool CirclePalette::eventFilter(QObject* watched, QEvent* event)
{
	if (event->type() == QEvent::MouseMove && (watched == this))
	{
		const QMouseEvent* me = static_cast<const QMouseEvent*>(event);

		QPoint p = colorWheel->mapFromGlobal(QCursor::pos());
		QPoint wheelCenter = QPoint(colorWheel->width() / 2, colorWheel->height() / 2);
		int radius = colorWheel->width() / 2;

		// prevent it from going off the edge of the whole circle palette
		if (isDragging)
		{
			if (_is_collision(wheelCenter, radius - primaryRadius, p) == false)
			{
				// end it
				return true;
			}
		}

		if (_is_collision(wheelCenter, radius, p))
		{
			QColor color = colorWheel->pixmap()->toImage().pixelColor(p.x(), p.y());

			emit hoverColor(color);
		}
	}

	if (watched == drawnElements && event->type() == QEvent::Paint)
	{
		QPainter painter(drawnElements);

		QVector<QColor> colors;

		std::vector<QPoint*> sortedPoints;

		QPoint center = QPoint(drawnElements->width() / 2.0, drawnElements->height() / 2.0);

		/* draw the lighting icon */
		if(enableLighting)
		{
			painter.drawPixmap(QPoint(lighting->x() - (lightingPic.width() / 2),
						  lighting->y() - (lightingPic.height() / 2) + LIGHTING_ICON_MIDDLE_TO_TOP),
						  lightingPic);

			QColor lightingColor = colorWheel->pixmap()->toImage().pixelColor(lighting->x(), lighting->y());

			if (this->lightingColor != lightingColor)
			{
				this->lightingColor = lightingColor;

				// notify that colors changed
				emit lightingColorChanged(lightingColor);
			}
		}

		std::list<struct tup> intermediaryPoints;

		for (auto p : this->points)
		{
			auto t = QPoint(p->x(), p->y());
			t = t - center;<commit_msg>anti-aliased the lines in the palette<commit_after>#include "circlepalette.h"

#include <QEvent>
#include <QPainter>
#include <QWidget>
#include <QLabel>
#include <QMouseEvent>
#include <QDebug>
#include <QtMath>
#include <QFileDialog>

#include <algorithm>

#define LIGHTING_ICON_MIDDLE_TO_TOP 7

CirclePalette::CirclePalette(QWidget *parent) : QWidget(parent)
{
	lightingPic = QPixmap(QString::fromUtf8(":/main/graphics/LightingIcon.png"));

	circlePic = QPixmap(QString::fromUtf8(":/main/graphics/PrimaryHandle.png"));
	primaryRadius = (double)circlePic.width() / 2.0;

	gamutShape = PrestoPalette::GamutShapeNone;
	wheelShape = PrestoPalette::WheelShapeCourse;

	backgroundWheel = new QLabel(parent);
	backgroundWheel->setGeometry(QRect(44, 37, 549, 549));
	backgroundWheel->setPixmap(QPixmap(QString::fromUtf8(":/main/graphics/Wheel_BG.png")));

	colorWheel = new QLabel(parent);
	colorWheel->setGeometry(QRect(70, 62, 499, 499));
	colorWheel->setPixmap(QPixmap(QString::fromUtf8(":/main/graphics/YWheel_Course.png")));
	colorWheel->raise();
	//colorWheel->installEventFilter(this);
	//colorWheel->setMouseTracking(true);

	lighting = new QPoint(325, 40);

	drawnElements = new QWidget(parent);
	drawnElements->setGeometry(colorWheel->geometry());
	drawnElements->raise();
	drawnElements->installEventFilter(this);

	//drawnElements->setMouseTracking(true);

	setMouseTracking(true);

	installEventFilter(this);

	QMetaObject::connectSlotsByName(this);
}

void CirclePalette::_draw_primary_imp(QPainter &painter, QVector<QColor> *colors, QLabel *colorWheel, const QPoint &p, int circleRadius)
{
	QPoint p_center(p.x() - circleRadius, p.y() - circleRadius);

	painter.drawPixmap(p_center, circlePic);

	QColor color = colorWheel->pixmap()->toImage().pixelColor(p.x(), p.y());
	colors->append(color);
}

void CirclePalette::_draw_line_imp(QPainter &painter, QVector<QColor> *colors, QLabel *colorWheel, const QPoint &p1, const QPoint &p2, int circleRadius)
{
	// TODO Trim the lines, so they don't go into the circle

	QPen linePen(Qt::white);
	linePen.setWidth(3);
	painter.setPen(linePen);

	//TODO use the radius (9px) of the circles, then compute

	painter.drawLine(p1, p2);

	QPoint midpoint((p1.x() + p2.x()) / 2, (p1.y() + p2.y()) / 2);

	/* TODO use the image for these... */
	painter.setPen(QPen(Qt::white, 3));
	painter.drawEllipse(midpoint, circleRadius, circleRadius);

	QColor color = colorWheel->pixmap()->toImage().pixelColor(midpoint.x(), midpoint.y());
	colors->append(color);
}

void CirclePalette::_draw_centroid(QPainter &painter, QVector<QColor> *colors, QLabel *colorWheel, std::vector<QPoint*> &points, int circleRadius)
{
	QPoint centroid;

	for (auto p : points)
	{
		centroid += *p;
	}
	centroid /= points.size();

	// shift over the centroid (because the above is using top-left)
	centroid = QPoint(centroid.x() - circleRadius, centroid.y() - circleRadius);
	_draw_primary_imp(painter, colors, colorWheel, centroid, circleRadius);
}

struct tup
{
	QPoint *point;
	double angle; // in radians
};

bool sort_angles (struct tup i, struct tup j) { return (i.angle <= j.angle); }

bool CirclePalette::eventFilter(QObject* watched, QEvent* event)
{
	if (event->type() == QEvent::MouseMove && (watched == this))
	{
		const QMouseEvent* me = static_cast<const QMouseEvent*>(event);

		QPoint p = colorWheel->mapFromGlobal(QCursor::pos());
		QPoint wheelCenter = QPoint(colorWheel->width() / 2, colorWheel->height() / 2);
		int radius = colorWheel->width() / 2;

		// prevent it from going off the edge of the whole circle palette
		if (isDragging)
		{
			if (_is_collision(wheelCenter, radius - primaryRadius, p) == false)
			{
				// end it
				return true;
			}
		}

		if (_is_collision(wheelCenter, radius, p))
		{
			QColor color = colorWheel->pixmap()->toImage().pixelColor(p.x(), p.y());

			emit hoverColor(color);
		}
	}

	if (watched == drawnElements && event->type() == QEvent::Paint)
	{
		QPainter painter(drawnElements);
		painter.setRenderHint(QPainter::HighQualityAntialiasing, true);

		QVector<QColor> colors;

		std::vector<QPoint*> sortedPoints;

		QPoint center = QPoint(drawnElements->width() / 2.0, drawnElements->height() / 2.0);

		/* draw the lighting icon */
		if(enableLighting)
		{
			painter.drawPixmap(QPoint(lighting->x() - (lightingPic.width() / 2),
						  lighting->y() - (lightingPic.height() / 2) + LIGHTING_ICON_MIDDLE_TO_TOP),
						  lightingPic);

			QColor lightingColor = colorWheel->pixmap()->toImage().pixelColor(lighting->x(), lighting->y());

			if (this->lightingColor != lightingColor)
			{
				this->lightingColor = lightingColor;

				// notify that colors changed
				emit lightingColorChanged(lightingColor);
			}
		}

		std::list<struct tup> intermediaryPoints;

		for (auto p : this->points)
		{
			auto t = QPoint(p->x(), p->y());
			t = t - center;
{'fraction_non_alphanumeric': 0.15627615062761507, 'fraction_numerical': 0.011297071129707114, 'mean_word_length': 5.0207808564231735, 'pattern_counts': {'":': 8, '<': 39, '<?xml version=': 0, '>': 115, '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': '3050943', 'n_tokens_mistral': 3734, 'n_tokens_neox': 3373, 'n_words': 686}