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

186
922662adb72a work on circle tool
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
1 #include "circletool.h"
922662adb72a work on circle tool
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
2
922662adb72a work on circle tool
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
3 CircleTool::CircleTool(Document *document) :
922662adb72a work on circle tool
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
4 AbstractDrawTool{document}
922662adb72a work on circle tool
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
5 {
922662adb72a work on circle tool
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
6 }
922662adb72a work on circle tool
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
7
922662adb72a work on circle tool
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
8 QString CircleTool::name() const
922662adb72a work on circle tool
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
9 {
922662adb72a work on circle tool
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
10 return tr("Circle");
922662adb72a work on circle tool
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
11 }
922662adb72a work on circle tool
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
12
922662adb72a work on circle tool
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
13 QString CircleTool::toolTip() const
922662adb72a work on circle tool
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
14 {
922662adb72a work on circle tool
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
15 return tr("Draw circular primitives like circles or discs");
922662adb72a work on circle tool
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
16 }
922662adb72a work on circle tool
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
17
187
30204975694a work on circle tool
Teemu Piippo <teemu@hecknology.net>
parents: 186
diff changeset
18 std::vector<glm::vec3> circle(int divisions)
30204975694a work on circle tool
Teemu Piippo <teemu@hecknology.net>
parents: 186
diff changeset
19 {
30204975694a work on circle tool
Teemu Piippo <teemu@hecknology.net>
parents: 186
diff changeset
20 std::vector<glm::vec3> points;
30204975694a work on circle tool
Teemu Piippo <teemu@hecknology.net>
parents: 186
diff changeset
21 points.reserve(divisions + 1);
30204975694a work on circle tool
Teemu Piippo <teemu@hecknology.net>
parents: 186
diff changeset
22 for (int i = 0; i <= divisions; ++i) {
30204975694a work on circle tool
Teemu Piippo <teemu@hecknology.net>
parents: 186
diff changeset
23 float ang = i * 2.0f * glm::pi<float>() / divisions;
30204975694a work on circle tool
Teemu Piippo <teemu@hecknology.net>
parents: 186
diff changeset
24 points.push_back({std::sin(ang), std::cos(ang), 0.0f});
30204975694a work on circle tool
Teemu Piippo <teemu@hecknology.net>
parents: 186
diff changeset
25 }
30204975694a work on circle tool
Teemu Piippo <teemu@hecknology.net>
parents: 186
diff changeset
26 return points;
30204975694a work on circle tool
Teemu Piippo <teemu@hecknology.net>
parents: 186
diff changeset
27 }
30204975694a work on circle tool
Teemu Piippo <teemu@hecknology.net>
parents: 186
diff changeset
28
186
922662adb72a work on circle tool
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
29 void CircleTool::overpaint(Canvas *canvas, QPainter *painter) const
922662adb72a work on circle tool
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
30 {
922662adb72a work on circle tool
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
31 if (this->previewPolygon.size() >= 2)
922662adb72a work on circle tool
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
32 {
187
30204975694a work on circle tool
Teemu Piippo <teemu@hecknology.net>
parents: 186
diff changeset
33 for (int i : {0, 1}) {
30204975694a work on circle tool
Teemu Piippo <teemu@hecknology.net>
parents: 186
diff changeset
34 canvas->drawWorldPoint(painter, this->previewPolygon[i]);
30204975694a work on circle tool
Teemu Piippo <teemu@hecknology.net>
parents: 186
diff changeset
35 }
30204975694a work on circle tool
Teemu Piippo <teemu@hecknology.net>
parents: 186
diff changeset
36 painter->setPen(QPen{Qt::green, 2, Qt::DashLine, Qt::RoundCap, Qt::MiterJoin});
30204975694a work on circle tool
Teemu Piippo <teemu@hecknology.net>
parents: 186
diff changeset
37 canvas->drawWorldPolyline(painter, {this->previewPolygon[0], this->previewPolygon[1]});
30204975694a work on circle tool
Teemu Piippo <teemu@hecknology.net>
parents: 186
diff changeset
38 const float size = glm::distance(this->previewPolygon[1], this->previewPolygon[0]);
30204975694a work on circle tool
Teemu Piippo <teemu@hecknology.net>
parents: 186
diff changeset
39 glm::mat4 matrix = size * canvas->getGridMatrix();
30204975694a work on circle tool
Teemu Piippo <teemu@hecknology.net>
parents: 186
diff changeset
40 matrix[3] = {this->previewPolygon[0], 1};
30204975694a work on circle tool
Teemu Piippo <teemu@hecknology.net>
parents: 186
diff changeset
41 std::vector<glm::vec3> points = circle(16);
30204975694a work on circle tool
Teemu Piippo <teemu@hecknology.net>
parents: 186
diff changeset
42 for (std::size_t i = 0; i < points.size(); ++i) {
30204975694a work on circle tool
Teemu Piippo <teemu@hecknology.net>
parents: 186
diff changeset
43 points[i] = matrix * glm::vec4{points[i], 1.0f};
30204975694a work on circle tool
Teemu Piippo <teemu@hecknology.net>
parents: 186
diff changeset
44 }
30204975694a work on circle tool
Teemu Piippo <teemu@hecknology.net>
parents: 186
diff changeset
45 painter->setPen(QPen{Qt::black, 2, Qt::DashLine, Qt::RoundCap, Qt::MiterJoin});
30204975694a work on circle tool
Teemu Piippo <teemu@hecknology.net>
parents: 186
diff changeset
46 canvas->drawWorldPolyline(painter, points);
186
922662adb72a work on circle tool
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
47 }
922662adb72a work on circle tool
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
48 }
922662adb72a work on circle tool
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
49
922662adb72a work on circle tool
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
50 QString CircleTool::iconName() const
922662adb72a work on circle tool
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
51 {
922662adb72a work on circle tool
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
52 return ":/icons/linetype-circularprimitive.png";
922662adb72a work on circle tool
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
53 }
922662adb72a work on circle tool
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
54
922662adb72a work on circle tool
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
55 void CircleTool::closeShape()
922662adb72a work on circle tool
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
56 {
922662adb72a work on circle tool
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
57
922662adb72a work on circle tool
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
58 }

mercurial