From 64f555079a0ce5e411db8b80889dfab75d763161 Mon Sep 17 00:00:00 2001 From: jean-pierre charras Date: Wed, 2 Dec 2020 18:33:59 +0100 Subject: [PATCH] Code cleanup related to zoom: - remove outdated code or comment coming from old drawing code using wxDC - move zoom values lists to zoom_defines.h - fix incorrect zoom max and min values (gal scaling factor limits). --- common/eda_draw_frame.cpp | 6 +- common/tool/common_tools.cpp | 5 +- common/tool/zoom_menu.cpp | 2 +- eeschema/eeschema_config.cpp | 5 -- eeschema/sch_draw_panel.cpp | 4 +- eeschema/sch_preview_panel.cpp | 4 +- gerbview/gerbview_draw_panel_gal.cpp | 5 ++ gerbview/gerbview_frame.cpp | 5 +- include/convert_to_biu.h | 36 +++++++++++ include/zoom_defines.h | 80 +++++++++---------------- pagelayout_editor/pl_draw_panel_gal.cpp | 4 +- pagelayout_editor/pl_editor_frame.cpp | 3 - pcbnew/footprint_preview_panel.cpp | 3 - pcbnew/pcb_base_frame.cpp | 3 - pcbnew/pcb_draw_panel_gal.cpp | 4 ++ 15 files changed, 90 insertions(+), 79 deletions(-) diff --git a/common/eda_draw_frame.cpp b/common/eda_draw_frame.cpp index ab12308bcb..db95971e95 100644 --- a/common/eda_draw_frame.cpp +++ b/common/eda_draw_frame.cpp @@ -357,7 +357,7 @@ void EDA_DRAW_FRAME::UpdateZoomSelectBox() if( m_zoomSelectBox == NULL ) return; - double zoom = m_canvas->GetGAL()->GetZoomFactor() / ZOOM_COEFF; + double zoom = m_canvas->GetGAL()->GetZoomFactor(); m_zoomSelectBox->Clear(); m_zoomSelectBox->Append( _( "Zoom Auto" ) ); @@ -383,7 +383,7 @@ void EDA_DRAW_FRAME::OnUpdateSelectZoom( wxUpdateUIEvent& aEvent ) int current = 0; // display Auto if no match found // check for a match within 1% - double zoom = GetCanvas()->GetGAL()->GetZoomFactor() / ZOOM_COEFF; + double zoom = GetCanvas()->GetGAL()->GetZoomFactor(); for( unsigned i = 0; i < config()->m_Window.zoom_factors.size(); i++ ) { @@ -520,7 +520,7 @@ const wxString EDA_DRAW_FRAME::GetZoomLevelIndicator() const { // returns a human readable value which can be displayed as zoom // level indicator in dialogs. - double zoom = m_canvas->GetGAL()->GetZoomFactor() / ZOOM_COEFF; + double zoom = m_canvas->GetGAL()->GetZoomFactor(); return wxString::Format( wxT( "Z %.2f" ), zoom ); } diff --git a/common/tool/common_tools.cpp b/common/tool/common_tools.cpp index 318cea4f87..3ca8f2f95a 100644 --- a/common/tool/common_tools.cpp +++ b/common/tool/common_tools.cpp @@ -219,7 +219,7 @@ int COMMON_TOOLS::ZoomInOutCenter( const TOOL_EVENT& aEvent ) int COMMON_TOOLS::doZoomInOut( bool aDirection, bool aCenterOnCursor ) { - double zoom = getView()->GetGAL()->GetZoomFactor() / ZOOM_COEFF; + double zoom = getView()->GetGAL()->GetZoomFactor(); // Step must be AT LEAST 1.3 if( aDirection ) @@ -254,6 +254,7 @@ int COMMON_TOOLS::doZoomInOut( bool aDirection, bool aCenterOnCursor ) idx = 0; // if we ran off the end then peg to the end } + // Note: idx == 0 is Auto; idx == 1 is first entry in zoomList return doZoomToPreset( idx + 1, aCenterOnCursor ); } @@ -401,7 +402,7 @@ int COMMON_TOOLS::doZoomToPreset( int idx, bool aCenterOnCursor ) idx--; } - double scale = zoomList[idx] * ZOOM_COEFF; + double scale = zoomList[idx]; if( aCenterOnCursor ) { diff --git a/common/tool/zoom_menu.cpp b/common/tool/zoom_menu.cpp index bfe546c7e1..1064b4d9cf 100644 --- a/common/tool/zoom_menu.cpp +++ b/common/tool/zoom_menu.cpp @@ -60,7 +60,7 @@ OPT_TOOL_EVENT ZOOM_MENU::eventHandler( const wxMenuEvent& aEvent ) void ZOOM_MENU::update() { - double zoom = m_parent->GetCanvas()->GetGAL()->GetZoomFactor() / ZOOM_COEFF; + double zoom = m_parent->GetCanvas()->GetGAL()->GetZoomFactor(); const std::vector& zoomList = m_parent->config()->m_Window.zoom_factors; diff --git a/eeschema/eeschema_config.cpp b/eeschema/eeschema_config.cpp index 48d19a25e8..28a1140e93 100644 --- a/eeschema/eeschema_config.cpp +++ b/eeschema/eeschema_config.cpp @@ -200,11 +200,6 @@ void SCH_BASE_FRAME::LoadSettings( APP_SETTINGS_BASE* aCfg ) { aCfg->m_Window.zoom_factors = { ZOOM_LIST_EESCHEMA }; } - - // ensure factors < MAX_ZOOM_FACTOR (useful only when the user will be - // able to change/edit the factor list - for( double& factor : aCfg->m_Window.zoom_factors ) - factor = std::min( factor, MAX_ZOOM_FACTOR ); } diff --git a/eeschema/sch_draw_panel.cpp b/eeschema/sch_draw_panel.cpp index 70c9f3fb1a..8baefa6e10 100644 --- a/eeschema/sch_draw_panel.cpp +++ b/eeschema/sch_draw_panel.cpp @@ -48,6 +48,7 @@ #include #include +#include SCH_DRAW_PANEL::SCH_DRAW_PANEL( wxWindow* aParentWindow, wxWindowID aWindowId, @@ -74,7 +75,8 @@ SCH_DRAW_PANEL::SCH_DRAW_PANEL( wxWindow* aParentWindow, wxWindowID aWindowId, m_painter->GetSettings()->LoadColors( cs ); m_view->SetPainter( m_painter.get() ); - m_view->SetScaleLimits( 1000.0, 0.0001 ); // This fixes the zoom in and zoom out limits + // This fixes the zoom in and zoom out limits: + m_view->SetScaleLimits( ZOOM_MAX_LIMIT_EESCHEMA, ZOOM_MIN_LIMIT_EESCHEMA ); m_view->SetMirror( false, false ); // Early initialization of the canvas background color, diff --git a/eeschema/sch_preview_panel.cpp b/eeschema/sch_preview_panel.cpp index 10e2f93407..9b34e89000 100644 --- a/eeschema/sch_preview_panel.cpp +++ b/eeschema/sch_preview_panel.cpp @@ -33,6 +33,7 @@ #include #include #include +#include #include @@ -55,7 +56,8 @@ SCH_PREVIEW_PANEL::SCH_PREVIEW_PANEL( wxWindow* aParentWindow, wxWindowID aWindo m_painter->GetSettings()->LoadColors( Pgm().GetSettingsManager().GetColorSettings() ); m_view->SetPainter( m_painter.get() ); - m_view->SetScaleLimits( 20000.0, 0.002 ); + // This fixes the zoom in and zoom out limits: + m_view->SetScaleLimits( ZOOM_MAX_LIMIT_EESCHEMA_PREVIEW, ZOOM_MIN_LIMIT_EESCHEMA_PREVIEW ); m_view->SetMirror( false, false ); setDefaultLayerOrder(); diff --git a/gerbview/gerbview_draw_panel_gal.cpp b/gerbview/gerbview_draw_panel_gal.cpp index 86aceb7810..a855fdfd2c 100644 --- a/gerbview/gerbview_draw_panel_gal.cpp +++ b/gerbview/gerbview_draw_panel_gal.cpp @@ -22,6 +22,7 @@ #include #include #include +#include #include #include @@ -29,9 +30,11 @@ #include #include +#include #include #include + using namespace std::placeholders; @@ -46,6 +49,8 @@ EDA_DRAW_PANEL_GAL( aParentWindow, aWindowId, aPosition, aSize, aOptions, aGalTy m_painter = std::make_unique( m_gal ); m_view->SetPainter( m_painter.get() ); + // This fixes the zoom in and zoom out limits: + m_view->SetScaleLimits( ZOOM_MAX_LIMIT_GERBVIEW, ZOOM_MIN_LIMIT_GERBVIEW ); m_viewControls = new KIGFX::WX_VIEW_CONTROLS( m_view, this ); diff --git a/gerbview/gerbview_frame.cpp b/gerbview/gerbview_frame.cpp index 71812809e7..1877df62e2 100644 --- a/gerbview/gerbview_frame.cpp +++ b/gerbview/gerbview_frame.cpp @@ -301,12 +301,9 @@ void GERBVIEW_FRAME::LoadSettings( APP_SETTINGS_BASE* aCfg ) if( aCfg->m_Window.zoom_factors.empty() ) { - aCfg->m_Window.zoom_factors = { ZOOM_LIST_GERBER }; + aCfg->m_Window.zoom_factors = { ZOOM_LIST_GERBVIEW }; } - for( double& factor : aCfg->m_Window.zoom_factors ) - factor = std::min( factor, MAX_ZOOM_FACTOR ); - GERBVIEW_SETTINGS* cfg = dynamic_cast( aCfg ); wxCHECK( cfg, /*void*/ ); diff --git a/include/convert_to_biu.h b/include/convert_to_biu.h index 56701390ca..906b69f121 100644 --- a/include/convert_to_biu.h +++ b/include/convert_to_biu.h @@ -24,6 +24,42 @@ #pragma once + +/* Note about internal units and max size for boards and items + + The largest distance that we (and Kicad) can support is INT_MAX, since it represents + distance often in a wxCoord or wxSize. As a scalar, a distance is always + positive. Because int is 32 bits and INT_MAX is + 2147483647. The most difficult distance for a virtual (world) cartesian + space is the hypotenuse, or diagonal measurement at a 45 degree angle. This + puts the most stress on the distance magnitude within the bounded virtual + space. So if we allow this distance to be our constraint of <= INT_MAX, this + constraint then propagates to the maximum distance in X and in Y that can be + supported on each axis. Remember that the hypotenuse of a 1x1 square is + sqrt( 1x1 + 1x1 ) = sqrt(2) = 1.41421356. + + hypotenuse of any square = sqrt(2) * deltaX; + + Let maximum supported hypotenuse be INT_MAX, then: + + MAX_AXIS = INT_MAX / sqrt(2) = 2147483647 / 1.41421356 = 1518500251 + + The next choice is what to use for internal units (IU), sometimes called + world units. If nanometers, then the virtual space must be limited to + about 1.5 x 1.5 meters square. This is 1518500251 divided by 1e9 nm/meter. + + The maximum zoom factor then depends on the client window size. If we ask + wx to handle something outside INT_MIN to INT_MAX, there are unreported + problems in the non-Debug build because wxRound() goes silent. + + Pcbnew uses nanometers because we ned to convert coordintes and size between + milimeters and inches. using a iu = 1 nm avoid rounding issues + + Gerbview uses iu = 10 nm because we can have coordintes far from origin, and + 1 nm is too small to avoid int overflow. + (Conversions between milimeters and inches are not critical) +*/ + /** * @brief some define and functions to convert a value in mils, decimils or mm * to the internal unit used in pcbnew, cvpcb or gerbview (nanometer or deci-mil) diff --git a/include/zoom_defines.h b/include/zoom_defines.h index 7ed6eac20b..11b8f108c7 100644 --- a/include/zoom_defines.h +++ b/include/zoom_defines.h @@ -24,58 +24,10 @@ #pragma once -/* ZOOM LIMITS - - The largest distance that we (and Kicad) can support is INT_MAX, since it represents - distance often in a wxCoord or wxSize. As a scalar, a distance is always - positive. Because int is 32 bits and INT_MAX is - 2147483647. The most difficult distance for a virtual (world) cartesian - space is the hypotenuse, or diagonal measurement at a 45 degree angle. This - puts the most stress on the distance magnitude within the bounded virtual - space. So if we allow this distance to be our constraint of <= INT_MAX, this - constraint then propagates to the maximum distance in X and in Y that can be - supported on each axis. Remember that the hypotenuse of a 1x1 square is - sqrt( 1x1 + 1x1 ) = sqrt(2) = 1.41421356. - - hypotenuse of any square = sqrt(2) * deltaX; - - Let maximum supported hypotenuse be INT_MAX, then: - - MAX_AXIS = INT_MAX / sqrt(2) = 2147483647 / 1.41421356 = 1518500251 - - This maximum distance is imposed by wxWidgets, not by KiCad. The imposition - comes in the form of the data structures used in the graphics API at the - wxDC level. Obviously when we are not interacting with wx we can use double - to compute distances larger than this. For example the computation of the - total length of a net, can and should be done in double, since it might - actually be longer than a single diagonal line. - - The next choice is what to use for internal units (IU), sometimes called - world units. If nanometers, then the virtual space must be limited to - about 1.5 x 1.5 meters square. This is 1518500251 divided by 1e9 nm/meter. - - The maximum zoom factor then depends on the client window size. If we ask - wx to handle something outside INT_MIN to INT_MAX, there are unreported - problems in the non-Debug build because wxRound() goes silent. - - Let: - const double MAX_AXIS = 1518500251; - - Then a maximum zoom factor for a screen of 1920 pixels wide is - 1518500251 / 1920 = 790885. - - The largest zoom factor allowed is therefore ~ 300 (which computes to 762000). -*/ - -#define MAX_ZOOM_FACTOR 300.0 - -// Adjusted to display zoom level ~ 1 when the screen shows a 1:1 image. -// Obviously depends on the monitor, but this is an acceptable value. -#define ZOOM_COEFF 1.1 - -// List of predefined zooms used in zoom in/out from hotkeys and toolbar -#define ZOOM_LIST_GERBER 0.022, 0.035, 0.05, 0.08, 0.13, 0.22, 0.35, 0.6, 1.0,\ - 2.2, 3.5, 5.0, 8.0, 13.0, 22.0, 35.0, 50.0, 80.0, 130.0, 220.0 +// List of predefined zooms used in zoom in/out from hotkeys, context menu and toolbar +// Zooming using mouse wheel can have different limits +#define ZOOM_LIST_GERBVIEW 0.022, 0.035, 0.05, 0.08, 0.13, 0.22, 0.35, 0.6, 1.0,\ + 2.2, 3.5, 5.0, 8.0, 13.0, 22.0, 35.0, 50.0, 80.0, 130.0, 220.0 #define ZOOM_LIST_PCBNEW 0.13, 0.22, 0.35, 0.6, 1.0, 1.5, 2.2, 3.5, 5.0, 8.0, 13.0,\ 20.0, 35.0, 50.0, 80.0, 130.0, 220.0, 300.0 @@ -85,3 +37,27 @@ #define ZOOM_LIST_EESCHEMA 0.05, 0.07, 0.1, 0.15, 0.2, 0.3, 0.5, 0.7, 1.0, 1.5, 2.0,\ 3.0, 4.5, 6.5, 10.0, 15.0, 20.0, 30.0, 45.0, 65.0, 100.0 + +// Zoom scale limits for zoom (especially mouse wheel) +// the limits can differ from zoom list because the zoom list cannot be as long as +// we want because the zoom list is displayed in menus. +// But zoom by mouse wheel is limited mainly by the usability + +// Scale limits for zoom for Eeschema +#define ZOOM_MAX_LIMIT_EESCHEMA 100 +#define ZOOM_MIN_LIMIT_EESCHEMA 0.01 + +#define ZOOM_MAX_LIMIT_EESCHEMA_PREVIEW 20 +#define ZOOM_MIN_LIMIT_EESCHEMA_PREVIEW 0.5 + +// Scale limits for zoom for pl_editor +#define ZOOM_MAX_LIMIT_PLEDITOR 20 +#define ZOOM_MIN_LIMIT_PLEDITOR 0.05 + +// Scale limits for zoom for gerbview +#define ZOOM_MAX_LIMIT_GERBVIEW 220 +#define ZOOM_MIN_LIMIT_GERBVIEW 0.02 + +// Scale limits for zoom (especially mouse wheel) for Pcbnew +#define ZOOM_MAX_LIMIT_PCBNEW 5000 +#define ZOOM_MIN_LIMIT_PCBNEW 0.1 \ No newline at end of file diff --git a/pagelayout_editor/pl_draw_panel_gal.cpp b/pagelayout_editor/pl_draw_panel_gal.cpp index 53fc7ed21d..c35a4f48cb 100644 --- a/pagelayout_editor/pl_draw_panel_gal.cpp +++ b/pagelayout_editor/pl_draw_panel_gal.cpp @@ -37,6 +37,7 @@ #include "pl_editor_settings.h" #include "tools/pl_actions.h" #include "tools/pl_selection_tool.h" +#include using namespace std::placeholders; @@ -58,7 +59,8 @@ PL_DRAW_PANEL_GAL::PL_DRAW_PANEL_GAL( wxWindow* aParentWindow, wxWindowID aWindo m_painter->GetSettings()->LoadColors( settingsManager.GetColorSettings( cfg->m_ColorTheme ) ); m_view->SetPainter( m_painter.get() ); - m_view->SetScaleLimits( 20.0, 0.05 ); // This fixes the zoom in and zoom out limits + // This fixes the zoom in and zoom out limits + m_view->SetScaleLimits( ZOOM_MAX_LIMIT_PLEDITOR, ZOOM_MIN_LIMIT_PLEDITOR ); setDefaultLayerDeps(); diff --git a/pagelayout_editor/pl_editor_frame.cpp b/pagelayout_editor/pl_editor_frame.cpp index fd3a7a6315..74ad2f5c17 100644 --- a/pagelayout_editor/pl_editor_frame.cpp +++ b/pagelayout_editor/pl_editor_frame.cpp @@ -489,9 +489,6 @@ void PL_EDITOR_FRAME::LoadSettings( APP_SETTINGS_BASE* aCfg ) aCfg->m_Window.zoom_factors = { ZOOM_LIST_PL_EDITOR }; } - for( double& factor : aCfg->m_Window.zoom_factors ) - factor = std::min( factor, MAX_ZOOM_FACTOR ); - PL_EDITOR_SETTINGS* cfg = dynamic_cast( aCfg ); wxCHECK( cfg, /*void*/ ); diff --git a/pcbnew/footprint_preview_panel.cpp b/pcbnew/footprint_preview_panel.cpp index ad7350707e..23f58dae9b 100644 --- a/pcbnew/footprint_preview_panel.cpp +++ b/pcbnew/footprint_preview_panel.cpp @@ -449,9 +449,6 @@ FOOTPRINT_PREVIEW_PANEL* FOOTPRINT_PREVIEW_PANEL::New( KIWAY* aKiway, wxWindow* cfg->m_Window.zoom_factors = { ZOOM_LIST_PCBNEW }; } - for( double& factor : cfg->m_Window.zoom_factors ) - factor = std::min( factor, MAX_ZOOM_FACTOR ); - std::unique_ptr gal_opts; gal_opts = std::make_unique(); diff --git a/pcbnew/pcb_base_frame.cpp b/pcbnew/pcb_base_frame.cpp index 3fcde22d73..2145630a05 100644 --- a/pcbnew/pcb_base_frame.cpp +++ b/pcbnew/pcb_base_frame.cpp @@ -603,9 +603,6 @@ void PCB_BASE_FRAME::LoadSettings( APP_SETTINGS_BASE* aCfg ) aCfg->m_Window.zoom_factors = { ZOOM_LIST_PCBNEW }; } - for( double& factor : aCfg->m_Window.zoom_factors ) - factor = std::min( factor, MAX_ZOOM_FACTOR ); - // Some, but not all derived classes have a PCBNEW_SETTINGS. PCBNEW_SETTINGS* cfg = dynamic_cast( aCfg ); diff --git a/pcbnew/pcb_draw_panel_gal.cpp b/pcbnew/pcb_draw_panel_gal.cpp index 6e1838f3c2..296d38efd9 100644 --- a/pcbnew/pcb_draw_panel_gal.cpp +++ b/pcbnew/pcb_draw_panel_gal.cpp @@ -43,10 +43,12 @@ #include #include +#include #include #include #include + using namespace std::placeholders; const LAYER_NUM GAL_LAYER_ORDER[] = @@ -135,6 +137,8 @@ PCB_DRAW_PANEL_GAL::PCB_DRAW_PANEL_GAL( wxWindow* aParentWindow, wxWindowID aWin m_painter = std::make_unique( m_gal ); m_view->SetPainter( m_painter.get() ); + // This fixes the zoom in and zoom out limits: + m_view->SetScaleLimits( ZOOM_MAX_LIMIT_PCBNEW, ZOOM_MIN_LIMIT_PCBNEW ); setDefaultLayerOrder(); setDefaultLayerDeps();