Browse Source

Fix some last-minute barcode edits

This should fix most of the regressions
master
Seth Hillbrand 2 months ago
parent
commit
a8df1dac68
  1. 2
      libs/core/include/core/type_helpers.h
  2. 35
      pcbnew/pcb_barcode.cpp
  3. 4
      pcbnew/pcb_barcode.h
  4. 1
      pcbnew/pcb_painter.cpp
  5. 31
      pcbnew/tools/pcb_point_editor.cpp
  6. 84
      qa/tests/pcbnew/test_barcode_load_save.cpp

2
libs/core/include/core/type_helpers.h

@ -23,6 +23,8 @@
#pragma once
#include <type_traits>
/**
* @brief A type that is always false.
*

35
pcbnew/pcb_barcode.cpp

@ -72,8 +72,7 @@ PCB_BARCODE::~PCB_BARCODE()
void PCB_BARCODE::SetPosition( const VECTOR2I& aPos )
{
VECTOR2I delta = aPos - m_pos;
m_text.Offset( delta );
m_pos = aPos;
Move( delta );
}
@ -188,6 +187,9 @@ void PCB_BARCODE::SetBarcodeTextHeight( int aHeight )
void PCB_BARCODE::Move( const VECTOR2I& offset )
{
m_pos += offset;
m_symbolPoly.Move( offset );
m_poly.Move( offset );
m_textPoly.Move( offset );
m_text.Move( offset );
}
@ -213,10 +215,15 @@ void PCB_BARCODE::Flip( const VECTOR2I& aCentre, FLIP_DIRECTION aFlipLeftRight )
void PCB_BARCODE::AssembleBarcode( bool aRebuildBarcode, bool aRebuildText )
{
if( aRebuildBarcode )
// if( aRebuildBarcode )
ComputeBarcode();
if( aRebuildText )
// Scale the symbol polygon to the desired barcode width/height (property values) and center it at m_pos
// Note: SetRect will rescale the symbol-only polygon and then rebuild m_poly
SetRect( m_pos - VECTOR2I( m_width / 2, m_height / 2 ),
m_pos + VECTOR2I( m_width / 2, m_height / 2 ) );
// if( aRebuildText )
ComputeTextPoly();
// Build full m_poly from symbol + optional text, then apply knockout if requested
@ -268,7 +275,7 @@ void PCB_BARCODE::AssembleBarcode( bool aRebuildBarcode, bool aRebuildText )
// m_poly.BBox().Centre() == GetPosition() holds for tests and downstream logic.
if( !m_poly.IsEmpty() )
{
VECTOR2I center = m_poly.BBox().GetCenter();
VECTOR2I center = m_symbolPoly.BBox().GetCenter();
VECTOR2I delta = m_pos - center;
if( delta != VECTOR2I( 0, 0 ) )
m_poly.Move( delta );
@ -415,11 +422,6 @@ void PCB_BARCODE::ComputeBarcode()
}
m_symbolPoly.CacheTriangulation();
// Scale the symbol polygon to the desired barcode width/height (property values) and center it at m_pos
// Note: SetRect will rescale the symbol-only polygon and then rebuild m_poly
SetRect( m_pos - VECTOR2I( m_width / 2, m_height / 2 ),
m_pos + VECTOR2I( m_width / 2, m_height / 2 ) );
}
@ -476,7 +478,8 @@ void PCB_BARCODE::SetRect( const VECTOR2I& aTopLeft, const VECTOR2I& aBotRight )
int oldW = bbox.GetWidth();
int oldH = bbox.GetHeight();
SetPosition( ( aTopLeft + aBotRight ) / 2 );
VECTOR2I newPosition = ( aTopLeft + aBotRight ) / 2;
SetPosition( newPosition );
int newW = aBotRight.x - aTopLeft.x;
int newH = aBotRight.y - aTopLeft.y;
// Guard against zero/negative sizes from interactive edits; enforce a tiny minimum
@ -487,7 +490,15 @@ void PCB_BARCODE::SetRect( const VECTOR2I& aTopLeft, const VECTOR2I& aBotRight )
double scaleX = oldW ? static_cast<double>( newW ) / oldW : 1.0;
double scaleY = oldH ? static_cast<double>( newH ) / oldH : 1.0;
m_symbolPoly.Scale( scaleX, scaleY, bbox.GetCenter() );
VECTOR2I oldCenter = bbox.GetCenter();
m_symbolPoly.Scale( scaleX, scaleY, oldCenter );
// After scaling, move the symbol polygon to be centered at the new position
VECTOR2I newCenter = m_symbolPoly.BBox().GetCenter();
VECTOR2I delta = newPosition - newCenter;
if( delta != VECTOR2I( 0, 0 ) )
m_symbolPoly.Move( delta );
// Update intended barcode symbol size (without text/margins)
m_width = newW;

4
pcbnew/pcb_barcode.h

@ -32,11 +32,11 @@
#include <board_item.h>
#include <geometry/shape_poly_set.h>
#include <pcb_text.h>
class LINE_READER;
class MSG_PANEL_ITEM;
class SHAPE_POLY_SET;
class PCB_TEXT;
enum class BARCODE_T : int

1
pcbnew/pcb_painter.cpp

@ -2921,7 +2921,6 @@ void PCB_PAINTER::draw( const PCB_BARCODE* aBarcode, int aLayer )
if( shape.OutlineCount() != 0 )
{
m_gal->Save();
m_gal->Translate( aBarcode->GetPosition() );
m_gal->SetLineWidth( 0 );
m_gal->SetIsFill( true );

31
pcbnew/tools/pcb_point_editor.cpp

@ -816,24 +816,11 @@ public:
maxX = std::numeric_limits<int>::min();
maxY = std::numeric_limits<int>::min();
for( auto it = poly.CIterateWithHoles(); it; ++it )
{
const auto idx = it.GetIndex();
VECTOR2I wp = poly.CVertex( idx );
// Geometry is origin-centered; unrotate around origin, then translate to center-based frame
VECTOR2I lp = center + GetRotated( wp, -angle );
minX = std::min( minX, lp.x );
maxX = std::max( maxX, lp.x );
minY = std::min( minY, lp.y );
maxY = std::max( maxY, lp.y );
}
BOX2I fullLocalBBox = BOX2I( VECTOR2I( minX, minY ), VECTOR2I( maxX - minX, maxY - minY ) );
// Symbol bbox derived from current symbol width/height centered on 'center'
int symW = m_barcode.GetWidth();
int symH = m_barcode.GetHeight();
BOX2I symLocalBBox( VECTOR2I( center.x - symW / 2, center.y - symH / 2 ), VECTOR2I( symW, symH ) );
BOX2I fullLocalBBox = poly.BBox();
// Symbol bbox derived from current symbol width/height centered on 'center'
int symW = m_barcode.GetWidth();
int symH = m_barcode.GetHeight();
BOX2I symLocalBBox( VECTOR2I( center.x - symW / 2, center.y - symH / 2 ), VECTOR2I( symW, symH ) );
wxLogTrace( "KICAD_PCB_BARCODE_EDIT",
"UpdateItem: fullLocalBBox LRTB=(%d,%d,%d,%d) symLocalBBox LRTB=(%d,%d,%d,%d)",
@ -922,10 +909,10 @@ private:
wxLogTrace( "KICAD_PCB_BARCODE_EDIT",
" local bbox LRTB=(%d,%d,%d,%d)", minX, maxX, minY, maxY );
aTL = center + GetRotated( tlL, angle );
aTR = center + GetRotated( trL, angle );
aBR = center + GetRotated( brL, angle );
aBL = center + GetRotated( blL, angle );
aTL = GetRotated( tlL, angle );
aTR = GetRotated( trL, angle );
aBR = GetRotated( brL, angle );
aBL = GetRotated( blL, angle );
}
PCB_BARCODE& m_barcode;

84
qa/tests/pcbnew/test_barcode_load_save.cpp

@ -124,4 +124,88 @@ BOOST_AUTO_TEST_CASE( BarcodeFootprintWriteRead )
BOOST_CHECK_EQUAL( loaded->GetWidth(), barcode->GetWidth() );
BOOST_CHECK_EQUAL( loaded->GetHeight(), barcode->GetHeight() );
BOOST_CHECK_EQUAL( loaded->GetTextHeight(), barcode->GetTextHeight() );
BOX2I bbox = loaded->GetPolyShape().BBox();
BOOST_CHECK_EQUAL( bbox.Centre(), loaded->GetPosition() );
}
BOOST_AUTO_TEST_CASE( BarcodePositioningAlignment )
{
SETTINGS_MANAGER settingsManager( true );
std::unique_ptr<BOARD> board = std::make_unique<BOARD>();
// Test multiple barcode types and positions to ensure consistent alignment
struct TestCase
{
BARCODE_T kind;
VECTOR2I position;
int width;
int height;
bool withText;
bool knockout;
double angle;
wxString text;
};
std::vector<TestCase> testCases = {
// Basic QR codes at different positions
{ BARCODE_T::QR_CODE, VECTOR2I( 0, 0 ), 2000000, 2000000, false, false, 0.0, "TEST1" },
{ BARCODE_T::QR_CODE, VECTOR2I( 5000000, 3000000 ), 3000000, 3000000, false, false, 0.0, "TEST2" },
{ BARCODE_T::QR_CODE, VECTOR2I( -2000000, -1000000 ), 1500000, 1500000, false, false, 0.0, "TEST3" },
// With text
{ BARCODE_T::QR_CODE, VECTOR2I( 1000000, 2000000 ), 2500000, 2500000, true, false, 0.0, "WITHTEXT" },
// With knockout
{ BARCODE_T::QR_CODE, VECTOR2I( 2000000, 1000000 ), 2000000, 2000000, false, true, 0.0, "KNOCKOUT" },
// With rotation
{ BARCODE_T::QR_CODE, VECTOR2I( 3000000, 2000000 ), 2000000, 2000000, false, false, 45.0, "ROTATED" },
// Different barcode types
{ BARCODE_T::CODE_39, VECTOR2I( 4000000, 1000000 ), 3000000, 800000, false, false, 0.0, "CODE39TEST" },
{ BARCODE_T::CODE_128, VECTOR2I( 1000000, 4000000 ), 3500000, 1000000, false, false, 0.0, "CODE128" },
{ BARCODE_T::DATA_MATRIX, VECTOR2I( 3000000, 3000000 ), 1800000, 1800000, false, false, 0.0, "DATAMATRIX" },
{ BARCODE_T::MICRO_QR_CODE, VECTOR2I( 2000000, 4000000 ), 1200000, 1200000, false, false, 0.0, "µQR" },
// Combined scenarios
{ BARCODE_T::QR_CODE, VECTOR2I( 1500000, 1500000 ), 2200000, 2200000, true, true, 90.0, "COMPLEX" },
};
for( size_t i = 0; i < testCases.size(); ++i )
{
const auto& tc = testCases[i];
PCB_BARCODE* barcode = new PCB_BARCODE( board.get() );
barcode->SetText( tc.text );
barcode->SetLayer( F_SilkS );
barcode->SetPosition( tc.position );
barcode->SetWidth( tc.width );
barcode->SetHeight( tc.height );
barcode->SetKind( tc.kind );
barcode->SetErrorCorrection( BARCODE_ECC_T::M );
barcode->Text().SetVisible( tc.withText );
barcode->SetIsKnockout( tc.knockout );
if( tc.angle != 0.0 )
{
barcode->Rotate( tc.position, EDA_ANGLE( tc.angle, DEGREES_T ) );
}
barcode->AssembleBarcode( true, true );
// Check that the polygon center matches the position
BOX2I bbox = barcode->GetPolyShape().BBox();
VECTOR2I actualCenter = bbox.GetCenter();
VECTOR2I expectedCenter = barcode->GetPosition();
BOOST_CHECK_MESSAGE( actualCenter == expectedCenter,
"Test case " << i << " (" << tc.text.ToStdString() << "): "
<< "Expected center (" << expectedCenter.x << ", " << expectedCenter.y << ") "
<< "but got (" << actualCenter.x << ", " << actualCenter.y << ")" );
board->Add( barcode, ADD_MODE::APPEND, true );
}
}
Loading…
Cancel
Save