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-2021 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>
41 #include <layer_ids.h>
42 #include <math/vector2d.h>
43 #include <pgm_base.h>
45 #include <view/view.h>
46 #include <view/view_controls.h>
47 #include <view/wx_view_controls.h>
48 
49 #include <sch_base_frame.h>
50 #include <sch_painter.h>
51 #include <zoom_defines.h>
52 
53 
54 SCH_DRAW_PANEL::SCH_DRAW_PANEL( wxWindow* aParentWindow, wxWindowID aWindowId,
55  const wxPoint& aPosition, const wxSize& aSize,
56  KIGFX::GAL_DISPLAY_OPTIONS& aOptions, GAL_TYPE aGalType )
57  : EDA_DRAW_PANEL_GAL( aParentWindow, aWindowId, aPosition, aSize, aOptions, aGalType )
58 {
59  m_view = new KIGFX::SCH_VIEW( true, dynamic_cast<SCH_BASE_FRAME*>( GetParentEDAFrame() ) );
60  m_view->SetGAL( m_gal );
61 
63 
64  m_painter.reset( new KIGFX::SCH_PAINTER( m_gal ) );
65 
66  COLOR_SETTINGS* cs = nullptr;
67 
68  if( auto frame = dynamic_cast<SCH_BASE_FRAME*>( GetParentEDAFrame() ) )
69  cs = frame->GetColorSettings();
70  else
71  cs = Pgm().GetSettingsManager().GetColorSettings();
72 
73  wxASSERT( cs );
74  m_painter->GetSettings()->LoadColors( cs );
75 
76  m_view->SetPainter( m_painter.get() );
77  // This fixes the zoom in and zoom out limits:
79  m_view->SetMirror( false, false );
80 
81  // Early initialization of the canvas background color,
82  // before any OnPaint event is fired for the canvas using a wrong bg color
83  auto settings = m_painter->GetSettings();
84  m_gal->SetClearColor( settings->GetBackgroundColor() );
85 
88 
90 
91  // View controls is the first in the event handler chain, so the Tool Framework operates
92  // on updated viewport data.
94 
95  SetEvtHandlerEnabled( true );
96  SetFocus();
97  Show( true );
98  Raise();
99  StartDrawing();
100 }
101 
102 
104 {
105 }
106 
107 
109 {
110  GetView()->Clear();
111  GetView()->DisplaySymbol( aSymbol );
112 }
113 
114 
116 {
117  GetView()->Clear();
118 
119  if( aScreen )
120  GetView()->DisplaySheet( aScreen );
121  else
122  GetView()->Cleanup();
123 }
124 
125 
127 {
128  for( LAYER_NUM i = 0; (unsigned) i < sizeof( SCH_LAYER_ORDER ) / sizeof( LAYER_NUM ); ++i )
129  {
130  LAYER_NUM layer = SCH_LAYER_ORDER[i];
131  wxASSERT( layer < KIGFX::VIEW::VIEW_MAX_LAYERS );
132 
133  m_view->SetLayerOrder( layer, i );
134  }
135 }
136 
137 
139 {
140  bool rv = EDA_DRAW_PANEL_GAL::SwitchBackend( aGalType );
143 
144  Refresh();
145 
146  return rv;
147 }
148 
149 
151 {
152  // caching makes no sense for Cairo and other software renderers
154 
155  for( int i = 0; i < KIGFX::VIEW::VIEW_MAX_LAYERS; i++ )
156  m_view->SetLayerTarget( i, target );
157 
158  // Bitmaps are draw on a non cached GAL layer:
160 
161  // Some draw layers need specific settings
164 
167 
170 
171 // m_view->SetLayerTarget( LAYER_SELECTION_SHADOWS, KIGFX::TARGET_NONCACHED );
172 // m_view->SetLayerDisplayOnly( LAYER_SELECTION_SHADOWS ) ;
173 }
174 
175 
177 {
178  return static_cast<KIGFX::SCH_VIEW*>( m_view );
179 }
180 
181 
183 {
184  SCH_BASE_FRAME* frame = dynamic_cast<SCH_BASE_FRAME*>( GetParentEDAFrame() );
185 
186  try
187  {
188  // Check if the current rendering backend can be properly initialized
189  m_view->UpdateItems();
190  }
191  catch( const std::runtime_error& e )
192  {
193  DisplayInfoMessage( frame, e.what() );
194 
195  // Use fallback if one is available
196  if( GAL_FALLBACK != m_backend )
197  {
199 
200  if( frame )
201  frame->ActivateGalCanvas();
202  }
203  }
204 }
205 
206 
207 void SCH_DRAW_PANEL::onPaint( wxPaintEvent& aEvent )
208 {
209  // The first wxPaintEvent can be fired at startup before the GAL engine is fully initialized
210  // (depending on platforms). Do nothing in this case
211  if( !m_gal->IsInitialized() || !m_gal->IsVisible() )
212  return;
213 
214  EDA_DRAW_PANEL_GAL::onPaint( aEvent );
215 }
void SetScaleLimits(double aMaximum, double aMinimum)
Set minimum and maximum values for scale.
Definition: view.h:304
KIGFX::GAL * m_gal
Interface for drawing objects on a 2D-surface.
KIGFX::SCH_VIEW * GetView() const override
Return a pointer to the #VIEW instance used in the panel.
currently selected items overlay
Definition: layer_ids.h:214
void SetClearColor(const COLOR4D &aColor)
KIGFX::WX_VIEW_CONTROLS * m_viewControls
Control for VIEW (moving, zooming, etc.)
void SetLayerOrder(int aLayer, int aRenderingOrder)
Set rendering order of a particular layer.
Definition: view.cpp:642
GAL_TYPE m_backend
Currently used GAL.
This file is part of the common library.
void OnShow() override
Called when the window is shown for the first time.
An implementation of class VIEW_CONTROLS for wxWidgets library.
virtual bool SwitchBackend(GAL_TYPE aGalType)
Switch method of rendering graphics.
void Cleanup()
Definition: sch_view.cpp:64
static constexpr GAL_TYPE GAL_FALLBACK
int LAYER_NUM
This can be replaced with int and removed.
Definition: layer_ids.h:40
void UpdateAllLayersOrder()
Do everything that is needed to apply the rendering order of layers.
Definition: view.cpp:906
void SetMirror(bool aMirrorX, bool aMirrorY)
Control the mirroring of the VIEW.
Definition: view.cpp:542
Define a library symbol object.
Definition: lib_symbol.h:96
KIWAY Kiway & Pgm(), KFCTL_STANDALONE
The global Program "get" accessor.
Definition: single_top.cpp:106
WX_VIEW_CONTROLS class definition.
static constexpr int VIEW_MAX_LAYERS
maximum number of layers that may be shown
Definition: view.h:726
Auxiliary rendering target (noncached)
Definition: definitions.h:49
static const LAYER_NUM SCH_LAYER_ORDER[]
Definition: sch_view.h:45
void SetLayerTarget(int aLayer, RENDER_TARGET aTarget)
Change the rendering target for a particular layer.
Definition: view.h:461
virtual void onPaint(wxPaintEvent &WXUNUSED(aEvent))
void DisplaySheet(SCH_SCREEN *aScreen)
void DisplaySheet(const SCH_SCREEN *aScreen)
Definition: sch_view.cpp:92
void Clear()
Remove all items from the view.
Definition: view.cpp:1107
#define ZOOM_MAX_LIMIT_EESCHEMA
Definition: zoom_defines.h:47
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:449
Contains methods for drawing schematic-specific items.
Definition: sch_painter.h:134
virtual void onPaint(wxPaintEvent &WXUNUSED(aEvent)) override
Called when the window is shown for the first time.
Items that may change while the view stays the same (noncached)
Definition: definitions.h:50
virtual void SetFocus() override
void UpdateItems()
Iterate through the list of items that asked for updating and updates them.
Definition: view.cpp:1412
to handle and draw images bitmaps
Definition: layer_ids.h:218
drawingsheet frame and titleblock
Definition: layer_ids.h:212
void setDefaultLayerDeps()
Set rendering targets & dependencies for layers.
void DisplaySymbol(LIB_SYMBOL *aSymbol)
virtual void ActivateGalCanvas()
Use to start up the GAL drawing canvas.
void StartDrawing()
Begin drawing if it was stopped previously.
void SetWorldUnitLength(double aWorldUnitLength)
Set the unit length.
bool SwitchBackend(GAL_TYPE aGalType) override
Switch method of rendering graphics.
#define ZOOM_MIN_LIMIT_EESCHEMA
Definition: zoom_defines.h:48
SCH_DRAW_PANEL(wxWindow *aParentWindow, wxWindowID aWindowId, const wxPoint &aPosition, const wxSize &aSize, KIGFX::GAL_DISPLAY_OPTIONS &aOptions, GAL_TYPE aGalType=GAL_TYPE_OPENGL)
see class PGM_BASE
Main rendering target (cached)
Definition: definitions.h:48
constexpr double SCH_WORLD_UNIT(1e-7/0.0254)
KIGFX::VIEW * m_view
Stores view settings (scale, center, etc.) and items to be drawn.
general purpose overlay
Definition: layer_ids.h:213
EDA_DRAW_FRAME * GetParentEDAFrame() const
Returns parent EDA_DRAW_FRAME, if available or NULL otherwise.
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...
virtual bool IsVisible() const
Return true if the GAL canvas is visible on the screen.
void SetPainter(PAINTER *aPainter)
Set the painter object used by the view for drawing #VIEW_ITEMS.
Definition: view.h:198
Color settings are a bit different than most of the settings objects in that there can be more than o...
void DisplaySymbol(LIB_SYMBOL *aSymbol)
Definition: sch_view.cpp:121
void SetGAL(GAL *aGal)
Assign a rendering device for the VIEW.
Definition: view.cpp:496
void DisplayInfoMessage(wxWindow *aParent, const wxString &aMessage, const wxString &aExtraInfo)
Display an informational message box with aMessage.
Definition: confirm.cpp:299
A shim class between EDA_DRAW_FRAME and several derived classes: SYMBOL_EDIT_FRAME,...
void setDefaultLayerOrder()
Reassign layer order to the initial settings.
virtual bool IsInitialized() const
Return the initialization status for the canvas.