]> Shamusworld >> Repos - architektonas/blob - src/mirroraction.cpp
Changed Actions to emit signal when needing a graphical update.
[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), ctrlWasPressed(false),
31         mirror(new Container(Vector()))
32 {
33         ApplicationWindow::drawing->document.CopySelectedContentsTo(mirror);
34         mirror->Save();
35 }
36
37
38 MirrorAction::~MirrorAction()
39 {
40 }
41
42
43 /*virtual*/ void MirrorAction::Draw(Painter * painter)
44 {
45         painter->SetPen(QPen(Qt::red, 2.0, Qt::DotLine));
46
47         // I think stuff like crosshairs should be done in the DrawingView, tho
48         // (and it's done there now...)
49         if (state == FIRST_POINT)
50         {
51                 painter->DrawHandle(p1);
52         }
53         else
54         {
55                 Vector reflectedP2 = -(p2 - p1);
56                 Point newP2 = p1 + reflectedP2;
57                 painter->DrawLine(newP2, p2);
58                 painter->DrawHandle(p1);
59
60                 double absAngle = (Vector(p2 - p1).Angle()) * RADIANS_TO_DEGREES;
61
62                 // Keep the angle between 0 and 180 degrees
63                 if (absAngle > 180.0)
64                         absAngle -= 180.0;
65
66                 QString text = QChar(0x2221) + QObject::tr(": %1");
67                 text = text.arg(absAngle);
68
69                 if (ctrlWasPressed)
70                         text += " (Copy)";
71
72                 painter->DrawInformativeText(text);
73
74                 // Draw the mirror only if there's been a line to mirror around
75                 if (p1 != p2)
76                         mirror->Draw(painter);
77         }
78 }
79
80
81 /*virtual*/ void MirrorAction::MouseDown(Vector point)
82 {
83         // Clear our override...
84         shiftWasPressedOnNextPoint = false;
85
86         if (state == FIRST_POINT)
87                 p1 = point;
88         else
89                 p2 = point;
90 }
91
92
93 /*virtual*/ void MirrorAction::MouseMoved(Vector point)
94 {
95         if (state == FIRST_POINT)
96                 p1 = point;
97         else
98         {
99                 p2 = point;
100                 mirror->Restore();
101                 mirror->Mirror(p1, p2);
102         }
103 }
104
105
106 /*virtual*/ void MirrorAction::MouseReleased(void)
107 {
108         if (state == FIRST_POINT)
109         {
110                 p2 = p1;
111                 state = NEXT_POINT;
112         }
113         else if (state == NEXT_POINT)
114         {
115                 if (!ctrlWasPressed)
116                 {
117                         state = FIRST_POINT;
118                         ApplicationWindow::drawing->document.MirrorSelected(p1, p2);
119
120                         mirror->Clear();
121                         ApplicationWindow::drawing->document.CopySelectedContentsTo(mirror);
122                         mirror->Save();
123                 }
124                 else
125                 {
126                         mirror->CopyContentsTo(&(ApplicationWindow::drawing->document));
127 //                      mirror->CopyContentsTo(&(DrawingView.document));
128                 }
129         }
130 }
131
132
133 /*virtual*/ void MirrorAction::KeyDown(int key)
134 {
135         if ((key == Qt::Key_Shift) && (state == NEXT_POINT))
136         {
137                 shiftWasPressedOnNextPoint = true;
138                 p1Save = p1;
139                 p1 = p2;
140                 state = FIRST_POINT;
141                 emit(NeedRefresh());
142         }
143         else if (key == Qt::Key_Control)
144         {
145                 ctrlWasPressed = true;
146                 emit(NeedRefresh());
147         }
148 }
149
150
151 /*virtual*/ void MirrorAction::KeyReleased(int key)
152 {
153         if ((key == Qt::Key_Shift) && shiftWasPressedOnNextPoint)
154         {
155                 shiftWasPressedOnNextPoint = false;
156                 p2 = p1;
157                 p1 = p1Save;
158                 state = NEXT_POINT;
159                 emit(NeedRefresh());
160         }
161         else if (key == Qt::Key_Control)
162         {
163                 ctrlWasPressed = false;
164                 emit(NeedRefresh());
165         }
166 }
167