KiCad PCB EDA Suite
Loading...
Searching...
No Matches
test_pcb_render_settings.cpp
Go to the documentation of this file.
1/*
2 * This program source code file is part of KiCad, a free EDA CAD application.
3 *
4 * Copyright The KiCad Developers, see AUTHORS.TXT for contributors.
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <https://www.gnu.org/licenses/>.
18 */
19
24
26#include <pcb_painter.h>
27#include <pcb_point.h>
28#include <layer_ids.h>
30#include <board.h>
31#include <base_units.h>
32#include <pcb_track.h>
33#include <pcb_view.h>
34#include <footprint.h>
35#include <pad.h>
38
40{
41public:
44
46 {
47 m_colorSettings.SetColor( LAYER_POINTS, KIGFX::COLOR4D( 1.0, 0.0, 1.0, 1.0 ) );
48
49 for( int i = 0; i < PCB_LAYER_ID_COUNT; i++ )
50 m_colorSettings.SetColor( i, KIGFX::COLOR4D( 0.5, 0.5, 0.5, 1.0 ) );
51
52 m_settings.LoadColors( &m_colorSettings );
53 }
54};
55
56
57BOOST_FIXTURE_TEST_SUITE( PcbRenderSettings, TEST_PCB_RENDER_SETTINGS_FIXTURE )
58
59
60
68BOOST_AUTO_TEST_CASE( PointLayerColorIsVisible )
69{
70 PCB_POINT point( nullptr );
71 point.SetLayer( F_SilkS );
72
73 int pointLayer = POINT_LAYER_FOR( F_SilkS );
74
75 KIGFX::COLOR4D color = m_settings.GetColor( &point, pointLayer );
76
77 BOOST_CHECK_MESSAGE( color.a > 0.0,
78 "Color for POINT layer should not be transparent (alpha was "
79 + std::to_string( color.a ) + ")" );
80
81 BOOST_CHECK_MESSAGE( color == m_colorSettings.GetColor( LAYER_POINTS ),
82 "POINT layer color should match LAYER_POINTS color" );
83}
84
85
89BOOST_AUTO_TEST_CASE( AllPointLayersMapToLayerPoints )
90{
91 KIGFX::COLOR4D expectedColor = m_colorSettings.GetColor( LAYER_POINTS );
92
93 std::vector<PCB_LAYER_ID> testLayers = { F_Cu, B_Cu, F_SilkS, B_SilkS, Edge_Cuts, User_1 };
94
95 for( PCB_LAYER_ID boardLayer : testLayers )
96 {
97 PCB_POINT point( nullptr );
98 point.SetLayer( boardLayer );
99
100 int pointLayer = POINT_LAYER_FOR( boardLayer );
101 KIGFX::COLOR4D color = m_settings.GetColor( &point, pointLayer );
102
103 BOOST_CHECK_MESSAGE( color == expectedColor,
104 "POINT layer for " + std::to_string( boardLayer )
105 + " should have LAYER_POINTS color" );
106 }
107}
108
109
111
112
113namespace
114{
115
116class RECORDING_GAL : public KIGFX::GAL
117{
118public:
119 explicit RECORDING_GAL( KIGFX::GAL_DISPLAY_OPTIONS& aOptions ) :
120 GAL( aOptions )
121 {
122 }
123
124 int BeginGroup() override
125 {
126 m_openGroup = m_nextGroup++;
127 m_groups[m_openGroup].clear();
128 return m_openGroup;
129 }
130
131 void EndGroup() override { m_openGroup = -1; }
132
133 void DeleteGroup( int aGroupNumber ) override { m_groups.erase( aGroupNumber ); }
134
135 void ClearCache() override { m_groups.clear(); }
136
137 void DrawCircle( const VECTOR2D&, double ) override { record(); }
138
139 void DrawArc( const VECTOR2D&, double, const EDA_ANGLE&, const EDA_ANGLE& ) override { record(); }
140
141 // Mirrors the real backends, which rewrite the colour of every vertex in the group.
142 void ChangeGroupColor( int aGroupNumber, const KIGFX::COLOR4D& aNewColor ) override
143 {
144 auto it = m_groups.find( aGroupNumber );
145
146 if( it != m_groups.end() )
147 it->second.assign( it->second.size(), aNewColor );
148 }
149
150 bool HasGroupWith( const KIGFX::COLOR4D& aFirst, const KIGFX::COLOR4D& aSecond ) const
151 {
152 for( const auto& [groupNumber, colors] : m_groups )
153 {
154 if( std::find( colors.begin(), colors.end(), aFirst ) != colors.end()
155 && std::find( colors.begin(), colors.end(), aSecond ) != colors.end() )
156 {
157 return true;
158 }
159 }
160
161 return false;
162 }
163
164private:
165 void record()
166 {
167 if( m_openGroup >= 0 )
168 m_groups[m_openGroup].push_back( GetIsFill() ? GetFillColor() : GetStrokeColor() );
169 }
170
171 std::map<int, std::vector<KIGFX::COLOR4D>> m_groups;
172 int m_nextGroup = 1;
173 int m_openGroup = -1;
174};
175
176} // namespace
177
178
179BOOST_AUTO_TEST_SUITE( PcbViaHoleRecolour )
180
181
182BOOST_AUTO_TEST_CASE( BlindViaHoleKeepsLayerPairColours )
183{
184 const KIGFX::COLOR4D topColor( 1.0, 0.0, 0.0, 1.0 );
185 const KIGFX::COLOR4D bottomColor( 0.0, 1.0, 0.0, 1.0 );
186
188 RECORDING_GAL gal( options );
189
190 // view must outlive board so board items unregister from a live view at teardown.
191 KIGFX::PCB_VIEW view;
192 KIGFX::PCB_PAINTER painter( &gal, FRAME_PCB_EDITOR );
193 BOARD board;
194
195 KIGFX::PCB_RENDER_SETTINGS* settings = painter.GetSettings();
196 settings->SetLayerColor( F_Cu, topColor );
197 settings->SetLayerColor( In1_Cu, bottomColor );
198 settings->SetLayerColor( LAYER_VIA_HOLES, KIGFX::COLOR4D( 1.0, 0.8, 0.0, 1.0 ) );
199
200 view.SetGAL( &gal );
201 view.SetPainter( &painter );
202
203 board.SetCopperLayerCount( 4 );
204
205 PCB_VIA* via = new PCB_VIA( &board );
206 via->SetPadstackMode( PADSTACK::MODE::NORMAL );
207 via->SetViaType( VIATYPE::BLIND );
208 via->SetLayerPair( F_Cu, In1_Cu );
209 via->SetStart( { 0, 0 } );
210 via->SetEnd( { 0, 0 } );
211 via->SetWidth( PADSTACK::ALL_LAYERS, pcbIUScale.mmToIU( 0.8 ) );
212 via->SetDrill( pcbIUScale.mmToIU( 0.4 ) );
213 board.Add( via );
214
215 view.Add( via );
216 view.UpdateItems();
217
218 BOOST_REQUIRE_MESSAGE( gal.HasGroupWith( topColor, bottomColor ),
219 "painter did not emit a two colour via hole to begin with" );
220
222
223 BOOST_CHECK_MESSAGE( gal.HasGroupWith( topColor, bottomColor ),
224 "recolouring flattened the blind via hole to a single colour" );
225}
226
227
228BOOST_AUTO_TEST_CASE( BackdrilledViaKeepsIndicatorColours )
229{
230 const KIGFX::COLOR4D startColor( 1.0, 0.0, 0.0, 1.0 );
231 const KIGFX::COLOR4D endColor( 0.0, 1.0, 0.0, 1.0 );
232
234 RECORDING_GAL gal( options );
235
236 // view must outlive board so board items unregister from a live view at teardown.
237 KIGFX::PCB_VIEW view;
238 KIGFX::PCB_PAINTER painter( &gal, FRAME_PCB_EDITOR );
239 BOARD board;
240
241 KIGFX::PCB_RENDER_SETTINGS* settings = painter.GetSettings();
242 settings->SetLayerColor( F_Cu, startColor );
243 settings->SetLayerColor( In1_Cu, endColor );
244 settings->SetLayerColor( LAYER_VIA_HOLEWALLS, KIGFX::COLOR4D( 1.0, 0.8, 0.0, 1.0 ) );
245
246 view.SetGAL( &gal );
247 view.SetPainter( &painter );
248
249 board.SetCopperLayerCount( 4 );
250
251 PCB_VIA* via = new PCB_VIA( &board );
252 via->SetPadstackMode( PADSTACK::MODE::NORMAL );
253 via->SetLayerPair( F_Cu, B_Cu );
254 via->SetStart( { 0, 0 } );
255 via->SetEnd( { 0, 0 } );
256 via->SetWidth( PADSTACK::ALL_LAYERS, pcbIUScale.mmToIU( 0.8 ) );
257 via->SetDrill( pcbIUScale.mmToIU( 0.4 ) );
258 via->SetSecondaryDrillSize( std::optional<int>( pcbIUScale.mmToIU( 0.6 ) ) );
259 via->SetSecondaryDrillStartLayer( F_Cu );
260 via->SetSecondaryDrillEndLayer( In1_Cu );
261 board.Add( via );
262
263 view.Add( via );
264 view.UpdateItems();
265
266 BOOST_REQUIRE_MESSAGE( gal.HasGroupWith( startColor, endColor ),
267 "painter did not emit a two colour backdrill indicator to begin with" );
268
270
271 BOOST_CHECK_MESSAGE( gal.HasGroupWith( startColor, endColor ),
272 "recolouring flattened the backdrill indicator to a single colour" );
273}
274
275
276BOOST_AUTO_TEST_CASE( BackdrilledPadKeepsIndicatorColours )
277{
278 const KIGFX::COLOR4D startColor( 1.0, 0.0, 0.0, 1.0 );
279 const KIGFX::COLOR4D endColor( 0.0, 1.0, 0.0, 1.0 );
280
282 RECORDING_GAL gal( options );
283
284 // view must outlive board so board items unregister from a live view at teardown.
285 KIGFX::PCB_VIEW view;
286 KIGFX::PCB_PAINTER painter( &gal, FRAME_PCB_EDITOR );
287 BOARD board;
288
289 KIGFX::PCB_RENDER_SETTINGS* settings = painter.GetSettings();
290 settings->SetLayerColor( F_Cu, startColor );
291 settings->SetLayerColor( In1_Cu, endColor );
292 settings->SetLayerColor( LAYER_PAD_HOLEWALLS, KIGFX::COLOR4D( 1.0, 0.8, 0.0, 1.0 ) );
293
294 view.SetGAL( &gal );
295 view.SetPainter( &painter );
296
297 board.SetCopperLayerCount( 4 );
298
299 FOOTPRINT* footprint = new FOOTPRINT( &board );
300 board.Add( footprint );
301
302 PAD* pad = new PAD( footprint );
303 pad->SetAttribute( PAD_ATTRIB::PTH );
304 pad->SetPosition( { 0, 0 } );
305 pad->SetSizeX( pcbIUScale.mmToIU( 1.6 ) );
306 pad->SetSizeY( pcbIUScale.mmToIU( 1.6 ) );
307 pad->SetDrillSizeX( pcbIUScale.mmToIU( 0.8 ) );
308 pad->SetDrillSizeY( pcbIUScale.mmToIU( 0.8 ) );
309 pad->SetSecondaryDrillSizeX( pcbIUScale.mmToIU( 1.2 ) );
310 pad->SetSecondaryDrillSizeY( pcbIUScale.mmToIU( 1.2 ) );
311 pad->SetSecondaryDrillStartLayer( F_Cu );
312 pad->SetSecondaryDrillEndLayer( In1_Cu );
313 footprint->Add( pad );
314
315 view.Add( pad );
316 view.UpdateItems();
317
318 BOOST_REQUIRE_MESSAGE( gal.HasGroupWith( startColor, endColor ),
319 "painter did not emit a two colour pad backdrill to begin with" );
320
322
323 BOOST_CHECK_MESSAGE( gal.HasGroupWith( startColor, endColor ),
324 "recolouring flattened the pad backdrill indicator to a single colour" );
325}
326
327
constexpr EDA_IU_SCALE pcbIUScale
Definition base_units.h:121
virtual void SetLayer(PCB_LAYER_ID aLayer)
Set the layer this item is on.
Definition board_item.h:374
Information pertinent to a Pcbnew printed circuit board.
Definition board.h:409
void Add(BOARD_ITEM *aItem, ADD_MODE aMode=ADD_MODE::INSERT, bool aSkipConnectivity=false) override
Removes an item from the container.
Definition board.cpp:1497
void SetCopperLayerCount(int aCount)
Definition board.cpp:1137
Color settings are a bit different than most of the settings objects in that there can be more than o...
void Add(BOARD_ITEM *aItem, ADD_MODE aMode=ADD_MODE::INSERT, bool aSkipConnectivity=false) override
Removes an item from the container.
A color representation with 4 components: red, green, blue, alpha.
Definition color4d.h:101
double a
Alpha component.
Definition color4d.h:393
Abstract interface for drawing on a 2D-surface.
Contains methods for drawing PCB-specific items.
virtual PCB_RENDER_SETTINGS * GetSettings() override
Return a pointer to current settings that are going to be used when drawing items.
PCB specific render settings.
Definition pcb_painter.h:84
virtual void Add(VIEW_ITEM *aItem, int aDrawPriority=-1) override
Add a VIEW_ITEM to the view.
Definition pcb_view.cpp:53
void SetLayerColor(int aLayer, const COLOR4D &aColor)
Change the color used to draw a layer.
void SetPainter(PAINTER *aPainter)
Set the painter object used by the view for drawing #VIEW_ITEMS.
Definition view.h:215
void UpdateAllLayersColor()
Apply the new coloring scheme to all layers.
Definition view.cpp:862
void SetGAL(GAL *aGal)
Assign a rendering device for the VIEW.
Definition view.cpp:596
void UpdateItems()
Iterate through the list of items that asked for updating and updates them.
Definition view.cpp:1586
@ NORMAL
Shape is the same on all layers.
Definition padstack.h:170
static constexpr PCB_LAYER_ID ALL_LAYERS
! The layer identifier to use for the single defintion on normal padstacks
Definition padstack.h:179
Definition pad.h:61
A PCB_POINT is a 0-dimensional point that is used to mark a position on a PCB, or more usually a foot...
Definition pcb_point.h:39
@ FRAME_PCB_EDITOR
Definition frame_type.h:38
#define POINT_LAYER_FOR(boardLayer)
Definition layer_ids.h:394
@ LAYER_POINTS
PCB reference/manual snap points visibility.
Definition layer_ids.h:317
@ LAYER_VIA_HOLEWALLS
Definition layer_ids.h:294
@ LAYER_VIA_HOLES
Draw via holes (pad holes do not use this layer).
Definition layer_ids.h:270
@ LAYER_PAD_HOLEWALLS
Definition layer_ids.h:293
PCB_LAYER_ID
A quick note on layer IDs:
Definition layer_ids.h:56
@ Edge_Cuts
Definition layer_ids.h:108
@ B_Cu
Definition layer_ids.h:61
@ F_SilkS
Definition layer_ids.h:96
@ In1_Cu
Definition layer_ids.h:62
@ User_1
Definition layer_ids.h:120
@ B_SilkS
Definition layer_ids.h:97
@ PCB_LAYER_ID_COUNT
Definition layer_ids.h:167
@ F_Cu
Definition layer_ids.h:60
void DrawArc(FOOTPRINT &aFootprint, const VECTOR2I &aCentre, const VECTOR2I &aStart, const EDA_ANGLE &aAngle, int aWidth, PCB_LAYER_ID aLayer)
Draw an arc on a footprint.
@ PTH
Plated through hole pad.
Definition padstack.h:97
BOOST_AUTO_TEST_CASE(HorizontalAlignment)
BOOST_AUTO_TEST_SUITE(CadstarPartParser)
BOOST_AUTO_TEST_SUITE_END()
BOOST_AUTO_TEST_CASE(PointLayerColorIsVisible)
Test that PCB_RENDER_SETTINGS::GetColor returns a visible color for POINT layers.
VECTOR2< double > VECTOR2D
Definition vector2d.h:682