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