]> Shamusworld >> Repos - architektonas/blob - src/drawlineaction.cpp
5490e9829bcc553fc71fbd8213c402a4b66a74b8
[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 "painter.h"
18
19
20 enum { FIRST_POINT, NEXT_POINT };
21
22
23 DrawLineAction::DrawLineAction(): state(FIRST_POINT), line(NULL),
24         shiftWasPressedOnNextPoint(false)
25 {
26 }
27
28
29 DrawLineAction::~DrawLineAction()
30 {
31 }
32
33
34 /*virtual*/ void DrawLineAction::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                 QString text = tr("Length: %1 in.");
50                 text = text.arg(Vector::Magnitude(p1, p2));
51                 painter->DrawInformativeText(text);
52         }
53 }
54
55
56 /*virtual*/ void DrawLineAction::MouseDown(Vector point)
57 {
58         // Clear our override...
59         shiftWasPressedOnNextPoint = false;
60
61         if (state == FIRST_POINT)
62                 p1 = point;
63         else
64                 p2 = point;
65 }
66
67
68 /*virtual*/ void DrawLineAction::MouseMoved(Vector point)
69 {
70         if (state == FIRST_POINT)
71                 p1 = point;
72         else
73                 p2 = point;
74 }
75
76
77 /*virtual*/ void DrawLineAction::MouseReleased(void)
78 {
79         if (state == FIRST_POINT)
80         {
81                 p2 = p1;
82                 state = NEXT_POINT;
83                 line = NULL;
84         }
85         else if (state == NEXT_POINT)
86         {
87                 Line * oldLine = line;
88                 // We create the new object here, and then pass it off to the
89                 // DrawingView which stuffs it into the document.
90                 line = new Line(p1, p2);
91
92                 // Connect Lines by default
93                 if (oldLine)
94                 {
95                         oldLine->Connect(line, 0);
96                         line->Connect(oldLine, 1.0);
97                 }
98
99                 // We don't need no stinkin' sentinels, when we have signals & slots!
100                 emit ObjectReady(line);
101
102                 p1 = p2;
103                 state = NEXT_POINT;
104         }
105 }
106
107
108 /*virtual*/ void DrawLineAction::KeyDown(int key)
109 {
110         if ((key == Qt::Key_Shift) && (state == NEXT_POINT))
111         {
112                 shiftWasPressedOnNextPoint = true;
113                 p1Save = p1;
114                 p1 = p2;
115                 state = FIRST_POINT;
116                 emit NeedRefresh();
117         }
118 }
119
120
121 /*virtual*/ void DrawLineAction::KeyReleased(int key)
122 {
123         if ((key == Qt::Key_Shift) && shiftWasPressedOnNextPoint)
124         {
125                 shiftWasPressedOnNextPoint = false;
126                 p2 = p1;
127                 p1 = p1Save;
128                 state = NEXT_POINT;
129                 emit(NeedRefresh());
130         }
131 }
132