]> Shamusworld >> Repos - architektonas/blob - src/drawarcaction.cpp
Fix DrawArcAction to actually allow creation of Arcs.
[architektonas] / src / drawarcaction.cpp
1 // drawarcaction.cpp: Draw arc object
2 //
3 // Part of the Architektonas Project
4 // (C) 2011 Underground Software
5 // See the README and GPLv3 files for licensing and warranty information
6 //
7 // JLH = James Hammons <jlhamm@acm.org>
8 //
9 // WHO  WHEN        WHAT
10 // ---  ----------  ------------------------------------------------------------
11 // JLH  08/13/2013  Created this file
12 //
13
14 #include "drawarcaction.h"
15 #include "arc.h"
16 #include "mathconstants.h"
17 #include "painter.h"
18 //#include "vector.h"
19
20
21 enum { FIRST_POINT, SECOND_POINT, THIRD_POINT };
22
23
24 DrawArcAction::DrawArcAction(): state(FIRST_POINT), arc(NULL)
25 {
26 }
27
28
29 DrawArcAction::~DrawArcAction()
30 {
31 }
32
33
34 /*virtual*/ void DrawArcAction::Draw(Painter * painter)
35 {
36         painter->SetPen(QPen(Qt::red, 2.0, Qt::DotLine));
37
38         // I think stuff like crosshairs should be done in the DrawingView, tho
39         // (and it is! :-D)
40         if (state == FIRST_POINT)
41         {
42                 painter->DrawHandle(p1);
43         }
44         else if (state == SECOND_POINT)
45         {
46                 painter->DrawLine(p1, p2);
47                 painter->DrawHandle(p2);
48         }
49         else if (state == THIRD_POINT)
50         {
51                 painter->DrawLine(p1, p2);
52                 painter->DrawArc(p1, radius, startAngle, span);
53                 painter->DrawHandle(p3);
54         }
55 }
56
57
58 /*virtual*/ void DrawArcAction::MouseDown(Vector point)
59 {
60         if (state == FIRST_POINT)
61                 p1 = point;
62         else if (state == SECOND_POINT)
63         {
64                 p2 = point;
65                 Vector r1(p2, p1);
66                 radius = Vector::Magnitude(p1, p2);
67                 startAngle = r1.Angle();
68         }
69         else if (state == THIRD_POINT)
70         {
71                 p3 = point;
72                 Vector r2(p3, p1);
73                 span = r2.Angle() - startAngle;
74
75                 if (span < 0)
76                         span += 2.0 * PI;
77         }
78 }
79
80
81 /*virtual*/ void DrawArcAction::MouseMoved(Vector point)
82 {
83         if (state == FIRST_POINT)
84                 p1 = point;
85         else if (state == SECOND_POINT)
86         {
87                 p2 = point;
88                 Vector r1(p2, p1);
89                 radius = Vector::Magnitude(p1, p2);
90                 startAngle = r1.Angle();
91         }
92         else if (state == THIRD_POINT)
93         {
94                 p3 = point;
95                 Vector r2(p3, p1);
96                 span = r2.Angle() - startAngle;
97
98                 if (span < 0)
99                         span += 2.0 * PI;
100         }
101 }
102
103
104 /*virtual*/ void DrawArcAction::MouseReleased(void)
105 {
106         if (state == FIRST_POINT)
107         {
108                 state = SECOND_POINT;
109         }
110         else if (state == SECOND_POINT)
111         {
112                 state = THIRD_POINT;
113         }
114         else if (state == THIRD_POINT)
115         {
116                 // We create the new object here, and then pass it off to the
117                 // DrawingView which stuffs it into the document.
118                 arc = new Arc(p1, radius, startAngle, span);
119                 // We don't need no stinkin' sentinels, when we have signals & slots!
120                 emit ObjectReady(arc);
121
122                 state = FIRST_POINT;
123         }
124 }
125