← Back to team overview

kicad-developers team mailing list archive

[PATCH] Move ZOOM_TOOL to common; add RMB-drag to zoom out

 

Hi all,

The attached patch refactors ZOOM_TOOL to common so that it can be used in
GerbView, and adds a new behavior where you can use the right mouse button
to zoom out when the zoom to selection tool is active.  I find this
behavior useful when using a 2-button mouse.

Best,
Jon
From 21d6d116c4157f0cec132e2c9f64b9c4c1895de7 Mon Sep 17 00:00:00 2001
From: Jon Evans <jon@xxxxxxxxxxxxx>
Date: Wed, 30 Aug 2017 22:35:30 -0400
Subject: [PATCH] Move ZOOM_TOOL to common; add RMB-drag to zoom out

---
 common/CMakeLists.txt                       |  1 +
 common/tool/actions.cpp                     |  4 ++++
 {pcbnew/tools => common/tool}/zoom_tool.cpp | 36 ++++++++++++++++++-----------
 {pcbnew/tools => common/tool}/zoom_tool.h   |  6 ++---
 include/draw_frame.h                        | 14 +++++++++++
 include/tool/actions.h                      |  1 +
 pcbnew/CMakeLists.txt                       |  1 -
 pcbnew/moduleframe.cpp                      |  2 +-
 pcbnew/tools/pcb_actions.cpp                |  2 +-
 pcbnew/tools/pcb_actions.h                  |  3 +--
 pcbnew/tools/pcbnew_control.cpp             |  4 ----
 pcbnew/tools/tools_common.cpp               |  2 +-
 12 files changed, 50 insertions(+), 26 deletions(-)
 rename {pcbnew/tools => common/tool}/zoom_tool.cpp (75%)
 rename {pcbnew/tools => common/tool}/zoom_tool.h (91%)

