7
mirror of https://gitlab.com/kicad/code/kicad.git synced 2025-04-20 00:21:31 +00:00

ADDED: Menu option to enable/disable snap to grid

You can now enable and disable snap to grid when drawing/editing across
all apps.  You can also tie snap to grid to the visibility of the grid
to allow rapid enable/disable via grid display.
This commit is contained in:
Seth Hillbrand 2020-09-10 20:07:56 -07:00
parent d6322dcf0d
commit f493e270ea
37 changed files with 84 additions and 71 deletions

View File

@ -1,7 +1,7 @@
/*
* This program source code file is part of KICAD, a free EDA CAD application.
*
* Copyright (C) 2016-2017 Kicad Developers, see change_log.txt for contributors.
* Copyright (C) 2016-2020 Kicad Developers, see change_log.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
@ -39,6 +39,13 @@ static const UTIL::CFG_MAP<KIGFX::GRID_STYLE> gridStyleConfigVals =
{ KIGFX::GRID_STYLE::SMALL_CROSS,2 },
};
static const UTIL::CFG_MAP<KIGFX::GRID_SNAPPING> gridSnapConfigVals =
{
{ KIGFX::GRID_SNAPPING::ALWAYS, 0 },
{ KIGFX::GRID_SNAPPING::WITH_GRID, 1 },
{ KIGFX::GRID_SNAPPING::NEVER, 2 }
};
/**
* Flag to enable GAL_DISPLAY_OPTIONS logging
@ -55,6 +62,7 @@ GAL_DISPLAY_OPTIONS::GAL_DISPLAY_OPTIONS()
cairo_antialiasing_mode( CAIRO_ANTIALIASING_MODE::NONE ),
m_dpi( nullptr, nullptr ),
m_gridStyle( GRID_STYLE::DOTS ),
m_gridSnapping( GRID_SNAPPING::ALWAYS ),
m_gridLineWidth( 1.0 ),
m_gridMinSpacing( 10.0 ),
m_axesEnabled( false ),
@ -69,6 +77,7 @@ void GAL_DISPLAY_OPTIONS::ReadWindowSettings( WINDOW_SETTINGS& aCfg )
wxLogTrace( traceGalDispOpts, "Reading app-specific options" );
m_gridStyle = UTIL::GetValFromConfig( gridStyleConfigVals, aCfg.grid.style );
m_gridSnapping = UTIL::GetValFromConfig( gridSnapConfigVals, aCfg.grid.snap );
m_gridLineWidth = aCfg.grid.line_width;
m_gridMinSpacing = aCfg.grid.min_spacing;
m_axesEnabled = aCfg.grid.axes_enabled;
@ -113,6 +122,7 @@ void GAL_DISPLAY_OPTIONS::WriteConfig( WINDOW_SETTINGS& aCfg )
wxLogTrace( traceGalDispOpts, "Writing window settings" );
aCfg.grid.style = UTIL::GetConfigForVal( gridStyleConfigVals, m_gridStyle );
aCfg.grid.snap = UTIL::GetConfigForVal( gridSnapConfigVals, m_gridSnapping );
aCfg.grid.line_width = m_gridLineWidth;
aCfg.grid.min_spacing = m_gridMinSpacing;
aCfg.grid.axes_enabled = m_axesEnabled;
@ -123,8 +133,11 @@ void GAL_DISPLAY_OPTIONS::WriteConfig( WINDOW_SETTINGS& aCfg )
void GAL_DISPLAY_OPTIONS::UpdateScaleFactor()
{
m_scaleFactor = m_dpi.GetScaleFactor();
NotifyChanged();
if( m_scaleFactor != m_dpi.GetScaleFactor() )
{
m_scaleFactor = m_dpi.GetScaleFactor();
NotifyChanged();
}
}

View File

@ -312,6 +312,9 @@ void APP_SETTINGS_BASE::addParamsForWindow( WINDOW_SETTINGS* aWindow, const std:
m_params.emplace_back( new PARAM<int>( aJsonPath + ".grid.style",
&aWindow->grid.style, 0 ) );
m_params.emplace_back( new PARAM<int>( aJsonPath + ".grid.snap",
&aWindow->grid.snap, 0 ) );
m_params.emplace_back( new PARAM<bool>( aJsonPath + ".cursor.always_show_cursor",
&aWindow->cursor.always_show_cursor, true ) );

View File

@ -87,7 +87,6 @@ void VIEW_CONTROLS::ApplySettings( const VC_SETTINGS& aSettings )
{
ShowCursor( aSettings.m_showCursor );
CaptureCursor( aSettings.m_cursorCaptured );
SetGridSnapping( aSettings.m_snappingEnabled );
SetGrabMouse( aSettings.m_grabMouse );
SetAutoPan( aSettings.m_autoPanEnabled );
SetAutoPanMargin( aSettings.m_autoPanMargin );

View File

@ -498,9 +498,11 @@ VECTOR2D WX_VIEW_CONTROLS::GetMousePosition( bool aWorldCoordinates ) const
VECTOR2D WX_VIEW_CONTROLS::GetRawCursorPosition( bool aEnableSnapping ) const
{
if( aEnableSnapping )
GAL* gal = m_view->GetGAL();
if( aEnableSnapping && gal->GetGridSnapping() )
{
return m_view->GetGAL()->GetGridPoint( m_cursorPos );
return gal->GetGridPoint( m_cursorPos );
}
else
{

View File

@ -24,6 +24,7 @@
#include <wx/sizer.h>
#include <wx/checkbox.h>
#include <wx/choice.h>
#include <wx/radiobox.h>
#include <wx/spinctrl.h>
#include <wx/stattext.h>
@ -45,7 +46,7 @@ static const double gridMinSpacingMin = 5;
static const double gridMinSpacingMax = 200;
static const double gridMinSpacingStep = 5;
///TODO: These are duplicated in gal_display_options - Unify!
static const UTIL::CFG_MAP<KIGFX::GRID_STYLE> gridStyleSelectMap =
{
{ KIGFX::GRID_STYLE::DOTS, 0 }, // Default
@ -53,6 +54,12 @@ static const UTIL::CFG_MAP<KIGFX::GRID_STYLE> gridStyleSelectMap =
{ KIGFX::GRID_STYLE::SMALL_CROSS, 2 },
};
static const UTIL::CFG_MAP<KIGFX::GRID_SNAPPING> gridSnapConfigVals =
{
{ KIGFX::GRID_SNAPPING::ALWAYS, 0 },
{ KIGFX::GRID_SNAPPING::WITH_GRID, 1 },
{ KIGFX::GRID_SNAPPING::NEVER, 2 }
};
GAL_OPTIONS_PANEL::GAL_OPTIONS_PANEL( wxWindow* aParent, KIGFX::GAL_DISPLAY_OPTIONS& aGalOpts ):
wxPanel( aParent, wxID_ANY ),
@ -124,6 +131,25 @@ GAL_OPTIONS_PANEL::GAL_OPTIONS_PANEL( wxWindow* aParent, KIGFX::GAL_DISPLAY_OPTI
l_gridMinSpacingUnits->Wrap( -1 );
sGridSettingsGrid->Add( l_gridMinSpacingUnits, 0, wxALIGN_CENTER_VERTICAL | wxALL, 5 );
l_gridSnapOptions = new wxStaticText( sGridSettings->GetStaticBox(), wxID_ANY, _( "Snap to Grid:" ) );
l_gridSnapOptions->Wrap( -1 );
sGridSettingsGrid->Add( l_gridSnapOptions, 0, wxALIGN_CENTER_VERTICAL | wxALL, 5 );
wxString gridSnapChoices[] = { _( "Always"), _("When grid shown"), _("Never") };
int gridSnapNChoices = sizeof( gridSnapChoices ) / sizeof( wxString );
m_gridSnapOptions = new wxChoice( sGridSettings->GetStaticBox(), wxID_ANY,
wxDefaultPosition, wxDefaultSize, gridSnapNChoices, gridSnapChoices );
m_gridSnapOptions->Select( 0 );
sGridSettingsGrid->Add( m_gridSnapOptions, 0, wxALIGN_CENTER_VERTICAL | wxEXPAND | wxALL, 5 );
l_gridSnapSpace = new wxStaticText( sGridSettings->GetStaticBox(),
wxID_ANY, _( "px" ) );
l_gridSnapSpace->Wrap( -1 );
l_gridSnapSpace->Hide();
sGridSettingsGrid->Add( l_gridSnapSpace, 0,
wxALIGN_CENTER_VERTICAL | wxALL | wxRESERVE_SPACE_EVEN_IF_HIDDEN, 5 );
sGridSettings->Add( sGridSettingsGrid, 1, wxALL | wxEXPAND, 5 );
sLeftSizer->Add( sGridSettings, 0, wxTOP | wxBOTTOM | wxRIGHT | wxEXPAND, 5 );
@ -161,6 +187,9 @@ GAL_OPTIONS_PANEL::GAL_OPTIONS_PANEL( wxWindow* aParent, KIGFX::GAL_DISPLAY_OPTI
bool GAL_OPTIONS_PANEL::TransferDataToWindow()
{
m_gridSnapOptions->SetSelection(
UTIL::GetConfigForVal( gridSnapConfigVals, m_galOptions.m_gridSnapping ) );
m_gridStyle->SetSelection( UTIL::GetConfigForVal(
gridStyleSelectMap, m_galOptions.m_gridStyle ) );
@ -178,6 +207,9 @@ bool GAL_OPTIONS_PANEL::TransferDataToWindow()
bool GAL_OPTIONS_PANEL::TransferDataFromWindow()
{
m_galOptions.m_gridSnapping = UTIL::GetValFromConfig( gridSnapConfigVals,
m_gridSnapOptions->GetSelection() );
m_galOptions.m_gridStyle = UTIL::GetValFromConfig(
gridStyleSelectMap, m_gridStyle->GetSelection() );

View File

@ -194,7 +194,6 @@ LIB_VIEW_FRAME::LIB_VIEW_FRAME( KIWAY* aKiway, wxWindow* aParent, FRAME_T aFrame
}
SyncView();
GetCanvas()->GetViewControls()->SetGridSnapping( IsGridVisible() );
GetCanvas()->SetCanFocus( false );
// Set the working/draw area size to display a symbol to a reasonable value:

View File

@ -175,7 +175,6 @@ LIB_EDIT_FRAME::LIB_EDIT_FRAME( KIWAY* aKiway, wxWindow* aParent ) :
Show( true );
SyncView();
GetCanvas()->GetViewControls()->SetGridSnapping( IsGridVisible() );
GetCanvas()->GetView()->UseDrawPriority( true );
GetCanvas()->GetGAL()->SetAxesEnabled( true );

View File

@ -91,8 +91,6 @@ SCH_DRAW_PANEL::SCH_DRAW_PANEL( wxWindow* aParentWindow, wxWindowID aWindowId,
// on updated viewport data.
m_viewControls = new KIGFX::WX_VIEW_CONTROLS( m_view, this );
m_viewControls->SetGridSnapping( m_gal->GetGridVisibility() );
SetEvtHandlerEnabled( true );
SetFocus();
Show( true );

View File

@ -70,8 +70,6 @@ SCH_PREVIEW_PANEL::SCH_PREVIEW_PANEL( wxWindow* aParentWindow, wxWindowID aWindo
m_gal->SetCursorEnabled( false );
m_gal->SetGridSize( VECTOR2D( Mils2iu( 100.0 ), Mils2iu( 100.0 ) ) );
m_viewControls->SetGridSnapping( m_gal->GetGridVisibility() );
SetEvtHandlerEnabled( true );
SetFocus();
Show( true );

View File

@ -108,7 +108,7 @@ void EE_GRID_HELPER::SetAuxAxes( bool aEnable, const VECTOR2I& aOrigin )
VECTOR2I EE_GRID_HELPER::Align( const VECTOR2I& aPoint ) const
{
if( !m_toolMgr->GetView()->GetGAL()->GetGridVisibility() )
if( !m_toolMgr->GetView()->GetGAL()->GetGridSnapping() )
return aPoint;
const VECTOR2D gridOffset( GetOrigin() );

View File

@ -397,7 +397,6 @@ int LIB_DRAWING_TOOLS::DrawShape( const TOOL_EVENT& aEvent )
int LIB_DRAWING_TOOLS::PlaceAnchor( const TOOL_EVENT& aEvent )
{
getViewControls()->ShowCursor( true );
getViewControls()->SetGridSnapping( m_frame->IsGridVisible() );
std::string tool = aEvent.GetCommandStr().get();
m_frame->PushTool( tool );

View File

@ -70,7 +70,6 @@ void LIB_MOVE_TOOL::Reset( RESET_REASON aReason )
int LIB_MOVE_TOOL::Main( const TOOL_EVENT& aEvent )
{
KIGFX::VIEW_CONTROLS* controls = getViewControls();
controls->SetGridSnapping( m_frame->IsGridVisible() );
m_anchorPos = { 0, 0 };
@ -103,7 +102,6 @@ int LIB_MOVE_TOOL::Main( const TOOL_EVENT& aEvent )
do
{
m_frame->GetCanvas()->SetCurrentCursor( wxCURSOR_ARROW );
controls->SetGridSnapping( m_frame->IsGridVisible() );
if( evt->IsAction( &EE_ACTIONS::move ) || evt->IsMotion() || evt->IsDrag( BUT_LEFT )
|| evt->IsAction( &ACTIONS::refreshPreview )

View File

@ -449,7 +449,6 @@ int SCH_DRAWING_TOOLS::SingleClickPlace( const TOOL_EVENT& aEvent )
m_toolMgr->RunAction( EE_ACTIONS::clearSelection, true );
getViewControls()->ShowCursor( true );
getViewControls()->SetGridSnapping( m_frame->IsGridVisible() );
SCH_ITEM* previewItem;
switch( type )

View File

@ -469,7 +469,6 @@ int SCH_LINE_WIRE_BUS_TOOL::doDrawSegments( const std::string& aTool, int aType
m_toolMgr->RunAction( EE_ACTIONS::clearSelection, true );
controls->ShowCursor( true );
controls->SetGridSnapping( m_frame->IsGridVisible() );
Activate();
@ -489,7 +488,6 @@ int SCH_LINE_WIRE_BUS_TOOL::doDrawSegments( const std::string& aTool, int aType
m_frame->GetCanvas()->SetCurrentCursor( wxCURSOR_PENCIL );
grid.SetSnap( !evt->Modifier( MD_SHIFT ) );
controls->SetGridSnapping( m_frame->IsGridVisible() );
wxPoint cursorPos = wxPoint( grid.BestSnapAnchor(
evt->IsPrime() ? evt->Position() : controls->GetMousePosition(), nullptr ) );
controls->ForceCursorPosition( true, cursorPos );

View File

@ -110,7 +110,6 @@ int SCH_MOVE_TOOL::Main( const TOOL_EVENT& aEvent )
EESCHEMA_SETTINGS* cfg = Pgm().GetSettingsManager().GetAppSettings<EESCHEMA_SETTINGS>();
KIGFX::VIEW_CONTROLS* controls = getViewControls();
controls->SetGridSnapping( m_frame->IsGridVisible() );
EE_GRID_HELPER grid( m_toolMgr );
m_anchorPos.reset();
@ -173,7 +172,6 @@ int SCH_MOVE_TOOL::Main( const TOOL_EVENT& aEvent )
do
{
m_frame->GetCanvas()->SetCurrentCursor( wxCURSOR_ARROW );
controls->SetGridSnapping( m_frame->IsGridVisible() );
if( evt->IsAction( &EE_ACTIONS::moveActivate ) || evt->IsAction( &EE_ACTIONS::restartMove )
|| evt->IsAction( &EE_ACTIONS::move ) || evt->IsAction( &EE_ACTIONS::drag )

View File

@ -49,7 +49,6 @@ EDA_DRAW_PANEL_GAL( aParentWindow, aWindowId, aPosition, aSize, aOptions, aGalTy
m_view->SetPainter( m_painter.get() );
m_viewControls = new KIGFX::WX_VIEW_CONTROLS( m_view, this );
m_viewControls->SetGridSnapping( m_gal->GetGridVisibility() );
setDefaultLayerDeps();

View File

@ -571,7 +571,6 @@ int GERBVIEW_SELECTION_TOOL::MeasureTool( const TOOL_EVENT& aEvent )
bool originSet = false;
controls.ShowCursor( true );
controls.SetGridSnapping( m_frame->IsGridVisible() );
while( TOOL_EVENT* evt = Wait() )
{

View File

@ -62,6 +62,13 @@ namespace KIGFX
BEST,
};
enum class GRID_SNAPPING
{
ALWAYS,
WITH_GRID,
NEVER
};
class GAL_DISPLAY_OPTIONS;
class GAL_DISPLAY_OPTIONS_OBSERVER
@ -116,6 +123,9 @@ namespace KIGFX
///> The grid style to draw the grid in
KIGFX::GRID_STYLE m_gridStyle;
///> Snapping options for the grid
GRID_SNAPPING m_gridSnapping;
///> Thickness to render grid lines/dots
double m_gridLineWidth;

View File

@ -861,6 +861,11 @@ public:
bool GetGridVisibility() const { return gridVisibility; }
bool GetGridSnapping() const
{
return ( options.m_gridSnapping == KIGFX::GRID_SNAPPING::ALWAYS ||
( gridVisibility && options.m_gridSnapping == KIGFX::GRID_SNAPPING::WITH_GRID ) );
}
/**
* @brief Set the origin point for the grid.
*

View File

@ -59,6 +59,7 @@ struct GRID_SETTINGS
double min_spacing;
bool show;
int style;
int snap;
};
/**

View File

@ -153,25 +153,6 @@ public:
{
}
/**
* Function SetGridSnapping()
* Enables/disables snapping cursor to grid.
*
* @param aEnabled says whether the opion should be enabled or disabled.
*/
virtual void SetGridSnapping( bool aEnabled )
{
m_settings.m_snappingEnabled = aEnabled;
}
/**
* @return the current state of the snapping cursor to grid.
*/
virtual bool GetGridSnapping()
{
return m_settings.m_snappingEnabled;
}
/**
* Function SetGrabMouse
* Turns on/off mouse grabbing. When the mouse is grabbed, it cannot go outside the VIEW.

View File

@ -63,6 +63,10 @@ private:
wxSpinCtrlDouble* m_gridMinSpacing;
wxStaticText* l_gridMinSpacingUnits;
wxStaticText* l_gridSnapOptions;
wxChoice* m_gridSnapOptions;
wxStaticText* l_gridSnapSpace;
wxRadioBox* m_cursorShape;
wxCheckBox* m_forceCursorDisplay;

View File

@ -65,7 +65,6 @@ PL_DRAW_PANEL_GAL::PL_DRAW_PANEL_GAL( wxWindow* aParentWindow, wxWindowID aWindo
m_view->SetLayerVisible( LAYER_WORKSHEET_PAGEn, false );
m_viewControls = new KIGFX::WX_VIEW_CONTROLS( m_view, this );
m_viewControls->SetGridSnapping( m_gal->GetGridVisibility() );
}

View File

@ -93,7 +93,6 @@ int PL_EDIT_TOOL::Main( const TOOL_EVENT& aEvent )
{
KIGFX::VIEW_CONTROLS* controls = getViewControls();
controls->SetGridSnapping( m_frame->IsGridVisible() );
VECTOR2I originalCursorPos = controls->GetCursorPosition();
// Be sure that there is at least one item that we can move. If there's no selection try
@ -123,7 +122,6 @@ int PL_EDIT_TOOL::Main( const TOOL_EVENT& aEvent )
do
{
m_frame->GetCanvas()->SetCurrentCursor( wxCURSOR_ARROW );
controls->SetGridSnapping( m_frame->IsGridVisible() );
if( evt->IsAction( &PL_ACTIONS::move ) || evt->IsMotion() || evt->IsDrag( BUT_LEFT )
|| evt->IsAction( &ACTIONS::refreshPreview ) )

View File

@ -130,7 +130,6 @@ int MICROWAVE_TOOL::drawMicrowaveInductor( const TOOL_EVENT& aEvent )
bool originSet = false;
controls.ShowCursor( true );
controls.SetGridSnapping( true );
controls.CaptureCursor( false );
controls.SetAutoPan( false );

Some files were not shown because too many files have changed in this diff Show More