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