]> Shamusworld >> Repos - architektonas/blob - src/actions/rs_actiondrawlinehorvert.cpp
Refactoring: Moved RS_GraphicView to GraphicView.
[architektonas] / src / actions / rs_actiondrawlinehorvert.cpp
1 // rs_actiondrawlinehorvert.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 "rs_actiondrawlinehorvert.h"
16
17 #include "rs_dialogfactory.h"
18 #include "graphicview.h"
19 #include "rs_preview.h"
20
21 RS_ActionDrawLineHorVert::RS_ActionDrawLineHorVert(RS_EntityContainer & container, GraphicView & graphicView): RS_PreviewActionInterface("Draw horizontal/vertical lines",
22                 container, graphicView)
23 {
24         reset();
25         RS_DEBUG->print("RS_ActionDrawLineHorVert::constructor");
26 }
27
28 RS_ActionDrawLineHorVert::~RS_ActionDrawLineHorVert()
29 {
30 }
31
32 void RS_ActionDrawLineHorVert::reset()
33 {
34         data = RS_LineData(Vector(false), Vector(false));
35 }
36
37 void RS_ActionDrawLineHorVert::init(int status)
38 {
39         RS_PreviewActionInterface::init(status);
40         reset();
41         RS_DEBUG->print("RS_ActionDrawLineHorVert::init");
42 }
43
44 void RS_ActionDrawLineHorVert::trigger()
45 {
46         RS_PreviewActionInterface::trigger();
47
48         RS_Line * line = new RS_Line(container, data);
49         line->setLayerToActive();
50         line->setPenToActive();
51         container->addEntity(line);
52
53         // upd. undo list:
54         if (document != NULL)
55         {
56                 document->startUndoCycle();
57                 document->addUndoable(line);
58                 document->endUndoCycle();
59         }
60
61         deleteSnapper();
62         graphicView->moveRelativeZero(Vector(0.0, 0.0));
63         graphicView->drawEntity(line);
64         graphicView->moveRelativeZero(line->getMiddlepoint());
65         RS_DEBUG->print("RS_ActionDrawLineHorVert::trigger():"
66                 " line added: %d", line->getId());
67 }
68
69 void RS_ActionDrawLineHorVert::mouseMoveEvent(QMouseEvent * e)
70 {
71         RS_DEBUG->print("RS_ActionDrawLineHorVert::mouseMoveEvent begin");
72
73         Vector mouse = snapPoint(e);
74
75         if (getStatus() == SetEndpoint && p1.valid)
76         {
77                 Vector p2x = Vector(mouse.x, p1.y);
78                 Vector p2y = Vector(p1.x, mouse.y);
79
80                 if (mouse.distanceTo(p2y) > mouse.distanceTo(p2x))
81                         p2 = p2x;
82                 else
83                         p2 = p2y;
84
85                 deletePreview();
86                 clearPreview();
87                 data = RS_LineData(p1, p2);
88                 preview->addEntity(new RS_Line(preview, data));
89                 drawPreview();
90         }
91
92         RS_DEBUG->print("RS_ActionDrawLineHorVert::mouseMoveEvent end");
93 }
94
95 void RS_ActionDrawLineHorVert::mouseReleaseEvent(QMouseEvent * e)
96 {
97 //      if (RS2::qtToRsButtonState(e->button())==RS2::LeftButton)
98         if (e->button() == Qt::LeftButton)
99         {
100                 Vector mouse = snapPoint(e);
101
102                 switch (getStatus())
103                 {
104                 case SetStartpoint:
105                         p1 = mouse;
106                         setStatus(SetEndpoint);
107                         break;
108
109                 case SetEndpoint:
110                         p2 = mouse;
111                         trigger();
112                         setStatus(SetStartpoint);
113                         break;
114
115                 default:
116                         break;
117                 }
118         }
119 //      else if (RS2::qtToRsButtonState(e->button())==RS2::RightButton)
120         else if (e->button() == Qt::RightButton)
121         {
122                 deletePreview();
123                 deleteSnapper();
124                 init(getStatus() - 1);
125         }
126 }
127
128 void RS_ActionDrawLineHorVert::updateMouseButtonHints()
129 {
130         switch (getStatus())
131         {
132         case SetStartpoint:
133                 RS_DIALOGFACTORY->updateMouseWidget(tr("Specify first point"), tr("Cancel"));
134                 break;
135
136         case SetEndpoint:
137                 RS_DIALOGFACTORY->updateMouseWidget(tr("Specify second point"), tr("Back"));
138                 break;
139
140         default:
141                 RS_DIALOGFACTORY->updateMouseWidget("", "");
142                 break;
143         }
144 }
145
146 void RS_ActionDrawLineHorVert::updateMouseCursor()
147 {
148         graphicView->setMouseCursor(RS2::CadCursor);
149 }
150
151 void RS_ActionDrawLineHorVert::updateToolBar()
152 {
153         if (!isFinished())
154                 RS_DIALOGFACTORY->requestToolBar(RS2::ToolBarSnap);
155         else
156                 RS_DIALOGFACTORY->requestToolBar(RS2::ToolBarLines);
157 }
158