]> Shamusworld >> Repos - architektonas/blobdiff - src/base/rs_snapper.cpp
In the middle of major refactoring...
[architektonas] / src / base / rs_snapper.cpp
index 1ee3007862032c1f1700345fec5ffe7a819ef85a..bbc306cc736098748264ea3b8091b0696b7ba217 100644 (file)
 #include "rs_snapper.h"
 
 #include "rs_dialogfactory.h"
-#include "rs_entitycontainer.h"
 #include "drawing.h"
-#include "rs_graphicview.h"
+#include "rs_entitycontainer.h"
+#include "graphicview.h"
 #include "rs_grid.h"
 #include "rs_information.h"
+#include "paintinterface.h"
 #include "settings.h"
 
 /**
  * Constructor.
  */
-RS_Snapper::RS_Snapper(RS_EntityContainer & c, RS_GraphicView & gv):
+RS_Snapper::RS_Snapper(RS_EntityContainer & c, GraphicView & gv):
        container(&c), graphicView(&gv), finished(false)
 {
        init();
 }
 
+RS_Snapper::RS_Snapper(void):
+       container(NULL), graphicView(NULL), finished(false)
+{
+       init();
+}
+
 /**
  * Destructor.
  */
@@ -43,8 +50,12 @@ RS_Snapper::~RS_Snapper()
  */
 void RS_Snapper::init()
 {
-       snapMode = graphicView->getDefaultSnapMode();
-       snapRes = graphicView->getSnapRestriction();
+       if (graphicView)
+       {
+               snapMode = graphicView->getDefaultSnapMode();
+               snapRes = graphicView->getSnapRestriction();
+       }
+
        keyEntity = NULL;
        snapSpot = Vector(false);
        snapCoord = Vector(false);
@@ -68,12 +79,23 @@ void RS_Snapper::finish()
        finished = true;
 }
 
