1 // rs_actiondimlinear.cpp
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
8 // JLH = James L. Hammons <jlhamm@acm.org>
11 // --- ---------- -----------------------------------------------------------
12 // JLH 05/22/2010 Added this text. :-)
15 #include "rs_actiondimlinear.h"
17 #include "rs_snapper.h"
18 #include "rs_constructionline.h"
19 #include "rs_dialogfactory.h"
24 * @param angle Initial angle in rad.
25 * @param fixedAngle true: The user can't change the angle.
26 * false: The user can change the angle in a option widget.
28 RS_ActionDimLinear::RS_ActionDimLinear(RS_EntityContainer& container,
29 RS_GraphicView& graphicView, double angle, bool fixedAngle):
30 RS_ActionDimension("Draw linear dimensions", container, graphicView)
33 this->fixedAngle = fixedAngle;
35 lastStatus = SetExtPoint1;
40 RS_ActionDimLinear::~RS_ActionDimLinear()
44 QAction * RS_ActionDimLinear::createGUIAction(RS2::ActionType type, QObject * /*parent*/)
51 case RS2::ActionDimLinear:
52 action = new QAction(tr("&Linear"), 0);
53 // action = new QAction(tr("Linear"), tr("&Linear"),
54 // QKeySequence(), NULL);
55 action->setStatusTip(tr("Linear Dimension"));
58 case RS2::ActionDimLinearHor:
59 action = new QAction(tr("&Horizontal"), 0);
60 // action = new QAction(tr("Horizontal"), tr("&Horizontal"),
61 // QKeySequence(), NULL);
62 action->setStatusTip(tr("Horizontal Dimension"));
65 case RS2::ActionDimLinearVer:
66 action = new QAction(tr("&Vertical"), 0);
67 // action = new QAction(tr("Vertical"), tr("&Vertical"),
68 // QKeySequence(), NULL);
69 action->setStatusTip(tr("Vertical Dimension"));
77 void RS_ActionDimLinear::reset() {
78 RS_ActionDimension::reset();
80 edata = RS_DimLinearData(Vector(false),
82 (fixedAngle ? edata.angle : 0.0), 0.0);
84 if (RS_DIALOGFACTORY!=NULL) {
85 RS_DIALOGFACTORY->requestOptions(this, true, true);
91 void RS_ActionDimLinear::trigger() {
92 RS_ActionDimension::trigger();
95 RS_DimLinear* dim = new RS_DimLinear(container, data, edata);
96 dim->setLayerToActive();
97 dim->setPenToActive();
99 container->addEntity(dim);
102 if (document!=NULL) {
103 document->startUndoCycle();
104 document->addUndoable(dim);
105 document->endUndoCycle();
109 Vector rz = graphicView->getRelativeZero();
110 graphicView->moveRelativeZero(Vector(0.0,0.0));
111 graphicView->drawEntity(dim);
112 graphicView->moveRelativeZero(rz);
115 RS_DEBUG->print("RS_ActionDimLinear::trigger():"
116 " dim added: %d", dim->getId());
120 void RS_ActionDimLinear::preparePreview() {
122 dirV.setPolar(100.0, edata.angle+M_PI/2.0);
124 RS_ConstructionLine cl(
126 RS_ConstructionLineData(
127 edata.extensionPoint2,
128 edata.extensionPoint2+dirV));
130 data.definitionPoint =
131 cl.getNearestPointOnEntity(data.definitionPoint);
137 void RS_ActionDimLinear::mouseMoveEvent(QMouseEvent* e) {
138 RS_DEBUG->print("RS_ActionDimLinear::mouseMoveEvent begin");
140 Vector mouse = snapPoint(e);
142 switch (getStatus()) {
147 if (edata.extensionPoint1.valid) {
150 preview->addEntity(new RS_Line(preview,
151 RS_LineData(edata.extensionPoint1,
158 if (edata.extensionPoint1.valid && edata.extensionPoint2.valid) {
161 data.definitionPoint = mouse;
165 RS_DimLinear* dim = new RS_DimLinear(preview, data, edata);
167 preview->addEntity(dim);
173 RS_DEBUG->print("RS_ActionDimLinear::mouseMoveEvent end");
178 void RS_ActionDimLinear::mouseReleaseEvent(QMouseEvent* e) {
179 if (RS2::qtToRsButtonState(e->button())==RS2::LeftButton) {
180 RS_CoordinateEvent ce(snapPoint(e));
181 coordinateEvent(&ce);
182 } else if (RS2::qtToRsButtonState(e->button())==RS2::RightButton) {
191 void RS_ActionDimLinear::coordinateEvent(RS_CoordinateEvent* e) {
196 Vector pos = e->getCoordinate();
198 switch (getStatus()) {
200 edata.extensionPoint1 = pos;
201 graphicView->moveRelativeZero(pos);
202 setStatus(SetExtPoint2);
206 edata.extensionPoint2 = pos;
207 graphicView->moveRelativeZero(pos);
208 setStatus(SetDefPoint);
212 data.definitionPoint = pos;
215 setStatus(SetExtPoint1);
225 void RS_ActionDimLinear::commandEvent(RS_CommandEvent* e) {
226 QString c = e->getCommand().toLower();
228 if (checkCommand("help", c)) {
229 if (RS_DIALOGFACTORY!=NULL) {
230 RS_DIALOGFACTORY->commandMessage(msgAvailableCommands()
231 + getAvailableCommands().join(", "));
236 switch (getStatus()) {
239 if (RS_DIALOGFACTORY!=NULL) {
240 RS_DIALOGFACTORY->requestOptions(this, true, true);
242 graphicView->enableCoordinateInput();
243 setStatus(lastStatus);
248 double a = RS_Math::eval(c, &ok);
250 setAngle(RS_Math::deg2rad(a));
252 if (RS_DIALOGFACTORY!=NULL) {
253 RS_DIALOGFACTORY->commandMessage(tr("Not a valid expression"));
256 if (RS_DIALOGFACTORY!=NULL) {
257 RS_DIALOGFACTORY->requestOptions(this, true, true);
259 setStatus(lastStatus);
264 lastStatus = (Status)getStatus();
268 if (checkCommand("text", c)) {
269 graphicView->disableCoordinateInput();
272 } else if (!fixedAngle && (checkCommand("angle", c))) {
281 QStringList RS_ActionDimLinear::getAvailableCommands() {
284 switch (getStatus()) {
288 cmd += command("text");
290 cmd += command("angle");
302 void RS_ActionDimLinear::updateMouseButtonHints() {
303 if (RS_DIALOGFACTORY!=NULL) {
304 switch (getStatus()) {
306 RS_DIALOGFACTORY->updateMouseWidget(
307 tr("Specify first extension line origin"),
311 RS_DIALOGFACTORY->updateMouseWidget(
312 tr("Specify second extension line origin"),
316 RS_DIALOGFACTORY->updateMouseWidget(
317 tr("Specify dimension line location"),
321 RS_DIALOGFACTORY->updateMouseWidget(tr("Enter dimension text:"), "");
324 RS_DIALOGFACTORY->updateMouseWidget(
325 tr("Enter dimension line angle:"), "");
328 RS_DIALOGFACTORY->updateMouseWidget("", "");
336 void RS_ActionDimLinear::showOptions() {
337 RS_ActionInterface::showOptions();
339 if (RS_DIALOGFACTORY!=NULL) {
340 RS_DIALOGFACTORY->requestOptions(this, true, true);
346 void RS_ActionDimLinear::hideOptions() {
347 RS_ActionInterface::hideOptions();
349 if (RS_DIALOGFACTORY!=NULL) {
350 RS_DIALOGFACTORY->requestOptions(this, false);