-// snapper.cpp
-//
-// Part of the Architektonas Project
-// Originally part of QCad Community Edition by Andrew Mustun
-// Extensively rewritten and refactored by James L. Hammons
-// Portions copyright (C) 2001-2003 RibbonSoft
-// Copyright (C) 2010 Underground Software
-// See the README and GPLv2 files for licensing and warranty information
-//
-// JLH = James L. Hammons <jlhamm@acm.org>
-//
-// Who When What
-// --- ---------- -----------------------------------------------------------
-// JLH 05/21/2010 Added this text. :-)
-// JLH 10/18/2010 Commented out all functions in preparation for deprecating
-// this class.
-//
-
-#include "snapper.h"
-
-#include "dialogfactory.h"
-#include "drawing.h"
-#include "entitycontainer.h"
-#include "graphicview.h"
-#include "grid.h"
-#include "information.h"
-#include "paintinterface.h"
-#include "settings.h"
-
-/**
- * Constructor.
- */
-Snapper::Snapper(EntityContainer & c, GraphicView & gv):
- container(&c), graphicView(&gv), finished(false)
-{
-#if 0
- init();
-#endif
-}
-
-Snapper::Snapper(void):
- container(NULL), graphicView(NULL), finished(false)
-{
-#if 0
- init();
-#endif
-}
-
-/**
- * Destructor.
- */
-Snapper::~Snapper()
-{
-}
-
-/**
- * Initialize (called by all constructors)
- */
-void Snapper::init()
-{
-#if 0
- if (graphicView)
- {
- snapMode = graphicView->getDefaultSnapMode();
- snapRes = graphicView->getSnapRestriction();
- }
-
- keyEntity = NULL;
- snapSpot = Vector(false);
- snapCoord = Vector(false);
- visible = false;
- distance = 1.0;
-
- settings.beginGroup("Snap");
- snapRange = settings.value("Range", 20).toInt();
- settings.endGroup();
- settings.beginGroup("Appearance");
- showCrosshairs = settings.value("ShowCrosshairs", true).toBool();
- settings.endGroup();
-
- // Sanity check
- if (snapRange < 2)
- snapRange = 20;
-#endif
-}
-
-void Snapper::finish()
-{
-#if 0
- finished = true;
-#endif
-}
-
-//bleh
-void Snapper::SetContainer(EntityContainer * c)
-{
-#if 0
- container = c;
-#endif
-}
-
-//bleh
-void Snapper::SetGraphicView(GraphicView * v)
-{
-#if 0
- graphicView = v;
-#endif
-}
-
-/**
- * @return Pointer to the entity which was the key entity for the last
- * successful snapping action. If the snap mode is "end point" the key entity
- * is the entity whose end point was caught. If the snap mode didn't require an
- * entity (e.g. free, grid) this method will return NULL.
- */
-Entity * Snapper::getKeyEntity()
-{
-#if 0
- return keyEntity;
-#endif
- return NULL;
-}
-
-/** Sets a new snap mode. */
-void Snapper::setSnapMode(RS2::SnapMode snapMode)
-{
-#if 0
- this->snapMode = snapMode;
-#endif
-}
-
-/** Sets a new snap restriction. */
-void Snapper::setSnapRestriction(RS2::SnapRestriction snapRes)
-{
-#if 0
- this->snapRes = snapRes;
-#endif
-}
-
-RS2::SnapMode Snapper::getSnapMode(void)
-{
-#if 0
- return snapMode;
-#endif
- return RS2::SnapFree;
-}
-
-RS2::SnapRestriction Snapper::getSnapRestriction(void)
-{
-#if 0
- return snapRes;
-#endif
- return RS2::RestrictNothing;
-}
-
-/**
- * Sets the snap range in pixels for catchEntity().
- *
- * @see catchEntity()
- */
-void Snapper::setSnapRange(int r)
-{
-#if 0
- snapRange = r;
-#endif
-}
-
-#if 0
-This should get moved into GraphicView...
-#endif
-/**
- * Snap to a coordinate in the drawing using the current snap mode.
- *
- * @param e A mouse event.
- * @return The coordinates of the point or an invalid vector.
- */
-Vector Snapper::snapPoint(QMouseEvent * e)
-{
-#if 0
- DEBUG->print("Snapper::snapPoint");
- snapSpot = Vector(false);
-
- if (!e)
- {
- DEBUG->print(Debug::D_WARNING, "Snapper::snapPoint: event is NULL");
- return snapSpot;
- }
-
- Vector mouseCoord = graphicView->toGraph(e->x(), e->y());
-
- switch (snapMode)
- {
- case RS2::SnapFree:
- snapSpot = snapFree(mouseCoord);
- break;
-
- case RS2::SnapEndpoint:
- snapSpot = snapEndpoint(mouseCoord);
- break;
-
- case RS2::SnapGrid:
- snapSpot = snapGrid(mouseCoord);
- break;
-
- case RS2::SnapOnEntity:
- snapSpot = snapOnEntity(mouseCoord);
- break;
-
- case RS2::SnapCenter:
- snapSpot = snapCenter(mouseCoord);
- break;
-
- case RS2::SnapMiddle:
- snapSpot = snapMiddle(mouseCoord);
- break;
-
- case RS2::SnapDist:
- snapSpot = snapDist(mouseCoord);
- break;
-
- case RS2::SnapIntersection:
- snapSpot = snapIntersection(mouseCoord);
- break;
-
- default:
- break;
- }
-
- // Handle snap restrictions that can be activated in addition to the ones above:
- switch (snapRes)
- {
- case RS2::RestrictOrthogonal:
- snapCoord = restrictOrthogonal(snapSpot);
- break;
- case RS2::RestrictHorizontal:
- snapCoord = restrictHorizontal(snapSpot);
- break;
- case RS2::RestrictVertical:
- snapCoord = restrictVertical(snapSpot);
- break;
- default:
- case RS2::RestrictNothing:
- snapCoord = snapSpot;
- break;
- }
-
- if (DIALOGFACTORY)
- DIALOGFACTORY->updateCoordinateWidget(snapCoord, snapCoord - graphicView->getRelativeZero());
-
- DEBUG->print("Snapper::snapPoint: OK");
-
- return snapCoord;
-#endif
- return Vector(false);
-}
-
-/**
- * Snaps to a free coordinate.
- *
- * @param coord The mouse coordinate.
- * @return The coordinates of the point or an invalid vector.
- */
-Vector Snapper::snapFree(Vector coord)
-{
-#if 0
- keyEntity = NULL;
- return coord;
-#endif
- return Vector(false);
-}
-
-/**
- * Snaps to the closest endpoint.
- *
- * @param coord The mouse coordinate.
- * @return The coordinates of the point or an invalid vector.
- */
-Vector Snapper::snapEndpoint(Vector coord)
-{
-#if 0
- Vector vec(false);
- vec = container->getNearestEndpoint(coord, NULL);
-
- return vec;
-#endif
- return Vector(false);
-}
-
-/**
- * Snaps to a grid point.
- *
- * @param coord The mouse coordinate.
- * @return The coordinates of the point or an invalid vector.
- */
-Vector Snapper::snapGrid(Vector coord)
-{
-#if 0
- DEBUG->print("Snapper::snapGrid begin");
-
- Vector vec(false);
- double dist = 0.0;
-
- Grid * grid = graphicView->getGrid();
-
- DEBUG->print("Snapper::snapGrid 001");
-
- if (grid)
- {
- DEBUG->print("Snapper::snapGrid 002");
- Vector * pts = grid->getPoints();
- DEBUG->print("Snapper::snapGrid 003");
- int closest = -1;
- dist = 32000.00;
- DEBUG->print("Snapper::snapGrid 004");
-
- for(int i=0; i<grid->count(); ++i)
- {
- double d = pts[i].distanceTo(coord);
-
- if (d < dist)
- {
- closest = i;
- dist = d;
- }
- }
-
- DEBUG->print("Snapper::snapGrid 005");
-
- if (closest >= 0)
- vec = pts[closest];
-
- DEBUG->print("Snapper::snapGrid 006");
- }
-
- keyEntity = NULL;
-
- DEBUG->print("Snapper::snapGrid end");
-
- return vec;
-#endif
- return Vector(false);
-}
-
-/**
- * Snaps to a point on an entity.
- *
- * @param coord The mouse coordinate.
- * @return The coordinates of the point or an invalid vector.
- */
-Vector Snapper::snapOnEntity(Vector coord)
-{
-#if 0
- Vector vec(false);
- vec = container->getNearestPointOnEntity(coord, true, NULL, &keyEntity);
-
- return vec;
-#endif
- return Vector(false);
-}
-
-/**
- * Snaps to the closest center.
- *
- * @param coord The mouse coordinate.
- * @return The coordinates of the point or an invalid vector.
- */
-Vector Snapper::snapCenter(Vector coord)
-{
-#if 0
- Vector vec(false);
- vec = container->getNearestCenter(coord, NULL);
-
- return vec;
-#endif
- return Vector(false);
-}
-
-/**
- * Snaps to the closest middle.
- *
- * @param coord The mouse coordinate.
- * @return The coordinates of the point or an invalid vector.
- */
-Vector Snapper::snapMiddle(Vector coord)
-{
-#if 0
- Vector vec(false);
- vec = container->getNearestMiddle(coord, NULL);
-
- return vec;
-#endif
- return Vector(false);
-}
-
-/**
- * Snaps to the closest point with a given distance to the endpoint.
- *
- * @param coord The mouse coordinate.
- * @return The coordinates of the point or an invalid vector.
- */
-Vector Snapper::snapDist(Vector coord)
-{
-#if 0
- Vector vec(false);
- vec = container->getNearestDist(distance, coord, NULL);
-
- return vec;
-#endif
- return Vector(false);
-}
-
-/**
- * Snaps to the closest intersection point.
- *
- * @param coord The mouse coordinate.
- * @return The coordinates of the point or an invalid vector.
- */
-Vector Snapper::snapIntersection(Vector coord)
-{
-#if 0
- Vector vec(false);
- vec = container->getNearestIntersection(coord, NULL);
-
- return vec;
-#endif
- return Vector(false);
-}
-
-/**
- * 'Corrects' the given coordinates to 0, 90, 180, 270 degrees relative to
- * the current relative zero point.
- *
- * @param coord The uncorrected coordinates.
- * @return The corrected coordinates.
- */
-Vector Snapper::restrictOrthogonal(Vector coord)
-{
-#if 0
- Vector rz = graphicView->getRelativeZero();
- Vector ret(coord);
-
- Vector retx = Vector(rz.x, ret.y);
- Vector rety = Vector(ret.x, rz.y);
-
- if (retx.distanceTo(ret) > rety.distanceTo(ret))
- ret = rety;
- else
- ret = retx;
-
- return ret;
-#endif
- return Vector(false);
-}
-
-/**
- * 'Corrects' the given coordinates to 0, 180 degrees relative to
- * the current relative zero point.
- *
- * @param coord The uncorrected coordinates.
- * @return The corrected coordinates.
- */
-Vector Snapper::restrictHorizontal(Vector coord)
-{
-#if 0
- Vector rz = graphicView->getRelativeZero();
-
- return Vector(coord.x, rz.y);
-#endif
- return Vector(false);
-}
-
-/**
- * 'Corrects' the given coordinates to 90, 270 degrees relative to
- * the current relative zero point.
- *
- * @param coord The uncorrected coordinates.
- * @return The corrected coordinates.
- */
-Vector Snapper::restrictVertical(Vector coord)
-{
-#if 0
- Vector rz = graphicView->getRelativeZero();
-
- return Vector(rz.x, coord.y);
-#endif
- return Vector(false);
-}
-
-/**
- * Catches an entity which is close to the given position 'pos'.
- *
- * @param pos A graphic coordinate.
- * @param level The level of resolving for iterating through the entity
- * container
- * @return Pointer to the entity or NULL.
- */
-Entity * Snapper::catchEntity(const Vector & pos, RS2::ResolveLevel level)
-{
-#if 0
- DEBUG->print("Snapper::catchEntity");
-
- // Set default distance for points inside solids
- double dist = graphicView->toGraphDX(snapRange) * 0.9;
- Entity * entity = container->getNearestEntity(pos, &dist, level);
- int idx = -1;
-
- if (entity && entity->getParent())
- idx = entity->getParent()->findEntity(entity);
-
- if (entity && dist <= graphicView->toGraphDX(snapRange))
- {
- // Highlight:
- DEBUG->print("Snapper::catchEntity: found: %d", idx);
- return entity;
- }
-// else
-// {
- DEBUG->print("Snapper::catchEntity: not found");
- return NULL;
-// }
-
-// DEBUG->print("Snapper::catchEntity: OK");
-#endif
- return NULL;
-}
-
-/**
- * Catches an entity which is close to the mouse cursor.
- *
- * @param e A mouse event.
- * @param level The level of resolving for iterating through the entity
- * container
- * @return Pointer to the entity or NULL.
- */
-Entity * Snapper::catchEntity(QMouseEvent * e, RS2::ResolveLevel level)
-{
-#if 0
- return catchEntity(Vector(graphicView->toGraphX(e->x()),
- graphicView->toGraphY(e->y())), level);
-#endif
- return NULL;
-}
-
-#if 0
-I think that these suspend() & resume() functions are not used anymore...
-#endif
-/**
- * Suspends this snapper while another action takes place.
- */
-/*virtual*/ void Snapper::suspend()
-{
-#if 0
-#warning "!!! This may need to have SetVisibility() called !!!"
-// deleteSnapper();
- snapSpot = snapCoord = Vector(false);
-#endif
-}
-
-/**
- * Resumes this snapper after it has been suspended.
- */
-/*virtual*/ void Snapper::resume()
-{
-#if 0
-#warning "!!! This may need to have SetVisibility() called !!!"
-// drawSnapper();
-#endif
-}
-
-/**
- * Hides the snapper options.
- */
-/*virtual*/ void Snapper::hideOptions()
-{
-#if 0
- if (snapMode == RS2::SnapDist && DIALOGFACTORY)
- DIALOGFACTORY->requestSnapDistOptions(distance, false);
-#endif
-}
-
-/**
- * Shows the snapper options.
- */
-/*virtual*/ void Snapper::showOptions()
-{
-#if 0
- if (snapMode == RS2::SnapDist && DIALOGFACTORY)
- DIALOGFACTORY->requestSnapDistOptions(distance, true);
-#endif
-}
-
-void Snapper::SetVisible(bool visibility/*= true*/)
-{
-#if 0
- visible = visibility;
-#endif
-}
-
-bool Snapper::Visible(void)
-{
-#if 0
- return visible;
-#endif
-}
-
-/*
-We need to figure out how to kick the GraphicView out of this class. Perhaps we
-need to move the toGuiX/Y() functions into another class as static functions.
-Further inspection seems to rule this out as they are pretty well coupled to the
-GraphicView class... What to do?
-*/
-void Snapper::Draw(GraphicView * view, PaintInterface * painter)
-{
-#if 0
- if (finished || !snapSpot.valid || !snapCoord.valid)
- return;
-
-//hm, I don't like graphicView kicking around in here, especially since it now
-//lives inside GraphicView... How to !!! FIX !!!?
-//We'll pass it in for now...
- // Snap point (need to make sure the brush is NULL!)
-// painter->setPen(Pen(Color(0, 127, 255), RS2::Width00, RS2::DashLine));
- painter->setPen(Pen(Color(255, 127, 0), RS2::Width00, RS2::DashLine));
- painter->drawCircle(view->toGui(snapCoord), 4);
-
- // Crosshairs
- if (showCrosshairs)
- {
- painter->setPen(Pen(Color(0, 255, 255), RS2::Width00, RS2::DashLine));
- painter->drawLine(Vector(0, view->toGuiY(snapCoord.y)),
- Vector(view->getWidth(), view->toGuiY(snapCoord.y)));
- painter->drawLine(Vector(view->toGuiX(snapCoord.x), 0),
- Vector(view->toGuiX(snapCoord.x), view->getHeight()));
- }
-
- if (snapCoord != snapSpot)
- {
- painter->drawLine(view->toGui(snapSpot) + Vector(-5, 0),
- view->toGui(snapSpot) + Vector(-1, 4));
- painter->drawLine(view->toGui(snapSpot) + Vector(0, 5),
- view->toGui(snapSpot) + Vector(4, 1));
- painter->drawLine(view->toGui(snapSpot) + Vector(5, 0),
- view->toGui(snapSpot) + Vector(1, -4));
- painter->drawLine(view->toGui(snapSpot) + Vector(0, -5),
- view->toGui(snapSpot) + Vector(-4, -1));
- }
-#endif
-}