← Back to team overview

kicad-developers team mailing list archive

[PATCH] Angle snap of ruler tool

 

HI,

Here's a small patch building on the two-point geometry manager to
allow use of that manager's angle snapping function.

This was mentioned as a comment in
https://bugs.launchpad.net/kicad/+bug/16720400, but there's no
specific issue for it.

Cheers,

John
From 0f0b4d02d4c8a6857a553fe210ebc6f9e76d3a09 Mon Sep 17 00:00:00 2001
From: John Beard <john.j.beard@xxxxxxxxx>
Date: Thu, 16 Mar 2017 22:37:39 +0800
Subject: [PATCH] Enable angle snap for pcbnew GAL ruler tool

This uses the two-point geometry manager to split the logic of the ruler
geometry and the preview item display. This allows the ruler to use that
manager's angle snap feature.
---
 common/preview_items/ruler_item.cpp | 24 ++++++++++++++----------
 include/preview_items/ruler_item.h  | 21 ++++-----------------
 pcbnew/tools/edit_tool.cpp          | 16 ++++++++++------
 3 files changed, 28 insertions(+), 33 deletions(-)

diff --git a/common/preview_items/ruler_item.cpp b/common/preview_items/ruler_item.cpp
index 331fc45fb..c820f7f87 100644
--- a/common/preview_items/ruler_item.cpp
+++ b/common/preview_items/ruler_item.cpp
@@ -203,8 +203,9 @@ void drawBacksideTicks( KIGFX::GAL& aGal, const VECTOR2D& aOrigin,
 }
 
 
-RULER_ITEM::RULER_ITEM():
-    EDA_ITEM( NOT_USED )    // Never added to anything - just a preview
+RULER_ITEM::RULER_ITEM( const TWO_POINT_GEOMETRY_MANAGER& aGeomMgr ):
+    EDA_ITEM( NOT_USED ),    // Never added to anything - just a preview
+    m_geomMgr( aGeomMgr )
 {}
 
 
@@ -212,8 +213,8 @@ const BOX2I RULER_ITEM::ViewBBox() const
 {
     BOX2I tmp;
 
-    tmp.SetOrigin( m_origin );
-    tmp.SetEnd( m_end );
+    tmp.SetOrigin( m_geomMgr.GetOrigin() );
+    tmp.SetEnd( m_geomMgr.GetEnd() );
     tmp.Normalize();
     return tmp;
 }
@@ -230,20 +231,23 @@ void RULER_ITEM::ViewDraw( int aLayer, KIGFX::VIEW* aView ) const
 {
     auto& gal = *aView->GetGAL();
 
+    const auto origin = m_geomMgr.GetOrigin();
+    const auto end = m_geomMgr.GetEnd();
+
     gal.SetLineWidth( 1.0 );
     gal.SetIsStroke( true );
     gal.SetIsFill( false );
     gal.SetStrokeColor( PreviewOverlayDefaultColor() );
 
     // draw the main line from the origin to cursor
-    gal.DrawLine( m_origin, m_end );
+    gal.DrawLine( origin, end );
 
-    VECTOR2D rulerVec( m_end - m_origin );
+    VECTOR2D rulerVec( end - origin );
 
     // constant text size on screen
     SetConstantGlyphHeight( gal, 12.0 );
 
-    drawCursorStrings( gal, m_end, rulerVec );
+    drawCursorStrings( gal, end, rulerVec );
 
     // tick label size
     SetConstantGlyphHeight( gal, 10.0 );
@@ -251,11 +255,11 @@ void RULER_ITEM::ViewDraw( int aLayer, KIGFX::VIEW* aView ) const
     // basic tick size
     const double minorTickLen = 5.0 / gal.GetWorldScale();
 
-    drawTicksAlongLine( gal, m_origin, rulerVec, minorTickLen );
+    drawTicksAlongLine( gal, origin, rulerVec, minorTickLen );
 
     gal.SetStrokeColor( PreviewOverlayDefaultColor().WithAlpha( PreviewOverlayDeemphAlpha( true ) ) );
-    drawBacksideTicks( gal, m_origin, rulerVec, minorTickLen * majorTickLengthFactor, 2 );
+    drawBacksideTicks( gal, origin, rulerVec, minorTickLen * majorTickLengthFactor, 2 );
 
     // draw the back of the origin "crosshair"
-    gal.DrawLine( m_origin, m_origin + rulerVec.Resize( -minorTickLen * midTickLengthFactor ) );
+    gal.DrawLine( origin, origin + rulerVec.Resize( -minorTickLen * midTickLengthFactor ) );
 }
