← Back to team overview

kicad-developers team mailing list archive

Re: [PATCH] Add Insert hotkey to add a zone/segment corner

 

Hi Seth,

Sorry about that, I think I've checked the right thing here.

I suppose many global TOOL_ACTIONs are actually unsafe to call from a
global context but are defended implicitly by not providing a hotkey
or menu item when the action isn't ready to get called.

Cheers,

John

On Wed, Aug 1, 2018 at 6:23 PM, Seth Hillbrand <seth@xxxxxxxxxxxxx> wrote:
> Hi John-
>
> I get a segfault when trying to use the hotkey to insert a point on a zone
> when it is not selected.
>
> -Seth
>
>
> Am Di., 24. Juli 2018 um 10:00 Uhr schrieb John Beard
> <john.j.beard@xxxxxxxxx>:
>>
>> This patch adds a hotkey for adding a corner to a zone or segment.
>> This is an action I have really struggled without a hotkey for! I have
>> used Insert, as:
>>
>> 1) it's available and
>> 2) it's what Inkscape uses (but Inkscape adds the point at the
>> midpoint of the segment, this adds at the cursor).
>>
>> It doesn't work on polygons, as that is still unimplemented in the
>> underlying action.
>>
>> Cheers,
>>
>> John
>> _______________________________________________
>> Mailing list: https://launchpad.net/~kicad-developers
>> Post to     : kicad-developers@xxxxxxxxxxxxxxxxxxx
>> Unsubscribe : https://launchpad.net/~kicad-developers
>> More help   : https://help.launchpad.net/ListHelp
From 3dea08d584e56e2fa65a01844aa4a425557704ff Mon Sep 17 00:00:00 2001
From: John Beard <john.j.beard@xxxxxxxxx>
Date: Tue, 24 Jul 2018 16:15:05 +0100
Subject: [PATCH] Add hotkey (Insert) for zone create corner

Adds a hotkey to the TOOL_ACTION, and also checks for
action validity prior to running the actions (previously
implicitly gated by  the enablement of the menu item).
---
 pcbnew/hotkeys.cpp            |  6 ++++++
 pcbnew/hotkeys.h              |  3 ++-
 pcbnew/tools/point_editor.cpp | 28 ++++++++++++++++++++++------
 pcbnew/tools/point_editor.h   |  3 +++
 4 files changed, 33 insertions(+), 7 deletions(-)

