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/21/2010 Added this text. :-)
15 #include "rs_snapper.h"
17 #include "rs_dialogfactory.h"
19 #include "rs_entitycontainer.h"
20 #include "graphicview.h"
22 #include "rs_information.h"
23 #include "paintintf.h"
29 RS_Snapper::RS_Snapper(RS_EntityContainer & c, GraphicView & gv):
30 container(&c), graphicView(&gv), finished(false)
35 RS_Snapper::RS_Snapper(void):
36 container(NULL), graphicView(NULL), finished(false)
44 RS_Snapper::~RS_Snapper()
49 * Initialize (called by all constructors)
51 void RS_Snapper::init()
55 snapMode = graphicView->getDefaultSnapMode();
56 snapRes = graphicView->getSnapRestriction();
60 snapSpot = Vector(false);
61 snapCoord = Vector(false);
65 settings.beginGroup("Snap");
66 snapRange = settings.value("Range", 20).toInt();
68 settings.beginGroup("Appearance");
69 showCrosshairs = settings.value("ShowCrosshairs", true).toBool();
77 void RS_Snapper::finish()
83 void RS_Snapper::SetContainer(RS_EntityContainer * c)
89 void RS_Snapper::SetGraphicView(GraphicView * v)
95 * @return Pointer to the entity which was the key entity for the
96 * last successful snapping action. If the snap mode is "end point"
97 * the key entity is the entity whos end point was caught.
98 * If the snap mode didn't require an entity (e.g. free, grid) this
99 * method will return NULL.
101 RS_Entity * RS_Snapper::getKeyEntity()
106 /** Sets a new snap mode. */
107 void RS_Snapper::setSnapMode(RS2::SnapMode snapMode)
109 this->snapMode = snapMode;
112 /** Sets a new snap restriction. */
113 void RS_Snapper::setSnapRestriction(RS2::SnapRestriction snapRes)
115 this->snapRes = snapRes;
119 * Sets the snap range in pixels for catchEntity().
123 void RS_Snapper::setSnapRange(int r)
129 * Snap to a coordinate in the drawing using the current snap mode.
131 * @param e A mouse event.
132 * @return The coordinates of the point or an invalid vector.
134 Vector RS_Snapper::snapPoint(QMouseEvent * e)
136 RS_DEBUG->print("RS_Snapper::snapPoint");
137 snapSpot = Vector(false);
141 RS_DEBUG->print(RS_Debug::D_WARNING, "RS_Snapper::snapPoint: event is NULL");
145 Vector mouseCoord = graphicView->toGraph(e->x(), e->y());
150 snapSpot = snapFree(mouseCoord);
153 case RS2::SnapEndpoint:
154 snapSpot = snapEndpoint(mouseCoord);
158 snapSpot = snapGrid(mouseCoord);
161 case RS2::SnapOnEntity:
162 snapSpot = snapOnEntity(mouseCoord);
165 case RS2::SnapCenter:
166 snapSpot = snapCenter(mouseCoord);
169 case RS2::SnapMiddle:
170 snapSpot = snapMiddle(mouseCoord);
174 snapSpot = snapDist(mouseCoord);
177 case RS2::SnapIntersection:
178 snapSpot = snapIntersection(mouseCoord);
185 // Handle snap restrictions that can be activated in addition to the ones above:
188 case RS2::RestrictOrthogonal:
189 snapCoord = restrictOrthogonal(snapSpot);
191 case RS2::RestrictHorizontal:
192 snapCoord = restrictHorizontal(snapSpot);
194 case RS2::RestrictVertical:
195 snapCoord = restrictVertical(snapSpot);
198 case RS2::RestrictNothing:
199 snapCoord = snapSpot;
203 if (RS_DIALOGFACTORY)
204 RS_DIALOGFACTORY->updateCoordinateWidget(snapCoord, snapCoord - graphicView->getRelativeZero());
206 RS_DEBUG->print("RS_Snapper::snapPoint: OK");
212 * Snaps to a free coordinate.
214 * @param coord The mouse coordinate.
215 * @return The coordinates of the point or an invalid vector.
217 Vector RS_Snapper::snapFree(Vector coord)
224 * Snaps to the closest endpoint.
226 * @param coord The mouse coordinate.
227 * @return The coordinates of the point or an invalid vector.
229 Vector RS_Snapper::snapEndpoint(Vector coord)
232 vec = container->getNearestEndpoint(coord, NULL/*, &keyEntity*/);
238 * Snaps to a grid point.
240 * @param coord The mouse coordinate.
241 * @return The coordinates of the point or an invalid vector.
243 Vector RS_Snapper::snapGrid(Vector coord)
245 RS_DEBUG->print("RS_Snapper::snapGrid begin");
250 RS_Grid * grid = graphicView->getGrid();
252 RS_DEBUG->print("RS_Snapper::snapGrid 001");
256 RS_DEBUG->print("RS_Snapper::snapGrid 002");
257 Vector * pts = grid->getPoints();
258 RS_DEBUG->print("RS_Snapper::snapGrid 003");
261 RS_DEBUG->print("RS_Snapper::snapGrid 004");
263 for(int i=0; i<grid->count(); ++i)
265 double d = pts[i].distanceTo(coord);
274 RS_DEBUG->print("RS_Snapper::snapGrid 005");
279 RS_DEBUG->print("RS_Snapper::snapGrid 006");
284 RS_DEBUG->print("RS_Snapper::snapGrid end");
290 * Snaps to a point on an entity.
292 * @param coord The mouse coordinate.
293 * @return The coordinates of the point or an invalid vector.
295 Vector RS_Snapper::snapOnEntity(Vector coord)
298 vec = container->getNearestPointOnEntity(coord, true, NULL, &keyEntity);
304 * Snaps to the closest center.
306 * @param coord The mouse coordinate.
307 * @return The coordinates of the point or an invalid vector.
309 Vector RS_Snapper::snapCenter(Vector coord)
312 vec = container->getNearestCenter(coord, NULL);
318 * Snaps to the closest middle.
320 * @param coord The mouse coordinate.
321 * @return The coordinates of the point or an invalid vector.
323 Vector RS_Snapper::snapMiddle(Vector coord)
326 vec = container->getNearestMiddle(coord, NULL);
332 * Snaps to the closest point with a given distance to the endpoint.
334 * @param coord The mouse coordinate.
335 * @return The coordinates of the point or an invalid vector.
337 Vector RS_Snapper::snapDist(Vector coord)
340 vec = container->getNearestDist(distance, coord, NULL);
346 * Snaps to the closest intersection point.
348 * @param coord The mouse coordinate.
349 * @return The coordinates of the point or an invalid vector.
351 Vector RS_Snapper::snapIntersection(Vector coord)
354 vec = container->getNearestIntersection(coord, NULL);
360 * 'Corrects' the given coordinates to 0, 90, 180, 270 degrees relative to
361 * the current relative zero point.
363 * @param coord The uncorrected coordinates.
364 * @return The corrected coordinates.
366 Vector RS_Snapper::restrictOrthogonal(Vector coord)
368 Vector rz = graphicView->getRelativeZero();
371 Vector retx = Vector(rz.x, ret.y);
372 Vector rety = Vector(ret.x, rz.y);
374 if (retx.distanceTo(ret) > rety.distanceTo(ret))
383 * 'Corrects' the given coordinates to 0, 180 degrees relative to
384 * the current relative zero point.
386 * @param coord The uncorrected coordinates.
387 * @return The corrected coordinates.
389 Vector RS_Snapper::restrictHorizontal(Vector coord)
391 Vector rz = graphicView->getRelativeZero();
393 return Vector(coord.x, rz.y);
397 * 'Corrects' the given coordinates to 90, 270 degrees relative to
398 * the current relative zero point.
400 * @param coord The uncorrected coordinates.
401 * @return The corrected coordinates.
403 Vector RS_Snapper::restrictVertical(Vector coord)
405 Vector rz = graphicView->getRelativeZero();
407 return Vector(rz.x, coord.y);
411 * Catches an entity which is close to the given position 'pos'.
413 * @param pos A graphic coordinate.
414 * @param level The level of resolving for iterating through the entity
416 * @return Pointer to the entity or NULL.
418 RS_Entity * RS_Snapper::catchEntity(const Vector& pos, RS2::ResolveLevel level)
420 RS_DEBUG->print("RS_Snapper::catchEntity");
422 // set default distance for points inside solids
423 double dist = graphicView->toGraphDX(snapRange) * 0.9;
425 RS_Entity * entity = container->getNearestEntity(pos, &dist, level);
429 if (entity && entity->getParent())
430 idx = entity->getParent()->findEntity(entity);
432 if (entity && dist <= graphicView->toGraphDX(snapRange))
435 RS_DEBUG->print("RS_Snapper::catchEntity: found: %d", idx);
440 RS_DEBUG->print("RS_Snapper::catchEntity: not found");
444 RS_DEBUG->print("RS_Snapper::catchEntity: OK");
448 * Catches an entity which is close to the mouse cursor.
450 * @param e A mouse event.
451 * @param level The level of resolving for iterating through the entity
453 * @return Pointer to the entity or NULL.
455 RS_Entity * RS_Snapper::catchEntity(QMouseEvent * e, RS2::ResolveLevel level)
457 return catchEntity(Vector(graphicView->toGraphX(e->x()),
458 graphicView->toGraphY(e->y())), level);
462 * Suspends this snapper while another action takes place.
464 /*virtual*/ void RS_Snapper::suspend()
467 snapSpot = snapCoord = Vector(false);
471 * Resumes this snapper after it has been suspended.
473 /*virtual*/ void RS_Snapper::resume()
479 * Hides the snapper options.
481 /*virtual*/ void RS_Snapper::hideOptions()
483 if (snapMode == RS2::SnapDist && RS_DIALOGFACTORY)
484 RS_DIALOGFACTORY->requestSnapDistOptions(distance, false);
488 * Shows the snapper options.
490 /*virtual*/ void RS_Snapper::showOptions()
492 if (snapMode == RS2::SnapDist && RS_DIALOGFACTORY)
493 RS_DIALOGFACTORY->requestSnapDistOptions(distance, true);
497 * Draws the snapper on the screen.
499 void RS_Snapper::drawSnapper()
501 printf("RS_Snapper::drawSnapper(): Using DEPRECATED function!!!\n");
507 * Deletes the snapper from the screen.
509 void RS_Snapper::deleteSnapper()
511 printf("RS_Snapper::deleteSnapper(): Using DEPRECATED function!!!\n");
515 snapSpot = Vector(false);
516 snapCoord = Vector(false);
521 * Draws / deletes the current snapper spot.
523 void RS_Snapper::xorSnapper()
525 #warning "!!! RS_Snapper::xorSnapper() is DEPRECATED !!!"
526 //Not completely true...
527 //#warning "!!! xorSnapper() not working AT ALL !!!"
529 if (!finished && snapSpot.valid)
531 RS_Painter * painter = graphicView->createDirectPainter();
532 painter->setPreviewMode();
537 painter->drawCircle(graphicView->toGui(snapCoord), 4);
540 if (showCrosshairs == true)
542 painter->setPen(RS_Pen(RS_Color(0, 255, 255), RS2::Width00, RS2::DashLine));
543 painter->drawLine(Vector(0, graphicView->toGuiY(snapCoord.y)),
544 Vector(graphicView->getWidth(), graphicView->toGuiY(snapCoord.y)));
545 painter->drawLine(Vector(graphicView->toGuiX(snapCoord.x), 0),
546 Vector(graphicView->toGuiX(snapCoord.x), graphicView->getHeight()));
550 if (snapCoord.valid && snapCoord != snapSpot)
552 painter->drawLine(graphicView->toGui(snapSpot) + Vector(-5, 0),
553 graphicView->toGui(snapSpot) + Vector(-1, 4));
554 painter->drawLine(graphicView->toGui(snapSpot) + Vector(0, 5),
555 graphicView->toGui(snapSpot) + Vector(4, 1));
556 painter->drawLine(graphicView->toGui(snapSpot) + Vector(5, 0),
557 graphicView->toGui(snapSpot) + Vector(1, -4));
558 painter->drawLine(graphicView->toGui(snapSpot) + Vector(0, -5),
559 graphicView->toGui(snapSpot) + Vector(-4, -1));
562 graphicView->destroyPainter();
566 if (finished || !snapSpot.valid || !graphicView)
569 graphicView->SetSnapperDraw(true);
570 graphicView->SetSnapperVars(snapSpot, snapCoord, showCrosshairs);
571 //Apparently, this gets hit anyway by the preview code...
572 // graphicView->redraw();
578 void RS_Snapper::SetVisible(bool visibility/*= true*/)
580 visible = visibility;
583 bool RS_Snapper::Visible(void)
588 void RS_Snapper::Draw(GraphicView * view, PaintInterface * painter)
590 if (finished || !snapSpot.valid)
593 //hm, I don't like graphicView kicking around in here, especially since it now
594 //lives inside GraphicView... How to !!! FIX !!!?
595 //We'll pass it in for now...
599 painter->setPen(RS_Pen(RS_Color(0, 127, 255), RS2::Width00, RS2::DashLine));
600 painter->drawCircle(view->toGui(snapCoord), 4);
605 painter->setPen(RS_Pen(RS_Color(0, 255, 255), RS2::Width00, RS2::DashLine));
606 painter->drawLine(Vector(0, view->toGuiY(snapCoord.y)),
607 Vector(view->getWidth(), view->toGuiY(snapCoord.y)));
608 painter->drawLine(Vector(view->toGuiX(snapCoord.x), 0),
609 Vector(view->toGuiX(snapCoord.x), view->getHeight()));
613 if (snapCoord.valid && snapCoord != snapSpot)
615 painter->drawLine(view->toGui(snapSpot) + Vector(-5, 0),
616 view->toGui(snapSpot) + Vector(-1, 4));
617 painter->drawLine(view->toGui(snapSpot) + Vector(0, 5),
618 view->toGui(snapSpot) + Vector(4, 1));
619 painter->drawLine(view->toGui(snapSpot) + Vector(5, 0),
620 view->toGui(snapSpot) + Vector(1, -4));
621 painter->drawLine(view->toGui(snapSpot) + Vector(0, -5),
622 view->toGui(snapSpot) + Vector(-4, -1));