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 The 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>
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 <dialog_shim.h>
47#include <project_pcb.h>
48
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 ),
56 m_displayOptions( std::move( aOpts ) )
57{
58 SetStealsFocus( false );
59 ShowScrollbars( wxSHOW_SB_NEVER, wxSHOW_SB_NEVER );
60 EnableScrolling( false, false ); // otherwise Zoom Auto disables GAL canvas
61
62 m_userUnits = aUnitsProvider->GetUserUnits();
63
64 m_dummyBoard = std::make_unique<BOARD>();
65 m_dummyBoard->SetUserUnits( m_userUnits );
66 m_dummyBoard->SetBoardUse( BOARD_USE::FPHOLDER );
69
70 Raise();
71 Show( true );
73
74 Bind( wxEVT_SIZE, &FOOTPRINT_PREVIEW_PANEL::onSize, this );
75}
76
77
82
83
85{
86 m_dummyBoard->DetachAllFootprints();
87
90
93
94 GetView()->Clear();
95
96 m_currentFootprint = nullptr;
97 m_otherFootprint = nullptr;
98}
99
100
102{
103 KIGFX::PAINTER* painter = GetView()->GetPainter();
104 auto settings = static_cast<KIGFX::PCB_RENDER_SETTINGS*>( painter->GetSettings() );
105
106 return settings->GetBackgroundColor();
107}
108
109
111{
112 KIGFX::PAINTER* painter = GetView()->GetPainter();
113 auto settings = static_cast<KIGFX::PCB_RENDER_SETTINGS*>( painter->GetSettings() );
114
115 return settings->GetLayerColor( F_Fab );
116}
117
118
119void FOOTPRINT_PREVIEW_PANEL::renderFootprint( std::shared_ptr<FOOTPRINT> aFootprint )
120{
121 m_dummyBoard->Add( aFootprint.get() );
122
123 INSPECTOR_FUNC inspector =
124 [&]( EDA_ITEM* descendant, void* aTestData )
125 {
126 static_cast<PCB_DIMENSION_BASE*>( descendant )->UpdateUnits();
128 };
129
130 aFootprint->Visit( inspector, nullptr, { PCB_DIM_LEADER_T,
135
136 for( PAD* pad : aFootprint->Pads() )
137 pad->SetPinFunction( m_pinFunctions[ pad->GetNumber() ] );
138
139 // Ensure we are not using the high contrast mode to display the selected footprint
140 KIGFX::PAINTER* painter = GetView()->GetPainter();
141 auto settings = static_cast<KIGFX::PCB_RENDER_SETTINGS*>( painter->GetSettings() );
143
144 GetView()->Add( aFootprint.get() );
145 GetView()->SetVisible( aFootprint.get(), true );
146 GetView()->Update( aFootprint.get(), KIGFX::ALL );
147}
148
149
151{
152 bool includeText = m_currentFootprint->TextOnly();
153 BOX2I bbox = m_currentFootprint->GetBoundingBox( includeText );
154
155 if( bbox.GetSize().x > 0 && bbox.GetSize().y > 0 )
156 {
157 // Autozoom
158 GetView()->SetViewport( BOX2D( bbox.GetOrigin(), bbox.GetSize() ) );
159
160 // Add a margin
161 GetView()->SetScale( GetView()->GetScale() * 0.7 );
162
163 Refresh();
164 }
165}
166
167
168void FOOTPRINT_PREVIEW_PANEL::onSize( wxSizeEvent& aEvent )
169{
170 aEvent.Skip();
171
172 if( m_pendingFit && m_currentFootprint && IsShownOnScreen() )
173 {
174 m_pendingFit = false;
175
176 // Defer the fit until after the base class onSize handler has called
177 // GAL::ResizeScreen(), so SetViewport sees the correct screen dimensions.
178 CallAfter( [this]() { fitToCurrentFootprint(); } );
179 }
180}
181
182
184{
185 m_dummyBoard->DetachAllFootprints();
186
189
190 GetView()->Clear();
191
193
194 try
195 {
196 m_currentFootprint.reset( adapter->LoadFootprint( aFPID.GetLibNickname(), aFPID.GetLibItemName(), false ) );
197 }
198 catch( ... )
199 {
200 m_currentFootprint.reset();
201 }
202
204 {
206 m_pendingFit = true;
208 }
209
210 ForceRefresh();
211
212 return m_currentFootprint != nullptr;
213}
214
215
216void FOOTPRINT_PREVIEW_PANEL::DisplayFootprints( std::shared_ptr<FOOTPRINT> aFootprintA,
217 std::shared_ptr<FOOTPRINT> aFootprintB )
218{
219 m_dummyBoard->DetachAllFootprints();
220
223
224 if( m_otherFootprint )
225 GetView()->Remove( m_otherFootprint.get() );
226
227 GetView()->Clear();
228
229 m_currentFootprint = aFootprintA;
230 m_otherFootprint = aFootprintB;
231
233 {
234 wxCHECK( m_otherFootprint, /* void */ );
235
238 m_pendingFit = true;
240 }
241
242 Layout();
243 Show();
244}
245
246
252
253
255 UNITS_PROVIDER* aUnitsProvider )
256{
258 COMMON_SETTINGS* commonSettings = Pgm().GetCommonSettings();
259 std::unique_ptr<GAL_DISPLAY_OPTIONS_IMPL> gal_opts;
260
261 gal_opts = std::make_unique<GAL_DISPLAY_OPTIONS_IMPL>();
262 gal_opts->ReadConfig( *commonSettings, cfg->m_Window, aParent );
263
264 auto galType = static_cast<EDA_DRAW_PANEL_GAL::GAL_TYPE>( commonSettings->m_Graphics.canvas_type );
265 FOOTPRINT_PREVIEW_PANEL* panel = new FOOTPRINT_PREVIEW_PANEL( aKiway, aParent, aUnitsProvider,
266 std::move( gal_opts ), galType );
267
268 panel->UpdateColors();
269
270 const GRID_SETTINGS& gridCfg = cfg->m_Window.grid;
271
272 panel->GetGAL()->SetGridVisibility( gridCfg.show );
273
274 //Bounds checking cannot include number of elements as an index!
275 int gridIdx = std::clamp( gridCfg.last_size_idx, 0, (int) gridCfg.grids.size() - 1 );
277 gridCfg.grids[gridIdx].x );
279 gridCfg.grids[gridIdx].y );
280 panel->GetGAL()->SetGridSize( VECTOR2D( gridSizeX, gridSizeY ) );
281
282 auto painter = static_cast<KIGFX::PCB_PAINTER*>( panel->GetView()->GetPainter() );
283 auto settings = static_cast<KIGFX::PCB_RENDER_SETTINGS*>( painter->GetSettings() );
284 settings->SetHighlight( false );
285 settings->SetNetColorMode( NET_COLOR_MODE::OFF );
286
287 return panel;
288}
constexpr EDA_IU_SCALE pcbIUScale
Definition base_units.h:112
@ FPHOLDER
Definition board.h:315
@ NORMAL
Inactive layers are shown normally (no high-contrast mode)
@ OFF
Net (and netclass) colors are not shown.
BOX2< VECTOR2I > BOX2I
Definition box2.h:922
BOX2< VECTOR2D > BOX2D
Definition box2.h:923
WINDOW_SETTINGS m_Window
constexpr const Vec & GetOrigin() const
Definition box2.h:210
constexpr const SizeVec & GetSize() const
Definition box2.h:206
void ForceRefresh()
Force a redraw.
virtual void Refresh(bool aEraseBackground=true, const wxRect *aRect=nullptr) override
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:100
An interface to the global shared library manager that is schematic-specific and linked to one projec...
FOOTPRINT * LoadFootprint(const wxString &aNickname, const wxString &aName, bool aKeepUUID)
Load a FOOTPRINT having aName from the library given by aNickname.
void renderFootprint(std::shared_ptr< FOOTPRINT > aFootprint)
std::shared_ptr< FOOTPRINT > m_currentFootprint
void DisplayFootprints(std::shared_ptr< FOOTPRINT > aFootprintA, std::shared_ptr< FOOTPRINT > aFootprintB) override
Display a pair of footprints.
std::map< wxString, wxString > m_pinFunctions
void RefreshAll() override
Force the redrawing of all contents.
std::shared_ptr< FOOTPRINT > m_otherFootprint
const KIGFX::COLOR4D & GetBackgroundColor() const override
Get the colors to use in a preview widget to match the preview panel.
std::unique_ptr< KIGFX::GAL_DISPLAY_OPTIONS > m_displayOptions
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)
const KIGFX::COLOR4D & GetForegroundColor() const override
void onSize(wxSizeEvent &aEvent)
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.
A color representation with 4 components: red, green, blue, alpha.
Definition color4d.h:105
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.
PCB specific render settings.
Definition pcb_painter.h:82
const COLOR4D & GetBackgroundColor() const override
Return current background color settings.
HIGH_CONTRAST_MODE m_ContrastModeDisplay
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:638
void SetViewport(const BOX2D &aViewport)
Set the visible area of the VIEW.
Definition view.cpp:610
void Clear()
Remove all items from the view.
Definition view.cpp:1213
void UpdateAllItems(int aUpdateFlags)
Update all items in the view according to the given flags.
Definition view.cpp:1666
PAINTER * GetPainter() const
Return the painter object used by the view for drawing #VIEW_ITEMS.
Definition view.h:229
void SetVisible(VIEW_ITEM *aItem, bool aIsVisible=true)
Set the item visibility.
Definition view.cpp:1736
PROJECT & Prj() const
Return a reference to the PROJECT associated with this KIWAY.
KIWAY_HOLDER(KIWAY *aKiway, HOLDER_TYPE aType)
A minimalistic software bus for communications between various DLLs/DSOs (DSOs) within the same KiCad...
Definition kiway.h:315
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:55
Abstract dimension API.
void UpdateColors()
Update the color settings in the painter and GAL.
PCB_DRAW_PANEL_GAL(wxWindow *aParentWindow, wxWindowID aWindowId, const wxPoint &aPosition, const wxSize &aSize, KIGFX::GAL_DISPLAY_OPTIONS &aOptions, GAL_TYPE aGalType=GAL_TYPE_OPENGL)
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 COMMON_SETTINGS * GetCommonSettings() const
Definition pgm_base.cpp:541
virtual SETTINGS_MANAGER & GetSettingsManager() const
Definition pgm_base.h:130
static FOOTPRINT_LIBRARY_ADAPTER * FootprintLibAdapter(PROJECT *aProject)
T * GetAppSettings(const char *aFilename)
Return a handle to the a given settings by type.
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:90
@ 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)
Convert aTextValue to a double.
@ REPAINT
Item needs to be redrawn.
Definition view_item.h:58
@ ALL
All except INITIAL_ADD.
Definition view_item.h:59
STL namespace.
PGM_BASE & Pgm()
The global program "get" accessor.
see class PGM_BASE
std::vector< GRID > grids
GRID_SETTINGS grid
@ PCB_DIM_ORTHOGONAL_T
class PCB_DIM_ORTHOGONAL, a linear dimension constrained to x/y
Definition typeinfo.h:103
@ PCB_DIM_LEADER_T
class PCB_DIM_LEADER, a leader dimension (graphic item)
Definition typeinfo.h:100
@ PCB_DIM_CENTER_T
class PCB_DIM_CENTER, a center point marking (graphic item)
Definition typeinfo.h:101
@ PCB_DIM_ALIGNED_T
class PCB_DIM_ALIGNED, a linear dimension (graphic item)
Definition typeinfo.h:99
@ PCB_DIM_RADIAL_T
class PCB_DIM_RADIAL, a radius or diameter dimension
Definition typeinfo.h:102
VECTOR2< double > VECTOR2D
Definition vector2d.h:686