diff --git a/pcbnew/hotkeys.cpp b/pcbnew/hotkeys.cpp
index 25a93e694..32fcc3192 100644
--- a/pcbnew/hotkeys.cpp
+++ b/pcbnew/hotkeys.cpp
@@ -310,6 +310,9 @@ static EDA_HOTKEY HkToggleCursor( _HKI( "Toggle Cursor Display (Modern Toolset o
 static EDA_HOTKEY HkMeasureTool( _HKI( "Measure Distance (Modern Toolset only)" ),
                                  HK_MEASURE_TOOL, 'M' + GR_KB_SHIFTCTRL );
 
+static EDA_HOTKEY HkInsertCorner( _HKI( "Insert Corner (Modern Toolset only)" ),
+                                 HK_INSERT_CORNER, WXK_INSERT );
+
 // List of common hotkey descriptors
 EDA_HOTKEY* common_Hotkey_List[] =
 {
@@ -384,6 +387,9 @@ EDA_HOTKEY* board_edit_Hotkey_List[] =
     &HkZoneFillOrRefill,
     &HkZoneRemoveFilled,
 
+    // Point editor (zones and segments)
+    &HkInsertCorner,
+
     // Highlight and display
     &HkSelectConnection,
     &HkSelectCopper,
diff --git a/pcbnew/hotkeys.h b/pcbnew/hotkeys.h
index f4e6fa446..b22817675 100644
--- a/pcbnew/hotkeys.h
+++ b/pcbnew/hotkeys.h
@@ -133,7 +133,8 @@ enum hotkey_id_commnand {
     HK_DP_DIMENSIONS,
     HK_VIA_SIZE_INC,
     HK_VIA_SIZE_DEC,
-    HK_HIGHLIGHT_NET_SELECTION
+    HK_HIGHLIGHT_NET_SELECTION,
+    HK_INSERT_CORNER
 };
 
 // Full list of hotkey descriptors for board editor and footprint editor
diff --git a/pcbnew/tools/point_editor.cpp b/pcbnew/tools/point_editor.cpp
index 9d994479e..8fcc3f420 100644
--- a/pcbnew/tools/point_editor.cpp
+++ b/pcbnew/tools/point_editor.cpp
@@ -50,7 +50,7 @@ using namespace std::placeholders;
 
 // Point editor
 TOOL_ACTION PCB_ACTIONS::pointEditorAddCorner( "pcbnew.PointEditor.addCorner",
-        AS_GLOBAL, 0,
+        AS_GLOBAL, WXK_INSERT,
         _( "Create Corner" ), _( "Create a corner" ), add_corner_xpm );
 
 TOOL_ACTION PCB_ACTIONS::pointEditorRemoveCorner( "pcbnew.PointEditor.removeCorner",
@@ -850,17 +850,25 @@ void POINT_EDITOR::setTransitions()
 }
 
 
+bool POINT_EDITOR::canAddCorner( const EDA_ITEM& aItem )
+{
+    const auto type = aItem.Type();
+
+    // Works only for zones and line segments
+    return type == PCB_ZONE_AREA_T ||
+           ( ( type == PCB_LINE_T || type == PCB_MODULE_EDGE_T ) &&
+               static_cast<const DRAWSEGMENT&>( aItem ).GetShape() == S_SEGMENT );
+}
+
+
 bool POINT_EDITOR::addCornerCondition( const SELECTION& aSelection )
 {
     if( aSelection.Size() != 1 )
         return false;
 
-    auto item = aSelection.Front();
+    const EDA_ITEM* item = aSelection.Front();
 
-    // Works only for zones and line segments
-    return item->Type() == PCB_ZONE_AREA_T ||
-           ( ( item->Type() == PCB_LINE_T || item->Type() == PCB_MODULE_EDGE_T ) &&
-               static_cast<DRAWSEGMENT*>( item )->GetShape() == S_SEGMENT );
+    return ( item != nullptr ) && canAddCorner( *item );
 }
 
 
@@ -916,9 +924,17 @@ bool POINT_EDITOR::removeCornerCondition( const SELECTION& )
 
 int POINT_EDITOR::addCorner( const TOOL_EVENT& aEvent )
 {
+    if( !m_editPoints )
+        return 0;
+
     EDA_ITEM* item = m_editPoints->GetParent();
     PCB_BASE_EDIT_FRAME* frame = getEditFrame<PCB_BASE_EDIT_FRAME>();
     const VECTOR2I& cursorPos = getViewControls()->GetCursorPosition();
+
+    // called without an active edited polygon
+    if( !item || !canAddCorner( *item ) )
+        return 0;
+
     BOARD_COMMIT commit( frame );
 
     if( item->Type() == PCB_ZONE_AREA_T )
diff --git a/pcbnew/tools/point_editor.h b/pcbnew/tools/point_editor.h
index 230a6f5c7..6ebcb1787 100644
--- a/pcbnew/tools/point_editor.h
+++ b/pcbnew/tools/point_editor.h
@@ -129,6 +129,9 @@ private:
     ///> Condition to display "Create corner" context menu entry.
     static bool addCornerCondition( const SELECTION& aSelection );
 
+    ///> Determine if the tool can currently add a corner to the given item
+    static bool canAddCorner( const EDA_ITEM& aItem );
+
     ///> Condition to display "Remove corner" context menu entry.
     bool removeCornerCondition( const SELECTION& aSelection );
 
-- 
2.17.1


Follow ups

References