src/tools/drawtool.cpp

Sun, 25 Jul 2021 13:49:37 +0300

author
Teemu Piippo <teemu@hecknology.net>
date
Sun, 25 Jul 2021 13:49:37 +0300
changeset 108
94c92c923713
parent 106
128efb9d148b
child 109
40a1cf2f38f5
permissions
-rw-r--r--

work on editing tools

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
108
94c92c923713 work on editing tools
Teemu Piippo <teemu@hecknology.net>
parents: 106
diff changeset
4 static const QBrush brush = {Qt::white};
94c92c923713 work on editing tools
Teemu Piippo <teemu@hecknology.net>
parents: 106
diff changeset
5 static const QPen pen = {Qt::black};
94c92c923713 work on editing tools
Teemu Piippo <teemu@hecknology.net>
parents: 106
diff changeset
6 static const QBrush polygonBrush = {QColor{64, 255, 128}};
94c92c923713 work on editing tools
Teemu Piippo <teemu@hecknology.net>
parents: 106
diff changeset
7
96
165777a20dc7 added tool base code
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
8 DrawTool::DrawTool(QObject* parent) :
165777a20dc7 added tool base code
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
9 BaseTool{parent} {}
165777a20dc7 added tool base code
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
10
165777a20dc7 added tool base code
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
11 QString DrawTool::name() const
165777a20dc7 added tool base code
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
12 {
165777a20dc7 added tool base code
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
13 static const QString result = tr("Draw");
165777a20dc7 added tool base code
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
14 return result;
165777a20dc7 added tool base code
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
15 }
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 QString DrawTool::toolTip() const
165777a20dc7 added tool base code
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
18 {
165777a20dc7 added tool base code
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
19 static const QString result = tr("Draw new elements into the model.");
165777a20dc7 added tool base code
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
20 return result;
165777a20dc7 added tool base code
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
21 }
103
Teemu Piippo <teemu@hecknology.net>
parents: 96
diff changeset
22
104
cd4df75924b7 Add connections
Teemu Piippo <teemu@hecknology.net>
parents: 103
diff changeset
23 bool DrawTool::mouseClick(const Canvas::MouseClickInfo& info)
103
Teemu Piippo <teemu@hecknology.net>
parents: 96
diff changeset
24 {
106
128efb9d148b work on draw preview
Teemu Piippo <teemu@hecknology.net>
parents: 104
diff changeset
25 if (info.worldPosition.has_value())
128efb9d148b work on draw preview
Teemu Piippo <teemu@hecknology.net>
parents: 104
diff changeset
26 {
128efb9d148b work on draw preview
Teemu Piippo <teemu@hecknology.net>
parents: 104
diff changeset
27 const glm::vec3& pos = info.worldPosition.value();
128efb9d148b work on draw preview
Teemu Piippo <teemu@hecknology.net>
parents: 104
diff changeset
28 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
29 if (any(this->polygon, isCloseToPos))
128efb9d148b work on draw preview
Teemu Piippo <teemu@hecknology.net>
parents: 104
diff changeset
30 {
128efb9d148b work on draw preview
Teemu Piippo <teemu@hecknology.net>
parents: 104
diff changeset
31 QMessageBox::information(nullptr, "test", "close the polygon");
128efb9d148b work on draw preview
Teemu Piippo <teemu@hecknology.net>
parents: 104
diff changeset
32 }
128efb9d148b work on draw preview
Teemu Piippo <teemu@hecknology.net>
parents: 104
diff changeset
33 else
128efb9d148b work on draw preview
Teemu Piippo <teemu@hecknology.net>
parents: 104
diff changeset
34 {
128efb9d148b work on draw preview
Teemu Piippo <teemu@hecknology.net>
parents: 104
diff changeset
35 this->polygon.push_back(pos);
108
94c92c923713 work on editing tools
Teemu Piippo <teemu@hecknology.net>
parents: 106
diff changeset
36 auto& previewLayer = info.invoker->modifyPreviewLayer(Canvas::DrawToolPreview);
94c92c923713 work on editing tools
Teemu Piippo <teemu@hecknology.net>
parents: 106
diff changeset
37 previewLayer.points.resize(this->polygon.size());
94c92c923713 work on editing tools
Teemu Piippo <teemu@hecknology.net>
parents: 106
diff changeset
38 previewLayer.points.back() = {pos, brush, pen};
94c92c923713 work on editing tools
Teemu Piippo <teemu@hecknology.net>
parents: 106
diff changeset
39 previewLayer.polygons.clear();
94c92c923713 work on editing tools
Teemu Piippo <teemu@hecknology.net>
parents: 106
diff changeset
40 previewLayer.polygons.push_back({geom::NPolygon{this->polygon}, polygonBrush, pen});
106
128efb9d148b work on draw preview
Teemu Piippo <teemu@hecknology.net>
parents: 104
diff changeset
41 if (this->polygon.size() == 4)
128efb9d148b work on draw preview
Teemu Piippo <teemu@hecknology.net>
parents: 104
diff changeset
42 {
128efb9d148b work on draw preview
Teemu Piippo <teemu@hecknology.net>
parents: 104
diff changeset
43 QMessageBox::information(nullptr, "test", "close the polygon");
128efb9d148b work on draw preview
Teemu Piippo <teemu@hecknology.net>
parents: 104
diff changeset
44 }
128efb9d148b work on draw preview
Teemu Piippo <teemu@hecknology.net>
parents: 104
diff changeset
45 }
128efb9d148b work on draw preview
Teemu Piippo <teemu@hecknology.net>
parents: 104
diff changeset
46 }
104
cd4df75924b7 Add connections
Teemu Piippo <teemu@hecknology.net>
parents: 103
diff changeset
47 return true;
103
Teemu Piippo <teemu@hecknology.net>
parents: 96
diff changeset
48 }
106
128efb9d148b work on draw preview
Teemu Piippo <teemu@hecknology.net>
parents: 104
diff changeset
49
108
94c92c923713 work on editing tools
Teemu Piippo <teemu@hecknology.net>
parents: 106
diff changeset
50 bool DrawTool::mouseMove(const Canvas::MouseMoveInfo& info)
94c92c923713 work on editing tools
Teemu Piippo <teemu@hecknology.net>
parents: 106
diff changeset
51 {
94c92c923713 work on editing tools
Teemu Piippo <teemu@hecknology.net>
parents: 106
diff changeset
52 if (this->polygon.size() < 4 and info.worldPosition.has_value())
94c92c923713 work on editing tools
Teemu Piippo <teemu@hecknology.net>
parents: 106
diff changeset
53 {
94c92c923713 work on editing tools
Teemu Piippo <teemu@hecknology.net>
parents: 106
diff changeset
54 auto& previewLayer = info.invoker->modifyPreviewLayer(Canvas::DrawToolPreview);
94c92c923713 work on editing tools
Teemu Piippo <teemu@hecknology.net>
parents: 106
diff changeset
55 previewLayer.points.resize(this->polygon.size() + 1);
94c92c923713 work on editing tools
Teemu Piippo <teemu@hecknology.net>
parents: 106
diff changeset
56 previewLayer.points.back() = {info.worldPosition.value(), brush, pen};
94c92c923713 work on editing tools
Teemu Piippo <teemu@hecknology.net>
parents: 106
diff changeset
57 if (previewLayer.polygons.size() > 0)
94c92c923713 work on editing tools
Teemu Piippo <teemu@hecknology.net>
parents: 106
diff changeset
58 {
94c92c923713 work on editing tools
Teemu Piippo <teemu@hecknology.net>
parents: 106
diff changeset
59 auto& polygon = previewLayer.polygons.back();
94c92c923713 work on editing tools
Teemu Piippo <teemu@hecknology.net>
parents: 106
diff changeset
60 polygon.geometry.points.resize(this->polygon.size() + 1);
94c92c923713 work on editing tools
Teemu Piippo <teemu@hecknology.net>
parents: 106
diff changeset
61 polygon.geometry.points.back() = info.worldPosition.value();
94c92c923713 work on editing tools
Teemu Piippo <teemu@hecknology.net>
parents: 106
diff changeset
62 }
94c92c923713 work on editing tools
Teemu Piippo <teemu@hecknology.net>
parents: 106
diff changeset
63 return true;
94c92c923713 work on editing tools
Teemu Piippo <teemu@hecknology.net>
parents: 106
diff changeset
64 }
94c92c923713 work on editing tools
Teemu Piippo <teemu@hecknology.net>
parents: 106
diff changeset
65 else
94c92c923713 work on editing tools
Teemu Piippo <teemu@hecknology.net>
parents: 106
diff changeset
66 {
94c92c923713 work on editing tools
Teemu Piippo <teemu@hecknology.net>
parents: 106
diff changeset
67 return false;
94c92c923713 work on editing tools
Teemu Piippo <teemu@hecknology.net>
parents: 106
diff changeset
68 }
94c92c923713 work on editing tools
Teemu Piippo <teemu@hecknology.net>
parents: 106
diff changeset
69 }
94c92c923713 work on editing tools
Teemu Piippo <teemu@hecknology.net>
parents: 106
diff changeset
70
106
128efb9d148b work on draw preview
Teemu Piippo <teemu@hecknology.net>
parents: 104
diff changeset
71 void DrawTool::reset()
128efb9d148b work on draw preview
Teemu Piippo <teemu@hecknology.net>
parents: 104
diff changeset
72 {
128efb9d148b work on draw preview
Teemu Piippo <teemu@hecknology.net>
parents: 104
diff changeset
73 this->polygon.clear();
128efb9d148b work on draw preview
Teemu Piippo <teemu@hecknology.net>
parents: 104
diff changeset
74 }

mercurial