]> Shamusworld >> Repos - architektonas/blob - src/actions/actiondrawlinehorvert.cpp
GPL compliance check...
[architektonas] / src / actions / actiondrawlinehorvert.cpp
1 // 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 // Portions copyright (C) 2001-2003 RibbonSoft
7 // Copyright (C) 2010 Underground Software
8 // See the README and GPLv2 files for licensing and warranty information
9 //
10 // JLH = James L. Hammons <jlhamm@acm.org>
11 //
12 // Who  When        What
13 // ---  ----------  -----------------------------------------------------------
14 // JLH  06/04/2010  Added this text. :-)
15 //
16
17 #include "actiondrawlinehorvert.h"
18
19 #include "rs_dialogfactory.h"
20 #include "graphicview.h"
21 #include "rs_preview.h"
22
23 ActionDrawLineHorVert::ActionDrawLineHorVert(RS_EntityContainer & container, GraphicView & graphicView): ActionInterface("Draw horizontal/vertical lines",
24                 container, graphicView)
25 {
26         reset();
27         RS_DEBUG->print("ActionDrawLineHorVert::constructor");
28 }
29
30 ActionDrawLineHorVert::~ActionDrawLineHorVert()
31 {
32 }
33
34 void ActionDrawLineHorVert::reset()
35 {
36         data = RS_LineData(Vector(false), Vector(false));
37 }
38
39 void ActionDrawLineHorVert::init(int status)
40 {
41         ActionInterface::init(status);
42         reset();
43         RS_DEBUG->print("ActionDrawLineHorVert::init");
44 }
45
46 void ActionDrawLineHorVert::trigger()
47 {
48         ActionInterface::trigger();
49
50         RS_Line * line = new RS_Line(container, data);
51         line->setLayerToActive();
52         line->setPenToActive();
53         container->addEntity(line);
54
55         // upd. undo list:
56         if (document != NULL)
57         {
58                 document->startUndoCycle();
59                 document->addUndoable(line);
60                 document->endUndoCycle();
61         }
62
63         deleteSnapper();
64         graphicView->moveRelativeZero(Vector(0.0, 0.0));
65         graphicView->drawEntity(line);
66         graphicView->moveRelativeZero(line->getMiddlepoint());
67         RS_DEBUG->print("ActionDrawLineHorVert::trigger():"
68                 " line added: %d", line->getId());
69 }
70
71 void ActionDrawLineHorVert::mouseMoveEvent(QMouseEvent * e)
72 {
73         RS_DEBUG->print("ActionDrawLineHorVert::mouseMoveEvent begin");
74
75         Vector mouse = snapPoint(e);
76
77         if (getStatus() == SetEndpoint && p1.valid)
78         {
79                 Vector p2x = Vector(mouse.x, p1.y);
80                 Vector p2y = Vector(p1.x, mouse.y);
81
82                 if (mouse.distanceTo(p2y) > mouse.distanceTo(p2x))
83                         p2 = p2x;
84                 else
85                         p2 = p2y;
86
87                 deletePreview();
88                 clearPreview();
89                 data = RS_LineData(p1, p2);
90 //              preview->addEntity(new RS_Line(preview, data));
91                 drawPreview();
92         }
93
94         RS_DEBUG->print("ActionDrawLineHorVert::mouseMoveEvent end");
95 }
96
97 void ActionDrawLineHorVert::mouseReleaseEvent(QMouseEvent * e)
98 {
99         if (e->button() == Qt::LeftButton)
100         {
101                 Vector mouse = snapPoint(e);
102
103                 switch (getStatus())
104                 {
105                 case SetStartpoint:
106                         p1 = mouse;
107                         setStatus(SetEndpoint);
108                         break;
109
110                 case SetEndpoint:
111                         p2 = mouse;
112                         trigger();
113                         setStatus(SetStartpoint);
114                         break;
115
116                 default:
117                         break;
118                 }
119         }
120         else if (e->button() == Qt::RightButton)
121         {
122                 deletePreview();
123                 deleteSnapper();
124                 init(getStatus() - 1);
125         }
126 }
127
128 void 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 ActionDrawLineHorVert::updateMouseCursor()
147 {
148         graphicView->setMouseCursor(RS2::CadCursor);
149 }
150
151 void ActionDrawLineHorVert::updateToolBar()
152 {
153         if (!isFinished())
154                 RS_DIALOGFACTORY->requestToolBar(RS2::ToolBarSnap);
155         else
156                 RS_DIALOGFACTORY->requestToolBar(RS2::ToolBarLines);
157 }