diff --git a/include/preview_items/ruler_item.h b/include/preview_items/ruler_item.h
index ea32d83c6..c651ca4c0 100644
--- a/include/preview_items/ruler_item.h
+++ b/include/preview_items/ruler_item.h
@@ -25,6 +25,7 @@
 #define PREVIEW_ITEMS_RULER_ITEM_H
 
 #include <base_struct.h>
+#include <preview_items/two_point_geom_manager.h>
 
 namespace KIGFX
 {
@@ -32,6 +33,7 @@ class GAL;
 
 namespace PREVIEW
 {
+class TWO_POINT_GEOMETRY_MANAGER;
 
 /**
  * Class RULER_ITEM
@@ -42,7 +44,7 @@ class RULER_ITEM : public EDA_ITEM
 {
 public:
 
-    RULER_ITEM();
+    RULER_ITEM( const TWO_POINT_GEOMETRY_MANAGER& m_geomMgr );
 
     ///> @copydoc EDA_ITEM::ViewBBox()
     const BOX2I ViewBBox() const override;
@@ -69,24 +71,9 @@ public:
         return wxT( "RULER_ITEM" );
     }
 
-    ///> Set the origin of the ruler (the fixed end)
-    void SetOrigin( VECTOR2I aOrigin )
-    {
-        m_origin = aOrigin;
-    }
-
-    /**
-     * Set the current end of the rectangle (the end that moves
-     * with the cursor.
-     */
-    void SetEnd( VECTOR2I aEnd )
-    {
-        m_end = aEnd;
-    }
-
 private:
 
-    VECTOR2I m_origin, m_end;
+    const TWO_POINT_GEOMETRY_MANAGER& m_geomMgr;
 };
 
 } // PREVIEW
diff --git a/pcbnew/tools/edit_tool.cpp b/pcbnew/tools/edit_tool.cpp
index f6de28069..00d9e99a3 100644
--- a/pcbnew/tools/edit_tool.cpp
+++ b/pcbnew/tools/edit_tool.cpp
@@ -950,7 +950,10 @@ int EDIT_TOOL::MeasureTool( const TOOL_EVENT& aEvent )
                                          : ID_PCB_MEASUREMENT_TOOL,
                         wxCURSOR_PENCIL, _( "Measure distance between two points" ) );
 
-    KIGFX::PREVIEW::RULER_ITEM ruler;
+    KIGFX::PREVIEW::TWO_POINT_GEOMETRY_MANAGER twoPtMgr;
+
+    KIGFX::PREVIEW::RULER_ITEM ruler( twoPtMgr );
+
     view.Add( &ruler );
     view.SetVisible( &ruler, false );
 
@@ -974,8 +977,8 @@ int EDIT_TOOL::MeasureTool( const TOOL_EVENT& aEvent )
         {
             if( !evt->IsDrag( BUT_LEFT ) )
             {
-                ruler.SetOrigin( cursorPos );
-                ruler.SetEnd( cursorPos );
+                twoPtMgr.SetOrigin( cursorPos );
+                twoPtMgr.SetEnd( cursorPos );
             }
 
             controls.CaptureCursor( true );
@@ -988,8 +991,8 @@ int EDIT_TOOL::MeasureTool( const TOOL_EVENT& aEvent )
         {
             // make sure the origin is set before a drag starts
             // otherwise you can miss a step
-            ruler.SetOrigin( cursorPos );
-            ruler.SetEnd( cursorPos );
+            twoPtMgr.SetOrigin( cursorPos );
+            twoPtMgr.SetEnd( cursorPos );
         }
 
         // second click or mouse up after drag ends
@@ -1008,7 +1011,8 @@ int EDIT_TOOL::MeasureTool( const TOOL_EVENT& aEvent )
         else if( originSet &&
                 ( evt->IsMotion() || evt->IsDrag( BUT_LEFT ) ) )
         {
-            ruler.SetEnd( cursorPos );
+            twoPtMgr.SetAngleSnap( evt->Modifier( MD_CTRL ) );
+            twoPtMgr.SetEnd( cursorPos );
 
             view.SetVisible( &ruler, true );
             view.Update( &ruler, KIGFX::GEOMETRY );
-- 
2.12.0


Follow ups