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"
18 #include "rs_entitycontainer.h"
20 #include "rs_graphicview.h"
22 #include "rs_information.h"
28 RS_Snapper::RS_Snapper(RS_EntityContainer & c, RS_GraphicView & gv):
29 container(&c), graphicView(&gv), finished(false)
37 RS_Snapper::~RS_Snapper()
42 * Initialize (called by all constructors)
44 void RS_Snapper::init()
46 snapMode = graphicView->getDefaultSnapMode();
47 snapRes = graphicView->getSnapRestriction();
49 snapSpot = Vector(false);
50 snapCoord = Vector(false);
54 settings.beginGroup("Snap");
55 snapRange = settings.value("Range", 20).toInt();
57 settings.beginGroup("Appearance");
58 showCrosshairs = settings.value("ShowCrosshairs", true).toBool();
66 void RS_Snapper::finish()
72 * @return Pointer to the entity which was the key entity for the
73 * last successful snapping action. If the snap mode is "end point"
74 * the key entity is the entity whos end point was caught.
75 * If the snap mode didn't require an entity (e.g. free, grid) this
76 * method will return NULL.
78 RS_Entity * RS_Snapper::getKeyEntity()
83 /** Sets a new snap mode. */
84 void RS_Snapper::setSnapMode(RS2::SnapMode snapMode)
86 this->snapMode = snapMode;
89 /** Sets a new snap restriction. */
90 void RS_Snapper::setSnapRestriction(RS2::SnapRestriction snapRes)
92 this->snapRes = snapRes;
96 * Sets the snap range in pixels for catchEntity().
100 void RS_Snapper::setSnapRange(int r)
106 * Snap to a coordinate in the drawing using the current snap mode.
108 * @param e A mouse event.
109 * @return The coordinates of the point or an invalid vector.
111 Vector RS_Snapper::snapPoint(QMouseEvent * e)
113 RS_DEBUG->print("RS_Snapper::snapPoint");
116 snapSpot = Vector(false);
120 RS_DEBUG->print(RS_Debug::D_WARNING, "RS_Snapper::snapPoint: event is NULL");
124 Vector mouseCoord = graphicView->toGraph(e->x(), e->y());
129 snapSpot = snapFree(mouseCoord);
132 case RS2::SnapEndpoint:
133 snapSpot = snapEndpoint(mouseCoord);
137 snapSpot = snapGrid(mouseCoord);
140 case RS2::SnapOnEntity:
141 snapSpot = snapOnEntity(mouseCoord);
144 case RS2::SnapCenter:
145 snapSpot = snapCenter(mouseCoord);
148 case RS2::SnapMiddle:
149 snapSpot = snapMiddle(mouseCoord);
153 snapSpot = snapDist(mouseCoord);
156 case RS2::SnapIntersection:
157 snapSpot = snapIntersection(mouseCoord);
164 // handle snap restrictions that can be activated in addition
165 // to the ones above:
168 case RS2::RestrictOrthogonal:
169 snapCoord = restrictOrthogonal(snapSpot);
171 case RS2::RestrictHorizontal:
172 snapCoord = restrictHorizontal(snapSpot);
174 case RS2::RestrictVertical:
175 snapCoord = restrictVertical(snapSpot);
178 case RS2::RestrictNothing:
179 snapCoord = snapSpot;
183 #warning "!!! THIS IS WHERE THE SNAPPER IS BEING DRAWN... !!!"
186 if (RS_DIALOGFACTORY != NULL)
187 RS_DIALOGFACTORY->updateCoordinateWidget(snapCoord, snapCoord - graphicView->getRelativeZero());
189 RS_DEBUG->print("RS_Snapper::snapPoint: OK");
195 * Snaps to a free coordinate.
197 * @param coord The mouse coordinate.
198 * @return The coordinates of the point or an invalid vector.
200 Vector RS_Snapper::snapFree(Vector coord)
207 * Snaps to the closest endpoint.
209 * @param coord The mouse coordinate.
210 * @return The coordinates of the point or an invalid vector.
212 Vector RS_Snapper::snapEndpoint(Vector coord)
216 vec = container->getNearestEndpoint(coord, NULL/*, &keyEntity*/);
221 * Snaps to a grid point.
223 * @param coord The mouse coordinate.
224 * @return The coordinates of the point or an invalid vector.
226 Vector RS_Snapper::snapGrid(Vector coord)
228 RS_DEBUG->print("RS_Snapper::snapGrid begin");
233 RS_Grid * grid = graphicView->getGrid();
235 RS_DEBUG->print("RS_Snapper::snapGrid 001");
239 RS_DEBUG->print("RS_Snapper::snapGrid 002");
240 Vector * pts = grid->getPoints();
241 RS_DEBUG->print("RS_Snapper::snapGrid 003");
244 RS_DEBUG->print("RS_Snapper::snapGrid 004");
246 for(int i=0; i<grid->count(); ++i)
248 double d = pts[i].distanceTo(coord);
257 RS_DEBUG->print("RS_Snapper::snapGrid 005");
262 RS_DEBUG->print("RS_Snapper::snapGrid 006");
267 RS_DEBUG->print("RS_Snapper::snapGrid end");
273 * Snaps to a point on an entity.
275 * @param coord The mouse coordinate.
276 * @return The coordinates of the point or an invalid vector.
278 Vector RS_Snapper::snapOnEntity(Vector coord)
281 vec = container->getNearestPointOnEntity(coord, true, NULL, &keyEntity);
286 * Snaps to the closest center.
288 * @param coord The mouse coordinate.
289 * @return The coordinates of the point or an invalid vector.
291 Vector RS_Snapper::snapCenter(Vector coord)
295 vec = container->getNearestCenter(coord, NULL);
300 * Snaps to the closest middle.
302 * @param coord The mouse coordinate.
303 * @return The coordinates of the point or an invalid vector.
305 Vector RS_Snapper::snapMiddle(Vector coord)
309 vec = container->getNearestMiddle(coord, NULL);
314 * Snaps to the closest point with a given distance to the endpoint.
316 * @param coord The mouse coordinate.
317 * @return The coordinates of the point or an invalid vector.
319 Vector RS_Snapper::snapDist(Vector coord)
323 vec = container->getNearestDist(distance, coord, NULL);
328 * Snaps to the closest intersection point.
330 * @param coord The mouse coordinate.
331 * @return The coordinates of the point or an invalid vector.
333 Vector RS_Snapper::snapIntersection(Vector coord)
337 vec = container->getNearestIntersection(coord, NULL);
342 * 'Corrects' the given coordinates to 0, 90, 180, 270 degrees relative to
343 * the current relative zero point.
345 * @param coord The uncorrected coordinates.
346 * @return The corrected coordinates.
348 Vector RS_Snapper::restrictOrthogonal(Vector coord)
350 Vector rz = graphicView->getRelativeZero();
353 Vector retx = Vector(rz.x, ret.y);
354 Vector rety = Vector(ret.x, rz.y);
356 if (retx.distanceTo(ret) > rety.distanceTo(ret))
365 * 'Corrects' the given coordinates to 0, 180 degrees relative to
366 * the current relative zero point.
368 * @param coord The uncorrected coordinates.
369 * @return The corrected coordinates.
371 Vector RS_Snapper::restrictHorizontal(Vector coord)
373 Vector rz = graphicView->getRelativeZero();
374 Vector ret = Vector(coord.x, rz.y);
379 * 'Corrects' the given coordinates to 90, 270 degrees relative to
380 * the current relative zero point.
382 * @param coord The uncorrected coordinates.
383 * @return The corrected coordinates.
385 Vector RS_Snapper::restrictVertical(Vector coord)
387 Vector rz = graphicView->getRelativeZero();
388 Vector ret = Vector(rz.x, coord.y);
393 * Catches an entity which is close to the given position 'pos'.
395 * @param pos A graphic coordinate.
396 * @param level The level of resolving for iterating through the entity
398 * @return Pointer to the entity or NULL.
400 RS_Entity * RS_Snapper::catchEntity(const Vector& pos, RS2::ResolveLevel level)
402 RS_DEBUG->print("RS_Snapper::catchEntity");
404 // set default distance for points inside solids
405 double dist = graphicView->toGraphDX(snapRange) * 0.9;
407 RS_Entity * entity = container->getNearestEntity(pos, &dist, level);
411 if (entity != NULL && entity->getParent() != NULL)
412 idx = entity->getParent()->findEntity(entity);
414 if (entity != NULL && dist <= graphicView->toGraphDX(snapRange))
417 RS_DEBUG->print("RS_Snapper::catchEntity: found: %d", idx);
422 RS_DEBUG->print("RS_Snapper::catchEntity: not found");
426 RS_DEBUG->print("RS_Snapper::catchEntity: OK");
430 * Catches an entity which is close to the mouse cursor.
432 * @param e A mouse event.
433 * @param level The level of resolving for iterating through the entity
435 * @return Pointer to the entity or NULL.
437 RS_Entity * RS_Snapper::catchEntity(QMouseEvent * e, RS2::ResolveLevel level)
439 return catchEntity(Vector(graphicView->toGraphX(e->x()),
440 graphicView->toGraphY(e->y())), level);
444 * Suspends this snapper while another action takes place.
446 void RS_Snapper::suspend()
449 snapSpot = snapCoord = Vector(false);
453 * Resumes this snapper after it has been suspended.
455 void RS_Snapper::resume()
461 * Hides the snapper options. Default implementation does nothing.
463 void RS_Snapper::hideOptions()
465 if (snapMode == RS2::SnapDist)
466 if (RS_DIALOGFACTORY!=NULL)
467 RS_DIALOGFACTORY->requestSnapDistOptions(distance, false);
471 * Shows the snapper options. Default implementation does nothing.
473 void RS_Snapper::showOptions()
475 if (snapMode == RS2::SnapDist)
476 if (RS_DIALOGFACTORY != NULL)
477 RS_DIALOGFACTORY->requestSnapDistOptions(distance, true);
481 * Draws the snapper on the screen.
483 void RS_Snapper::drawSnapper()
490 * Deletes the snapper from the screen.
492 void RS_Snapper::deleteSnapper()
497 snapSpot = Vector(false);
498 snapCoord = Vector(false);
503 * Draws / deletes the current snapper spot.
505 void RS_Snapper::xorSnapper()
507 //Not completely true...
508 //#warning "!!! xorSnapper() not working AT ALL !!!"
510 if (!finished && snapSpot.valid)
512 RS_Painter * painter = graphicView->createDirectPainter();
513 painter->setPreviewMode();
518 painter->drawCircle(graphicView->toGui(snapCoord), 4);
521 if (showCrosshairs == true)
523 painter->setPen(RS_Pen(RS_Color(0, 255, 255), RS2::Width00, RS2::DashLine));
524 painter->drawLine(Vector(0, graphicView->toGuiY(snapCoord.y)),
525 Vector(graphicView->getWidth(), graphicView->toGuiY(snapCoord.y)));
526 painter->drawLine(Vector(graphicView->toGuiX(snapCoord.x), 0),
527 Vector(graphicView->toGuiX(snapCoord.x), graphicView->getHeight()));
531 if (snapCoord.valid && snapCoord != snapSpot)
533 painter->drawLine(graphicView->toGui(snapSpot) + Vector(-5, 0),
534 graphicView->toGui(snapSpot) + Vector(-1, 4));
535 painter->drawLine(graphicView->toGui(snapSpot) + Vector(0, 5),
536 graphicView->toGui(snapSpot) + Vector(4, 1));
537 painter->drawLine(graphicView->toGui(snapSpot) + Vector(5, 0),
538 graphicView->toGui(snapSpot) + Vector(1, -4));
539 painter->drawLine(graphicView->toGui(snapSpot) + Vector(0, -5),
540 graphicView->toGui(snapSpot) + Vector(-4, -1));
543 graphicView->destroyPainter();
547 if (finished || !snapSpot.valid || graphicView == NULL)
550 graphicView->SetSnapperDraw(true);
551 graphicView->SetSnapperVars(snapSpot, snapCoord, showCrosshairs);
552 //Apparently, this gets hit anyway by the preview code...
553 // graphicView->redraw();