src/editmodes/drawMode.cpp

Tue, 14 Feb 2017 15:11:33 +0200

author
Teemu Piippo <teemu@hecknology.net>
date
Tue, 14 Feb 2017 15:11:33 +0200
changeset 1148
96cb15a7611f
parent 1147
a26568aa3cce
child 1149
502c866b8512
permissions
-rw-r--r--

Moved LDTriangle into its own source pair.

829
bb903e89e23c - now with license headers
Santeri Piippo <crimsondusk64@gmail.com>
parents: 827
diff changeset
1 /*
bb903e89e23c - now with license headers
Santeri Piippo <crimsondusk64@gmail.com>
parents: 827
diff changeset
2 * LDForge: LDraw parts authoring CAD
1072
9ce9496427f2 Happy new year 2017!
Teemu Piippo <teemu@hecknology.net>
parents: 1065
diff changeset
3 * Copyright (C) 2013 - 2017 Teemu Piippo
829
bb903e89e23c - now with license headers
Santeri Piippo <crimsondusk64@gmail.com>
parents: 827
diff changeset
4 *
bb903e89e23c - now with license headers
Santeri Piippo <crimsondusk64@gmail.com>
parents: 827
diff changeset
5 * This program is free software: you can redistribute it and/or modify
bb903e89e23c - now with license headers
Santeri Piippo <crimsondusk64@gmail.com>
parents: 827
diff changeset
6 * it under the terms of the GNU General Public License as published by
bb903e89e23c - now with license headers
Santeri Piippo <crimsondusk64@gmail.com>
parents: 827
diff changeset
7 * the Free Software Foundation, either version 3 of the License, or
1006
a6b462051ae0 Copyright header fix, some minor stuff
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1000
diff changeset
8 * (at your option) any later version.
829
bb903e89e23c - now with license headers
Santeri Piippo <crimsondusk64@gmail.com>
parents: 827
diff changeset
9 *
bb903e89e23c - now with license headers
Santeri Piippo <crimsondusk64@gmail.com>
parents: 827
diff changeset
10 * This program is distributed in the hope that it will be useful,
bb903e89e23c - now with license headers
Santeri Piippo <crimsondusk64@gmail.com>
parents: 827
diff changeset
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
bb903e89e23c - now with license headers
Santeri Piippo <crimsondusk64@gmail.com>
parents: 827
diff changeset
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
bb903e89e23c - now with license headers
Santeri Piippo <crimsondusk64@gmail.com>
parents: 827
diff changeset
13 * GNU General Public License for more details.
bb903e89e23c - now with license headers
Santeri Piippo <crimsondusk64@gmail.com>
parents: 827
diff changeset
14 *
bb903e89e23c - now with license headers
Santeri Piippo <crimsondusk64@gmail.com>
parents: 827
diff changeset
15 * You should have received a copy of the GNU General Public License
bb903e89e23c - now with license headers
Santeri Piippo <crimsondusk64@gmail.com>
parents: 827
diff changeset
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
bb903e89e23c - now with license headers
Santeri Piippo <crimsondusk64@gmail.com>
parents: 827
diff changeset
17 */
bb903e89e23c - now with license headers
Santeri Piippo <crimsondusk64@gmail.com>
parents: 827
diff changeset
18
823
1a2f593f0c02 - saving work done on edit mode revamp
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
19 #include <QPainter>
827
82469b40567e - now compiles
Santeri Piippo <crimsondusk64@gmail.com>
parents: 825
diff changeset
20 #include <QMouseEvent>
830
a741a0b9df49 - updated filenames
Santeri Piippo <crimsondusk64@gmail.com>
parents: 829
diff changeset
21 #include "drawMode.h"
1147
a26568aa3cce Renamed ldObject.cpp → linetypes/modelobject.cpp
Teemu Piippo <teemu@hecknology.net>
parents: 1144
diff changeset
22 #include "../linetypes/modelobject.h"
1144
4f226fd97826 Renamed glRenderer.cpp → glrenderer.cpp
Teemu Piippo <teemu@hecknology.net>
parents: 1141
diff changeset
23 #include "../glrenderer.h"
1141
7dc2c981937e Split LDConditionalEdge and LDEdgeLine into new source pairs
Teemu Piippo <teemu@hecknology.net>
parents: 1139
diff changeset
24 #include "../linetypes/edgeline.h"
1148
96cb15a7611f Moved LDTriangle into its own source pair.
Teemu Piippo <teemu@hecknology.net>
parents: 1147
diff changeset
25 #include "../linetypes/triangle.h"
823
1a2f593f0c02 - saving work done on edit mode revamp
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
26
1104
edddb9b0db9e Moved document editing methods from GLRenderer to Canvas
Teemu Piippo <teemu@hecknology.net>
parents: 1079
diff changeset
27 DrawMode::DrawMode (Canvas* canvas) :
edddb9b0db9e Moved document editing methods from GLRenderer to Canvas
Teemu Piippo <teemu@hecknology.net>
parents: 1079
diff changeset
28 Super (canvas) {}
823
1a2f593f0c02 - saving work done on edit mode revamp
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
29
1a2f593f0c02 - saving work done on edit mode revamp
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
30 EditModeType DrawMode::type() const
1a2f593f0c02 - saving work done on edit mode revamp
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
31 {
1a2f593f0c02 - saving work done on edit mode revamp
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
32 return EditModeType::Draw;
1a2f593f0c02 - saving work done on edit mode revamp
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
33 }
1a2f593f0c02 - saving work done on edit mode revamp
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
34
1a2f593f0c02 - saving work done on edit mode revamp
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
35 void DrawMode::render (QPainter& painter) const
1a2f593f0c02 - saving work done on edit mode revamp
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
36 {
841
1243abd47381 - split the rectangle ability out of the draw mode into its own edit mode
Teemu Piippo <crimsondusk64@gmail.com>
parents: 830
diff changeset
37 QVector<Vertex> poly;
823
1a2f593f0c02 - saving work done on edit mode revamp
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
38
861
83426c5fa732 - major identifier renaming
Teemu Piippo <crimsondusk64@gmail.com>
parents: 844
diff changeset
39 for (Vertex const& vert : m_drawedVerts)
841
1243abd47381 - split the rectangle ability out of the draw mode into its own edit mode
Teemu Piippo <crimsondusk64@gmail.com>
parents: 830
diff changeset
40 poly << vert;
823
1a2f593f0c02 - saving work done on edit mode revamp
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
41
841
1243abd47381 - split the rectangle ability out of the draw mode into its own edit mode
Teemu Piippo <crimsondusk64@gmail.com>
parents: 830
diff changeset
42 // Draw the cursor vertex as the last one in the list.
1065
c8ecddbd99e9 Actually, let's call it countof(). Makes more sense.
Teemu Piippo <teemu@hecknology.net>
parents: 1063
diff changeset
43 if (countof(poly) < 4)
897
ef9e4469975a - pressing ctrl while drawing now locks the draw to cardinal directions
Teemu Piippo <crimsondusk64@gmail.com>
parents: 866
diff changeset
44 poly << getCursorVertex();
823
1a2f593f0c02 - saving work done on edit mode revamp
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
45
866
4951b737f8cb - now able to spawn partial circular primitives
Teemu Piippo <crimsondusk64@gmail.com>
parents: 861
diff changeset
46 renderPolygon (painter, poly, true, true);
823
1a2f593f0c02 - saving work done on edit mode revamp
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
47 }
1a2f593f0c02 - saving work done on edit mode revamp
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
48
1a2f593f0c02 - saving work done on edit mode revamp
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
49 bool DrawMode::preAddVertex (Vertex const& pos)
1a2f593f0c02 - saving work done on edit mode revamp
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
50 {
1a2f593f0c02 - saving work done on edit mode revamp
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
51 // If we picked an already-existing vertex, stop drawing
861
83426c5fa732 - major identifier renaming
Teemu Piippo <crimsondusk64@gmail.com>
parents: 844
diff changeset
52 for (Vertex& vert : m_drawedVerts)
823
1a2f593f0c02 - saving work done on edit mode revamp
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
53 {
1a2f593f0c02 - saving work done on edit mode revamp
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
54 if (vert == pos)
1a2f593f0c02 - saving work done on edit mode revamp
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
55 {
1065
c8ecddbd99e9 Actually, let's call it countof(). Makes more sense.
Teemu Piippo <teemu@hecknology.net>
parents: 1063
diff changeset
56 if (countof(m_drawedVerts) >= 2)
959
737245eee433 Remove the vertex object type
Teemu Piippo <crimsondusk64@gmail.com>
parents: 952
diff changeset
57 endDraw();
737245eee433 Remove the vertex object type
Teemu Piippo <crimsondusk64@gmail.com>
parents: 952
diff changeset
58
823
1a2f593f0c02 - saving work done on edit mode revamp
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
59 return true;
1a2f593f0c02 - saving work done on edit mode revamp
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
60 }
1a2f593f0c02 - saving work done on edit mode revamp
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
61 }
1a2f593f0c02 - saving work done on edit mode revamp
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
62
1a2f593f0c02 - saving work done on edit mode revamp
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
63 return false;
1a2f593f0c02 - saving work done on edit mode revamp
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
64 }
1a2f593f0c02 - saving work done on edit mode revamp
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
65
825
f7649c9bfd9a - more work on edit modes
Santeri Piippo <crimsondusk64@gmail.com>
parents: 824
diff changeset
66 void DrawMode::endDraw()
f7649c9bfd9a - more work on edit modes
Santeri Piippo <crimsondusk64@gmail.com>
parents: 824
diff changeset
67 {
f7649c9bfd9a - more work on edit modes
Santeri Piippo <crimsondusk64@gmail.com>
parents: 824
diff changeset
68 // Clean the selection and create the object
861
83426c5fa732 - major identifier renaming
Teemu Piippo <crimsondusk64@gmail.com>
parents: 844
diff changeset
69 QList<Vertex>& verts = m_drawedVerts;
1079
67c6e5d32e68 More rework on model stuff, removals of LDSpawn calls. Most importantly, the LDraw code parsing function was moved to Model.
Teemu Piippo <teemu@hecknology.net>
parents: 1077
diff changeset
70 Model model {m_documents};
825
f7649c9bfd9a - more work on edit modes
Santeri Piippo <crimsondusk64@gmail.com>
parents: 824
diff changeset
71
1065
c8ecddbd99e9 Actually, let's call it countof(). Makes more sense.
Teemu Piippo <teemu@hecknology.net>
parents: 1063
diff changeset
72 switch (countof(verts))
825
f7649c9bfd9a - more work on edit modes
Santeri Piippo <crimsondusk64@gmail.com>
parents: 824
diff changeset
73 {
1079
67c6e5d32e68 More rework on model stuff, removals of LDSpawn calls. Most importantly, the LDraw code parsing function was moved to Model.
Teemu Piippo <teemu@hecknology.net>
parents: 1077
diff changeset
74 case 2:
841
1243abd47381 - split the rectangle ability out of the draw mode into its own edit mode
Teemu Piippo <crimsondusk64@gmail.com>
parents: 830
diff changeset
75 // 2 verts - make a line
1139
51303023d651 LDObject subclass renaming:
Teemu Piippo <teemu@hecknology.net>
parents: 1104
diff changeset
76 model.emplace<LDEdgeLine>(verts[0], verts[1]);
1079
67c6e5d32e68 More rework on model stuff, removals of LDSpawn calls. Most importantly, the LDraw code parsing function was moved to Model.
Teemu Piippo <teemu@hecknology.net>
parents: 1077
diff changeset
77 break;
825
f7649c9bfd9a - more work on edit modes
Santeri Piippo <crimsondusk64@gmail.com>
parents: 824
diff changeset
78
841
1243abd47381 - split the rectangle ability out of the draw mode into its own edit mode
Teemu Piippo <crimsondusk64@gmail.com>
parents: 830
diff changeset
79 case 3:
1243abd47381 - split the rectangle ability out of the draw mode into its own edit mode
Teemu Piippo <crimsondusk64@gmail.com>
parents: 830
diff changeset
80 case 4:
1243abd47381 - split the rectangle ability out of the draw mode into its own edit mode
Teemu Piippo <crimsondusk64@gmail.com>
parents: 830
diff changeset
81 {
1077
952d6b3e7d11 Replaced uses of LDSpawn with the Model class in edit modes
Teemu Piippo <teemu@hecknology.net>
parents: 1072
diff changeset
82 LDObject* obj = (countof(verts) == 3) ?
952d6b3e7d11 Replaced uses of LDSpawn with the Model class in edit modes
Teemu Piippo <teemu@hecknology.net>
parents: 1072
diff changeset
83 static_cast<LDObject*>(model.emplace<LDTriangle>()) :
1139
51303023d651 LDObject subclass renaming:
Teemu Piippo <teemu@hecknology.net>
parents: 1104
diff changeset
84 static_cast<LDObject*>(model.emplace<LDQuadrilateral>());
825
f7649c9bfd9a - more work on edit modes
Santeri Piippo <crimsondusk64@gmail.com>
parents: 824
diff changeset
85
1065
c8ecddbd99e9 Actually, let's call it countof(). Makes more sense.
Teemu Piippo <teemu@hecknology.net>
parents: 1063
diff changeset
86 for (int i = 0; i < countof(verts); ++i)
841
1243abd47381 - split the rectangle ability out of the draw mode into its own edit mode
Teemu Piippo <crimsondusk64@gmail.com>
parents: 830
diff changeset
87 obj->setVertex (i, verts[i]);
825
f7649c9bfd9a - more work on edit modes
Santeri Piippo <crimsondusk64@gmail.com>
parents: 824
diff changeset
88
841
1243abd47381 - split the rectangle ability out of the draw mode into its own edit mode
Teemu Piippo <crimsondusk64@gmail.com>
parents: 830
diff changeset
89 break;
825
f7649c9bfd9a - more work on edit modes
Santeri Piippo <crimsondusk64@gmail.com>
parents: 824
diff changeset
90 }
f7649c9bfd9a - more work on edit modes
Santeri Piippo <crimsondusk64@gmail.com>
parents: 824
diff changeset
91 }
f7649c9bfd9a - more work on edit modes
Santeri Piippo <crimsondusk64@gmail.com>
parents: 824
diff changeset
92
1077
952d6b3e7d11 Replaced uses of LDSpawn with the Model class in edit modes
Teemu Piippo <teemu@hecknology.net>
parents: 1072
diff changeset
93 finishDraw (model);
825
f7649c9bfd9a - more work on edit modes
Santeri Piippo <crimsondusk64@gmail.com>
parents: 824
diff changeset
94 }

mercurial