KiCad PCB EDA Suite
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-2020 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 <eda_draw_frame.h>
31#include <fp_lib_table.h>
32#include <kiway.h>
33#include <math/box2.h>
34#include <pcb_painter.h>
35#include <pcb_draw_panel_gal.h>
36#include <pcb_edit_frame.h>
37#include <pgm_base.h>
39#include <view/view.h>
40#include <wx/stattext.h>
41#include <zoom_defines.h>
42
44 std::unique_ptr<KIGFX::GAL_DISPLAY_OPTIONS> aOpts,
45 GAL_TYPE aGalType ) :
46 PCB_DRAW_PANEL_GAL( aParent, -1, wxPoint( 0, 0 ), wxSize( 200, 200 ), *aOpts, aGalType ),
47 KIWAY_HOLDER( aKiway, KIWAY_HOLDER::PANEL ),
48 m_displayOptions( std::move( aOpts ) ),
49 m_currentFootprint( nullptr )
50{
51 SetStealsFocus( false );
52 ShowScrollbars( wxSHOW_SB_NEVER, wxSHOW_SB_NEVER );
53 EnableScrolling( false, false ); // otherwise Zoom Auto disables GAL canvas
54
55 m_dummyBoard = std::make_unique<BOARD>();
58
59 Raise();
60 Show( true );
62}
63
64
66{
68 {
70 GetView()->Clear();
71 m_currentFootprint->SetParent( nullptr );
72 }
73}
74
75
77{
78 KIGFX::PAINTER* painter = GetView()->GetPainter();
79 auto settings = static_cast<KIGFX::PCB_RENDER_SETTINGS*>( painter->GetSettings() );
80
81 return settings->GetBackgroundColor();
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->GetLayerColor( F_Fab );
91}
92
93
94void FOOTPRINT_PREVIEW_PANEL::renderFootprint( std::shared_ptr<FOOTPRINT> aFootprint )
95{
97 {
99 GetView()->Clear();
100 m_currentFootprint->SetParent( nullptr );
101 }
102
103 m_currentFootprint = aFootprint;
104
105 if( !m_currentFootprint )
106 return;
107
108 m_currentFootprint->SetParent( m_dummyBoard.get() );
109
110 // Ensure we are not using the high contrast mode to display the selected footprint
111 KIGFX::PAINTER* painter = GetView()->GetPainter();
112 auto settings = static_cast<KIGFX::PCB_RENDER_SETTINGS*>( painter->GetSettings() );
114
115 GetView()->Add( m_currentFootprint.get() );
116 GetView()->SetVisible( m_currentFootprint.get(), true );
118
119 BOX2I bbox = m_currentFootprint->ViewBBox();
120 bbox.Merge( m_currentFootprint->Value().ViewBBox() );
121 bbox.Merge( m_currentFootprint->Reference().ViewBBox() );
122
123 if( bbox.GetSize().x > 0 && bbox.GetSize().y > 0 )
124 {
125 // Autozoom
126 GetView()->SetViewport( BOX2D( bbox.GetOrigin(), bbox.GetSize() ) );
127
128 // Add a margin
129 GetView()->SetScale( GetView()->GetScale() * 0.7 );
130
131 Refresh();
132 }
133}
134
135
137{
138 FP_LIB_TABLE* fptbl = Prj().PcbFootprintLibs();
139
140 try
141 {
142 const FOOTPRINT* fp = fptbl->GetEnumeratedFootprint( aFPID.GetLibNickname(), aFPID.GetLibItemName() );
143
144 if( fp )
145 m_currentFootprint.reset( static_cast<FOOTPRINT*>( fp->Duplicate() ) );
146 else
147 m_currentFootprint.reset();
148 }
149 catch( ... )
150 {
151 m_currentFootprint.reset();
152 }
153
155 Refresh();
156
157 return m_currentFootprint != nullptr;
158}
159
160
162{
163 return static_cast<wxWindow*>( this );
164}
165
166
168{
169 PCBNEW_SETTINGS* cfg = Pgm().GetSettingsManager().GetAppSettings<PCBNEW_SETTINGS>();
170
171 if( cfg->m_Window.grid.sizes.empty() )
173
174 // Currently values read from config file are not used because the user cannot
175 // change this config
176 //if( cfg->m_Window.zoom_factors.empty() )
177 {
179 }
180
181 std::unique_ptr<KIGFX::GAL_DISPLAY_OPTIONS> gal_opts;
182
183 gal_opts = std::make_unique<KIGFX::GAL_DISPLAY_OPTIONS>();
184 gal_opts->ReadConfig( *Pgm().GetCommonSettings(), cfg->m_Window, aParent );
185
186 auto canvasType = static_cast<EDA_DRAW_PANEL_GAL::GAL_TYPE>( cfg->m_Graphics.canvas_type );
187 auto panel = new FOOTPRINT_PREVIEW_PANEL( aKiway, aParent, std::move( gal_opts ), canvasType );
188
189 panel->UpdateColors();
190
191 const GRID_SETTINGS& gridCfg = cfg->m_Window.grid;
192
193 panel->GetGAL()->SetGridVisibility( gridCfg.show );
194
195 //Bounds checking cannot include number of elements as an index!
196 int gridIdx = std::max( 0, std::min( gridCfg.last_size_idx, (int) gridCfg.sizes.size() - 1 ) );
197 int gridSize = (int) EDA_UNIT_UTILS::UI::ValueFromString( pcbIUScale, EDA_UNITS::MILS, gridCfg.sizes[ gridIdx ] );
198 panel->GetGAL()->SetGridSize( VECTOR2D( gridSize, gridSize ) );
199
200 return panel;
201}
constexpr EDA_IU_SCALE pcbIUScale
Definition: base_units.h:109
@ NORMAL
Inactive layers are shown normally (no high-contrast mode)
BOX2< VECTOR2D > BOX2D
Definition: box2.h:848
WINDOW_SETTINGS m_Window
Definition: app_settings.h:184
virtual const std::vector< wxString > DefaultGridSizeList() const
By default, this returns the list of grids available in PcbNew-based applications.
const Vec & GetOrigin() const
Definition: box2.h:183
const Vec & GetSize() const
Definition: box2.h:179
BOX2< Vec > & Merge(const BOX2< Vec > &aRect)
Modify the position and size of the rectangle in order to contain aRect.
Definition: box2.h:588
virtual void Refresh(bool aEraseBackground=true, const wxRect *aRect=nullptr) override
Update the board display after modifying it by a python script (note: it is automatically called by a...
void StartDrawing()
Begin drawing if it was stopped previously.
void SetStealsFocus(bool aStealsFocus)
Set whether focus is taken on certain events (mouseover, keys, etc).
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.
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 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:1819
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
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.
PCB specific render settings.
Definition: pcb_painter.h:71
const COLOR4D & GetBackgroundColor() const override
Return current background color settings.
Definition: pcb_painter.h:99
HIGH_CONTRAST_MODE m_ContrastModeDisplay
Definition: pcb_painter.h:125
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.
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:1096
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:1512
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:267
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:101
const UTF8 & GetLibNickname() const
Return the logical library name portion of a LIB_ID.
Definition: lib_id.h:87
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:318
@ F_Fab
Definition: layer_ids.h:120
long long int ValueFromString(const EDA_IU_SCALE &aIuScale, EDA_UNITS aUnits, const wxString &aTextValue, EDA_DATA_TYPE aType=EDA_DATA_TYPE::DISTANCE)
Function ValueFromString converts aTextValue in aUnits to internal units used by the application.
Definition: eda_units.cpp:530
@ ALL
All except INITIAL_ADD.
Definition: view_item.h:53
Definition: bitmap.cpp:64
see class PGM_BASE
KIWAY Kiway & Pgm(), KFCTL_STANDALONE
The global Program "get" accessor.
Definition: single_top.cpp:111
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:90
std::vector< double > zoom_factors
Definition: app_settings.h:87
VECTOR2< double > VECTOR2D
Definition: vector2d.h:617
#define ZOOM_LIST_PCBNEW
Definition: zoom_defines.h:32