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 <pavlina.chris@gmail.com>
6  * Copyright (C) 2016 Tomasz Wlostowski <tomasz.wlostowski@cern.ch>
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 <board.h>
27 #include <footprint.h>
28 #include <eda_draw_frame.h>
30 #include <fp_lib_table.h>
31 #include <kiway.h>
32 #include <math/box2.h>
33 #include <pcb_painter.h>
34 #include <pcb_draw_panel_gal.h>
35 #include <pcb_edit_frame.h>
36 #include <pgm_base.h>
38 #include <view/view.h>
39 #include <wx/stattext.h>
40 #include <zoom_defines.h>
41 
43  std::unique_ptr<KIGFX::GAL_DISPLAY_OPTIONS> aOpts,
44  GAL_TYPE aGalType ) :
45  PCB_DRAW_PANEL_GAL( aParent, -1, wxPoint( 0, 0 ), wxSize( 200, 200 ), *aOpts, aGalType ),
46  KIWAY_HOLDER( aKiway, KIWAY_HOLDER::PANEL ),
47  m_displayOptions( std::move( aOpts ) ),
48  m_currentFootprint( nullptr )
49 {
50  SetStealsFocus( false );
51  ShowScrollbars( wxSHOW_SB_NEVER, wxSHOW_SB_NEVER );
52  EnableScrolling( false, false ); // otherwise Zoom Auto disables GAL canvas
53 
54  m_dummyBoard = std::make_unique<BOARD>();
55  UpdateColors();
57 
58  Raise();
59  Show( true );
60  StartDrawing();
61 }
62 
63 
65 {
66  if( m_currentFootprint )
67  {
68  GetView()->Remove( m_currentFootprint.get() );
69  GetView()->Clear();
70  m_currentFootprint->SetParent( nullptr );
71  }
72 }
73 
74 
76 {
77  KIGFX::PAINTER* painter = GetView()->GetPainter();
78  auto settings = static_cast<KIGFX::PCB_RENDER_SETTINGS*>( painter->GetSettings() );
79 
80  return settings->GetBackgroundColor();
81 }
82 
83 
85 {
86  KIGFX::PAINTER* painter = GetView()->GetPainter();
87  auto settings = static_cast<KIGFX::PCB_RENDER_SETTINGS*>( painter->GetSettings() );
88 
89  return settings->GetCursorColor();
90 }
91 
92 
93 void FOOTPRINT_PREVIEW_PANEL::renderFootprint( std::shared_ptr<FOOTPRINT> aFootprint )
94 {
95  if( m_currentFootprint )
96  {
97  GetView()->Remove( m_currentFootprint.get() );
98  GetView()->Clear();
99  m_currentFootprint->SetParent( nullptr );
100  }
101 
102  m_currentFootprint = aFootprint;
103 
104  if( !m_currentFootprint )
105  return;
106 
107  m_currentFootprint->SetParent( m_dummyBoard.get() );
108 
109  // Ensure we are not using the high contrast mode to display the selected footprint
110  KIGFX::PAINTER* painter = GetView()->GetPainter();
111  auto settings = static_cast<KIGFX::PCB_RENDER_SETTINGS*>( painter->GetSettings() );
112  settings->SetContrastModeDisplay( HIGH_CONTRAST_MODE::NORMAL );
113 
114  GetView()->Add( m_currentFootprint.get() );
115  GetView()->SetVisible( m_currentFootprint.get(), true );
117 
118  BOX2I bbox = m_currentFootprint->ViewBBox();
119  bbox.Merge( m_currentFootprint->Value().ViewBBox() );
120  bbox.Merge( m_currentFootprint->Reference().ViewBBox() );
121 
122  if( bbox.GetSize().x > 0 && bbox.GetSize().y > 0 )
123  {
124  // Autozoom
125  GetView()->SetViewport( BOX2D( bbox.GetOrigin(), bbox.GetSize() ) );
126 
127  // Add a margin
128  GetView()->SetScale( GetView()->GetScale() * 0.7 );
129 
130  Refresh();
131  }
132 }
133 
134 
136 {
137  FP_LIB_TABLE* fptbl = Prj().PcbFootprintLibs();
138 
139  try
140  {
141  const FOOTPRINT* fp = fptbl->GetEnumeratedFootprint( aFPID.GetLibNickname(), aFPID.GetLibItemName() );
142 
143  if( fp )
144  m_currentFootprint.reset( static_cast<FOOTPRINT*>( fp->Duplicate() ) );
145  else
146  m_currentFootprint.reset();
147  }
148  catch( ... )
149  {
150  m_currentFootprint.reset();
151  }
152 
154  Refresh();
155 
156  return m_currentFootprint != nullptr;
157 }
158 
159 
161 {
162  return static_cast<wxWindow*>( this );
163 }
164 
165 
167 {
168  PCBNEW_SETTINGS* cfg = Pgm().GetSettingsManager().GetAppSettings<PCBNEW_SETTINGS>();
169 
170  if( cfg->m_Window.grid.sizes.empty() )
171  cfg->m_Window.grid.sizes = cfg->DefaultGridSizeList();
172 
173  // Currently values read from config file are not used because the user cannot
174  // change this config
175  //if( cfg->m_Window.zoom_factors.empty() )
176  {
178  }
179 
180  std::unique_ptr<KIGFX::GAL_DISPLAY_OPTIONS> gal_opts;
181 
182  gal_opts = std::make_unique<KIGFX::GAL_DISPLAY_OPTIONS>();
183  gal_opts->ReadConfig( *Pgm().GetCommonSettings(), cfg->m_Window, aParent );
184 
185  auto canvasType = static_cast<EDA_DRAW_PANEL_GAL::GAL_TYPE>( cfg->m_Graphics.canvas_type );
186  auto panel = new FOOTPRINT_PREVIEW_PANEL( aKiway, aParent, std::move( gal_opts ), canvasType );
187 
188  panel->UpdateColors();
189 
190  const GRID_SETTINGS& gridCfg = cfg->m_Window.grid;
191 
192  panel->GetGAL()->SetGridVisibility( gridCfg.show );
193 
194  //Bounds checking cannot include number of elements as an index!
195  int gridIdx = std::max( 0, std::min( gridCfg.last_size_idx, (int) gridCfg.sizes.size() - 1 ) );
196  int gridSize = (int) ValueFromString( EDA_UNITS::MILS, gridCfg.sizes[ gridIdx ] );
197  panel->GetGAL()->SetGridSize( VECTOR2D( gridSize, gridSize ) );
198 
199  return panel;
200 }
#define ZOOM_LIST_PCBNEW
Definition: zoom_defines.h:32
A mix in class which holds the location of a wxWindow's KIWAY.
Definition: kiway_holder.h:36
const UTF8 & GetLibItemName() const
Definition: lib_id.h:104
void SetViewport(const BOX2D &aViewport)
Set the visible area of the VIEW.
Definition: view.cpp:525
virtual const KIGFX::COLOR4D & GetBackgroundColor() override
Get the colors to use in a preview widget to match the preview panel.
BOX2< VECTOR2D > BOX2D
Definition: box2.h:507
Definition: bitmap.cpp:64
A logical library item identifier and consists of various portions much like a URI.
Definition: lib_id.h:51
virtual bool DisplayFootprint(const LIB_ID &aFPID) override
Set the currently displayed footprint.
All except INITIAL_ADD.
Definition: view_item.h:58
KIWAY Kiway & Pgm(), KFCTL_STANDALONE
The global Program "get" accessor.
Definition: single_top.cpp:106
std::shared_ptr< FOOTPRINT > m_currentFootprint
std::vector< double > zoom_factors
Definition: app_settings.h:86
virtual void Remove(VIEW_ITEM *aItem) override
Remove a VIEW_ITEM from the view.
Definition: pcb_view.cpp:75
PAINTER * GetPainter() const
Return the painter object used by the view for drawing #VIEW_ITEMS.
Definition: view.h:207
Panel that renders a single footprint via Cairo GAL, meant to be exported through Kiface.
void renderFootprint(std::shared_ptr< FOOTPRINT > aFootprint)
Contains all the knowledge about how to draw graphical object onto any particular output device.
Definition: painter.h:57
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 wxWindow * GetWindow() override
Get the underlying wxWindow.
std::vector< wxString > sizes
Definition: app_settings.h:52
VECTOR2< double > VECTOR2D
Definition: vector2d.h:622
std::unique_ptr< BOARD > m_dummyBoard
long long int ValueFromString(EDA_UNITS aUnits, const wxString &aTextValue, EDA_DATA_TYPE aType)
Function ValueFromString converts aTextValue in aUnits to internal units used by the application.
Definition: base_units.cpp:416
void Clear()
Remove all items from the view.
Definition: view.cpp:1091
GRID_SETTINGS grid
Definition: app_settings.h:89
PROJECT & Prj() const
Return a reference to the PROJECT associated with this KIWAY.
Inactive layers are shown normally (no high-contrast mode)
void SetStealsFocus(bool aStealsFocus)
Set whether focus is taken on certain events (mouseover, keys, etc).
static FOOTPRINT_PREVIEW_PANEL * New(KIWAY *aKiway, wxWindow *aParent)
A minimalistic software bus for communications between various DLLs/DSOs (DSOs) within the same KiCad...
Definition: kiway.h:260
const UTF8 & GetLibNickname() const
Return the logical library name portion of a LIB_ID.
Definition: lib_id.h:90
BOARD_ITEM * Duplicate() const override
Create a copy of this BOARD_ITEM.
Definition: footprint.cpp:1615
FOOTPRINT_PREVIEW_PANEL(KIWAY *aKiway, wxWindow *aParent, std::unique_ptr< KIGFX::GAL_DISPLAY_OPTIONS > aOpts, GAL_TYPE aGalType)
Create a new panel.
BOX2< Vec > & Merge(const BOX2< Vec > &aRect)
Modify the position and size of the rectangle in order to contain aRect.
Definition: box2.h:363
void SyncLayersVisibility(const BOARD *aBoard)
Update "visibility" property of each layer of a given BOARD.
virtual KIGFX::PCB_VIEW * GetView() const override
Return a pointer to the #VIEW instance used in the panel.
void StartDrawing()
Begin drawing if it was stopped previously.
void UpdateColors()
Update the color settings in the painter and GAL.
const FOOTPRINT * GetEnumeratedFootprint(const wxString &aNickname, const wxString &aFootprintName)
A version of FootprintLoad() for use after FootprintEnumerate() for more efficient cache management.
virtual FP_LIB_TABLE * PcbFootprintLibs(KIWAY &aKiway)
Return the table of footprint libraries.
Definition: project.cpp:283
virtual void SetScale(double aScale, VECTOR2D aAnchor={ 0, 0 })
Set the scaling factor, zooming around a given anchor point.
Definition: view.cpp:553
virtual RENDER_SETTINGS * GetSettings()=0
Return a pointer to current settings that are going to be used when drawing items.
WINDOW_SETTINGS m_Window
Definition: app_settings.h:181
see class PGM_BASE
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 SetVisible(VIEW_ITEM *aItem, bool aIsVisible=true)
Set the item visibility.
Definition: view.cpp:1449
virtual void Add(VIEW_ITEM *aItem, int aDrawPriority=-1) override
Add a VIEW_ITEM to the view.
Definition: pcb_view.cpp:58
const Vec & GetSize() const
Definition: box2.h:172
const Vec & GetOrigin() const
Definition: box2.h:176
virtual const KIGFX::COLOR4D & GetForegroundColor() override
virtual const std::vector< wxString > DefaultGridSizeList() const
By default, this returns the list of grids available in PcbNew-based applications.
Common grid settings, available to every frame.
Definition: app_settings.h:49
A color representation with 4 components: red, green, blue, alpha.
Definition: color4d.h:103