]> Shamusworld >> Repos - architektonas/blob - src/actions/actiondrawlinetangent1.cpp
Last checkin before major refactor...
[architektonas] / src / actions / actiondrawlinetangent1.cpp
1 // actiondrawlinetangent1.cpp
2 //
3 // Part of the Architektonas Project
4 // Originally part of QCad Community Edition by Andrew Mustun
5 // Extensively rewritten and refactored by James L. Hammons
6 // (C) 2010 Underground Software
7 //
8 // JLH = James L. Hammons <jlhamm@acm.org>
9 //
10 // Who  When        What
11 // ---  ----------  -----------------------------------------------------------
12 // JLH  06/04/2010  Added this text. :-)
13 //
14
15 #include "actiondrawlinetangent1.h"
16
17 #include "rs_creation.h"
18 #include "rs_dialogfactory.h"
19 #include "graphicview.h"
20 #include "rs_preview.h"
21
22 ActionDrawLineTangent1::ActionDrawLineTangent1(RS_EntityContainer & container, GraphicView & graphicView): ActionInterface("Draw Tangents 1",
23                 container, graphicView)
24 {
25         tangent = NULL;
26         point = Vector(false);
27         circle = NULL;
28 }
29
30 ActionDrawLineTangent1::~ActionDrawLineTangent1()
31 {
32 }
33
34 void ActionDrawLineTangent1::trigger()
35 {
36         ActionInterface::trigger();
37
38         if (tangent)
39         {
40                 RS_Entity * newEntity = NULL;
41                 newEntity = new RS_Line(container, tangent->getData());
42
43                 if (newEntity)
44                 {
45                         newEntity->setLayerToActive();
46                         newEntity->setPenToActive();
47                         container->addEntity(newEntity);
48
49                         // upd. undo list:
50                         if (document)
51                         {
52                                 document->startUndoCycle();
53                                 document->addUndoable(newEntity);
54                                 document->endUndoCycle();
55                         }
56
57                         deleteSnapper();
58                         graphicView->drawEntity(newEntity);
59
60                         setStatus(SetPoint);
61                 }
62
63                 delete tangent;
64                 tangent = NULL;
65         }
66         else
67                 RS_DEBUG->print("ActionDrawLineTangent1::trigger:"
68                         " Entity is NULL\n");
69 }
70
71 void ActionDrawLineTangent1::mouseMoveEvent(QMouseEvent * e)
72 {
73         RS_DEBUG->print("ActionDrawLineTangent1::mouseMoveEvent begin");
74         Vector mouse(graphicView->toGraphX(e->x()), graphicView->toGraphY(e->y()));
75
76         switch (getStatus())
77         {
78         case SetPoint:
79                 point = snapPoint(e);
80                 break;
81
82         case SetCircle:
83         {
84                 RS_Entity * en = catchEntity(e, RS2::ResolveAll);
85
86                 if (en && (en->rtti() == RS2::EntityCircle
87                         || en->rtti() == RS2::EntityArc
88                         || en->rtti() == RS2::EntityEllipse))
89                 {
90                         circle = en;
91                         RS_Creation creation(NULL, NULL);
92                         RS_Line * t = creation.createTangent1(mouse, point, circle);
93
94                         if (t)
95                         {
96                                 if (tangent)
97                                         delete tangent;
98
99                                 tangent = (RS_Line *)t->clone();
100
101                                 deletePreview();
102                                 clearPreview();
103 //                              preview->addEntity(t);
104                                 drawPreview();
105                         }
106                 }
107         }
108                 break;
109
110         default:
111                 break;
112         }
113
114         RS_DEBUG->print("ActionDrawLineTangent1::mouseMoveEvent end");
115 }
116
117 void ActionDrawLineTangent1::mouseReleaseEvent(QMouseEvent * e)
118 {
119         if (e->button() == Qt::RightButton)
120         {
121                 deletePreview();
122                 clearPreview();
123                 deleteSnapper();
124                 init(getStatus() - 1);
125         }
126         else
127         {
128                 switch (getStatus())
129                 {
130                 case SetPoint:
131                 {
132                         Vector ce(snapPoint(e));
133                         coordinateEvent(&ce);
134                 }
135                         break;
136
137                 case SetCircle:
138                         trigger();
139                         break;
140                 }
141         }
142 }
143
144 void ActionDrawLineTangent1::coordinateEvent(Vector * e)
145 {
146         if (e == NULL)
147                 return;
148
149         switch (getStatus())
150         {
151         case SetPoint:
152                 point = *e;
153                 deleteSnapper();
154                 graphicView->moveRelativeZero(point);
155                 setStatus(SetCircle);
156                 break;
157
158         default:
159                 break;
160         }
161 }
162
163 void ActionDrawLineTangent1::updateMouseButtonHints()
164 {
165         if (RS_DIALOGFACTORY != NULL)
166         {
167                 switch (getStatus())
168                 {
169                 case SetPoint:
170                         RS_DIALOGFACTORY->updateMouseWidget(tr("Specify point"), tr("Cancel"));
171                         break;
172
173                 case SetCircle:
174                         RS_DIALOGFACTORY->updateMouseWidget(tr("Select circle, arc or ellipse"),
175                                 tr("Back"));
176                         break;
177
178                 default:
179                         RS_DIALOGFACTORY->updateMouseWidget("", "");
180                         break;
181                 }
182         }
183 }
184
185 void ActionDrawLineTangent1::updateMouseCursor()
186 {
187         graphicView->setMouseCursor(RS2::CadCursor);
188 }
189
190 void ActionDrawLineTangent1::updateToolBar()
191 {
192         if (RS_DIALOGFACTORY)
193         {
194                 if (!isFinished())
195                         RS_DIALOGFACTORY->requestToolBar(RS2::ToolBarSnap);
196                 else
197                         RS_DIALOGFACTORY->requestToolBar(RS2::ToolBarLines);
198         }
199 }