]> Shamusworld >> Repos - architektonas/blob - src/rotateaction.cpp
071b31d93ba321a712ea97b7961090c029b92650
[architektonas] / src / rotateaction.cpp
1 // rotateaction.cpp: Action class for rotating 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  09/03/2011  Created this file
12 //
13
14 #include "rotateaction.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
22
23 enum { FIRST_POINT, NEXT_POINT };
24
25
26 RotateAction::RotateAction(): state(FIRST_POINT), line(NULL),
27         shiftWasPressedOnNextPoint(false), ctrlWasPressed(false),
28         spin(new Container(Vector()))
29 {
30         ApplicationWindow::drawing->document.CopySelectedContentsTo(spin);
31         spin->Save();
32 }
33
34
35 RotateAction::~RotateAction()
36 {
37 }
38
39
40 /*virtual*/ void RotateAction::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->DrawHandle(p1);
53
54                 // Draw the rotated objects only if there's been a line to spin around
55                 if (p1 == p2)
56                         return;
57
58                 painter->DrawLine(p1, p2);
59
60                 double absAngle = (Vector(p2 - p1).Angle()) * RADIANS_TO_DEGREES;
61
62                 QString text = QChar(0x2221) + QObject::tr(": %1");
63                 text = text.arg(absAngle);
64
65                 if (ctrlWasPressed)
66                         text += " (Copy)";
67
68                 painter->DrawInformativeText(text);
69
70 // this does nothing, because the objects override any color set here with
71 // their own pens... :-/
72 // Which means we have to have a flag in the object to tell it not to color itself.
73 //              if (ctrlWasPressed)
74 //                      painter->SetPen(QPen(Qt::green, 3.0, Qt::SolidLine));
75
76                 spin->Draw(painter);
77         }
78 }
79
80
81 /*virtual*/ void RotateAction::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 RotateAction::MouseMoved(Vector point)
94 {
95         if (state == FIRST_POINT)
96                 p1 = point;
97         else
98         {
99                 p2 = point;
100                 double angle = Vector(p2, p1).Angle();
101                 spin->Restore();
102                 spin->Rotate(p1, angle);
103         }
104 }
105
106
107 /*virtual*/ void RotateAction::MouseReleased(void)
108 {
109         if (state == FIRST_POINT)
110         {
111                 p2 = p1;
112                 state = NEXT_POINT;
113         }
114         else if (state == NEXT_POINT)
115         {
116                 if (!ctrlWasPressed)
117                 {
118                         state = FIRST_POINT;
119                         double angle = Vector(p2, p1).Angle();
120                         ApplicationWindow::drawing->document.RotateSelected(p1, angle);
121
122                         spin->Clear();
123                         ApplicationWindow::drawing->document.CopySelectedContentsTo(spin);
124                         spin->Save();
125                 }
126                 else
127                 {
128                         spin->CopyContentsTo(&(ApplicationWindow::drawing->document));
129                 }
130         }
131 }
132
133
134 /*virtual*/ void RotateAction::KeyDown(int key)
135 {
136         if ((key == Qt::Key_Shift) && (state == NEXT_POINT))
137         {
138                 shiftWasPressedOnNextPoint = true;
139                 p1Save = p1;
140                 p1 = p2;
141                 state = FIRST_POINT;
142                 emit(NeedRefresh());
143         }
144         else if (key == Qt::Key_Control)
145         {
146                 ctrlWasPressed = true;
147                 emit(NeedRefresh());
148         }
149 }
150
151
152 /*virtual*/ void RotateAction::KeyReleased(int key)
153 {
154         if ((key == Qt::Key_Shift) && shiftWasPressedOnNextPoint)
155         {
156                 shiftWasPressedOnNextPoint = false;
157                 p2 = p1;
158                 p1 = p1Save;
159                 state = NEXT_POINT;
160                 emit(NeedRefresh());
161         }
162         else if (key == Qt::Key_Control)
163         {
164                 ctrlWasPressed = false;
165                 emit(NeedRefresh());
166         }
167 }
168