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 "paintinterface.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 last
96 * successful snapping action. If the snap mode is "end point" the key entity
97 * is the entity whos end point was caught. If the snap mode didn't require an
98 * entity (e.g. free, grid) this method will return NULL.
100 RS_Entity * RS_Snapper::getKeyEntity()
105 /** Sets a new snap mode. */
106 void RS_Snapper::setSnapMode(RS2::SnapMode snapMode)
108 this->snapMode = snapMode;
111 /** Sets a new snap restriction. */
112 void RS_Snapper::setSnapRestriction(RS2::SnapRestriction snapRes)
114 this->snapRes = snapRes;
118 * Sets the snap range in pixels for catchEntity().
122 void RS_Snapper::setSnapRange(int r)
128 * Snap to a coordinate in the drawing using the current snap mode.
130 * @param e A mouse event.
131 * @return The coordinates of the point or an invalid vector.
133 Vector RS_Snapper::snapPoint(QMouseEvent * e)
135 RS_DEBUG->print("RS_Snapper::snapPoint");
136 snapSpot = Vector(false);
140 RS_DEBUG->print(RS_Debug::D_WARNING, "RS_Snapper::snapPoint: event is NULL");
144 Vector mouseCoord = graphicView->toGraph(e->x(), e->y());
149 snapSpot = snapFree(mouseCoord);
152 case RS2::SnapEndpoint:
153 snapSpot = snapEndpoint(mouseCoord);
157 snapSpot = snapGrid(mouseCoord);
160 case RS2::SnapOnEntity:
161 snapSpot = snapOnEntity(mouseCoord);
164 case RS2::SnapCenter:
165 snapSpot = snapCenter(mouseCoord);
168 case RS2::SnapMiddle:
169 snapSpot = snapMiddle(mouseCoord);
173 snapSpot = snapDist(mouseCoord);
176 case RS2::SnapIntersection:
177 snapSpot = snapIntersection(mouseCoord);
184 // Handle snap restrictions that can be activated in addition to the ones above:
187 case RS2::RestrictOrthogonal:
188 snapCoord = restrictOrthogonal(snapSpot);
190 case RS2::RestrictHorizontal:
191 snapCoord = restrictHorizontal(snapSpot);
193 case RS2::RestrictVertical:
194 snapCoord = restrictVertical(snapSpot);
197 case RS2::RestrictNothing:
198 snapCoord = snapSpot;
202 if (RS_DIALOGFACTORY)
203 RS_DIALOGFACTORY->updateCoordinateWidget(snapCoord, snapCoord - graphicView->getRelativeZero());
205 RS_DEBUG->print("RS_Snapper::snapPoint: OK");
211 * Snaps to a free coordinate.
213 * @param coord The mouse coordinate.
214 * @return The coordinates of the point or an invalid vector.
216 Vector RS_Snapper::snapFree(Vector coord)
223 * Snaps to the closest endpoint.
225 * @param coord The mouse coordinate.
226 * @return The coordinates of the point or an invalid vector.
228 Vector RS_Snapper::snapEndpoint(Vector coord)
231 vec = container->getNearestEndpoint(coord, NULL/*, &keyEntity*/);
237 * Snaps to a grid point.
239 * @param coord The mouse coordinate.
240 * @return The coordinates of the point or an invalid vector.
242 Vector RS_Snapper::snapGrid(Vector coord)
244 RS_DEBUG->print("RS_Snapper::snapGrid begin");
249 RS_Grid * grid = graphicView->getGrid();
251 RS_DEBUG->print("RS_Snapper::snapGrid 001");
255 RS_DEBUG->print("RS_Snapper::snapGrid 002");
256 Vector * pts = grid->getPoints();
257 RS_DEBUG->print("RS_Snapper::snapGrid 003");
260 RS_DEBUG->print("RS_Snapper::snapGrid 004");
262 for(int i=0; i<grid->count(); ++i)
264 double d = pts[i].distanceTo(coord);
273 RS_DEBUG->print("RS_Snapper::snapGrid 005");
278 RS_DEBUG->print("RS_Snapper::snapGrid 006");
283 RS_DEBUG->print("RS_Snapper::snapGrid end");
289 * Snaps to a point on an entity.
291 * @param coord The mouse coordinate.
292 * @return The coordinates of the point or an invalid vector.
294 Vector RS_Snapper::snapOnEntity(Vector coord)
297 vec = container->getNearestPointOnEntity(coord, true, NULL, &keyEntity);
303 * Snaps to the closest center.
305 * @param coord The mouse coordinate.
306 * @return The coordinates of the point or an invalid vector.
308 Vector RS_Snapper::snapCenter(Vector coord)
311 vec = container->getNearestCenter(coord, NULL);
317 * Snaps to the closest middle.
319 * @param coord The mouse coordinate.
320 * @return The coordinates of the point or an invalid vector.
322 Vector RS_Snapper::snapMiddle(Vector coord)
325 vec = container->getNearestMiddle(coord, NULL);
331 * Snaps to the closest point with a given distance to the endpoint.
333 * @param coord The mouse coordinate.
334 * @return The coordinates of the point or an invalid vector.
336 Vector RS_Snapper::snapDist(Vector coord)
339 vec = container->getNearestDist(distance, coord, NULL);
345 * Snaps to the closest intersection point.
347 * @param coord The mouse coordinate.
348 * @return The coordinates of the point or an invalid vector.
350 Vector RS_Snapper::snapIntersection(Vector coord)
353 vec = container->getNearestIntersection(coord, NULL);
359 * 'Corrects' the given coordinates to 0, 90, 180, 270 degrees relative to
360 * the current relative zero point.
362 * @param coord The uncorrected coordinates.
363 * @return The corrected coordinates.
365 Vector RS_Snapper::restrictOrthogonal(Vector coord)
367 Vector rz = graphicView->getRelativeZero();
370 Vector retx = Vector(rz.x, ret.y);
371 Vector rety = Vector(ret.x, rz.y);
373 if (retx.distanceTo(ret) > rety.distanceTo(ret))
382 * 'Corrects' the given coordinates to 0, 180 degrees relative to
383 * the current relative zero point.
385 * @param coord The uncorrected coordinates.
386 * @return The corrected coordinates.
388 Vector RS_Snapper::restrictHorizontal(Vector coord)
390 Vector rz = graphicView->getRelativeZero();
392 return Vector(coord.x, rz.y);
396 * 'Corrects' the given coordinates to 90, 270 degrees relative to
397 * the current relative zero point.
399 * @param coord The uncorrected coordinates.
400 * @return The corrected coordinates.
402 Vector RS_Snapper::restrictVertical(Vector coord)
404 Vector rz = graphicView->getRelativeZero();
406 return Vector(rz.x, coord.y);
410 * Catches an entity which is close to the given position 'pos'.
412 * @param pos A graphic coordinate.
413 * @param level The level of resolving for iterating through the entity
415 * @return Pointer to the entity or NULL.
417 RS_Entity * RS_Snapper::catchEntity(const Vector& pos, RS2::ResolveLevel level)
419 RS_DEBUG->print("RS_Snapper::catchEntity");
421 // set default distance for points inside solids
422 double dist = graphicView->toGraphDX(snapRange) * 0.9;
424 RS_Entity * entity = container->getNearestEntity(pos, &dist, level);
428 if (entity && entity->getParent())
429 idx = entity->getParent()->findEntity(entity);
431 if (entity && dist <= graphicView->toGraphDX(snapRange))
434 RS_DEBUG->print("RS_Snapper::catchEntity: found: %d", idx);
439 RS_DEBUG->print("RS_Snapper::catchEntity: not found");
443 RS_DEBUG->print("RS_Snapper::catchEntity: OK");
447 * Catches an entity which is close to the mouse cursor.
449 * @param e A mouse event.
450 * @param level The level of resolving for iterating through the entity
452 * @return Pointer to the entity or NULL.
454 RS_Entity * RS_Snapper::catchEntity(QMouseEvent * e, RS2::ResolveLevel level)
456 return catchEntity(Vector(graphicView->toGraphX(e->x()),
457 graphicView->toGraphY(e->y())), level);
461 * Suspends this snapper while another action takes place.
463 /*virtual*/ void RS_Snapper::suspend()
466 snapSpot = snapCoord = Vector(false);
470 * Resumes this snapper after it has been suspended.
472 /*virtual*/ void RS_Snapper::resume()
478 * Hides the snapper options.
480 /*virtual*/ void RS_Snapper::hideOptions()
482 if (snapMode == RS2::SnapDist && RS_DIALOGFACTORY)
483 RS_DIALOGFACTORY->requestSnapDistOptions(distance, false);
487 * Shows the snapper options.
489 /*virtual*/ void RS_Snapper::showOptions()
491 if (snapMode == RS2::SnapDist && RS_DIALOGFACTORY)
492 RS_DIALOGFACTORY->requestSnapDistOptions(distance, true);
496 * Draws the snapper on the screen.
498 void RS_Snapper::drawSnapper()
500 printf("RS_Snapper::drawSnapper(): Using DEPRECATED function!!!\n");
506 * Deletes the snapper from the screen.
508 void RS_Snapper::deleteSnapper()
510 printf("RS_Snapper::deleteSnapper(): Using DEPRECATED function!!!\n");
514 snapSpot = Vector(false);
515 snapCoord = Vector(false);
520 * Draws / deletes the current snapper spot.
522 void RS_Snapper::xorSnapper()
524 #warning "!!! RS_Snapper::xorSnapper() is DEPRECATED !!!"
525 //Not completely true...
526 //#warning "!!! xorSnapper() not working AT ALL !!!"
528 if (!finished && snapSpot.valid)
530 RS_Painter * painter = graphicView->createDirectPainter();
531 painter->setPreviewMode();
536 painter->drawCircle(graphicView->toGui(snapCoord), 4);
539 if (showCrosshairs == true)
541 painter->setPen(RS_Pen(RS_Color(0, 255, 255), RS2::Width00, RS2::DashLine));
542 painter->drawLine(Vector(0, graphicView->toGuiY(snapCoord.y)),
543 Vector(graphicView->getWidth(), graphicView->toGuiY(snapCoord.y)));
544 painter->drawLine(Vector(graphicView->toGuiX(snapCoord.x), 0),
545 Vector(graphicView->toGuiX(snapCoord.x), graphicView->getHeight()));
549 if (snapCoord.valid && snapCoord != snapSpot)
551 painter->drawLine(graphicView->toGui(snapSpot) + Vector(-5, 0),
552 graphicView->toGui(snapSpot) + Vector(-1, 4));
553 painter->drawLine(graphicView->toGui(snapSpot) + Vector(0, 5),
554 graphicView->toGui(snapSpot) + Vector(4, 1));
555 painter->drawLine(graphicView->toGui(snapSpot) + Vector(5, 0),
556 graphicView->toGui(snapSpot) + Vector(1, -4));
557 painter->drawLine(graphicView->toGui(snapSpot) + Vector(0, -5),
558 graphicView->toGui(snapSpot) + Vector(-4, -1));
561 graphicView->destroyPainter();
565 if (finished || !snapSpot.valid || !graphicView)
568 graphicView->SetSnapperDraw(true);
569 graphicView->SetSnapperVars(snapSpot, snapCoord, showCrosshairs);
570 //Apparently, this gets hit anyway by the preview code...
571 // graphicView->redraw();
577 void RS_Snapper::SetVisible(bool visibility/*= true*/)
579 visible = visibility;
582 bool RS_Snapper::Visible(void)
587 void RS_Snapper::Draw(GraphicView * view, PaintInterface * painter)
589 if (finished || !snapSpot.valid)
592 //hm, I don't like graphicView kicking around in here, especially since it now
593 //lives inside GraphicView... How to !!! FIX !!!?
594 //We'll pass it in for now...
598 painter->setPen(RS_Pen(RS_Color(0, 127, 255), RS2::Width00, RS2::DashLine));
599 painter->drawCircle(view->toGui(snapCoord), 4);
604 painter->setPen(RS_Pen(RS_Color(0, 255, 255), RS2::Width00, RS2::DashLine));
605 painter->drawLine(Vector(0, view->toGuiY(snapCoord.y)),
606 Vector(view->getWidth(), view->toGuiY(snapCoord.y)));
607 painter->drawLine(Vector(view->toGuiX(snapCoord.x), 0),
608 Vector(view->toGuiX(snapCoord.x), view->getHeight()));
612 if (snapCoord.valid && snapCoord != snapSpot)
614 painter->drawLine(view->toGui(snapSpot) + Vector(-5, 0),
615 view->toGui(snapSpot) + Vector(-1, 4));
616 painter->drawLine(view->toGui(snapSpot) + Vector(0, 5),
617 view->toGui(snapSpot) + Vector(4, 1));
618 painter->drawLine(view->toGui(snapSpot) + Vector(5, 0),
619 view->toGui(snapSpot) + Vector(1, -4));
620 painter->drawLine(view->toGui(snapSpot) + Vector(0, -5),
621 view->toGui(snapSpot) + Vector(-4, -1));