]> Shamusworld >> Repos - architektonas/blob - src/drawlineaction.cpp
e14ba81ca1f440876c5e60dabba0cfe9b95c7962
[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 //#include "vector.h"
19
20
21 //#define FIRST_POINT 0
22 //#define NEXT_POINT 1
23 enum { FIRST_POINT, NEXT_POINT };
24
25
26 DrawLineAction::DrawLineAction(): state(FIRST_POINT), line(NULL),
27         shiftWasPressedOnNextPoint(false)
28 {
29 }
30
31
32 DrawLineAction::~DrawLineAction()
33 {
34 }
35
36
37 /*virtual*/ void DrawLineAction::Draw(Painter * painter)
38 {
39         painter->SetPen(QPen(Qt::red, 2.0, Qt::DotLine));
40
41         // I think stuff like crosshairs should be done in the DrawingView, tho
42         // (and it's done there now...)
43         if (state == FIRST_POINT)
44         {
45                 painter->DrawHandle(p1);
46         }
47         else
48         {
49                 painter->DrawLine(p1, p2);
50                 painter->DrawHandle(p2);
51
52                 QString text = tr("Length: %1 in.");
53                 text = text.arg(Vector::Magnitude(p1, p2));
54                 painter->DrawInformativeText(text);
55         }
56 }
57
58
59 /*virtual*/ void DrawLineAction::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 DrawLineAction::MouseMoved(Vector point)
72 {
73         if (state == FIRST_POINT)
74                 p1 = point;
75         else
76                 p2 = point;
77 }
78
79
80 /*virtual*/ void DrawLineAction::MouseReleased(void)
81 {
82         if (state == FIRST_POINT)
83         {
84                 p2 = p1;
85                 state = NEXT_POINT;
86         }
87         else if (state == NEXT_POINT)
88         {
89                 // We create the new object here, and then pass it off to the
90                 // DrawingView which stuffs it into the document.
91                 line = new Line(p1, p2);
92                 // We don't need no stinkin' sentinels, when we have signals & slots!
93                 emit ObjectReady(line);
94
95                 p1 = p2;
96                 state = NEXT_POINT;
97         }
98 }
99
100
101 /*virtual*/ bool DrawLineAction::KeyDown(int key)
102 {
103         if ((key == Qt::Key_Shift) && (state == NEXT_POINT))
104         {
105                 shiftWasPressedOnNextPoint = true;
106                 p1Save = p1;
107                 p1 = p2;
108                 state = FIRST_POINT;
109                 return true;
110         }
111
112         return false;
113 }
114
115
116 /*virtual*/ bool DrawLineAction::KeyReleased(int key)
117 {
118         if ((key == Qt::Key_Shift) && shiftWasPressedOnNextPoint)
119         {
120                 shiftWasPressedOnNextPoint = false;
121                 p2 = p1;
122                 p1 = p1Save;
123                 state = NEXT_POINT;
124                 return true;
125         }
126
127         return false;
128 }
129