]> Shamusworld >> Repos - architektonas/blob - src/drawsplineaction.cpp
Added new Spline object and Add Spline tool to GUI.
[architektonas] / src / drawsplineaction.cpp
1 // drawsplineaction.cpp: Action class for drawing NURBS
2 //
3 // Part of the Architektonas Project
4 // (C) 2014 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  03/20/2014  Created this file
12 //
13
14 #include "drawsplineaction.h"
15 #include "spline.h"
16 #include "mathconstants.h"
17 #include "painter.h"
18
19
20 enum { FIRST_POINT, NEXT_POINT };
21
22
23 DrawSplineAction::DrawSplineAction(): state(FIRST_POINT), spline(NULL),
24         shiftWasPressedOnNextPoint(false)
25 {
26 }
27
28
29 DrawSplineAction::~DrawSplineAction()
30 {
31 }
32
33
34 /*virtual*/ void DrawSplineAction::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's done there now...)
40         if (state == FIRST_POINT)
41         {
42                 painter->DrawHandle(p1);
43         }
44         else
45         {
46                 painter->DrawLine(p1, p2);
47                 painter->DrawHandle(p2);
48
49                 Vector v(p1, p2);
50                 double absAngle = v.Angle() * RADIANS_TO_DEGREES;
51                 double absLength = v.Magnitude();
52                 QString text = tr("Length: %1 in.\n") + QChar(0x2221) + tr(": %2");
53                 text = text.arg(absLength).arg(absAngle);
54                 painter->DrawInformativeText(text);
55         }
56 }
57
58
59 /*virtual*/ void DrawSplineAction::MouseDown(Vector point)
60 {
61         // Clear our override...
62         shiftWasPressedOnNextPoint = false;
63
64         if (state == FIRST_POINT)
65                 p1 = point;
66         else
67                 p2 = point;
68 }
69
70
71 /*virtual*/ void DrawSplineAction::MouseMoved(Vector point)
72 {
73         if (state == FIRST_POINT)
74                 p1 = point;
75         else
76                 p2 = point;
77 }
78
79
80 /*virtual*/ void DrawSplineAction::MouseReleased(void)
81 {
82         if (state == FIRST_POINT)
83         {
84                 p2 = p1;
85                 state = NEXT_POINT;
86                 spline = NULL;
87         }
88         else if (state == NEXT_POINT)
89         {
90                 Spline * oldSpline = spline;
91                 // We create the new object here, and then pass it off to the
92                 // DrawingView which stuffs it into the document.
93                 spline = new Spline(p1, p2.Angle());
94
95                 // Connect Lines by default
96                 if (oldSpline)
97                 {
98                         oldSpline->Connect(spline, 0);
99                         spline->Connect(oldSpline, 1.0);
100                 }
101
102                 // We don't need no stinkin' sentinels, when we have signals & slots!
103                 emit ObjectReady(spline);
104
105                 p1 = p2;
106                 state = NEXT_POINT;
107         }
108 }
109
110
111 /*virtual*/ void DrawSplineAction::KeyDown(int key)
112 {
113         if ((key == Qt::Key_Shift) && (state == NEXT_POINT))
114         {
115                 shiftWasPressedOnNextPoint = true;
116                 p1Save = p1;
117                 p1 = p2;
118                 state = FIRST_POINT;
119                 emit NeedRefresh();
120         }
121 }
122
123
124 /*virtual*/ void DrawSplineAction::KeyReleased(int key)
125 {
126         if ((key == Qt::Key_Shift) && shiftWasPressedOnNextPoint)
127         {
128                 shiftWasPressedOnNextPoint = false;
129                 p2 = p1;
130                 p1 = p1Save;
131                 state = NEXT_POINT;
132                 emit(NeedRefresh());
133         }
134 }
135