From f7861c90ec93b5fe2660a2102d9489567aba0dca Mon Sep 17 00:00:00 2001 From: Jeff Young Date: Tue, 6 Mar 2018 12:06:46 +0000 Subject: [PATCH] Use more self-documenting names for conversion routines. In particular, make sure no one thinks the low-precision ones are OK for dialogs and/or files. (cherry picked from commit bc1fcfa) --- common/base_units.cpp | 35 +++++++++++++---------- common/draw_frame.cpp | 11 ------- include/base_units.h | 18 ++++++++---- include/draw_frame.h | 22 -------------- pcbnew/class_drawsegment.cpp | 2 +- pcbnew/class_track.cpp | 12 ++++---- pcbnew/netinfo_item.cpp | 6 ++-- pcbnew/router/pns_dp_meander_placer.cpp | 6 ++-- pcbnew/router/pns_meander_placer.cpp | 4 +-- pcbnew/router/pns_meander_skew_placer.cpp | 4 +-- 10 files changed, 49 insertions(+), 71 deletions(-) diff --git a/common/base_units.cpp b/common/base_units.cpp index af5d545bc1..7dca5e3f12 100644 --- a/common/base_units.cpp +++ b/common/base_units.cpp @@ -120,20 +120,29 @@ double To_User_Unit( EDA_UNITS_T aUnit, double aValue, bool aUseMils ) * but not in dialogs, because 4 digits only * could truncate the actual value */ + +// JEY TODO: retire this in favour of MessageTextFromValue().... wxString CoordinateToString( int aValue, bool aUseMils ) { - return LengthDoubleToString( (double) aValue, aUseMils ); + return MessageTextFromValue( g_UserUnit, aValue, aUseMils ); } -// JEY TODO: remove; use StringFromValue() instead -wxString LengthDoubleToString( double aValue, bool aUseMils ) +// A lower-precision (for readability) version of StringFromValue() +wxString MessageTextFromValue( EDA_UNITS_T aUnits, int aValue, bool aUseMils ) +{ + return MessageTextFromValue( aUnits, (double) aValue, aUseMils ); +} + + +// A lower-precision (for readability) version of StringFromValue() +wxString MessageTextFromValue( EDA_UNITS_T aUnits, double aValue, bool aUseMils ) { wxString text; const wxChar* format; - double value = To_User_Unit( g_UserUnit, aValue ); + double value = To_User_Unit( aUnits, aValue, aUseMils ); - if( g_UserUnit == INCHES ) + if( aUnits == INCHES ) { if( aUseMils ) { @@ -142,7 +151,6 @@ wxString LengthDoubleToString( double aValue, bool aUseMils ) #else format = wxT( "%.1f" ); #endif - value *= 1000; } else { @@ -165,10 +173,7 @@ wxString LengthDoubleToString( double aValue, bool aUseMils ) text.Printf( format, value ); text += " "; - if( g_UserUnit == INCHES ) - text += ( aUseMils ) ? _( " mils" ) : _( " in" ); - else - text += _( "mm" ); + text += GetAbbreviatedUnitsLabel( aUnits ); return text; } @@ -209,9 +214,9 @@ void StripTrailingZeros( wxString& aStringValue, unsigned aTrailingZeroAllowed ) * otherwise the actual value is rounded when read from dialog and converted * in internal units, and therefore modified. */ -wxString StringFromValue( EDA_UNITS_T aUnit, int aValue, bool aAddUnitSymbol, bool aUseMils ) +wxString StringFromValue( EDA_UNITS_T aUnits, int aValue, bool aAddUnitSymbol, bool aUseMils ) { - double value_to_print = To_User_Unit( aUnit, aValue, aUseMils ); + double value_to_print = To_User_Unit( aUnits, aValue, aUseMils ); #if defined( EESCHEMA ) wxString stringValue = wxString::Format( wxT( "%.3f" ), value_to_print ); @@ -248,7 +253,7 @@ wxString StringFromValue( EDA_UNITS_T aUnit, int aValue, bool aAddUnitSymbol, bo if( aAddUnitSymbol ) { - switch( aUnit ) + switch( aUnits ) { case INCHES: if( aUseMils ) @@ -283,9 +288,9 @@ void PutValueInLocalUnits( wxTextCtrl& aTextCtr, int aValue ) } -double From_User_Unit( EDA_UNITS_T aUnit, double aValue, bool aUseMils ) +double From_User_Unit( EDA_UNITS_T aUnits, double aValue, bool aUseMils ) { - switch( aUnit ) + switch( aUnits ) { case MILLIMETRES: return MM_TO_IU( aValue ); diff --git a/common/draw_frame.cpp b/common/draw_frame.cpp index 131c861be1..d401803cd2 100644 --- a/common/draw_frame.cpp +++ b/common/draw_frame.cpp @@ -884,17 +884,6 @@ void EDA_DRAW_FRAME::PushPreferences( const EDA_DRAW_PANEL* aParentCanvas ) m_canvas->SetEnableAutoPan( aParentCanvas->GetEnableAutoPan() ); } -wxString EDA_DRAW_FRAME::CoordinateToString( int aValue, bool aConvertToMils ) const -{ - return ::CoordinateToString( aValue, aConvertToMils ); -} - -wxString EDA_DRAW_FRAME::LengthDoubleToString( double aValue, bool aConvertToMils ) const -{ - return ::LengthDoubleToString( aValue, aConvertToMils ); -} - - bool EDA_DRAW_FRAME::HandleBlockBegin( wxDC* aDC, EDA_KEY aKey, const wxPoint& aPosition, int aExplicitCommand ) { diff --git a/include/base_units.h b/include/base_units.h index 3ff720a4ae..894a8c694c 100644 --- a/include/base_units.h +++ b/include/base_units.h @@ -105,20 +105,26 @@ wxString CoordinateToString( int aValue, bool aUseMils = false ); wxString AngleToStringDegrees( double aAngle ); /** - * Function LengthDoubleToString + * Function MessageTextFromValue * is a helper to convert the \a double length \a aValue to a string in inches, - * millimeters, or unscaled units according to the current user units setting. + * millimeters, or unscaled units. * * Should be used only to display a coordinate in status, but not in dialogs, - * because the mantissa of the number displayed has 4 digits max for readability. - * (i.e. the value shows the decimils or the microns ) - * However the actual internal value could need up to 8 digits to be printed + * files, etc., because the mantissa of the number displayed has 4 digits max + * for readability. The actual internal value could need up to 8 digits to be + * printed. * + * Use StringFromValue() instead where precision matters. + * + * @param aUnits The units to show the value in. The unit string is added to the + * message text. * @param aValue The double value to convert. * @param aUseMils Convert inch values to mils if true. * @return The converted string for display in user interface elements. */ -wxString LengthDoubleToString( double aValue, bool aUseMils = false ); +wxString MessageTextFromValue( EDA_UNITS_T aUnits, double aValue, bool aUseMils = false ); + +wxString MessageTextFromValue( EDA_UNITS_T aUnits, int aValue, bool aUseMils = false ); /** * Function StringFromValue diff --git a/include/draw_frame.h b/include/draw_frame.h index 3480028808..d06c4448df 100644 --- a/include/draw_frame.h +++ b/include/draw_frame.h @@ -857,28 +857,6 @@ public: */ virtual void PrintPage( wxDC* aDC, LSET aPrintMask, bool aPrintMirrorMode, void* aData = NULL ); - /** - * Function CoordinateToString - * is a helper to convert the \a integer coordinate \a aValue to a string in inches or mm - * according to the current user units setting. - * @param aValue The coordinate to convert. - * @param aConvertToMils Convert inch values to mils if true. This setting has no effect if - * the current user unit is millimeters. - * @return The converted string for display in user interface elements. - */ - wxString CoordinateToString( int aValue, bool aConvertToMils = false ) const; - - /** - * Function LengthDoubleToString - * is a helper to convert the \a double value \a aValue to a string in inches or mm - * according to the current user units setting. - * @param aValue The coordinate to convert. - * @param aConvertToMils Convert inch values to mils if true. This setting has no effect if - * the current user unit is millimeters. - * @return The converted string for display in user interface elements. - */ - wxString LengthDoubleToString( double aValue, bool aConvertToMils = false ) const; - /** * Function UseGalCanvas * used to switch between standard and GAL-based canvas. diff --git a/pcbnew/class_drawsegment.cpp b/pcbnew/class_drawsegment.cpp index 4426b05cef..c62cc2d8dd 100644 --- a/pcbnew/class_drawsegment.cpp +++ b/pcbnew/class_drawsegment.cpp @@ -727,7 +727,7 @@ wxString DRAWSEGMENT::GetSelectMenuText() const text.Printf( _( "Pcb Graphic %s, length %s on %s" ), GetChars( ShowShape( m_Shape ) ), - GetChars( ::LengthDoubleToString( GetLength() ) ), + GetChars( ::MessageTextFromValue( g_UserUnit, GetLength() ) ), GetChars( GetLayerName() ) ); return text; diff --git a/pcbnew/class_track.cpp b/pcbnew/class_track.cpp index ad790b1224..80e4865366 100644 --- a/pcbnew/class_track.cpp +++ b/pcbnew/class_track.cpp @@ -1080,15 +1080,15 @@ void TRACK::GetMsgPanelInfo( std::vector< MSG_PANEL_ITEM >& aList ) track_buffer_start = track_buffer_start->Back(); board->MarkTrace( track_buffer_start, this, NULL, &trackLen, &lenPadToDie, false ); - msg = ::LengthDoubleToString( trackLen ); + msg = ::MessageTextFromValue( g_UserUnit, trackLen ); aList.push_back( MSG_PANEL_ITEM( _( "Length" ), msg, DARKCYAN ) ); if( lenPadToDie != 0 ) { - msg = ::LengthDoubleToString( trackLen + lenPadToDie ); + msg = ::MessageTextFromValue( g_UserUnit, trackLen + lenPadToDie ); aList.push_back( MSG_PANEL_ITEM( _( "Full Length" ), msg, DARKCYAN ) ); - msg = ::LengthDoubleToString( lenPadToDie ); + msg = ::MessageTextFromValue( g_UserUnit, lenPadToDie ); aList.push_back( MSG_PANEL_ITEM( _( "Pad To Die Length" ), msg, DARKCYAN ) ); } } @@ -1196,7 +1196,7 @@ void TRACK::GetMsgPanelInfoBase( std::vector< MSG_PANEL_ITEM >& aList ) aList.push_back( MSG_PANEL_ITEM( _( "Width" ), msg, DARKCYAN ) ); // Display segment length - msg = ::LengthDoubleToString( GetLength() ); + msg = ::MessageTextFromValue( g_UserUnit, GetLength() ); aList.push_back( MSG_PANEL_ITEM( _( "Segment Length" ), msg, DARKCYAN ) ); } @@ -1223,7 +1223,7 @@ void SEGZONE::GetMsgPanelInfoBase( std::vector< MSG_PANEL_ITEM >& aList ) aList.push_back( MSG_PANEL_ITEM( _( "Width" ), msg, DARKCYAN ) ); // Display segment length - msg = ::LengthDoubleToString( GetLength() ); + msg = ::MessageTextFromValue( g_UserUnit, GetLength() ); aList.push_back( MSG_PANEL_ITEM( _( "Segment Length" ), msg, DARKCYAN ) ); } @@ -1613,7 +1613,7 @@ wxString TRACK::GetSelectMenuText() const GetChars( ShowWidth() ), GetChars( GetNetnameMsg() ), GetChars( GetLayerName() ), - GetChars( ::LengthDoubleToString( GetLength() ) ) ); + GetChars( ::MessageTextFromValue( g_UserUnit, GetLength() ) ) ); return text; } diff --git a/pcbnew/netinfo_item.cpp b/pcbnew/netinfo_item.cpp index 3c3b323b9e..98eb8368d4 100644 --- a/pcbnew/netinfo_item.cpp +++ b/pcbnew/netinfo_item.cpp @@ -140,14 +140,14 @@ void NETINFO_ITEM::GetMsgPanelInfo( std::vector< MSG_PANEL_ITEM >& aList ) aList.push_back( MSG_PANEL_ITEM( _( "Vias" ), txt, BLUE ) ); // Displays the full net length (tracks on pcb + internal ICs connections ): - txt = ::LengthDoubleToString( lengthnet + lengthPadToDie ); + txt = ::MessageTextFromValue( g_UserUnit, lengthnet + lengthPadToDie ); aList.push_back( MSG_PANEL_ITEM( _( "Net Length" ), txt, RED ) ); // Displays the net length of tracks only: - txt = ::LengthDoubleToString( lengthnet ); + txt = ::MessageTextFromValue( g_UserUnit, lengthnet ); aList.push_back( MSG_PANEL_ITEM( _( "On Board" ), txt, RED ) ); // Displays the net length of internal ICs connections (wires inside ICs): - txt = ::LengthDoubleToString( lengthPadToDie ); + txt = ::MessageTextFromValue( g_UserUnit, lengthPadToDie ); aList.push_back( MSG_PANEL_ITEM( _( "In Package" ), txt, RED ) ); } diff --git a/pcbnew/router/pns_dp_meander_placer.cpp b/pcbnew/router/pns_dp_meander_placer.cpp index e340d7856a..39a3cd1e3e 100644 --- a/pcbnew/router/pns_dp_meander_placer.cpp +++ b/pcbnew/router/pns_dp_meander_placer.cpp @@ -377,11 +377,11 @@ const wxString DP_MEANDER_PLACER::TuningInfo() const return _( "?" ); } - status += LengthDoubleToString( (double) m_lastLength, false ); + status += ::MessageTextFromValue( g_UserUnit, m_lastLength, false ); status += "/"; - status += LengthDoubleToString( (double) m_settings.m_targetLength, false ); + status += ::MessageTextFromValue( g_UserUnit, m_settings.m_targetLength, false ); status += " (gap: "; - status += LengthDoubleToString( (double) m_originPair.Gap(), false ); + status += ::MessageTextFromValue( g_UserUnit, m_originPair.Gap(), false ); status += ")"; return status; diff --git a/pcbnew/router/pns_meander_placer.cpp b/pcbnew/router/pns_meander_placer.cpp index 46c392f7ab..5c36ed4ec6 100644 --- a/pcbnew/router/pns_meander_placer.cpp +++ b/pcbnew/router/pns_meander_placer.cpp @@ -252,9 +252,9 @@ const wxString MEANDER_PLACER::TuningInfo() const return _( "?" ); } - status += LengthDoubleToString( (double) m_lastLength, false ); + status += ::MessageTextFromValue( g_UserUnit, m_lastLength, false ); status += "/"; - status += LengthDoubleToString( (double) m_settings.m_targetLength, false ); + status += ::MessageTextFromValue( g_UserUnit, m_settings.m_targetLength, false ); return status; } diff --git a/pcbnew/router/pns_meander_skew_placer.cpp b/pcbnew/router/pns_meander_skew_placer.cpp index ae59b97cd1..0d5a485412 100644 --- a/pcbnew/router/pns_meander_skew_placer.cpp +++ b/pcbnew/router/pns_meander_skew_placer.cpp @@ -163,9 +163,9 @@ const wxString MEANDER_SKEW_PLACER::TuningInfo() const return _( "?" ); } - status += LengthDoubleToString( (double) m_lastLength - m_coupledLength, false ); + status += ::MessageTextFromValue( g_UserUnit, m_lastLength - m_coupledLength, false ); status += "/"; - status += LengthDoubleToString( (double) m_settings.m_targetSkew, false ); + status += ::MessageTextFromValue( g_UserUnit, m_settings.m_targetSkew, false ); return status; }