diff --git a/common/CMakeLists.txt b/common/CMakeLists.txt
index 13f2f9871..42fa4ddb2 100644
--- a/common/CMakeLists.txt
+++ b/common/CMakeLists.txt
@@ -337,6 +337,7 @@ set( COMMON_SRCS
     tool/tool_menu.cpp
     tool/conditional_menu.cpp
     tool/selection_conditions.cpp
+    tool/zoom_tool.cpp
 
     geometry/seg.cpp
     geometry/shape.cpp
diff --git a/common/tool/actions.cpp b/common/tool/actions.cpp
index 2bd8661d3..0aa24e8cb 100644
--- a/common/tool/actions.cpp
+++ b/common/tool/actions.cpp
@@ -35,6 +35,10 @@ TOOL_ACTION ACTIONS::zoomFitScreen( "common.Control.zoomFitScreen",
         AS_GLOBAL, TOOL_ACTION::LegacyHotKey( HK_ZOOM_AUTO ),
         _( "Zoom Auto" ), "", zoom_fit_in_page_xpm );
 
+TOOL_ACTION ACTIONS::zoomTool( "common.Control.zoomTool",
+        AS_GLOBAL, TOOL_ACTION::LegacyHotKey( HK_ZOOM_SELECTION ),
+        _( "Zoom to Selection" ), "", NULL, AF_ACTIVATE );
+
 TOOL_ACTION ACTIONS::zoomPreset( "common.Control.zoomPreset",
         AS_GLOBAL, 0,
         "", "" );
diff --git a/pcbnew/tools/zoom_tool.cpp b/common/tool/zoom_tool.cpp
similarity index 75%
rename from pcbnew/tools/zoom_tool.cpp
rename to common/tool/zoom_tool.cpp
index 685a0be0e..1180dde64 100644
--- a/pcbnew/tools/zoom_tool.cpp
+++ b/common/tool/zoom_tool.cpp
@@ -1,7 +1,7 @@
 /*
  * This program source code file is part of KiCad, a free EDA CAD application.
  *
- * Copyright (C) 2016 KiCad Developers, see AUTHORS.txt for contributors.
+ * Copyright (C) 2017 KiCad Developers, see AUTHORS.txt for contributors.
  *
  * This program is free software: you can redistribute it and/or modify it
  * under the terms of the GNU General Public License as published by the
@@ -23,14 +23,13 @@
 #include <view/view_controls.h>
 #include <view/view.h>
 #include <tool/tool_manager.h>
+#include <tool/actions.h>
+#include <tool/zoom_tool.h>
 #include <preview_items/selection_area.h>
 
-#include "zoom_tool.h"
-#include "pcb_actions.h"
-
 
 ZOOM_TOOL::ZOOM_TOOL() :
-        TOOL_INTERACTIVE( "pcbnew.Control.zoomTool" )
+        TOOL_INTERACTIVE( "common.Control.zoomTool" )
 {
     m_frame = NULL;
 }
@@ -41,14 +40,14 @@ ZOOM_TOOL::~ZOOM_TOOL() {}
 
 void ZOOM_TOOL::Reset( RESET_REASON aReason )
 {
-    m_frame = getEditFrame<PCB_EDIT_FRAME>();
+    m_frame = getEditFrame<EDA_DRAW_FRAME>();
 }
 
 
 int ZOOM_TOOL::Main( const TOOL_EVENT& aEvent )
 {
     // This method is called both when the zoom tool is activated (on) or deactivated (off)
-    bool zoom_tool_is_on = m_frame->GetMainToolBar()->GetToolToggled( ID_ZOOM_SELECTION );
+    bool zoom_tool_is_on = m_frame->GetToolToggled( ID_ZOOM_SELECTION );
 
     if( !zoom_tool_is_on )  // This is a tool deselection: do nothing
         return 0;
@@ -60,7 +59,7 @@ int ZOOM_TOOL::Main( const TOOL_EVENT& aEvent )
         if( evt->IsCancel() || evt->IsActivate() )
             break;
 
-        else if( evt->IsDrag( BUT_LEFT ) )
+        else if( evt->IsDrag( BUT_LEFT ) || evt->IsDrag( BUT_RIGHT ) )
         {
             if( selectRegion() )
                 break;
@@ -94,7 +93,7 @@ bool ZOOM_TOOL::selectRegion()
             break;
         }
 
-        if( evt->IsDrag( BUT_LEFT ) )
+        if( evt->IsDrag( BUT_LEFT ) || evt->IsDrag( BUT_RIGHT ) )
         {
             area.SetOrigin( evt->DragOrigin() );
             area.SetEnd( evt->Position() );
@@ -102,7 +101,7 @@ bool ZOOM_TOOL::selectRegion()
             view->Update( &area, KIGFX::GEOMETRY );
         }
 
-        if( evt->IsMouseUp( BUT_LEFT ) )
+        if( evt->IsMouseUp( BUT_LEFT ) || evt->IsMouseUp( BUT_RIGHT ) )
         {
             view->SetVisible( &area, false );
             auto selectionBox = area.ViewBBox();
@@ -116,8 +115,19 @@ bool ZOOM_TOOL::selectRegion()
             else
             {
                 VECTOR2D vsize = selectionBox.GetSize();
-                double scale = view->GetScale() / std::max( fabs( vsize.x / screenSize.x ),
-                                                            fabs( vsize.y / screenSize.y ) );
+                double scale;
+                double ratio = std::max( fabs( vsize.x / screenSize.x ),
+                                         fabs( vsize.y / screenSize.y ) );
+
+                if( evt->IsMouseUp( BUT_LEFT ) )
+                {
+                    scale = view->GetScale() / ratio;
+                }
+                else
+                {
+                    scale = view->GetScale() * ratio;
+                }
+
                 view->SetScale( scale );
                 view->SetCenter( selectionBox.Centre() );
 
@@ -136,5 +146,5 @@ bool ZOOM_TOOL::selectRegion()
 
 void ZOOM_TOOL::setTransitions()
 {
-    Go( &ZOOM_TOOL::Main, PCB_ACTIONS::zoomTool.MakeEvent() );
+    Go( &ZOOM_TOOL::Main, ACTIONS::zoomTool.MakeEvent() );
 }
diff --git a/pcbnew/tools/zoom_tool.h b/common/tool/zoom_tool.h
similarity index 91%
rename from pcbnew/tools/zoom_tool.h
rename to common/tool/zoom_tool.h
index 801128ae4..e3ad31a15 100644
--- a/pcbnew/tools/zoom_tool.h
+++ b/common/tool/zoom_tool.h
@@ -1,7 +1,7 @@
 /*
  * This program source code file is part of KiCad, a free EDA CAD application.
  *
- * Copyright (C) 2016 KiCad Developers, see AUTHORS.txt for contributors.
+ * Copyright (C) 2017 KiCad Developers, see AUTHORS.txt for contributors.
  *
  * This program is free software: you can redistribute it and/or modify it
  * under the terms of the GNU General Public License as published by the
@@ -22,7 +22,7 @@
 
 #include <tool/tool_interactive.h>
 
-class PCB_EDIT_FRAME;
+class EDA_DRAW_FRAME;
 
 
 class ZOOM_TOOL : public TOOL_INTERACTIVE
@@ -42,7 +42,7 @@ public:
 
 private:
     bool selectRegion();
-    PCB_EDIT_FRAME* m_frame;
+    EDA_DRAW_FRAME* m_frame;
 };
 
 #endif // _ZOOM_TOOL_H
diff --git a/include/draw_frame.h b/include/draw_frame.h
index 842d92063..aa01690d0 100644
--- a/include/draw_frame.h
+++ b/include/draw_frame.h
@@ -393,6 +393,20 @@ public:
     wxAuiToolBar* GetMainToolBar() const { return m_mainToolBar; }
 
     /**
+     * Checks all the toolbars and returns true if the given tool id is toggled.
+     *
+     * This is needed because GerbView and Pcbnew put some of the same tools in
+     * different toolbars (for example, zoom selection is in the main bar in
+     * Pcbnew and in the options bar in GerbView).
+     */
+    bool GetToolToggled( int aToolId )
+    {
+        return ( m_mainToolBar->GetToolToggled( aToolId ) ||
+                 m_optionsToolBar->GetToolToggled( aToolId ) ||
+                 m_drawToolBar->GetToolToggled( aToolId ) );
+    }
+
+    /**
      * Function SetToolID
      * sets the tool command ID to \a aId and sets the cursor to \a aCursor.  The
      * command ID must be greater or equal ::ID_NO_TOOL_SELECTED.  If the command
diff --git a/include/tool/actions.h b/include/tool/actions.h
index 114c7f0a7..1eb5be8f7 100644
--- a/include/tool/actions.h
+++ b/include/tool/actions.h
@@ -55,6 +55,7 @@ public:
     static TOOL_ACTION zoomCenter;
     static TOOL_ACTION zoomFitScreen;
     static TOOL_ACTION zoomPreset;
+    static TOOL_ACTION zoomTool;
 
     // Grid control
     static TOOL_ACTION gridFast1;
diff --git a/pcbnew/CMakeLists.txt b/pcbnew/CMakeLists.txt
index a57c035f5..8deba7d43 100644
--- a/pcbnew/CMakeLists.txt
+++ b/pcbnew/CMakeLists.txt
@@ -310,7 +310,6 @@ set( PCBNEW_CLASS_SRCS
     tools/grid_helper.cpp
     tools/pad_tool.cpp
     tools/picker_tool.cpp
-    tools/zoom_tool.cpp
     tools/zone_create_helper.cpp
     tools/tools_common.cpp
     tools/tool_event_utils.cpp
diff --git a/pcbnew/moduleframe.cpp b/pcbnew/moduleframe.cpp
index 526d024d8..974dc296d 100644
--- a/pcbnew/moduleframe.cpp
+++ b/pcbnew/moduleframe.cpp
@@ -62,9 +62,9 @@
 #include <tool/tool_manager.h>
 #include <tool/common_tools.h>
 #include <tool/tool_dispatcher.h>
+#include <tool/zoom_tool.h>
 
 #include "tools/selection_tool.h"
-#include "tools/zoom_tool.h"
 #include "tools/edit_tool.h"
 #include "tools/drawing_tool.h"
 #include "tools/point_editor.h"
diff --git a/pcbnew/tools/pcb_actions.cpp b/pcbnew/tools/pcb_actions.cpp
index a3a399351..aba31f22a 100644
--- a/pcbnew/tools/pcb_actions.cpp
+++ b/pcbnew/tools/pcb_actions.cpp
@@ -145,7 +145,7 @@ boost::optional<TOOL_EVENT> PCB_ACTIONS::TranslateLegacyId( int aId )
         return PCB_ACTIONS::selectionTool.MakeEvent();
 
     case ID_ZOOM_SELECTION:
-        return PCB_ACTIONS::zoomTool.MakeEvent();
+        return ACTIONS::zoomTool.MakeEvent();
 
     case ID_PCB_DELETE_ITEM_BUTT:
     case ID_MODEDIT_DELETE_TOOL:
diff --git a/pcbnew/tools/pcb_actions.h b/pcbnew/tools/pcb_actions.h
index a39138268..b82f14293 100644
--- a/pcbnew/tools/pcb_actions.h
+++ b/pcbnew/tools/pcb_actions.h
@@ -204,7 +204,7 @@ public:
 
     /// Activation of the Push and Shove router (inline dragging mode)
     static TOOL_ACTION routerInlineDrag;
-    
+
     // Point Editor
     /// Break outline (insert additional points to an edge)
     static TOOL_ACTION pointEditorAddCorner;
@@ -354,7 +354,6 @@ public:
 
     // Miscellaneous
     static TOOL_ACTION selectionTool;
-    static TOOL_ACTION zoomTool;
     static TOOL_ACTION pickerTool;
     static TOOL_ACTION resetCoords;
     static TOOL_ACTION measureTool;
diff --git a/pcbnew/tools/pcbnew_control.cpp b/pcbnew/tools/pcbnew_control.cpp
index c3cc52c5c..3ef2490bc 100644
--- a/pcbnew/tools/pcbnew_control.cpp
+++ b/pcbnew/tools/pcbnew_control.cpp
@@ -197,10 +197,6 @@ TOOL_ACTION PCB_ACTIONS::selectionTool( "pcbnew.Control.selectionTool",
         AS_GLOBAL, 0,
         "", "", NULL, AF_ACTIVATE );
 
-TOOL_ACTION PCB_ACTIONS::zoomTool( "pcbnew.Control.zoomTool",
-        AS_GLOBAL, TOOL_ACTION::LegacyHotKey( HK_ZOOM_SELECTION ),
-        _( "Zoom to Selection" ), "", NULL, AF_ACTIVATE );
-
 TOOL_ACTION PCB_ACTIONS::resetCoords( "pcbnew.Control.resetCoords",
         AS_GLOBAL, TOOL_ACTION::LegacyHotKey( HK_RESET_LOCAL_COORD ),
         "", "" );
diff --git a/pcbnew/tools/tools_common.cpp b/pcbnew/tools/tools_common.cpp
index 19ae71bf7..499a5f9fe 100644
--- a/pcbnew/tools/tools_common.cpp
+++ b/pcbnew/tools/tools_common.cpp
@@ -27,9 +27,9 @@
 
 #include <tool/tool_manager.h>
 #include <tool/common_tools.h>
+#include <tool/zoom_tool.h>
 
 #include <tools/selection_tool.h>
-#include <tools/zoom_tool.h>
 #include <tools/picker_tool.h>
 #include <tools/edit_tool.h>
 #include <tools/drawing_tool.h>
-- 
2.11.0


Follow ups