From c4888afbccc55482290bdd5881da9a91137682de Mon Sep 17 00:00:00 2001 From: Maciej Suminski Date: Thu, 30 Apr 2015 10:46:02 +0200 Subject: [PATCH] Refactored the way GAL handles zoom settings. --- common/draw_frame.cpp | 60 ++++++++++++--------------------- common/zoom.cpp | 41 ++++++++++++++-------- include/class_base_screen.h | 2 +- include/draw_frame.h | 13 +++++++ pcbnew/tools/common_actions.cpp | 16 +++++---- pcbnew/tools/common_actions.h | 1 + pcbnew/tools/pcbnew_control.cpp | 30 +++++++++++++++++ pcbnew/tools/pcbnew_control.h | 1 + 8 files changed, 104 insertions(+), 60 deletions(-) diff --git a/common/draw_frame.cpp b/common/draw_frame.cpp index a190f57ec6..0047ebd653 100644 --- a/common/draw_frame.cpp +++ b/common/draw_frame.cpp @@ -424,8 +424,7 @@ void EDA_DRAW_FRAME::OnSelectGrid( wxCommandEvent& event ) if( IsGalCanvasActive() ) { - GetGalCanvas()->GetGAL()->SetGridSize( VECTOR2D( screen->GetGrid().m_Size.x, - screen->GetGrid().m_Size.y ) ); + GetGalCanvas()->GetGAL()->SetGridSize( VECTOR2D( screen->GetGrid().m_Size ) ); GetGalCanvas()->GetView()->MarkTargetDirty( KIGFX::TARGET_NONCACHED ); } @@ -456,22 +455,14 @@ void EDA_DRAW_FRAME::OnSelectZoom( wxCommandEvent& event ) return; GetScreen()->SetZoom( selectedZoom ); - - if( IsGalCanvasActive() ) - { - // Apply computed view settings to GAL - KIGFX::VIEW* view = GetGalCanvas()->GetView(); - KIGFX::GAL* gal = GetGalCanvas()->GetGAL(); - - double zoomFactor = gal->GetWorldScale() / gal->GetZoomFactor(); - double zoom = 1.0 / ( zoomFactor * GetZoom() ); - - view->SetScale( zoom ); - GetGalCanvas()->Refresh(); - } - else - RedrawScreen( GetScrollCenterPosition(), false ); + RedrawScreen( GetScrollCenterPosition(), false ); } + + // Notify GAL + TOOL_MANAGER* mgr = GetToolManager(); + + if( mgr && IsGalCanvasActive() ) + mgr->RunAction( "common.Control.zoomPreset", true, id ); } @@ -1026,38 +1017,29 @@ void EDA_DRAW_FRAME::UseGalCanvas( bool aEnable ) KIGFX::GAL* gal = GetGalCanvas()->GetGAL(); double zoomFactor = gal->GetWorldScale() / gal->GetZoomFactor(); + BASE_SCREEN* screen = GetScreen(); // Display the same view after canvas switching - if( aEnable ) + if( aEnable ) // Switch to GAL rendering { - BASE_SCREEN* screen = GetScreen(); - - // Switch to GAL rendering - if( !IsGalCanvasActive() ) - { - // Set up viewport - double zoom = 1.0 / ( zoomFactor * m_canvas->GetZoom() ); - view->SetScale( zoom ); - view->SetCenter( VECTOR2D( m_canvas->GetScreenCenterLogicalPosition() ) ); - } + // Set up viewport + double zoom = 1.0 / ( zoomFactor * m_canvas->GetZoom() ); + view->SetScale( zoom ); + view->SetCenter( VECTOR2D( m_canvas->GetScreenCenterLogicalPosition() ) ); // Set up grid settings gal->SetGridVisibility( IsGridVisible() ); - gal->SetGridSize( VECTOR2D( screen->GetGridSize().x, screen->GetGridSize().y ) ); + gal->SetGridSize( VECTOR2D( screen->GetGridSize() ) ); gal->SetGridOrigin( VECTOR2D( GetGridOrigin() ) ); } - else + else // Switch to standard rendering { - // Switch to standard rendering - if( IsGalCanvasActive() ) - { - // Change view settings only if GAL was active previously - double zoom = 1.0 / ( zoomFactor * view->GetScale() ); - m_canvas->SetZoom( zoom ); + // Change view settings only if GAL was active previously + double zoom = 1.0 / ( zoomFactor * view->GetScale() ); + m_canvas->SetZoom( zoom ); - VECTOR2D center = view->GetCenter(); - RedrawScreen( wxPoint( center.x, center.y ), false ); - } + VECTOR2D center = view->GetCenter(); + AdjustScrollBars( wxPoint( center.x, center.y ) ); } m_canvas->SetEvtHandlerEnabled( !aEnable ); diff --git a/common/zoom.cpp b/common/zoom.cpp index 8f8d34d1b9..ef04c78dfc 100644 --- a/common/zoom.cpp +++ b/common/zoom.cpp @@ -33,8 +33,6 @@ #include #include #include -#include -#include #include #include #include @@ -46,6 +44,9 @@ void EDA_DRAW_FRAME::RedrawScreen( const wxPoint& aCenterPoint, bool aWarpPointer ) { + if( IsGalCanvasActive() ) + return; + AdjustScrollBars( aCenterPoint ); // Move the mouse cursor to the on grid graphic cursor position @@ -58,6 +59,9 @@ void EDA_DRAW_FRAME::RedrawScreen( const wxPoint& aCenterPoint, bool aWarpPointe void EDA_DRAW_FRAME::RedrawScreen2( const wxPoint& posBefore ) { + if( IsGalCanvasActive() ) + return; + wxPoint dPos = posBefore - m_canvas->GetClientSize() / 2; // relative screen position to center before zoom wxPoint newScreenPos = m_canvas->ToDeviceXY( GetCrossHairPosition() ); // screen position of crosshair after zoom wxPoint newCenter = m_canvas->ToLogicalXY( newScreenPos - dPos ); @@ -186,18 +190,7 @@ void EDA_DRAW_FRAME::OnZoom( wxCommandEvent& event ) break; default: - unsigned i; - - i = id - ID_POPUP_ZOOM_LEVEL_START; - - if( i >= screen->m_ZoomList.size() ) - { - wxLogDebug( wxT( "%s %d: index %d is outside the bounds of the zoom list." ), - __TFILE__, __LINE__, i ); - return; - } - if( screen->SetZoom( screen->m_ZoomList[i] ) ) - RedrawScreen( center, true ); + SetPresetZoom( id - ID_POPUP_ZOOM_LEVEL_START ); } UpdateStatusBar(); @@ -216,6 +209,26 @@ void EDA_DRAW_FRAME::SetPrevZoom() } +void EDA_DRAW_FRAME::SetPresetZoom( int aIndex ) +{ + BASE_SCREEN* screen = GetScreen(); + + if( aIndex >= (int) screen->m_ZoomList.size() ) + { + wxLogDebug( wxT( "%s %d: index %d is outside the bounds of the zoom list." ), + __TFILE__, __LINE__, aIndex ); + return; + } + + m_zoomSelectBox->SetSelection( aIndex ); + + if( screen->SetZoom( screen->m_ZoomList[aIndex] ) ) + RedrawScreen( GetScrollCenterPosition(), true ); + + UpdateStatusBar(); +} + + /* add the zoom list menu the the MasterMenu. * used in OnRightClick(wxMouseEvent& event) */ diff --git a/include/class_base_screen.h b/include/class_base_screen.h index 0ea8fa9e7b..64aba0e999 100644 --- a/include/class_base_screen.h +++ b/include/class_base_screen.h @@ -215,7 +215,7 @@ public: int m_ScreenNumber; int m_NumberOfScreens; - std::vector m_ZoomList; ///< standard zoom (i.e. scale) coefficients. + std::vector m_ZoomList; ///< standard zoom (i.e. scale) coefficients. bool m_IsPrinting; public: diff --git a/include/draw_frame.h b/include/draw_frame.h index 38613bb0ad..d37f03106c 100644 --- a/include/draw_frame.h +++ b/include/draw_frame.h @@ -346,6 +346,12 @@ public: */ virtual const wxString GetZoomLevelIndicator() const; + /** + * Function GetZoomLevelCoeff + * returns the coefficient to convert internal display scale factor to zoom level. + */ + inline double GetZoomLevelCoeff() const { return m_zoomLevelCoeff; } + void EraseMsgBox(); void Process_PageSettings( wxCommandEvent& event ); @@ -505,6 +511,13 @@ public: */ void SetPrevZoom(); + /** + * Function SetPresetZoom() + * changes zoom to one of the preset values. + * @param aIndex is the zoom index from the list. + */ + void SetPresetZoom( int aIndex ); + /** * Function RedrawScreen * redraws the entire screen area by updating the scroll bars and mouse pointer in diff --git a/pcbnew/tools/common_actions.cpp b/pcbnew/tools/common_actions.cpp index b6d2af06d1..9921844e22 100644 --- a/pcbnew/tools/common_actions.cpp +++ b/pcbnew/tools/common_actions.cpp @@ -181,30 +181,34 @@ TOOL_ACTION COMMON_ACTIONS::arcPosture( "pcbnew.InteractiveDrawing.arcPosture", // View Controls -TOOL_ACTION COMMON_ACTIONS::zoomIn( "pcbnew.Control.zoomIn", +TOOL_ACTION COMMON_ACTIONS::zoomIn( "common.Control.zoomIn", AS_GLOBAL, WXK_F1, "", "" ); -TOOL_ACTION COMMON_ACTIONS::zoomOut( "pcbnew.Control.zoomOut", +TOOL_ACTION COMMON_ACTIONS::zoomOut( "common.Control.zoomOut", AS_GLOBAL, WXK_F2, "", "" ); -TOOL_ACTION COMMON_ACTIONS::zoomInCenter( "pcbnew.Control.zoomInCenter", +TOOL_ACTION COMMON_ACTIONS::zoomInCenter( "common.Control.zoomInCenter", AS_GLOBAL, 0, "", "" ); -TOOL_ACTION COMMON_ACTIONS::zoomOutCenter( "pcbnew.Control.zoomOutCenter", +TOOL_ACTION COMMON_ACTIONS::zoomOutCenter( "common.Control.zoomOutCenter", AS_GLOBAL, 0, "", "" ); -TOOL_ACTION COMMON_ACTIONS::zoomCenter( "pcbnew.Control.zoomCenter", +TOOL_ACTION COMMON_ACTIONS::zoomCenter( "common.Control.zoomCenter", AS_GLOBAL, WXK_F4, "", "" ); -TOOL_ACTION COMMON_ACTIONS::zoomFitScreen( "pcbnew.Control.zoomFitScreen", +TOOL_ACTION COMMON_ACTIONS::zoomFitScreen( "common.Control.zoomFitScreen", AS_GLOBAL, WXK_HOME, "", "" ); +TOOL_ACTION COMMON_ACTIONS::zoomPreset( "common.Control.zoomPreset", + AS_GLOBAL, 0, + "", "" ); + // Display modes TOOL_ACTION COMMON_ACTIONS::trackDisplayMode( "pcbnew.Control.trackDisplayMode", diff --git a/pcbnew/tools/common_actions.h b/pcbnew/tools/common_actions.h index 2e51f81d30..65eedc7a68 100644 --- a/pcbnew/tools/common_actions.h +++ b/pcbnew/tools/common_actions.h @@ -188,6 +188,7 @@ public: static TOOL_ACTION zoomOutCenter; static TOOL_ACTION zoomCenter; static TOOL_ACTION zoomFitScreen; + static TOOL_ACTION zoomPreset; // Display modes static TOOL_ACTION trackDisplayMode; diff --git a/pcbnew/tools/pcbnew_control.cpp b/pcbnew/tools/pcbnew_control.cpp index bc4b469ef3..ed58de2cac 100644 --- a/pcbnew/tools/pcbnew_control.cpp +++ b/pcbnew/tools/pcbnew_control.cpp @@ -140,6 +140,35 @@ int PCBNEW_CONTROL::ZoomFitScreen( const TOOL_EVENT& aEvent ) } +int PCBNEW_CONTROL::ZoomPreset( const TOOL_EVENT& aEvent ) +{ + unsigned int idx = aEvent.Parameter(); + std::vector& zoomList = m_frame->GetScreen()->m_ZoomList; + KIGFX::VIEW* view = m_frame->GetGalCanvas()->GetView(); + KIGFX::GAL* gal = m_frame->GetGalCanvas()->GetGAL(); + + m_frame->SetPresetZoom( idx ); + + if( idx == 0 ) + { + return ZoomFitScreen( aEvent ); + } + else if( idx < 0 || idx >= zoomList.size() ) + { + setTransitions(); + return 0; + } + + double selectedZoom = zoomList[idx]; + double zoomFactor = gal->GetWorldScale() / gal->GetZoomFactor(); + view->SetScale( 1.0 / ( zoomFactor * selectedZoom ) ); + + setTransitions(); + + return 0; +} + + int PCBNEW_CONTROL::TrackDisplayMode( const TOOL_EVENT& aEvent ) { KIGFX::PCB_PAINTER* painter = @@ -539,6 +568,7 @@ void PCBNEW_CONTROL::setTransitions() Go( &PCBNEW_CONTROL::ZoomInOutCenter, COMMON_ACTIONS::zoomOutCenter.MakeEvent() ); Go( &PCBNEW_CONTROL::ZoomCenter, COMMON_ACTIONS::zoomCenter.MakeEvent() ); Go( &PCBNEW_CONTROL::ZoomFitScreen, COMMON_ACTIONS::zoomFitScreen.MakeEvent() ); + Go( &PCBNEW_CONTROL::ZoomPreset, COMMON_ACTIONS::zoomPreset.MakeEvent() ); // Display modes Go( &PCBNEW_CONTROL::TrackDisplayMode, COMMON_ACTIONS::trackDisplayMode.MakeEvent() ); diff --git a/pcbnew/tools/pcbnew_control.h b/pcbnew/tools/pcbnew_control.h index 7bfec224a2..bbdddbb136 100644 --- a/pcbnew/tools/pcbnew_control.h +++ b/pcbnew/tools/pcbnew_control.h @@ -51,6 +51,7 @@ public: int ZoomInOutCenter( const TOOL_EVENT& aEvent ); int ZoomCenter( const TOOL_EVENT& aEvent ); int ZoomFitScreen( const TOOL_EVENT& aEvent ); + int ZoomPreset( const TOOL_EVENT& aEvent ); // Display modes int TrackDisplayMode( const TOOL_EVENT& aEvent );