From 1baaf9afd04950b1ff76d40142dea15a1c7a9946 Mon Sep 17 00:00:00 2001 From: John Beard <john.j.beard@gmail.com> Date: Mon, 14 Oct 2024 20:17:07 +0800 Subject: [PATCH] Add height get/set to reference image properties Relates-To: https://gitlab.com/kicad/code/kicad/-/issues/18567 --- common/reference_image.cpp | 36 ++++++++++++++++++++++++++++---- eeschema/sch_bitmap.cpp | 38 ++++++++++++++++++++++++++++++++++ eeschema/sch_bitmap.h | 4 ++++ include/reference_image.h | 5 +++++ pcbnew/pcb_reference_image.cpp | 38 ++++++++++++++++++++++++++++++++++ pcbnew/pcb_reference_image.h | 4 ++++ 6 files changed, 121 insertions(+), 4 deletions(-) diff --git a/common/reference_image.cpp b/common/reference_image.cpp index 2a2ef521c3..fce2e63182 100644 --- a/common/reference_image.cpp +++ b/common/reference_image.cpp @@ -171,6 +171,26 @@ VECTOR2I REFERENCE_IMAGE::GetSize() const } +void REFERENCE_IMAGE::SetWidth( int aWidth ) +{ + if( aWidth <= 0 ) + return; + + const double ratio = aWidth / (double) m_bitmapBase->GetSize().x; + scaleBy( ratio ); +} + + +void REFERENCE_IMAGE::SetHeight( int aHeight ) +{ + if( aHeight <= 0 ) + return; + + const double ratio = aHeight / (double) m_bitmapBase->GetSize().y; + scaleBy( ratio ); +} + + double REFERENCE_IMAGE::GetImageScale() const { return m_bitmapBase->GetScale(); @@ -179,15 +199,23 @@ double REFERENCE_IMAGE::GetImageScale() const void REFERENCE_IMAGE::SetImageScale( double aScale ) { - if( aScale < 0 ) + if( aScale <= 0 ) return; const double ratio = aScale / m_bitmapBase->GetScale(); + scaleBy( ratio ); +} + + +void REFERENCE_IMAGE::scaleBy( double aRatio ) +{ + if( aRatio <= 0 ) + return; const VECTOR2D currentOrigin = m_pos + m_transformOriginOffset; - const VECTOR2D newOffset = m_transformOriginOffset * ratio; + const VECTOR2D newOffset = m_transformOriginOffset * aRatio; const VECTOR2D newCenter = currentOrigin - newOffset; - const VECTOR2D newSize = m_bitmapBase->GetSize() * ratio; + const VECTOR2D newSize = m_bitmapBase->GetSize() * aRatio; // The span of the image is limited to the size of the coordinate system if( !IsVec2SafeXY( newSize ) ) @@ -199,7 +227,7 @@ void REFERENCE_IMAGE::SetImageScale( double aScale ) if( !IsBOX2Safe( newBox ) ) return; - m_bitmapBase->SetScale( aScale ); + m_bitmapBase->SetScale( m_bitmapBase->GetScale() * aRatio ); SetTransformOriginOffset( KiROUND( newOffset ) ); // Don't need to recheck the box, we just did that m_pos = KiROUND( newCenter ); diff --git a/eeschema/sch_bitmap.cpp b/eeschema/sch_bitmap.cpp index a600d36eb6..ec1f246b07 100644 --- a/eeschema/sch_bitmap.cpp +++ b/eeschema/sch_bitmap.cpp @@ -276,6 +276,30 @@ void SCH_BITMAP::SetImageScale( double aScale ) } +int SCH_BITMAP::GetWidth() const +{ + return m_referenceImage.GetImage().GetSize().x; +} + + +void SCH_BITMAP::SetWidth( int aWidth ) +{ + m_referenceImage.SetWidth( aWidth ); +} + + +int SCH_BITMAP::GetHeight() const +{ + return m_referenceImage.GetImage().GetSize().y; +} + + +void SCH_BITMAP::SetHeight( int aHeight ) +{ + m_referenceImage.SetHeight( aHeight ); +} + + static struct SCH_BITMAP_DESC { SCH_BITMAP_DESC() @@ -313,5 +337,19 @@ static struct SCH_BITMAP_DESC &SCH_BITMAP::GetTransformOriginOffsetY, PROPERTY_DISPLAY::PT_COORD, ORIGIN_TRANSFORMS::ABS_Y_COORD ), groupImage ); + + propMgr.AddProperty( new PROPERTY<SCH_BITMAP, int>( + _HKI( "Width" ), + &SCH_BITMAP::SetWidth, + &SCH_BITMAP::GetWidth, + PROPERTY_DISPLAY::PT_COORD ), + groupImage ); + + propMgr.AddProperty( new PROPERTY<SCH_BITMAP, int>( + _HKI( "Height" ), + &SCH_BITMAP::SetHeight, + &SCH_BITMAP::GetHeight, + PROPERTY_DISPLAY::PT_COORD ), + groupImage ); } } _SCH_BITMAP_DESC; diff --git a/eeschema/sch_bitmap.h b/eeschema/sch_bitmap.h index 24118898b7..f53039b966 100644 --- a/eeschema/sch_bitmap.h +++ b/eeschema/sch_bitmap.h @@ -123,6 +123,10 @@ private: friend struct SCH_BITMAP_DESC; // Property manager interfaces + int GetWidth() const; + void SetWidth( int aWidth ); + int GetHeight() const; + void SetHeight( int aHeight ); int GetTransformOriginOffsetX() const; void SetTransformOriginOffsetX( int aX ); int GetTransformOriginOffsetY() const; diff --git a/include/reference_image.h b/include/reference_image.h index c7a128131a..f39e933f0c 100644 --- a/include/reference_image.h +++ b/include/reference_image.h @@ -80,6 +80,9 @@ public: */ void SetImageScale( double aScale ); + void SetWidth( int aWidth ); + void SetHeight( int aHeight ); + void Flip( const VECTOR2I& aCentre, FLIP_DIRECTION aFlipDirection ); void Rotate( const VECTOR2I& aCenter, const EDA_ANGLE& aAngle ); @@ -131,6 +134,8 @@ public: void SetTransformOriginOffset( const VECTOR2I& aCenter ); private: + void scaleBy( double ratio ); + void updatePixelSizeInIU(); const EDA_IU_SCALE& m_iuScale; diff --git a/pcbnew/pcb_reference_image.cpp b/pcbnew/pcb_reference_image.cpp index 4d4fae5e4f..d88e718ed2 100644 --- a/pcbnew/pcb_reference_image.cpp +++ b/pcbnew/pcb_reference_image.cpp @@ -307,6 +307,30 @@ void PCB_REFERENCE_IMAGE::SetImageScale( double aScale ) } +int PCB_REFERENCE_IMAGE::GetWidth() const +{ + return m_referenceImage.GetImage().GetSize().x; +} + + +void PCB_REFERENCE_IMAGE::SetWidth( int aWidth ) +{ + m_referenceImage.SetWidth( aWidth ); +} + + +int PCB_REFERENCE_IMAGE::GetHeight() const +{ + return m_referenceImage.GetImage().GetSize().y; +} + + +void PCB_REFERENCE_IMAGE::SetHeight( int aHeight ) +{ + m_referenceImage.SetHeight( aHeight ); +} + + static struct PCB_REFERENCE_IMAGE_DESC { PCB_REFERENCE_IMAGE_DESC() @@ -340,6 +364,20 @@ static struct PCB_REFERENCE_IMAGE_DESC PROPERTY_DISPLAY::PT_COORD, ORIGIN_TRANSFORMS::ABS_Y_COORD ), groupImage ); + propMgr.AddProperty( new PROPERTY<PCB_REFERENCE_IMAGE, int>( + _HKI( "Width" ), + &PCB_REFERENCE_IMAGE::SetWidth, + &PCB_REFERENCE_IMAGE::GetWidth, + PROPERTY_DISPLAY::PT_COORD ), + groupImage ); + + propMgr.AddProperty( new PROPERTY<PCB_REFERENCE_IMAGE, int>( + _HKI( "Height" ), + &PCB_REFERENCE_IMAGE::SetHeight, + &PCB_REFERENCE_IMAGE::GetHeight, + PROPERTY_DISPLAY::PT_COORD ), + groupImage ); + // For future use const wxString greyscale = _HKI( "Greyscale" ); } diff --git a/pcbnew/pcb_reference_image.h b/pcbnew/pcb_reference_image.h index 2650d98074..f05c7cb3bb 100644 --- a/pcbnew/pcb_reference_image.h +++ b/pcbnew/pcb_reference_image.h @@ -117,6 +117,10 @@ private: friend struct PCB_REFERENCE_IMAGE_DESC; // Property manager interfaces + int GetWidth() const; + void SetWidth( int aWidth ); + int GetHeight() const; + void SetHeight( int aHeight ); int GetTransformOriginOffsetX() const; void SetTransformOriginOffsetX( int aX ); int GetTransformOriginOffsetY() const;