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 <base_units.h>
27#include <board.h>
28#include <footprint.h>
29#include <pcb_dimension.h>
30#include <eda_draw_frame.h>
32#include <fp_lib_table.h>
33#include <kiway.h>
34#include <math/box2.h>
35#include <pcb_painter.h>
36#include <pcb_draw_panel_gal.h>
37#include <pcb_edit_frame.h>
38#include <pgm_base.h>
40#include <view/view.h>
41#include <wx/stattext.h>
42#include <zoom_defines.h>
43
45 std::unique_ptr<KIGFX::GAL_DISPLAY_OPTIONS> aOpts,
46 GAL_TYPE aGalType ) :
47 PCB_DRAW_PANEL_GAL( aParent, -1, wxPoint( 0, 0 ), wxSize( 200, 200 ), *aOpts, aGalType ),
48 KIWAY_HOLDER( aKiway, KIWAY_HOLDER::PANEL ),
49 m_displayOptions( std::move( aOpts ) ),
50 m_currentFootprint( nullptr )
51{
52 SetStealsFocus( false );
53 ShowScrollbars( wxSHOW_SB_NEVER, wxSHOW_SB_NEVER );
54 EnableScrolling( false, false ); // otherwise Zoom Auto disables GAL canvas
55
56 m_dummyBoard = std::make_unique<BOARD>();
57 m_dummyBoard->SetUserUnits( m_userUnits );
60
61 Raise();
62 Show( true );
64}
65
66
68{
70 {
72 GetView()->Clear();
73 m_currentFootprint->SetParent( nullptr );
74 }
75
77 {
79 GetView()->Clear();
80 m_otherFootprint->SetParent( nullptr );
81 }
82}
83
84
86{
87 KIGFX::PAINTER* painter = GetView()->GetPainter();
88 auto settings = static_cast<KIGFX::PCB_RENDER_SETTINGS*>( painter->GetSettings() );
89
90 return settings->GetBackgroundColor();
91}
92
93
95{
96 KIGFX::PAINTER* painter = GetView()->GetPainter();
97 auto settings = static_cast<KIGFX::PCB_RENDER_SETTINGS*>( painter->GetSettings() );
98
99 return settings->GetLayerColor( F_Fab );
100}
101
102
103void FOOTPRINT_PREVIEW_PANEL::renderFootprint( std::shared_ptr<FOOTPRINT> aFootprint )
104{
105 aFootprint->SetParent( m_dummyBoard.get() );
106
107 INSPECTOR_FUNC inspector =
108 [&]( EDA_ITEM* descendant, void* aTestData )
109 {
110 static_cast<PCB_DIMENSION_BASE*>( descendant )->UpdateUnits();
111 return INSPECT_RESULT::CONTINUE;
112 };
113
114 aFootprint->Visit( inspector, nullptr, { PCB_DIM_LEADER_T,
119
120 // Ensure we are not using the high contrast mode to display the selected footprint
121 KIGFX::PAINTER* painter = GetView()->GetPainter();
122 auto settings = static_cast<KIGFX::PCB_RENDER_SETTINGS*>( painter->GetSettings() );
123 settings->m_ContrastModeDisplay = HIGH_CONTRAST_MODE::NORMAL;
124
125 GetView()->Add( aFootprint.get() );
126 GetView()->SetVisible( aFootprint.get(), true );
127 GetView()->Update( aFootprint.get(), KIGFX::ALL );
128}
129
130
132{
133 BOX2I bbox = m_currentFootprint->GetBoundingBox( true, false );
134
135 if( bbox.GetSize().x > 0 && bbox.GetSize().y > 0 )
136 {
137 // Autozoom
138 GetView()->SetViewport( BOX2D( bbox.GetOrigin(), bbox.GetSize() ) );
139
140 // Add a margin
141 GetView()->SetScale( GetView()->GetScale() * 0.7 );
142
143 Refresh();
144 }
145}
146
147
149{
151 {
153 GetView()->Clear();
154 m_currentFootprint->SetParent( nullptr );
155 }
156
157 FP_LIB_TABLE* fptbl = Prj().PcbFootprintLibs();
158
159 try
160 {
161 const FOOTPRINT* fp = fptbl->GetEnumeratedFootprint( aFPID.GetLibNickname(),
162 aFPID.GetLibItemName() );
163
164 if( fp )
165 m_currentFootprint.reset( static_cast<FOOTPRINT*>( fp->Duplicate() ) );
166 else
167 m_currentFootprint.reset();
168 }
169 catch( ... )
170 {
171 m_currentFootprint.reset();
172 }
173
175 {
178 }
179
180 Refresh();
181
182 return m_currentFootprint != nullptr;
183}
184
185
186void FOOTPRINT_PREVIEW_PANEL::DisplayFootprints( std::shared_ptr<FOOTPRINT> aFootprintA,
187 std::shared_ptr<FOOTPRINT> aFootprintB )
188{
190 {
192 m_currentFootprint->SetParent( nullptr );
193
194 wxASSERT( m_otherFootprint );
195
196 GetView()->Remove( m_otherFootprint.get() );
197 m_otherFootprint->SetParent( nullptr );
198
199 GetView()->Clear();
200 }
201
202 m_currentFootprint = aFootprintA;
203 m_otherFootprint = aFootprintB;
204
206 {
207 wxASSERT( m_otherFootprint );
208
212 }
213
214 Layout();
215 Show();
216}
217
218
220{
222 ForceRefresh();
223}
224
225
227{
228 return static_cast<wxWindow*>( this );
229}
230
231
233{
234 PCBNEW_SETTINGS* cfg = Pgm().GetSettingsManager().GetAppSettings<PCBNEW_SETTINGS>();
235
236 if( cfg->m_Window.grid.sizes.empty() )
238
239 // Currently values read from config file are not used because the user cannot
240 // change this config
241 //if( cfg->m_Window.zoom_factors.empty() )
242 {
244 }
245
246 std::unique_ptr<KIGFX::GAL_DISPLAY_OPTIONS> gal_opts;
247
248 gal_opts = std::make_unique<KIGFX::GAL_DISPLAY_OPTIONS>();
249 gal_opts->ReadConfig( *Pgm().GetCommonSettings(), cfg->m_Window, aParent );
250
251 auto canvasType = static_cast<EDA_DRAW_PANEL_GAL::GAL_TYPE>( cfg->m_Graphics.canvas_type );
252 auto panel = new FOOTPRINT_PREVIEW_PANEL( aKiway, aParent, std::move( gal_opts ), canvasType );
253
254 panel->UpdateColors();
255
256 const GRID_SETTINGS& gridCfg = cfg->m_Window.grid;
257
258 panel->GetGAL()->SetGridVisibility( gridCfg.show );
259
260 //Bounds checking cannot include number of elements as an index!
261 int gridIdx = std::max( 0, std::min( gridCfg.last_size_idx, (int) gridCfg.sizes.size() - 1 ) );
262 double gridSize = EDA_UNIT_UTILS::UI::DoubleValueFromString( pcbIUScale, EDA_UNITS::MILS,
263 gridCfg.sizes[ gridIdx ] );
264 panel->GetGAL()->SetGridSize( VECTOR2D( gridSize, gridSize ) );
265
266 auto painter = static_cast<KIGFX::PCB_PAINTER*>( panel->GetView()->GetPainter() );
267 auto settings = static_cast<KIGFX::PCB_RENDER_SETTINGS*>( painter->GetSettings() );
268 settings->SetHighlight( false );
269 settings->SetNetColorMode( NET_COLOR_MODE::OFF );
270
271 return panel;
272}
constexpr EDA_IU_SCALE pcbIUScale
Definition: base_units.h:109
BOX2< VECTOR2D > BOX2D
Definition: box2.h:848
WINDOW_SETTINGS m_Window
Definition: app_settings.h:187
const std::vector< wxString > DefaultGridSizeList() const
const Vec & GetOrigin() const
Definition: box2.h:183
const Vec & GetSize() const
Definition: box2.h:179
void ForceRefresh()
Force a redraw.
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:85
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:90
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.
static FOOTPRINT_PREVIEW_PANEL * New(KIWAY *aKiway, wxWindow *aParent)
FOOTPRINT_PREVIEW_PANEL(KIWAY *aKiway, wxWindow *aParent, std::unique_ptr< KIGFX::GAL_DISPLAY_OPTIONS > aOpts, GAL_TYPE aGalType)
Create a new panel.
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
virtual const KIGFX::COLOR4D & GetForegroundColor() const override
BOARD_ITEM * Duplicate() const override
Create a copy of this BOARD_ITEM.
Definition: footprint.cpp:1757
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:103
Contains all the knowledge about how to draw graphical object onto any particular output device.
Definition: painter.h:58
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:156
PCB specific render settings.
Definition: pcb_painter.h:70
const COLOR4D & GetBackgroundColor() const override
Return current background color settings.
Definition: pcb_painter.h:98
HIGH_CONTRAST_MODE m_ContrastModeDisplay
Definition: pcb_painter.h:123
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:92
virtual void Add(VIEW_ITEM *aItem, int aDrawPriority=-1) override
Add a VIEW_ITEM to the view.
Definition: pcb_view.cpp:58
virtual void Remove(VIEW_ITEM *aItem) override
Remove a VIEW_ITEM from the view.
Definition: pcb_view.cpp:75
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:548
void SetViewport(const BOX2D &aViewport)
Set the visible area of the VIEW.
Definition: view.cpp:520
void Clear()
Remove all items from the view.
Definition: view.cpp:1116
void UpdateAllItems(int aUpdateFlags)
Update all items in the view according to the given flags.
Definition: view.cpp:1501
PAINTER * GetPainter() const
Return the painter object used by the view for drawing #VIEW_ITEMS.
Definition: view.h:213
void SetVisible(VIEW_ITEM *aItem, bool aIsVisible=true)
Set the item visibility.
Definition: view.cpp:1546
A mix in class which holds the location of a wxWindow's KIWAY.
Definition: kiway_holder.h:37
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:279
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
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 FP_LIB_TABLE * PcbFootprintLibs(KIWAY &aKiway)
Return the table of footprint libraries.
Definition: project.cpp:324
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:75
@ F_Fab
Definition: layer_ids.h:120
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:445
@ REPAINT
Item needs to be redrawn.
Definition: view_item.h:52
@ ALL
All except INITIAL_ADD.
Definition: view_item.h:53
STL namespace.
void Refresh()
Update the board display after modifying it by a python script (note: it is automatically called by a...
see class PGM_BASE
KIWAY Kiway & Pgm(), KFCTL_STANDALONE
The global Program "get" accessor.
Definition: single_top.cpp:115
Common grid settings, available to every frame.
Definition: app_settings.h:51
std::vector< wxString > sizes
Definition: app_settings.h:53
GRID_SETTINGS grid
Definition: app_settings.h:99
std::vector< double > zoom_factors
Definition: app_settings.h:96
@ PCB_DIM_ORTHOGONAL_T
class PCB_DIM_ORTHOGONAL, a linear dimension constrained to x/y
Definition: typeinfo.h:101
@ PCB_DIM_LEADER_T
class PCB_DIM_LEADER, a leader dimension (graphic item)
Definition: typeinfo.h:98
@ PCB_DIM_CENTER_T
class PCB_DIM_CENTER, a center point marking (graphic item)
Definition: typeinfo.h:99
@ PCB_DIM_ALIGNED_T
class PCB_DIM_ALIGNED, a linear dimension (graphic item)
Definition: typeinfo.h:97
@ PCB_DIM_RADIAL_T
class PCB_DIM_RADIAL, a radius or diameter dimension
Definition: typeinfo.h:100
VECTOR2< double > VECTOR2D
Definition: vector2d.h:587
#define ZOOM_LIST_PCBNEW
Definition: zoom_defines.h:32