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