src/tools/circletool.cpp

Mon, 16 May 2022 01:40:49 +0300

author
Teemu Piippo <teemu@hecknology.net>
date
Mon, 16 May 2022 01:40:49 +0300
changeset 187
30204975694a
parent 186
922662adb72a
child 188
64ea7282611e
permissions
-rw-r--r--

work on circle tool

#include "circletool.h"

CircleTool::CircleTool(Document *document) :
	AbstractDrawTool{document}
{
}

QString CircleTool::name() const
{
	return tr("Circle");
}

QString CircleTool::toolTip() const
{
	return tr("Draw circular primitives like circles or discs");
}

std::vector<glm::vec3> circle(int divisions)
{
	std::vector<glm::vec3> points;
	points.reserve(divisions + 1);
	for (int i = 0; i <= divisions; ++i) {
		float ang = i * 2.0f * glm::pi<float>() / divisions;
		points.push_back({std::sin(ang), std::cos(ang), 0.0f});
	}
	return points;
}

void CircleTool::overpaint(Canvas *canvas, QPainter *painter) const
{
	if (this->previewPolygon.size() >= 2)
	{
		for (int i : {0, 1}) {
			canvas->drawWorldPoint(painter, this->previewPolygon[i]);
		}
		painter->setPen(QPen{Qt::green, 2, Qt::DashLine, Qt::RoundCap, Qt::MiterJoin});
		canvas->drawWorldPolyline(painter, {this->previewPolygon[0], this->previewPolygon[1]});
		const float size = glm::distance(this->previewPolygon[1], this->previewPolygon[0]);
		glm::mat4 matrix = size * canvas->getGridMatrix();
		matrix[3] = {this->previewPolygon[0], 1};
		std::vector<glm::vec3> points = circle(16);
		for (std::size_t i = 0; i < points.size(); ++i) {
			points[i] = matrix * glm::vec4{points[i], 1.0f};
		}
		painter->setPen(QPen{Qt::black, 2, Qt::DashLine, Qt::RoundCap, Qt::MiterJoin});
		canvas->drawWorldPolyline(painter, points);
	}
}

QString CircleTool::iconName() const
{
	return ":/icons/linetype-circularprimitive.png";
}

void CircleTool::closeShape()
{
	
}

mercurial