KiCad PCB EDA Suite
sch_draw_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) 2014-2019 CERN
5  * @author Maciej Suminski <maciej.suminski@cern.ch>
6  * Copyright (C) 2019 KiCad Developers, see AUTHORS.txt for contributors.
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License
10  * as published by the Free Software Foundation; either version 2
11  * of the License, or (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, you may find one here:
20  * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
21  * or you may search the http://www.gnu.org website for the version 2 license,
22  * or you may write to the Free Software Foundation, Inc.,
23  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
24  */
25 
26 
27 #include <sch_draw_panel.h>
28 
29 #include <wx/debug.h>
30 #include <wx/event.h>
31 #include <wx/gdicmn.h>
32 #include <wx/window.h>
33 #include <wx/windowid.h>
34 #include <memory>
35 #include <stdexcept>
36 
37 #include <confirm.h>
38 #include <eda_draw_frame.h>
39 #include <gal/definitions.h>
42 #include <math/vector2d.h>
43 #include <view/view.h>
44 #include <view/view_controls.h>
45 #include <view/wx_view_controls.h>
46 
47 #include <sch_base_frame.h>
48 #include <sch_painter.h>
49 
50 
51 SCH_DRAW_PANEL::SCH_DRAW_PANEL( wxWindow* aParentWindow, wxWindowID aWindowId,
52  const wxPoint& aPosition, const wxSize& aSize,
53  KIGFX::GAL_DISPLAY_OPTIONS& aOptions, GAL_TYPE aGalType ) :
54  EDA_DRAW_PANEL_GAL( aParentWindow, aWindowId, aPosition, aSize, aOptions, aGalType ),
55  m_parent( aParentWindow )
56 {
57  m_currentCursor = wxCURSOR_ARROW;
58  m_view = new KIGFX::SCH_VIEW( true, dynamic_cast<SCH_BASE_FRAME*>( aParentWindow ) );
59  m_view->SetGAL( m_gal );
60 
62 
63  m_painter.reset( new KIGFX::SCH_PAINTER( m_gal ) );
64 
65  m_view->SetPainter( m_painter.get() );
66  m_view->SetScaleLimits( 50.0, 0.05 ); // This fixes the zoom in and zoom out limits
67  m_view->SetMirror( false, false );
68 
69  // Early initialization of the canvas background color,
70  // before any OnPaint event is fired for the canvas using a wrong bg color
71  auto settings = m_painter->GetSettings();
72  m_gal->SetClearColor( settings->GetBackgroundColor() );
73 
76 
78 
79  // View controls is the first in the event handler chain, so the Tool Framework operates
80  // on updated viewport data.
82 
83  m_viewControls->SetSnapping( true );
84 
85  SetEvtHandlerEnabled( true );
86  SetFocus();
87  Show( true );
88  Raise();
89  StartDrawing();
90 }
91 
92 
94 {
95 }
96 
97 
98 void SCH_DRAW_PANEL::DisplayComponent( const LIB_PART* aComponent )
99 {
100  GetView()->Clear();
101  GetView()->DisplayComponent( const_cast<LIB_PART*>(aComponent) );
102 
103 }
104 
105 
107 {
108  GetView()->Clear();
109 
110  if( aScreen )
111  GetView()->DisplaySheet( const_cast<SCH_SCREEN*>( aScreen ) );
112 }
113 
114 
116 {
117  for( LAYER_NUM i = 0; (unsigned) i < sizeof( SCH_LAYER_ORDER ) / sizeof( LAYER_NUM ); ++i )
118  {
119  LAYER_NUM layer = SCH_LAYER_ORDER[i];
120  wxASSERT( layer < KIGFX::VIEW::VIEW_MAX_LAYERS );
121 
122  m_view->SetLayerOrder( layer, i );
123  }
124 }
125 
126 
128 {
129  VECTOR2D grid_size = m_gal->GetGridSize();
130  bool rv = EDA_DRAW_PANEL_GAL::SwitchBackend( aGalType );
133 
134  // Keep grid size and grid visibility:
135  m_gal->SetGridSize( grid_size );
136  SCH_BASE_FRAME* frame = dynamic_cast<SCH_BASE_FRAME*>( GetParent() );
137 
138  if( frame )
140 
141  Refresh();
142 
143  return rv;
144 }
145 
146 
148 {
149  // caching makes no sense for Cairo and other software renderers
151 
152  for( int i = 0; i < KIGFX::VIEW::VIEW_MAX_LAYERS; i++ )
153  m_view->SetLayerTarget( i, target );
154 
155  // Bitmaps are draw on a non cached GAL layer:
157 
158  // Some draw layers need specific settings
161 
164 
167 
168 // m_view->SetLayerTarget( LAYER_SELECTION_SHADOWS, KIGFX::TARGET_NONCACHED );
169 // m_view->SetLayerDisplayOnly( LAYER_SELECTION_SHADOWS ) ;
170 }
171 
172 
174 {
175  return static_cast<KIGFX::SCH_VIEW*>( m_view );
176 }
177 
178 
180 {
181  SCH_BASE_FRAME* frame = dynamic_cast<SCH_BASE_FRAME*>( GetParent() );
182 
183  try
184  {
185  // Check if the current rendering backend can be properly initialized
186  m_view->UpdateItems();
187  }
188  catch( const std::runtime_error& e )
189  {
190  // Fallback to software renderer
191  DisplayInfoMessage( frame, e.what() );
193 
194  if( frame )
195  frame->ActivateGalCanvas();
196  }
197 }
198 
199 
200 void SCH_DRAW_PANEL::onPaint( wxPaintEvent& aEvent )
201 {
202  // The first wxPaintEvent can be fired at startup before the GAL engine is fully initialized
203  // (depending on platforms). Do nothing in this case
204  if( !m_gal->IsInitialized() || !m_gal->IsVisible() )
205  return;
206 
207  if( m_painter )
208  static_cast<KIGFX::SCH_PAINTER*>( m_painter.get() )->GetSettings()->ImportLegacyColors( nullptr );
209 
210  EDA_DRAW_PANEL_GAL::onPaint( aEvent );
211 }
void DisplaySheet(const SCH_SCREEN *aScreen)
void SetScaleLimits(double aMaximum, double aMinimum)
Function SetScaleLimits() Sets minimum and maximum values for scale.
Definition: view.h:298
KIGFX::GAL * m_gal
Interface for drawing objects on a 2D-surface.
KIGFX::SCH_VIEW * GetView() const override
Function GetView() Returns a pointer to the VIEW instance used in the panel.
void SetClearColor(const COLOR4D &aColor)
KIGFX::WX_VIEW_CONTROLS * m_viewControls
Control for VIEW (moving, zooming, etc.)
void DisplayComponent(LIB_PART *aPart)
Definition: sch_view.cpp:128
void SetLayerOrder(int aLayer, int aRenderingOrder)
Function SetLayerOrder() Sets rendering order of a particular layer.
Definition: view.cpp:664
GAL_TYPE m_backend
Currently used GAL.
This file is part of the common library.
void OnShow() override
to handle and draw images bitmaps
Class WX_VIEW_CONTROLS is a specific implementation of class VIEW_CONTROLS for wxWidgets library.
constexpr double SCH_WORLD_UNIT
Definition: sch_view.h:42
VIEW_CONTROLS class definition.
virtual bool SwitchBackend(GAL_TYPE aGalType)
Function SwitchBackend Switches method of rendering graphics.
virtual bool IsGridVisible() const
virtual void SetSnapping(bool aEnabled)
Function SetSnapping() Enables/disables snapping cursor to grid.
void DisplaySheet(SCH_SHEET *aSheet)
Definition: sch_view.cpp:122
void UpdateAllLayersOrder()
Function UpdateLayerOrder() Does everything that is needed to apply the rendering order of layers.
Definition: view.cpp:927
void SetMirror(bool aMirrorX, bool aMirrorY)
Function SetMirror() Controls the mirroring of the VIEW.
Definition: view.cpp:565
WX_VIEW_CONTROLS class definition.
static constexpr int VIEW_MAX_LAYERS
maximum number of layers that may be shown
Definition: view.h:701
Auxiliary rendering target (noncached)
Definition: definitions.h:49
static const LAYER_NUM SCH_LAYER_ORDER[]
Definition: sch_view.h:44
void SetLayerTarget(int aLayer, RENDER_TARGET aTarget)
Function SetLayerTarget() Changes the rendering target for a particular layer.
Definition: view.h:434
virtual void onPaint(wxPaintEvent &WXUNUSED(aEvent))
void Clear()
Function Clear() Removes all items from the view.
Definition: view.cpp:1111
std::unique_ptr< KIGFX::PAINTER > m_painter
Contains information about how to draw items using GAL.
void SetLayerDisplayOnly(int aLayer, bool aDisplayOnly=true)
Definition: view.h:422
Class SCH_PAINTER Contains methods for drawing schematic-specific items.
Definition: sch_painter.h:118
virtual void onPaint(wxPaintEvent &WXUNUSED(aEvent)) override
Define a library symbol object.
Items that may change while the view stays the same (noncached)
Definition: definitions.h:50
virtual void SetFocus() override
void UpdateItems()
Function UpdateItems() Iterates through the list of items that asked for updating and updates them.
Definition: view.cpp:1421
void setDefaultLayerDeps()
Reassigns layer order to the initial settings.
virtual void ActivateGalCanvas()
Use to start up the GAL drawing canvas.
void SetGridSize(const VECTOR2D &aGridSize)
Set the grid size.
void StartDrawing()
Function StartDrawing() Begins drawing if it was stopped previously.
void SetWorldUnitLength(double aWorldUnitLength)
Set the unit length.
bool SwitchBackend(GAL_TYPE aGalType) override
Function SwitchBackend Switches method of rendering graphics.
virtual void Refresh(bool aEraseBackground=true, const wxRect *aRect=NULL) override
Update the board display after modifying it by a python script (note: it is automatically called by a...
SCH_DRAW_PANEL(wxWindow *aParentWindow, wxWindowID aWindowId, const wxPoint &aPosition, const wxSize &aSize, KIGFX::GAL_DISPLAY_OPTIONS &aOptions, GAL_TYPE aGalType=GAL_TYPE_OPENGL)
int LAYER_NUM
Type LAYER_NUM can be replaced with int and removed.
const VECTOR2D & GetGridSize() const
Returns the grid size.
Board layer functions and definitions.
Main rendering target (cached)
Definition: definitions.h:48
KIGFX::VIEW * m_view
Stores view settings (scale, center, etc.) and items to be drawn.
size_t i
Definition: json11.cpp:649
virtual bool IsVisible() const
Returns true if the GAL canvas is visible on the screen.
currently selected items overlay
void SetGridVisibility(bool aVisibility)
Sets the visibility setting of the grid.
void SetPainter(PAINTER *aPainter)
Function SetPainter() Sets the painter object used by the view for drawing VIEW_ITEMS.
Definition: view.h:189
void SetGAL(GAL *aGal)
Function SetGAL() Assigns a rendering device for the VIEW.
Definition: view.cpp:519
void DisplayInfoMessage(wxWindow *aParent, const wxString &aMessage, const wxString &aExtraInfo)
Display an informational message box with aMessage.
Definition: confirm.cpp:264
A shim class between EDA_DRAW_FRAME and several derived classes: LIB_EDIT_FRAME, LIB_VIEW_FRAME,...
void DisplayComponent(const LIB_PART *aComponent)
void setDefaultLayerOrder()
virtual bool IsInitialized() const
Returns the initalization status for the canvas.