src/tools/drawtool.cpp

Sun, 25 Jul 2021 16:29:08 +0300

author
Teemu Piippo <teemu@hecknology.net>
date
Sun, 25 Jul 2021 16:29:08 +0300
changeset 110
d922431eacf7
parent 109
40a1cf2f38f5
child 111
1f42c03fafca
permissions
-rw-r--r--

stuff

103
Teemu Piippo <teemu@hecknology.net>
parents: 96
diff changeset
1 #include <QMessageBox>
96
165777a20dc7 added tool base code
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
2 #include "drawtool.h"
165777a20dc7 added tool base code
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
3
109
40a1cf2f38f5 replaced preview layers in favor of overpainting callback
Teemu Piippo <teemu@hecknology.net>
parents: 108
diff changeset
4 static const QBrush pointBrush = {Qt::white};
40a1cf2f38f5 replaced preview layers in favor of overpainting callback
Teemu Piippo <teemu@hecknology.net>
parents: 108
diff changeset
5 static const QPen polygonPen = {QBrush{Qt::black}, 2.0, Qt::DashLine};
40a1cf2f38f5 replaced preview layers in favor of overpainting callback
Teemu Piippo <teemu@hecknology.net>
parents: 108
diff changeset
6 static const QPen pointPen = {QBrush{Qt::black}, 2.0};
40a1cf2f38f5 replaced preview layers in favor of overpainting callback
Teemu Piippo <teemu@hecknology.net>
parents: 108
diff changeset
7 static const QBrush polygonBrush = {QColor{64, 255, 128, 192}};
108
94c92c923713 work on editing tools
Teemu Piippo <teemu@hecknology.net>
parents: 106
diff changeset
8
96
165777a20dc7 added tool base code
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
9 DrawTool::DrawTool(QObject* parent) :
165777a20dc7 added tool base code
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
10 BaseTool{parent} {}
165777a20dc7 added tool base code
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
11
165777a20dc7 added tool base code
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
12 QString DrawTool::name() const
165777a20dc7 added tool base code
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
13 {
165777a20dc7 added tool base code
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
14 static const QString result = tr("Draw");
165777a20dc7 added tool base code
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
15 return result;
165777a20dc7 added tool base code
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
16 }
165777a20dc7 added tool base code
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
17
165777a20dc7 added tool base code
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
18 QString DrawTool::toolTip() const
165777a20dc7 added tool base code
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
19 {
165777a20dc7 added tool base code
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
20 static const QString result = tr("Draw new elements into the model.");
165777a20dc7 added tool base code
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
21 return result;
165777a20dc7 added tool base code
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
22 }
103
Teemu Piippo <teemu@hecknology.net>
parents: 96
diff changeset
23
104
cd4df75924b7 Add connections
Teemu Piippo <teemu@hecknology.net>
parents: 103
diff changeset
24 bool DrawTool::mouseClick(const Canvas::MouseClickInfo& info)
103
Teemu Piippo <teemu@hecknology.net>
parents: 96
diff changeset
25 {
106
128efb9d148b work on draw preview
Teemu Piippo <teemu@hecknology.net>
parents: 104
diff changeset
26 if (info.worldPosition.has_value())
128efb9d148b work on draw preview
Teemu Piippo <teemu@hecknology.net>
parents: 104
diff changeset
27 {
128efb9d148b work on draw preview
Teemu Piippo <teemu@hecknology.net>
parents: 104
diff changeset
28 const glm::vec3& pos = info.worldPosition.value();
128efb9d148b work on draw preview
Teemu Piippo <teemu@hecknology.net>
parents: 104
diff changeset
29 const auto isCloseToPos = [&](const glm::vec3& x){return geom::isclose(x, pos);};
128efb9d148b work on draw preview
Teemu Piippo <teemu@hecknology.net>
parents: 104
diff changeset
30 if (any(this->polygon, isCloseToPos))
128efb9d148b work on draw preview
Teemu Piippo <teemu@hecknology.net>
parents: 104
diff changeset
31 {
109
40a1cf2f38f5 replaced preview layers in favor of overpainting callback
Teemu Piippo <teemu@hecknology.net>
parents: 108
diff changeset
32 this->closeShape();
106
128efb9d148b work on draw preview
Teemu Piippo <teemu@hecknology.net>
parents: 104
diff changeset
33 }
128efb9d148b work on draw preview
Teemu Piippo <teemu@hecknology.net>
parents: 104
diff changeset
34 else
128efb9d148b work on draw preview
Teemu Piippo <teemu@hecknology.net>
parents: 104
diff changeset
35 {
128efb9d148b work on draw preview
Teemu Piippo <teemu@hecknology.net>
parents: 104
diff changeset
36 this->polygon.push_back(pos);
128efb9d148b work on draw preview
Teemu Piippo <teemu@hecknology.net>
parents: 104
diff changeset
37 if (this->polygon.size() == 4)
128efb9d148b work on draw preview
Teemu Piippo <teemu@hecknology.net>
parents: 104
diff changeset
38 {
109
40a1cf2f38f5 replaced preview layers in favor of overpainting callback
Teemu Piippo <teemu@hecknology.net>
parents: 108
diff changeset
39 this->closeShape();
106
128efb9d148b work on draw preview
Teemu Piippo <teemu@hecknology.net>
parents: 104
diff changeset
40 }
128efb9d148b work on draw preview
Teemu Piippo <teemu@hecknology.net>
parents: 104
diff changeset
41 }
128efb9d148b work on draw preview
Teemu Piippo <teemu@hecknology.net>
parents: 104
diff changeset
42 }
109
40a1cf2f38f5 replaced preview layers in favor of overpainting callback
Teemu Piippo <teemu@hecknology.net>
parents: 108
diff changeset
43 this->previewPolygon = this->polygon;
104
cd4df75924b7 Add connections
Teemu Piippo <teemu@hecknology.net>
parents: 103
diff changeset
44 return true;
103
Teemu Piippo <teemu@hecknology.net>
parents: 96
diff changeset
45 }
106
128efb9d148b work on draw preview
Teemu Piippo <teemu@hecknology.net>
parents: 104
diff changeset
46
108
94c92c923713 work on editing tools
Teemu Piippo <teemu@hecknology.net>
parents: 106
diff changeset
47 bool DrawTool::mouseMove(const Canvas::MouseMoveInfo& info)
94c92c923713 work on editing tools
Teemu Piippo <teemu@hecknology.net>
parents: 106
diff changeset
48 {
109
40a1cf2f38f5 replaced preview layers in favor of overpainting callback
Teemu Piippo <teemu@hecknology.net>
parents: 108
diff changeset
49 if (info.worldPosition.has_value())
108
94c92c923713 work on editing tools
Teemu Piippo <teemu@hecknology.net>
parents: 106
diff changeset
50 {
109
40a1cf2f38f5 replaced preview layers in favor of overpainting callback
Teemu Piippo <teemu@hecknology.net>
parents: 108
diff changeset
51 this->previewPoint = info.worldPosition.value();
40a1cf2f38f5 replaced preview layers in favor of overpainting callback
Teemu Piippo <teemu@hecknology.net>
parents: 108
diff changeset
52 if (this->polygon.size() < 4)
108
94c92c923713 work on editing tools
Teemu Piippo <teemu@hecknology.net>
parents: 106
diff changeset
53 {
109
40a1cf2f38f5 replaced preview layers in favor of overpainting callback
Teemu Piippo <teemu@hecknology.net>
parents: 108
diff changeset
54 this->previewPolygon.resize(this->polygon.size() + 1);
40a1cf2f38f5 replaced preview layers in favor of overpainting callback
Teemu Piippo <teemu@hecknology.net>
parents: 108
diff changeset
55 this->previewPolygon.back() = this->previewPoint;
108
94c92c923713 work on editing tools
Teemu Piippo <teemu@hecknology.net>
parents: 106
diff changeset
56 }
94c92c923713 work on editing tools
Teemu Piippo <teemu@hecknology.net>
parents: 106
diff changeset
57 }
109
40a1cf2f38f5 replaced preview layers in favor of overpainting callback
Teemu Piippo <teemu@hecknology.net>
parents: 108
diff changeset
58 return false;
108
94c92c923713 work on editing tools
Teemu Piippo <teemu@hecknology.net>
parents: 106
diff changeset
59 }
94c92c923713 work on editing tools
Teemu Piippo <teemu@hecknology.net>
parents: 106
diff changeset
60
106
128efb9d148b work on draw preview
Teemu Piippo <teemu@hecknology.net>
parents: 104
diff changeset
61 void DrawTool::reset()
128efb9d148b work on draw preview
Teemu Piippo <teemu@hecknology.net>
parents: 104
diff changeset
62 {
128efb9d148b work on draw preview
Teemu Piippo <teemu@hecknology.net>
parents: 104
diff changeset
63 this->polygon.clear();
128efb9d148b work on draw preview
Teemu Piippo <teemu@hecknology.net>
parents: 104
diff changeset
64 }
109
40a1cf2f38f5 replaced preview layers in favor of overpainting callback
Teemu Piippo <teemu@hecknology.net>
parents: 108
diff changeset
65
40a1cf2f38f5 replaced preview layers in favor of overpainting callback
Teemu Piippo <teemu@hecknology.net>
parents: 108
diff changeset
66 void DrawTool::overpaint(Canvas* canvas, QPainter* painter) const
40a1cf2f38f5 replaced preview layers in favor of overpainting callback
Teemu Piippo <teemu@hecknology.net>
parents: 108
diff changeset
67 {
40a1cf2f38f5 replaced preview layers in favor of overpainting callback
Teemu Piippo <teemu@hecknology.net>
parents: 108
diff changeset
68 painter->setBrush(::polygonBrush);
40a1cf2f38f5 replaced preview layers in favor of overpainting callback
Teemu Piippo <teemu@hecknology.net>
parents: 108
diff changeset
69 painter->setPen(::polygonPen);
40a1cf2f38f5 replaced preview layers in favor of overpainting callback
Teemu Piippo <teemu@hecknology.net>
parents: 108
diff changeset
70 canvas->drawWorldPolygon(painter, this->previewPolygon);
40a1cf2f38f5 replaced preview layers in favor of overpainting callback
Teemu Piippo <teemu@hecknology.net>
parents: 108
diff changeset
71 painter->setBrush(::pointBrush);
40a1cf2f38f5 replaced preview layers in favor of overpainting callback
Teemu Piippo <teemu@hecknology.net>
parents: 108
diff changeset
72 painter->setPen(::pointPen);
40a1cf2f38f5 replaced preview layers in favor of overpainting callback
Teemu Piippo <teemu@hecknology.net>
parents: 108
diff changeset
73 for (const glm::vec3& point : this->polygon)
40a1cf2f38f5 replaced preview layers in favor of overpainting callback
Teemu Piippo <teemu@hecknology.net>
parents: 108
diff changeset
74 {
40a1cf2f38f5 replaced preview layers in favor of overpainting callback
Teemu Piippo <teemu@hecknology.net>
parents: 108
diff changeset
75 canvas->drawWorldPoint(painter, point);
40a1cf2f38f5 replaced preview layers in favor of overpainting callback
Teemu Piippo <teemu@hecknology.net>
parents: 108
diff changeset
76 }
40a1cf2f38f5 replaced preview layers in favor of overpainting callback
Teemu Piippo <teemu@hecknology.net>
parents: 108
diff changeset
77 if (this->polygon.size() < 4)
40a1cf2f38f5 replaced preview layers in favor of overpainting callback
Teemu Piippo <teemu@hecknology.net>
parents: 108
diff changeset
78 {
40a1cf2f38f5 replaced preview layers in favor of overpainting callback
Teemu Piippo <teemu@hecknology.net>
parents: 108
diff changeset
79 canvas->drawWorldPoint(painter, this->previewPoint);
40a1cf2f38f5 replaced preview layers in favor of overpainting callback
Teemu Piippo <teemu@hecknology.net>
parents: 108
diff changeset
80 }
40a1cf2f38f5 replaced preview layers in favor of overpainting callback
Teemu Piippo <teemu@hecknology.net>
parents: 108
diff changeset
81 }
40a1cf2f38f5 replaced preview layers in favor of overpainting callback
Teemu Piippo <teemu@hecknology.net>
parents: 108
diff changeset
82
40a1cf2f38f5 replaced preview layers in favor of overpainting callback
Teemu Piippo <teemu@hecknology.net>
parents: 108
diff changeset
83 void DrawTool::closeShape()
40a1cf2f38f5 replaced preview layers in favor of overpainting callback
Teemu Piippo <teemu@hecknology.net>
parents: 108
diff changeset
84 {
40a1cf2f38f5 replaced preview layers in favor of overpainting callback
Teemu Piippo <teemu@hecknology.net>
parents: 108
diff changeset
85 QMessageBox::information(nullptr, "test", "close the polygon");
40a1cf2f38f5 replaced preview layers in favor of overpainting callback
Teemu Piippo <teemu@hecknology.net>
parents: 108
diff changeset
86 }

mercurial