Sun, 25 Jul 2021 16:26:38 +0300
replaced preview layers in favor of overpainting callback
103 | 1 | #include <QMessageBox> |
96 | 2 | #include "drawtool.h" |
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 | 8 | |
96 | 9 | DrawTool::DrawTool(QObject* parent) : |
10 | BaseTool{parent} {} | |
11 | ||
12 | QString DrawTool::name() const | |
13 | { | |
14 | static const QString result = tr("Draw"); | |
15 | return result; | |
16 | } | |
17 | ||
18 | QString DrawTool::toolTip() const | |
19 | { | |
20 | static const QString result = tr("Draw new elements into the model."); | |
21 | return result; | |
22 | } | |
103 | 23 | |
104 | 24 | bool DrawTool::mouseClick(const Canvas::MouseClickInfo& info) |
103 | 25 | { |
106 | 26 | if (info.worldPosition.has_value()) |
27 | { | |
28 | const glm::vec3& pos = info.worldPosition.value(); | |
29 | const auto isCloseToPos = [&](const glm::vec3& x){return geom::isclose(x, pos);}; | |
30 | if (any(this->polygon, isCloseToPos)) | |
31 | { | |
109
40a1cf2f38f5
replaced preview layers in favor of overpainting callback
Teemu Piippo <teemu@hecknology.net>
parents:
108
diff
changeset
|
32 | this->closeShape(); |
106 | 33 | } |
34 | else | |
35 | { | |
36 | this->polygon.push_back(pos); | |
37 | if (this->polygon.size() == 4) | |
38 | { | |
109
40a1cf2f38f5
replaced preview layers in favor of overpainting callback
Teemu Piippo <teemu@hecknology.net>
parents:
108
diff
changeset
|
39 | this->closeShape(); |
106 | 40 | } |
41 | } | |
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 | 44 | return true; |
103 | 45 | } |
106 | 46 | |
108 | 47 | bool DrawTool::mouseMove(const Canvas::MouseMoveInfo& info) |
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 | 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 | 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 | 56 | } |
57 | } | |
109
40a1cf2f38f5
replaced preview layers in favor of overpainting callback
Teemu Piippo <teemu@hecknology.net>
parents:
108
diff
changeset
|
58 | return false; |
108 | 59 | } |
60 | ||
106 | 61 | void DrawTool::reset() |
62 | { | |
63 | this->polygon.clear(); | |
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 | } |