KiCad PCB EDA Suite
Loading...
Searching...
No Matches
footprint_preview_panel.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 (C) 2016-2023 KiCad Developers, see AUTHORS.txt for contributors.
5 * Copyright (C) 2017 Chris Pavlina <[email protected]>
6 * Copyright (C) 2016 Tomasz Wlostowski <[email protected]>
7 *
8 * This program is free software: you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License as published by the
10 * Free Software Foundation, either version 3 of the License, or (at your
11 * option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along
19 * with this program. If not, see <http://www.gnu.org/licenses/>.
20 */
21
22#include <memory>
23#include <utility>
24
25#include "pcbnew_settings.h"
26#include <advanced_config.h>
27#include <base_units.h>
28#include <board.h>
29#include <footprint.h>
30#include <pad.h>
31#include <pcb_dimension.h>
32#include <dpi_scaling_common.h>
33#include <eda_draw_frame.h>
35#include <fp_lib_table.h>
37#include <kiway.h>
38#include <math/box2.h>
39#include <pcb_painter.h>
40#include <pcb_draw_panel_gal.h>
41#include <pcb_edit_frame.h>
42#include <pgm_base.h>
44#include <view/view.h>
45#include <wx/stattext.h>
46#include <zoom_defines.h>
47#include <dialog_shim.h>
48#include <project_pcb.h>
49
51 UNITS_PROVIDER* aUnitsProvider,
52 std::unique_ptr<KIGFX::GAL_DISPLAY_OPTIONS> aOpts,
53 GAL_TYPE aGalType ) :
54 PCB_DRAW_PANEL_GAL( aParent, -1, wxPoint( 0, 0 ), wxSize( 200, 200 ), *aOpts, aGalType ),
55 KIWAY_HOLDER( aKiway, KIWAY_HOLDER::PANEL ),
56 m_displayOptions( std::move( aOpts ) ),
57 m_currentFootprint( nullptr )
58{
59 SetStealsFocus( false );
60 ShowScrollbars( wxSHOW_SB_NEVER, wxSHOW_SB_NEVER );
61 EnableScrolling( false, false ); // otherwise Zoom Auto disables GAL canvas
62
63 m_userUnits = aUnitsProvider->GetUserUnits();
64
65 m_dummyBoard = std::make_unique<BOARD>();
66 m_dummyBoard->SetUserUnits( m_userUnits );
67 m_dummyBoard->SetBoardUse( BOARD_USE::FPHOLDER );
70
71 Raise();
72 Show( true );
74}
75
76
78{
80 {
82 GetView()->Clear();
83 m_currentFootprint->SetParent( nullptr );
84 }
85
87 {
89 GetView()->Clear();
90 m_otherFootprint->SetParent( nullptr );
91 }
92}
93
94
96{
97 KIGFX::PAINTER* painter = GetView()->GetPainter();
98 auto settings = static_cast<KIGFX::PCB_RENDER_SETTINGS*>( painter->GetSettings() );
99
100 return settings->GetBackgroundColor();
101}
102
103
105{
106 KIGFX::PAINTER* painter = GetView()->GetPainter();
107 auto settings = static_cast<KIGFX::PCB_RENDER_SETTINGS*>( painter->GetSettings() );
108
109 return settings->GetLayerColor( F_Fab );
110}
111
112
113void FOOTPRINT_PREVIEW_PANEL::renderFootprint( std::shared_ptr<FOOTPRINT> aFootprint )
114{
115 aFootprint->SetParent( m_dummyBoard.get() );
116
117 INSPECTOR_FUNC inspector =
118 [&]( EDA_ITEM* descendant, void* aTestData )
119 {
120 static_cast<PCB_DIMENSION_BASE*>( descendant )->UpdateUnits();
121 return INSPECT_RESULT::CONTINUE;
122 };
123
124 aFootprint->Visit( inspector, nullptr, { PCB_DIM_LEADER_T,
129
130 for( PAD* pad : aFootprint->Pads() )
131 pad->SetPinFunction( m_pinFunctions[ pad->GetNumber() ] );
132
133 // Ensure we are not using the high contrast mode to display the selected footprint
134 KIGFX::PAINTER* painter = GetView()->GetPainter();
135 auto settings = static_cast<KIGFX::PCB_RENDER_SETTINGS*>( painter->GetSettings() );
136 settings->m_ContrastModeDisplay = HIGH_CONTRAST_MODE::NORMAL;
137
138 GetView()->Add( aFootprint.get() );
139 GetView()->SetVisible( aFootprint.get(), true );
140 GetView()->Update( aFootprint.get(), KIGFX::ALL );
141}
142
143
145{
146 bool includeText = m_currentFootprint->TextOnly();
147 BOX2I bbox = m_currentFootprint->GetBoundingBox( includeText );
148
149 if( bbox.GetSize().x > 0 && bbox.GetSize().y > 0 )
150 {
151 // Autozoom
152 GetView()->SetViewport( BOX2D( bbox.GetOrigin(), bbox.GetSize() ) );
153
154 // Add a margin
155 GetView()->SetScale( GetView()->GetScale() * 0.7 );
156
157 Refresh();
158 }
159}
160
161
163{
165 {
167 GetView()->Clear();
168 m_currentFootprint->SetParent( nullptr );
169 }
170
172
173 try
174 {
175 const FOOTPRINT* fp = fptbl->GetEnumeratedFootprint( aFPID.GetLibNickname(),
176 aFPID.GetLibItemName() );
177
178 if( fp )
179 m_currentFootprint.reset( static_cast<FOOTPRINT*>( fp->Duplicate() ) );
180 else
181 m_currentFootprint.reset();
182 }
183 catch( ... )
184 {
185 m_currentFootprint.reset();
186 }
187
189 {
192 }
193
194 ForceRefresh();
195
196 return m_currentFootprint != nullptr;
197}
198
199
200void FOOTPRINT_PREVIEW_PANEL::DisplayFootprints( std::shared_ptr<FOOTPRINT> aFootprintA,
201 std::shared_ptr<FOOTPRINT> aFootprintB )
202{
204 {
206 m_currentFootprint->SetParent( nullptr );
207
208 wxASSERT( m_otherFootprint );
209
210 GetView()->Remove( m_otherFootprint.get() );
211 m_otherFootprint->SetParent( nullptr );
212
213 GetView()->Clear();
214 }
215
216 m_currentFootprint = aFootprintA;
217 m_otherFootprint = aFootprintB;
218
220 {
221 wxASSERT( m_otherFootprint );
222
226 }
227
228 Layout();
229 Show();
230}
231
232
234{
236 ForceRefresh();
237}
238
239
241{
242 return static_cast<wxWindow*>( this );
243}
244
245
247 UNITS_PROVIDER* aUnitsProvider )
248{
250
251 if( cfg->m_Window.grid.grids.empty() )
253
254 // Currently values read from config file are not used because the user cannot
255 // change this config
256 //if( cfg->m_Window.zoom_factors.empty() )
257 {
260 else
262 }
263
264 std::unique_ptr<GAL_DISPLAY_OPTIONS_IMPL> gal_opts;
265
266 gal_opts = std::make_unique<GAL_DISPLAY_OPTIONS_IMPL>();
267 gal_opts->ReadConfig( *Pgm().GetCommonSettings(), cfg->m_Window, aParent );
268
269 auto galType = static_cast<EDA_DRAW_PANEL_GAL::GAL_TYPE>( cfg->m_Graphics.canvas_type );
270 FOOTPRINT_PREVIEW_PANEL* panel = new FOOTPRINT_PREVIEW_PANEL( aKiway, aParent, aUnitsProvider,
271 std::move( gal_opts ), galType );
272
273 panel->UpdateColors();
274
275 const GRID_SETTINGS& gridCfg = cfg->m_Window.grid;
276
277 panel->GetGAL()->SetGridVisibility( gridCfg.show );
278
279 //Bounds checking cannot include number of elements as an index!
280 int gridIdx = std::clamp( gridCfg.last_size_idx, 0, (int) gridCfg.grids.size() - 1 );
282 gridCfg.grids[gridIdx].x );
284 gridCfg.grids[gridIdx].y );
285 panel->GetGAL()->SetGridSize( VECTOR2D( gridSizeX, gridSizeY ) );
286
287 auto painter = static_cast<KIGFX::PCB_PAINTER*>( panel->GetView()->GetPainter() );
288 auto settings = static_cast<KIGFX::PCB_RENDER_SETTINGS*>( painter->GetSettings() );
289 settings->SetHighlight( false );
290 settings->SetNetColorMode( NET_COLOR_MODE::OFF );
291
292 return panel;
293}
constexpr EDA_IU_SCALE pcbIUScale
Definition: base_units.h:108
@ OFF
Net (and netclass) colors are not shown.
BOX2< VECTOR2D > BOX2D
Definition: box2.h:923
static const ADVANCED_CFG & GetCfg()
Get the singleton instance's config, which is shared by all consumers.
WINDOW_SETTINGS m_Window
Definition: app_settings.h:194
const std::vector< GRID > DefaultGridSizeList() const
constexpr const Vec & GetOrigin() const
Definition: box2.h:210
constexpr const SizeVec & GetSize() const
Definition: box2.h:206
void ForceRefresh()
Force a redraw.
KIGFX::GAL * GetGAL() const
Return a pointer to the GAL instance used in the panel.
void StartDrawing()
Begin drawing if it was stopped previously.
void SetStealsFocus(bool aStealsFocus)
Set whether focus is taken on certain events (mouseover, keys, etc).
A base class for most all the KiCad significant classes used in schematics and boards.
Definition: eda_item.h:89
virtual INSPECT_RESULT Visit(INSPECTOR inspector, void *testData, const std::vector< KICAD_T > &aScanTypes)
May be re-implemented for each derived class in order to handle all the types given by its member dat...
Definition: eda_item.cpp:94
Panel that renders a single footprint via Cairo GAL, meant to be exported through Kiface.
void renderFootprint(std::shared_ptr< FOOTPRINT > aFootprint)
std::shared_ptr< FOOTPRINT > m_currentFootprint
virtual wxWindow * GetWindow() override
Get the underlying wxWindow.
virtual void DisplayFootprints(std::shared_ptr< FOOTPRINT > aFootprintA, std::shared_ptr< FOOTPRINT > aFootprintB) override
Display a pair of footprints.
std::map< wxString, wxString > m_pinFunctions
virtual void RefreshAll() override
Force the redrawing of all contents.
std::shared_ptr< FOOTPRINT > m_otherFootprint
virtual const KIGFX::COLOR4D & GetBackgroundColor() const override
Get the colors to use in a preview widget to match the preview panel.
virtual bool DisplayFootprint(const LIB_ID &aFPID) override
Set the currently displayed footprint.
std::unique_ptr< BOARD > m_dummyBoard
static FOOTPRINT_PREVIEW_PANEL * New(KIWAY *aKiway, wxWindow *aParent, UNITS_PROVIDER *aUnitsProvider)
virtual const KIGFX::COLOR4D & GetForegroundColor() const override
FOOTPRINT_PREVIEW_PANEL(KIWAY *aKiway, wxWindow *aParent, UNITS_PROVIDER *aUnitsProvider, std::unique_ptr< KIGFX::GAL_DISPLAY_OPTIONS > aOpts, GAL_TYPE aGalType)
Create a new panel.
BOARD_ITEM * Duplicate() const override
Create a copy of this BOARD_ITEM.
Definition: footprint.cpp:2484
const FOOTPRINT * GetEnumeratedFootprint(const wxString &aNickname, const wxString &aFootprintName)
A version of FootprintLoad() for use after FootprintEnumerate() for more efficient cache management.
A color representation with 4 components: red, green, blue, alpha.
Definition: color4d.h:104
void SetGridSize(const VECTOR2D &aGridSize)
Set the grid size.
void SetGridVisibility(bool aVisibility)
Set the visibility setting of the grid.
Contains all the knowledge about how to draw graphical object onto any particular output device.
Definition: painter.h:59
virtual RENDER_SETTINGS * GetSettings()=0
Return a pointer to current settings that are going to be used when drawing items.
Contains methods for drawing PCB-specific items.
Definition: pcb_painter.h:180
PCB specific render settings.
Definition: pcb_painter.h:78
const COLOR4D & GetBackgroundColor() const override
Return current background color settings.
Definition: pcb_painter.h:118
HIGH_CONTRAST_MODE m_ContrastModeDisplay
Definition: pcb_painter.h:146
virtual void Update(const VIEW_ITEM *aItem, int aUpdateFlags) const override
For dynamic VIEWs, inform the associated VIEW that the graphical representation of this item has chan...
Definition: pcb_view.cpp:91
virtual void Add(VIEW_ITEM *aItem, int aDrawPriority=-1) override
Add a VIEW_ITEM to the view.
Definition: pcb_view.cpp:57
virtual void Remove(VIEW_ITEM *aItem) override
Remove a VIEW_ITEM from the view.
Definition: pcb_view.cpp:74
const COLOR4D & GetLayerColor(int aLayer) const
Return the color used to draw a layer.
void SetHighlight(bool aEnabled, int aNetcode=-1, bool aMulti=false)
Turns on/off highlighting.
virtual void SetScale(double aScale, VECTOR2D aAnchor={ 0, 0 })
Set the scaling factor, zooming around a given anchor point.
Definition: view.cpp:562
void SetViewport(const BOX2D &aViewport)
Set the visible area of the VIEW.
Definition: view.cpp:534
void Clear()
Remove all items from the view.
Definition: view.cpp:1127
void UpdateAllItems(int aUpdateFlags)
Update all items in the view according to the given flags.
Definition: view.cpp:1545
PAINTER * GetPainter() const
Return the painter object used by the view for drawing #VIEW_ITEMS.
Definition: view.h:217
void SetVisible(VIEW_ITEM *aItem, bool aIsVisible=true)
Set the item visibility.
Definition: view.cpp:1596
A mix in class which holds the location of a wxWindow's KIWAY.
Definition: kiway_holder.h:39
PROJECT & Prj() const
Return a reference to the PROJECT associated with this KIWAY.
A minimalistic software bus for communications between various DLLs/DSOs (DSOs) within the same KiCad...
Definition: kiway.h:284
A logical library item identifier and consists of various portions much like a URI.
Definition: lib_id.h:49
const UTF8 & GetLibItemName() const
Definition: lib_id.h:102
const UTF8 & GetLibNickname() const
Return the logical library name portion of a LIB_ID.
Definition: lib_id.h:87
Definition: pad.h:54
Abstract dimension API.
void UpdateColors()
Update the color settings in the painter and GAL.
virtual KIGFX::PCB_VIEW * GetView() const override
Return a pointer to the #VIEW instance used in the panel.
void SyncLayersVisibility(const BOARD *aBoard)
Update "visibility" property of each layer of a given BOARD.
virtual SETTINGS_MANAGER & GetSettingsManager() const
Definition: pgm_base.h:142
static FP_LIB_TABLE * PcbFootprintLibs(PROJECT *aProject)
Return the table of footprint libraries without Kiway.
Definition: project_pcb.cpp:37
T * GetAppSettings(const wxString &aFilename)
Returns a handle to the a given settings by type If the settings have already been loaded,...
EDA_UNITS GetUserUnits() const
std::function< INSPECT_RESULT(EDA_ITEM *aItem, void *aTestData) > INSPECTOR_FUNC
Used to inspect and possibly collect the (search) results of iterating over a list or tree of KICAD_T...
Definition: eda_item.h:79
bool m_HyperZoom
Slide the zoom steps over for debugging things "up close".
@ F_Fab
Definition: layer_ids.h:119
KICOMMON_API double DoubleValueFromString(const EDA_IU_SCALE &aIuScale, EDA_UNITS aUnits, const wxString &aTextValue, EDA_DATA_TYPE aType=EDA_DATA_TYPE::DISTANCE)
Function DoubleValueFromString converts aTextValue to a double.
Definition: eda_units.cpp:576
@ REPAINT
Item needs to be redrawn.
Definition: view_item.h:57
@ ALL
All except INITIAL_ADD.
Definition: view_item.h:58
STL namespace.
void Refresh()
Update the board display after modifying it by a python script (note: it is automatically called by a...
PGM_BASE & Pgm()
The global Program "get" accessor.
Definition: pgm_base.cpp:1060
see class PGM_BASE
std::vector< GRID > grids
Definition: grid_settings.h:66
GRID_SETTINGS grid
Definition: app_settings.h:81
std::vector< double > zoom_factors
Definition: app_settings.h:78
@ PCB_DIM_ORTHOGONAL_T
class PCB_DIM_ORTHOGONAL, a linear dimension constrained to x/y
Definition: typeinfo.h:105
@ PCB_DIM_LEADER_T
class PCB_DIM_LEADER, a leader dimension (graphic item)
Definition: typeinfo.h:102
@ PCB_DIM_CENTER_T
class PCB_DIM_CENTER, a center point marking (graphic item)
Definition: typeinfo.h:103
@ PCB_DIM_ALIGNED_T
class PCB_DIM_ALIGNED, a linear dimension (graphic item)
Definition: typeinfo.h:101
@ PCB_DIM_RADIAL_T
class PCB_DIM_RADIAL, a radius or diameter dimension
Definition: typeinfo.h:104
VECTOR2< double > VECTOR2D
Definition: vector2d.h:690
#define ZOOM_LIST_PCBNEW
Definition: zoom_defines.h:32
#define ZOOM_LIST_PCBNEW_HYPER
Definition: zoom_defines.h:35