]> Shamusworld >> Repos - architektonas/blob - src/mirroraction.cpp
720692ed85130725ac3c2170206fa2ec251463ea
[architektonas] / src / mirroraction.cpp
1 // mirroraction.cpp: Action class for mirroring selected objects
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/27/2011  Created this file
12 //
13
14 #include "mirroraction.h"
15 #include "applicationwindow.h"
16 #include "container.h"
17 #include "drawingview.h"
18 #include "line.h"
19 #include "mathconstants.h"
20 #include "painter.h"
21 //#include "vector.h"
22
23
24 //#define FIRST_POINT 0
25 //#define NEXT_POINT 1
26 enum { FIRST_POINT, NEXT_POINT };
27
28
29 MirrorAction::MirrorAction(): state(FIRST_POINT), line(NULL),
30         shiftWasPressedOnNextPoint(false), mirror(new Container(Vector()))
31 {
32 }
33
34
35 MirrorAction::~MirrorAction()
36 {
37 }
38
39
40 /*virtual*/ void MirrorAction::Draw(Painter * painter)
41 {
42         painter->SetPen(QPen(Qt::red, 2.0, Qt::DotLine));
43
44         // I think stuff like crosshairs should be done in the DrawingView, tho
45         // (and it's done there now...)
46         if (state == FIRST_POINT)
47         {
48                 painter->DrawHandle(p1);
49         }
50         else
51         {
52                 painter->DrawLine(p1, p2);
53                 painter->DrawHandle(p2);
54
55                 double absAngle = (Vector(p2 - p1).Angle()) * RADIANS_TO_DEGREES;
56 //              double absLength = Vector(position - endpoint).Magnitude();
57
58                 QString text = QChar(0x2221) + QObject::tr(": %1");
59                 text = text.arg(absAngle);
60 //              QString text = tr("Length: %1 in.");
61 //              text = text.arg(Vector::Magnitude(p1, p2));
62                 painter->DrawInformativeText(text);
63
64                 mirror->Draw(painter);
65         }
66 }
67
68
69 /*virtual*/ void MirrorAction::MouseDown(Vector point)
70 {
71         // Clear our override...
72         shiftWasPressedOnNextPoint = false;
73
74         if (state == FIRST_POINT)
75                 p1 = point;
76         else
77                 p2 = point;
78 }
79
80
81 /*virtual*/ void MirrorAction::MouseMoved(Vector point)
82 {
83         if (state == FIRST_POINT)
84                 p1 = point;
85         else
86         {
87                 p2 = point;
88
89                 mirror->Clear();
90                 int itemsSelected = ApplicationWindow::drawing->document.ItemsSelected();
91
92                 if (itemsSelected == 0)
93                         return;
94
95                 for(int i=0; i<itemsSelected; i++)
96                 {
97                         Object * object = ApplicationWindow::drawing->document.SelectedItem(i);
98                         Object * mirrored = object->Mirror(p1, p2);
99                         mirror->Add(mirrored);
100                 }
101         }
102 }
103
104
105 /*virtual*/ void MirrorAction::MouseReleased(void)
106 {
107         if (state == FIRST_POINT)
108         {
109                 p2 = p1;
110                 state = NEXT_POINT;
111         }
112         else if (state == NEXT_POINT)
113         {
114                 // We create the new object here, and then pass it off to the
115                 // DrawingView which stuffs it into the document.
116 //              line = new Line(p1, p2);
117                 // We don't need no stinkin' sentinels, when we have signals & slots!
118 //              emit ObjectReady(line);
119
120                 p1 = p2;
121                 state = NEXT_POINT;
122         }
123 }
124
125
126 /*virtual*/ bool MirrorAction::KeyDown(int key)
127 {
128         if ((key == Qt::Key_Shift) && (state == NEXT_POINT))
129         {
130                 shiftWasPressedOnNextPoint = true;
131                 p1Save = p1;
132                 p1 = p2;
133                 state = FIRST_POINT;
134                 return true;
135         }
136
137         return false;
138 }
139
140
141 /*virtual*/ bool MirrorAction::KeyReleased(int key)
142 {
143         if ((key == Qt::Key_Shift) && shiftWasPressedOnNextPoint)
144         {
145                 shiftWasPressedOnNextPoint = false;
146                 p2 = p1;
147                 p1 = p1Save;
148                 state = NEXT_POINT;
149                 return true;
150         }
151
152         return false;
153 }
154