From 921291c28bef50ac3e8201169c9d0eda994e6ec2 Mon Sep 17 00:00:00 2001 From: jean-pierre charras Date: Wed, 6 Dec 2023 16:39:29 +0100 Subject: [PATCH] Pcbnew, default values for sizes and thickness: add missing tests for validation. Especially for texts, the min and max values that are used are the same as for other dialogs. --- include/board_design_settings.h | 8 +- pcbnew/dialogs/panel_fp_editor_defaults.cpp | 89 +++++++++++++++++- pcbnew/dialogs/panel_fp_editor_defaults.h | 1 + .../dialogs/panel_setup_text_and_graphics.cpp | 92 ++++++++++++++++++- 4 files changed, 178 insertions(+), 12 deletions(-) diff --git a/include/board_design_settings.h b/include/board_design_settings.h index d14958131b..1089dc775d 100644 --- a/include/board_design_settings.h +++ b/include/board_design_settings.h @@ -93,8 +93,12 @@ #define DEFAULT_MINRESOLVEDSPOKES 2 // Fewer resolved spokes indicates a starved thermal -#define MINIMUM_ERROR_SIZE_MM 0.001 -#define MAXIMUM_ERROR_SIZE_MM 0.1 +#define MINIMUM_ERROR_SIZE_MM 0.001 // For arc approximation +#define MAXIMUM_ERROR_SIZE_MM 0.1 // For arc approximation + +// Min/max values used in dialogs to validate settings +#define MINIMUM_LINE_WIDTH_MM 0.005 // minimal line width entered in a dialog +#define MAXIMUM_LINE_WIDTH_MM 100.0 // max line width entered in a dialog /** diff --git a/pcbnew/dialogs/panel_fp_editor_defaults.cpp b/pcbnew/dialogs/panel_fp_editor_defaults.cpp index c953d58862..f6ea0faec8 100644 --- a/pcbnew/dialogs/panel_fp_editor_defaults.cpp +++ b/pcbnew/dialogs/panel_fp_editor_defaults.cpp @@ -29,6 +29,7 @@ #include #include #include +#include #include #include #include @@ -176,6 +177,7 @@ PANEL_FP_EDITOR_DEFAULTS::PANEL_FP_EDITOR_DEFAULTS( wxWindow* aParent, UNITS_PROVIDER* aUnitsProvider ) : PANEL_FP_EDITOR_DEFAULTS_BASE( aParent ) { + m_unitProvider = aUnitsProvider; m_fieldPropsGrid->SetDefaultRowSize( m_fieldPropsGrid->GetDefaultRowSize() + 4 ); m_fieldPropsGrid->SetTable( new TEXT_ITEMS_GRID_TABLE( true ), true ); @@ -356,16 +358,88 @@ bool PANEL_FP_EDITOR_DEFAULTS::TransferDataFromWindow() SETTINGS_MANAGER& mgr = Pgm().GetSettingsManager(); BOARD_DESIGN_SETTINGS& cfg = mgr.GetAppSettings()->m_DesignSettings; + // A minimal value for sizes and thickness: + const int minWidth = pcbIUScale.mmToIU( MINIMUM_LINE_WIDTH_MM ); + const int maxWidth = pcbIUScale.mmToIU( MAXIMUM_LINE_WIDTH_MM ); + const int minSize = pcbIUScale.MilsToIU( TEXT_MIN_SIZE_MILS ); + const int maxSize = pcbIUScale.MilsToIU( TEXT_MAX_SIZE_MILS ); + wxString errorsMsg; + for( int i = 0; i < ROW_COUNT; ++i ) { - cfg.m_LineThickness[ i ] = m_graphicsGrid->GetUnitValue( i, COL_LINE_THICKNESS ); + bool badParam = false; + + int lineWidth = m_graphicsGrid->GetUnitValue( i, COL_LINE_THICKNESS ); + + if( lineWidth < minWidth || lineWidth > maxWidth ) + { + if( !errorsMsg.IsEmpty() ) + errorsMsg += wxT( "\n\n" ); + + errorsMsg += wxString::Format( _( "%s: Incorrect line width.\n" + "It must be between %s and %s" ), + m_graphicsGrid->GetRowLabelValue( i ), + m_unitProvider->StringFromValue( minWidth , true), + m_unitProvider->StringFromValue( maxWidth , true) ); + badParam = true; + } + + if( !badParam ) + cfg.m_LineThickness[ i ] = lineWidth; if( i == ROW_EDGES || i == ROW_COURTYARD ) continue; - cfg.m_TextSize[i] = VECTOR2I( m_graphicsGrid->GetUnitValue( i, COL_TEXT_WIDTH ), - m_graphicsGrid->GetUnitValue( i, COL_TEXT_HEIGHT ) ); - cfg.m_TextThickness[ i ] = m_graphicsGrid->GetUnitValue( i, COL_TEXT_THICKNESS ); + badParam = false; + int textWidth = m_graphicsGrid->GetUnitValue( i, COL_TEXT_WIDTH ); + int textHeight = m_graphicsGrid->GetUnitValue( i, COL_TEXT_HEIGHT ); + int textThickness = m_graphicsGrid->GetUnitValue( i, COL_TEXT_THICKNESS ); + + if( textWidth < minSize || textHeight < minSize + || textWidth > maxSize || textHeight > maxSize ) + { + if( !errorsMsg.IsEmpty() ) + errorsMsg += wxT( "\n\n" ); + + errorsMsg += wxString::Format( _( "%s: Text size is incorrect.\n" + "Size must be between %s and %s" ), + m_graphicsGrid->GetRowLabelValue( i ), + m_unitProvider->StringFromValue( minSize , true), + m_unitProvider->StringFromValue( maxSize , true) ); + badParam = true; + } + + // Text thickness cannot be > text size /4 to be readable + int textMinDim = std::min( textWidth, textHeight ); + int textMaxThickness = std::min( maxWidth, textMinDim /4); + + if( !badParam && ( textThickness < minWidth || textThickness > textMaxThickness ) ) + { + if( !errorsMsg.IsEmpty() ) + errorsMsg += wxT( "\n\n" ); + + if( textThickness > textMaxThickness ) + errorsMsg += wxString::Format( _( "%s: Text thickness is too large.\n" + "It will be truncated to %s" ), + m_graphicsGrid->GetRowLabelValue( i ), + m_unitProvider->StringFromValue( textMaxThickness , true) ); + + else if( textThickness < minWidth ) + errorsMsg += wxString::Format( _( "%s: Text thickness is too small.\n" + "It will be truncated to %s" ), + m_graphicsGrid->GetRowLabelValue( i ), + m_unitProvider->StringFromValue( minWidth , true ) ); + + textThickness = std::min( textThickness, textMaxThickness ); + textThickness = std::max( textThickness, minWidth ); + m_graphicsGrid->SetUnitValue( i, COL_TEXT_THICKNESS, textThickness ); + } + + if( !badParam ) + { + cfg.m_TextSize[i] = VECTOR2I( textWidth, textHeight ); + cfg.m_TextThickness[ i ] = textThickness; + } wxString msg = m_graphicsGrid->GetCellValue( i, COL_TEXT_ITALIC ); cfg.m_TextItalic[ i ] = wxGridCellBoolEditor::IsTrueValue( msg ); @@ -396,7 +470,12 @@ bool PANEL_FP_EDITOR_DEFAULTS::TransferDataFromWindow() cfg.m_DefaultFPTextItems.emplace_back( text, visible, layer ); } - return true; + if( errorsMsg.IsEmpty() ) + return true; + + DisplayErrorMessage( this,errorsMsg, _( "Parameter error" ) ); + + return false; } diff --git a/pcbnew/dialogs/panel_fp_editor_defaults.h b/pcbnew/dialogs/panel_fp_editor_defaults.h index 6989e534c6..642be147d5 100644 --- a/pcbnew/dialogs/panel_fp_editor_defaults.h +++ b/pcbnew/dialogs/panel_fp_editor_defaults.h @@ -47,6 +47,7 @@ private: private: bool m_firstShow = true; + UNITS_PROVIDER* m_unitProvider; }; diff --git a/pcbnew/dialogs/panel_setup_text_and_graphics.cpp b/pcbnew/dialogs/panel_setup_text_and_graphics.cpp index 8c708f19ab..231deb5dfe 100644 --- a/pcbnew/dialogs/panel_setup_text_and_graphics.cpp +++ b/pcbnew/dialogs/panel_setup_text_and_graphics.cpp @@ -25,9 +25,11 @@ #include #include #include +#include #include #include +#include // Columns of layer classes grid @@ -199,16 +201,90 @@ bool PANEL_SETUP_TEXT_AND_GRAPHICS::TransferDataFromWindow() if( !m_grid->CommitPendingChanges() ) return false; + // A minimal value for sizes and thickness + const int minWidth = pcbIUScale.mmToIU( MINIMUM_LINE_WIDTH_MM ); + const int maxWidth = pcbIUScale.mmToIU( MAXIMUM_LINE_WIDTH_MM ); + const int minSize = pcbIUScale.MilsToIU( TEXT_MIN_SIZE_MILS ); + const int maxSize = pcbIUScale.MilsToIU( TEXT_MAX_SIZE_MILS ); + wxString errorsMsg; + UNITS_PROVIDER* unitProvider = m_Frame; + for( int i = 0; i < ROW_COUNT; ++i ) { - m_BrdSettings->m_LineThickness[ i ] = m_grid->GetUnitValue( i, COL_LINE_THICKNESS ); + bool badParam = false; + + int lineWidth = m_grid->GetUnitValue( i, COL_LINE_THICKNESS ); + + if( lineWidth < minWidth || lineWidth > maxWidth ) + { + if( !errorsMsg.IsEmpty() ) + errorsMsg += wxT( "\n\n" ); + + errorsMsg += wxString::Format( _( "%s: Incorrect line width.\n" + "It must be between %s and %s" ), + m_grid->GetRowLabelValue( i ), + unitProvider->StringFromValue( minWidth , true), + unitProvider->StringFromValue( maxWidth , true) ); + badParam = true; + } + + if( !badParam ) + m_BrdSettings->m_LineThickness[ i ] = lineWidth; if( i == ROW_EDGES || i == ROW_COURTYARD ) continue; - m_BrdSettings->m_TextSize[ i ] = VECTOR2I( m_grid->GetUnitValue( i, COL_TEXT_WIDTH ), - m_grid->GetUnitValue( i, COL_TEXT_HEIGHT ) ); - m_BrdSettings->m_TextThickness[ i ] = m_grid->GetUnitValue( i, COL_TEXT_THICKNESS ); + badParam = false; + int textWidth = m_grid->GetUnitValue( i, COL_TEXT_WIDTH ); + int textHeight = m_grid->GetUnitValue( i, COL_TEXT_HEIGHT ); + int textThickness = m_grid->GetUnitValue( i, COL_TEXT_THICKNESS ); + + if( textWidth < minSize || textHeight < minSize + || textWidth > maxSize || textHeight > maxSize ) + { + if( !errorsMsg.IsEmpty() ) + errorsMsg += wxT( "\n\n" ); + + errorsMsg += wxString::Format( _( "%s: Text size is incorrect.\n" + "Size must be between %s and %s" ), + m_grid->GetRowLabelValue( i ), + unitProvider->StringFromValue( minSize , true), + unitProvider->StringFromValue( maxSize , true) ); + badParam = true; + } + + // Text thickness cannot be > text size /4 to be readable + int textMaxThickness = std::min( maxWidth, std::min( textWidth, textHeight ) /4); + + if( !badParam && ( textThickness < minWidth || textThickness > textMaxThickness ) ) + { + if( !errorsMsg.IsEmpty() ) + errorsMsg += wxT( "\n\n" ); + + + if( textThickness > textMaxThickness ) + errorsMsg += wxString::Format( _( "%s: Text thickness is too large.\n" + "It will be truncated to %s" ), + m_grid->GetRowLabelValue( i ), + unitProvider->StringFromValue( textMaxThickness , true) ); + + else if( textThickness < minWidth ) + errorsMsg += wxString::Format( _( "%s: Text thickness is too small.\n" + "It will be truncated to %s" ), + m_grid->GetRowLabelValue( i ), + unitProvider->StringFromValue( minWidth , true ) ); + + textThickness = std::min( textThickness, textMaxThickness ); + textThickness = std::max( textThickness, minWidth ); + SET_MILS_CELL( i, COL_TEXT_THICKNESS, textThickness ); + } + + if( !badParam ) + { + m_BrdSettings->m_TextSize[ i ] = VECTOR2I( textWidth, textHeight ); + m_BrdSettings->m_TextThickness[ i ] = textThickness; + } + m_BrdSettings->m_TextItalic[ i ] = wxGridCellBoolEditor::IsTrueValue( m_grid->GetCellValue( i, COL_TEXT_ITALIC ) ); m_BrdSettings->m_TextUpright[ i ] = @@ -230,7 +306,13 @@ bool PANEL_SETUP_TEXT_AND_GRAPHICS::TransferDataFromWindow() m_BrdSettings->m_DimensionArrowLength = m_arrowLength.GetValue(); m_BrdSettings->m_DimensionExtensionOffset = m_extensionOffset.GetValue(); - return true; + + if( errorsMsg.IsEmpty() ) + return true; + + DisplayErrorMessage( this,errorsMsg, _( "Parameter error" ) ); + + return false; }