+//bleh
+void RS_Snapper::SetContainer(RS_EntityContainer * c)
+{
+       container = c;
+}
+
+//bleh
+void RS_Snapper::SetGraphicView(GraphicView * v)
+{
+       graphicView = v;
+}
+
 /**
- * @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 whos end point was caught.
- * If the snap mode didn't require an entity (e.g. free, grid) this
- * method will return NULL.
+ * @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 whos end point was caught. If the snap mode didn't require an
+ * entity (e.g. free, grid) this method will return NULL.
  */
 RS_Entity * RS_Snapper::getKeyEntity()
 {
@@ -111,11 +133,9 @@ void RS_Snapper::setSnapRange(int r)
 Vector RS_Snapper::snapPoint(QMouseEvent * e)
 {
        RS_DEBUG->print("RS_Snapper::snapPoint");
-
-       deleteSnapper();
        snapSpot = Vector(false);
 
-       if (e == NULL)
+       if (!e)
        {
                RS_DEBUG->print(RS_Debug::D_WARNING, "RS_Snapper::snapPoint: event is NULL");
                return snapSpot;
@@ -161,8 +181,7 @@ Vector RS_Snapper::snapPoint(QMouseEvent * e)
                break;
        }
 
-       // handle snap restrictions that can be activated in addition
-       //   to the ones above:
+       // Handle snap restrictions that can be activated in addition to the ones above:
        switch (snapRes)
        {
        case RS2::RestrictOrthogonal:
@@ -180,10 +199,7 @@ Vector RS_Snapper::snapPoint(QMouseEvent * e)
                break;
        }
 
-#warning "!!! THIS IS WHERE THE SNAPPER IS BEING DRAWN... !!!"
-       drawSnapper();
-
-       if (RS_DIALOGFACTORY != NULL)
+       if (RS_DIALOGFACTORY)
                RS_DIALOGFACTORY->updateCoordinateWidget(snapCoord, snapCoord - graphicView->getRelativeZero());
 
        RS_DEBUG->print("RS_Snapper::snapPoint: OK");
@@ -212,8 +228,8 @@ Vector RS_Snapper::snapFree(Vector coord)
 Vector RS_Snapper::snapEndpoint(Vector coord)
 {
        Vector vec(false);
-
        vec = container->getNearestEndpoint(coord, NULL/*, &keyEntity*/);
+
        return vec;
 }
 
@@ -234,7 +250,7 @@ Vector RS_Snapper::snapGrid(Vector coord)
 
        RS_DEBUG->print("RS_Snapper::snapGrid 001");
 
-       if (grid != NULL)
+       if (grid)
        {
                RS_DEBUG->print("RS_Snapper::snapGrid 002");
                Vector * pts = grid->getPoints();
@@ -279,6 +295,7 @@ Vector RS_Snapper::snapOnEntity(Vector coord)
 {
        Vector vec(false);
        vec = container->getNearestPointOnEntity(coord, true, NULL, &keyEntity);
+
        return vec;
 }
 
@@ -291,8 +308,8 @@ Vector RS_Snapper::snapOnEntity(Vector coord)
 Vector RS_Snapper::snapCenter(Vector coord)
 {
        Vector vec(false);
-
        vec = container->getNearestCenter(coord, NULL);
+
        return vec;
 }
 
@@ -305,8 +322,8 @@ Vector RS_Snapper::snapCenter(Vector coord)
 Vector RS_Snapper::snapMiddle(Vector coord)
 {
        Vector vec(false);
-
        vec = container->getNearestMiddle(coord, NULL);
+
        return vec;
 }
 
@@ -319,8 +336,8 @@ Vector RS_Snapper::snapMiddle(Vector coord)
 Vector RS_Snapper::snapDist(Vector coord)
 {
        Vector vec(false);
-
        vec = container->getNearestDist(distance, coord, NULL);
+
        return vec;
 }
 
@@ -333,8 +350,8 @@ Vector RS_Snapper::snapDist(Vector coord)
 Vector RS_Snapper::snapIntersection(Vector coord)
 {
        Vector vec(false);
-
        vec = container->getNearestIntersection(coord, NULL);
+
        return vec;
 }
 
@@ -371,8 +388,8 @@ Vector RS_Snapper::restrictOrthogonal(Vector coord)
 Vector RS_Snapper::restrictHorizontal(Vector coord)
 {
        Vector rz = graphicView->getRelativeZero();
-       Vector ret = Vector(coord.x, rz.y);
-       return ret;
+
+       return Vector(coord.x, rz.y);
 }
 
 /**
@@ -385,8 +402,8 @@ Vector RS_Snapper::restrictHorizontal(Vector coord)
 Vector RS_Snapper::restrictVertical(Vector coord)
 {
        Vector rz = graphicView->getRelativeZero();
-       Vector ret = Vector(rz.x, coord.y);
-       return ret;
+
+       return Vector(rz.x, coord.y);
 }
 
 /**
@@ -408,10 +425,10 @@ RS_Entity * RS_Snapper::catchEntity(const Vector& pos, RS2::ResolveLevel level)
 
        int idx = -1;
 
-       if (entity != NULL && entity->getParent() != NULL)
+       if (entity && entity->getParent())
                idx = entity->getParent()->findEntity(entity);
 
-       if (entity != NULL && dist <= graphicView->toGraphDX(snapRange))
+       if (entity && dist <= graphicView->toGraphDX(snapRange))
        {
                // highlight:
                RS_DEBUG->print("RS_Snapper::catchEntity: found: %d", idx);
@@ -424,7 +441,7 @@ RS_Entity * RS_Snapper::catchEntity(const Vector& pos, RS2::ResolveLevel level)
        }
 
        RS_DEBUG->print("RS_Snapper::catchEntity: OK");
-       }
+}
 
 /**
  * Catches an entity which is close to the mouse cursor.
@@ -443,7 +460,7 @@ RS_Entity * RS_Snapper::catchEntity(QMouseEvent * e, RS2::ResolveLevel level)
 /**
  * Suspends this snapper while another action takes place.
  */
-void RS_Snapper::suspend()
+/*virtual*/ void RS_Snapper::suspend()
 {
        deleteSnapper();
        snapSpot = snapCoord = Vector(false);
@@ -452,29 +469,27 @@ void RS_Snapper::suspend()
 /**
  * Resumes this snapper after it has been suspended.
  */
-void RS_Snapper::resume()
+/*virtual*/ void RS_Snapper::resume()
 {
        drawSnapper();
 }
 
 /**
- * Hides the snapper options. Default implementation does nothing.
+ * Hides the snapper options.
  */
-void RS_Snapper::hideOptions()
+/*virtual*/ void RS_Snapper::hideOptions()
 {
-       if (snapMode == RS2::SnapDist)
-               if (RS_DIALOGFACTORY!=NULL)
-                       RS_DIALOGFACTORY->requestSnapDistOptions(distance, false);
+       if (snapMode == RS2::SnapDist && RS_DIALOGFACTORY)
+               RS_DIALOGFACTORY->requestSnapDistOptions(distance, false);
 }
 
 /**
- * Shows the snapper options. Default implementation does nothing.
+ * Shows the snapper options.
  */
-void RS_Snapper::showOptions()
+/*virtual*/ void RS_Snapper::showOptions()
 {
-       if (snapMode == RS2::SnapDist)
-               if (RS_DIALOGFACTORY != NULL)
-                       RS_DIALOGFACTORY->requestSnapDistOptions(distance, true);
+       if (snapMode == RS2::SnapDist && RS_DIALOGFACTORY)
+               RS_DIALOGFACTORY->requestSnapDistOptions(distance, true);
 }
 
 /**
@@ -482,6 +497,7 @@ void RS_Snapper::showOptions()
  */
 void RS_Snapper::drawSnapper()
 {
+printf("RS_Snapper::drawSnapper(): Using DEPRECATED function!!!\n");
        if (!visible)
                xorSnapper();
 }
@@ -491,6 +507,7 @@ void RS_Snapper::drawSnapper()
  */
 void RS_Snapper::deleteSnapper()
 {
+printf("RS_Snapper::deleteSnapper(): Using DEPRECATED function!!!\n");
        if (visible)
        {
                xorSnapper();
@@ -504,6 +521,7 @@ void RS_Snapper::deleteSnapper()
  */
 void RS_Snapper::xorSnapper()
 {
+#warning "!!! RS_Snapper::xorSnapper() is DEPRECATED !!!"
 //Not completely true...
 //#warning "!!! xorSnapper() not working AT ALL !!!"
 #if 0
@@ -543,8 +561,8 @@ void RS_Snapper::xorSnapper()
                graphicView->destroyPainter();
                visible = !visible;
        }
-#else
-       if (finished || !snapSpot.valid || graphicView == NULL)
+//#else
+       if (finished || !snapSpot.valid || !graphicView)
                return;
 
        graphicView->SetSnapperDraw(true);
@@ -555,3 +573,51 @@ void RS_Snapper::xorSnapper()
        visible = !visible;
 #endif
 }
+
+void RS_Snapper::SetVisible(bool visibility/*= true*/)
+{
+       visible = visibility;
+}
+
+bool RS_Snapper::Visible(void)
+{
+       return visible;
+}
+
+void RS_Snapper::Draw(GraphicView * view, PaintInterface * painter)
+{
+       if (finished || !snapSpot.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...
+       if (snapCoord.valid)
+       {
+               // snap point
+               painter->setPen(RS_Pen(RS_Color(0, 127, 255), RS2::Width00, RS2::DashLine));
+               painter->drawCircle(view->toGui(snapCoord), 4);
+
+               // crosshairs
+               if (showCrosshairs)
+               {
+                       painter->setPen(RS_Pen(RS_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.valid && 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));
+       }
+}