KiCad PCB EDA Suite
draw_panel_gal.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) 2013-2017 CERN
5  * Copyright (C) 2013-2017 KiCad Developers, see AUTHORS.txt for contributors.
6  * @author Tomasz Wlostowski <tomasz.wlostowski@cern.ch>
7  * @author Maciej Suminski <maciej.suminski@cern.ch>
8  *
9  * This program is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU General Public License
11  * as published by the Free Software Foundation; either version 2
12  * of the License, or (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, you may find one here:
21  * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
22  * or you may search the http://www.gnu.org website for the version 2 license,
23  * or you may write to the Free Software Foundation, Inc.,
24  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
25  */
26 
27 #include <draw_frame.h>
28 #include <kiface_i.h>
29 #include <confirm.h>
30 
31 #include <class_draw_panel_gal.h>
32 #include <view/view.h>
33 #include <view/wx_view_controls.h>
34 #include <painter.h>
35 
37 #include <gal/opengl/opengl_gal.h>
38 #include <gal/cairo/cairo_gal.h>
39 
40 #include <tool/tool_dispatcher.h>
41 #include <tool/tool_manager.h>
42 
43 #ifdef __WXDEBUG__
44 #include <profile.h>
45 #endif /* PROFILE */
46 
47 
48 EDA_DRAW_PANEL_GAL::EDA_DRAW_PANEL_GAL( wxWindow* aParentWindow, wxWindowID aWindowId,
49  const wxPoint& aPosition, const wxSize& aSize,
50  KIGFX::GAL_DISPLAY_OPTIONS& aOptions, GAL_TYPE aGalType ) :
51  wxScrolledCanvas( aParentWindow, aWindowId, aPosition, aSize ), m_options( aOptions )
52 {
53  m_parent = aParentWindow;
54  m_edaFrame = dynamic_cast<EDA_DRAW_FRAME*>( aParentWindow );
55  m_gal = NULL;
57  m_view = NULL;
58  m_painter = NULL;
59  m_eventDispatcher = NULL;
60  m_lostFocus = false;
61  m_stealsFocus = true;
62 
63 #ifdef __WXMAC__
64  m_defaultCursor = m_currentCursor = wxCURSOR_CROSS;
65 #else
66  m_defaultCursor = m_currentCursor = wxCURSOR_ARROW;
67 #endif
68 
69  SetLayoutDirection( wxLayout_LeftToRight );
70 
71  SwitchBackend( aGalType );
72  SetBackgroundStyle( wxBG_STYLE_CUSTOM );
73 
74  ShowScrollbars( wxSHOW_SB_ALWAYS, wxSHOW_SB_ALWAYS );
75  EnableScrolling( false, false ); // otherwise Zoom Auto disables GAL canvas
76 
77  Connect( wxEVT_SIZE, wxSizeEventHandler( EDA_DRAW_PANEL_GAL::onSize ), NULL, this );
78  Connect( wxEVT_ENTER_WINDOW, wxEventHandler( EDA_DRAW_PANEL_GAL::onEnter ), NULL, this );
79  Connect( wxEVT_KILL_FOCUS, wxFocusEventHandler( EDA_DRAW_PANEL_GAL::onLostFocus ), NULL, this );
80 
81  const wxEventType events[] =
82  {
83  // Binding both EVT_CHAR and EVT_CHAR_HOOK ensures that all key events,
84  // especially special key like arrow keys, are handled by the GAL event dispatcher,
85  // and not sent to GUI without filtering, because they have a default action (scroll)
86  // that must not be called.
87  wxEVT_LEFT_UP, wxEVT_LEFT_DOWN, wxEVT_LEFT_DCLICK,
88  wxEVT_RIGHT_UP, wxEVT_RIGHT_DOWN, wxEVT_RIGHT_DCLICK,
89  wxEVT_MIDDLE_UP, wxEVT_MIDDLE_DOWN, wxEVT_MIDDLE_DCLICK,
90  wxEVT_MOTION, wxEVT_MOUSEWHEEL, wxEVT_CHAR, wxEVT_CHAR_HOOK,
91 #if wxCHECK_VERSION( 3, 1, 0 ) || defined( USE_OSX_MAGNIFY_EVENT )
92  wxEVT_MAGNIFY,
93 #endif
95  };
96 
97  for( wxEventType eventType : events )
98  {
99  Connect( eventType, wxEventHandler( EDA_DRAW_PANEL_GAL::onEvent ),
100  NULL, m_eventDispatcher );
101  }
102 
103  m_pendingRefresh = false;
104  m_drawing = false;
105  m_drawingEnabled = false;
106 
107  // Set up timer that prevents too frequent redraw commands
108  m_refreshTimer.SetOwner( this );
109  Connect( m_refreshTimer.GetId(), wxEVT_TIMER,
110  wxTimerEventHandler( EDA_DRAW_PANEL_GAL::onRefreshTimer ), NULL, this );
111 
112  // Set up timer to execute OnShow() method when the window appears on the screen
113  m_onShowTimer.SetOwner( this );
114  Connect( m_onShowTimer.GetId(), wxEVT_TIMER,
115  wxTimerEventHandler( EDA_DRAW_PANEL_GAL::onShowTimer ), NULL, this );
116  m_onShowTimer.Start( 10 );
117 }
118 
119 
121 {
122  StopDrawing();
123 
124  wxASSERT( !m_drawing );
125 
126  delete m_viewControls;
127  delete m_view;
128  delete m_gal;
129 }
130 
131 
133 {
134 // Windows has a strange manner on bringing up and activating windows
135 // containing a GAL canvas just after moving the mouse cursor into its area.
136 // Feel free to uncomment or extend the following #ifdef if you experience
137 // similar problems on your platform.
138 #ifdef __WINDOWS__
139  if( !GetParent()->IsDescendant( wxWindow::FindFocus() ) )
140  return;
141 #endif
142 
143  wxScrolledCanvas::SetFocus();
144  m_lostFocus = false;
145 }
146 
147 
148 void EDA_DRAW_PANEL_GAL::onPaint( wxPaintEvent& WXUNUSED( aEvent ) )
149 {
150  m_pendingRefresh = false;
151 
152  if( m_drawing )
153  return;
154 
155 #ifdef __WXDEBUG__
156  PROF_COUNTER totalRealTime;
157 #endif /* PROFILE */
158 
159  wxASSERT( m_painter );
160 
161  m_drawing = true;
162  KIGFX::RENDER_SETTINGS* settings = static_cast<KIGFX::RENDER_SETTINGS*>( m_painter->GetSettings() );
163 
165 
166  try
167  {
168  m_view->UpdateItems();
169 
170  m_gal->BeginDrawing();
171  m_gal->SetClearColor( settings->GetBackgroundColor() );
173  m_gal->ClearScreen( );
174 
175  if( m_view->IsDirty() )
176  {
177  m_view->ClearTargets();
178 
179  // Grid has to be redrawn only when the NONCACHED target is redrawn
181  m_gal->DrawGrid();
182 
183  m_view->Redraw();
184  }
185 
187  m_gal->EndDrawing();
188  }
189  catch( std::runtime_error& err )
190  {
191  constexpr auto GAL_FALLBACK = GAL_TYPE_CAIRO;
192 
193  if( m_edaFrame )
194  {
195  bool use_gal = m_edaFrame->SwitchCanvas( GAL_FALLBACK );
196  m_edaFrame->UseGalCanvas( use_gal );
197  }
198  else
199  {
201  }
202 
203  DisplayError( m_parent, wxString( err.what() ) );
204  }
205 
206 #ifdef __WXDEBUG__
207  totalRealTime.Stop();
208  wxLogTrace( "GAL_PROFILE", "EDA_DRAW_PANEL_GAL::onPaint(): %.1f ms", totalRealTime.msecs() );
209 #endif /* PROFILE */
210 
211  m_lastRefresh = wxGetLocalTimeMillis();
212  m_drawing = false;
213 }
214 
215 
216 void EDA_DRAW_PANEL_GAL::onSize( wxSizeEvent& aEvent )
217 {
218  wxSize clientSize = GetClientSize();
219  m_gal->ResizeScreen( clientSize.x, clientSize.y );
222 }
223 
224 
225 void EDA_DRAW_PANEL_GAL::Refresh( bool aEraseBackground, const wxRect* aRect )
226 {
227  if( m_pendingRefresh )
228  return;
229 
230  m_pendingRefresh = true;
231 
232 #ifdef __WXMAC__
233  // Timers on OS X may have a high latency (seen up to 500ms and more) which
234  // makes repaints jerky. No negative impact seen without throttling, so just
235  // do an unconditional refresh for OS X.
236  ForceRefresh();
237 #else
238  wxLongLong t = wxGetLocalTimeMillis();
239  wxLongLong delta = t - m_lastRefresh;
240 
241  if( delta >= MinRefreshPeriod )
242  {
243  ForceRefresh();
244  }
245  else
246  {
247  // One shot timer
248  m_refreshTimer.Start( ( MinRefreshPeriod - delta ).ToLong(), true );
249  }
250 #endif
251 }
252 
253 
255 {
256  wxPaintEvent redrawEvent;
257  wxPostEvent( this, redrawEvent );
258 }
259 
260 
262 {
263  m_eventDispatcher = aEventDispatcher;
264  const wxEventType eventTypes[] = { wxEVT_TOOL };
265 
266  if( m_eventDispatcher )
267  {
268  for( wxEventType type : eventTypes )
269  {
270  m_parent->Connect( type, wxCommandEventHandler( TOOL_DISPATCHER::DispatchWxCommand ),
271  NULL, m_eventDispatcher );
272  }
273  }
274  else
275  {
276  for( wxEventType type : eventTypes )
277  {
278  // While loop is used to be sure that all event handlers are removed.
279  while( m_parent->Disconnect( type,
280  wxCommandEventHandler( TOOL_DISPATCHER::DispatchWxCommand ),
281  NULL, m_eventDispatcher ) );
282  }
283  }
284 }
285 
286 
288 {
289  // Start querying GAL if it is ready
290  m_refreshTimer.StartOnce( 100 );
291 }
292 
293 
295 {
296  m_drawingEnabled = false;
297  Disconnect( wxEVT_PAINT, wxPaintEventHandler( EDA_DRAW_PANEL_GAL::onPaint ), NULL, this );
298  m_pendingRefresh = false;
299  m_refreshTimer.Stop();
300 }
301 
302 
304 {
305  // Set display settings for high contrast mode
307 
308  SetTopLayer( aLayer );
309 
310  rSettings->ClearActiveLayers();
311  rSettings->SetActiveLayer( aLayer );
312 
314 }
315 
316 
318 {
320  m_view->SetTopLayer( aLayer );
322 }
323 
324 
326 {
327  double zoomFactor = m_gal->GetWorldScale() / m_gal->GetZoomFactor();
328  return ( 1.0 / ( zoomFactor * m_view->GetScale() ) );
329 }
330 
331 
333 {
334  // Do not do anything if the currently used GAL is correct
335  if( aGalType == m_backend && m_gal != NULL )
336  return true;
337 
338  bool result = true; // assume everything will be fine
339 
340  // Prevent refreshing canvas during backend switch
341  StopDrawing();
342 
343  KIGFX::GAL* new_gal = NULL;
344 
345  try
346  {
347  switch( aGalType )
348  {
349  case GAL_TYPE_OPENGL:
350  new_gal = new KIGFX::OPENGL_GAL( m_options, this, this, this );
351  break;
352 
353  case GAL_TYPE_CAIRO:
354  new_gal = new KIGFX::CAIRO_GAL( m_options, this, this, this );
355  break;
356 
357  default:
358  assert( false );
359  // warn about unhandled GAL canvas type, but continue with the fallback option
360 
361  case GAL_TYPE_NONE:
362  // KIGFX::GAL is a stub - it actually does cannot display anything,
363  // but prevents code relying on GAL canvas existence from crashing
364  new_gal = new KIGFX::GAL( m_options );
365  break;
366  }
367  }
368  catch( std::runtime_error& err )
369  {
370  // Create a dummy GAL
371  new_gal = new KIGFX::GAL( m_options );
372  aGalType = GAL_TYPE_NONE;
373  DisplayError( m_parent, wxString( err.what() ) );
374  result = false;
375  }
376 
377  // trigger update of the gal options in case they differ
378  // from the defaults
380 
381  assert( new_gal );
382  delete m_gal;
383  m_gal = new_gal;
384 
385  wxSize size = GetClientSize();
386  m_gal->ResizeScreen( size.GetX(), size.GetY() );
387 
388  if( m_painter )
389  m_painter->SetGAL( m_gal );
390 
391  if( m_view )
392  {
393  m_view->SetGAL( m_gal );
394  // Note: OpenGL requires reverse draw order when draw priority is enabled
395  m_view->ReverseDrawOrder( aGalType == GAL_TYPE_OPENGL );
396  }
397 
398  m_backend = aGalType;
399 
400  return result;
401 }
402 
403 
404 void EDA_DRAW_PANEL_GAL::onEvent( wxEvent& aEvent )
405 {
406  if( m_lostFocus && m_stealsFocus )
407  SetFocus();
408 
409  if( !m_eventDispatcher )
410  aEvent.Skip();
411  else
413 
414  Refresh();
415 }
416 
417 
418 void EDA_DRAW_PANEL_GAL::onEnter( wxEvent& aEvent )
419 {
420  // Getting focus is necessary in order to receive key events properly
421  if( m_stealsFocus )
422  SetFocus();
423 
424  aEvent.Skip();
425 }
426 
427 
428 void EDA_DRAW_PANEL_GAL::onLostFocus( wxFocusEvent& aEvent )
429 {
430  m_lostFocus = true;
431 
432  aEvent.Skip();
433 }
434 
435 
436 void EDA_DRAW_PANEL_GAL::onRefreshTimer( wxTimerEvent& aEvent )
437 {
438  if( !m_drawingEnabled )
439  {
440  if( m_gal && m_gal->IsInitialized() )
441  {
442  m_drawing = false;
443  m_pendingRefresh = true;
444  Connect( wxEVT_PAINT, wxPaintEventHandler( EDA_DRAW_PANEL_GAL::onPaint ), NULL, this );
445  m_drawingEnabled = true;
446  }
447  else
448  {
449  // Try again soon
450  m_refreshTimer.StartOnce( 100 );
451  return;
452  }
453  }
454 
455  wxPaintEvent redrawEvent;
456  wxPostEvent( this, redrawEvent );
457 }
458 
459 
460 void EDA_DRAW_PANEL_GAL::onShowTimer( wxTimerEvent& aEvent )
461 {
462  if( m_gal && m_gal->IsVisible() )
463  {
464  m_onShowTimer.Stop();
465  OnShow();
466  }
467 }
void Stop()
save the time when this function was called, and set the counter stane to stop
Definition: profile.h:82
bool m_pendingRefresh
Is there a redraw event requested?
wxTimer m_refreshTimer
Timer responsible for preventing too frequent refresh.
KIGFX::GAL * m_gal
Interface for drawing objects on a 2D-surface.
void SetClearColor(const COLOR4D &aColor)
virtual void SetHighContrastLayer(int aLayer)
Function SetHighContrastLayer Takes care of display settings for the given layer to be displayed in h...
void ForceRefresh()
Function ForceRefresh() Forces a redraw.
KIGFX::WX_VIEW_CONTROLS * m_viewControls
Control for VIEW (moving, zooming, etc.)
void ReverseDrawOrder(bool aFlag)
Function ReverseDrawOrder() Only takes effect if UseDrawPriority is true.
Definition: view.h:670
GAL_TYPE m_backend
Currently used GAL.
virtual void SetTopLayer(int aLayer, bool aEnabled=true)
Function SetTopLayer() Sets given layer to be displayed on the top or sets back the default order of ...
Definition: view.cpp:803
Class RENDER_SETTINGS Contains all the knowledge about how graphical objects are drawn on any output ...
Definition: painter.h:56
static const wxEventType EVT_REFRESH_MOUSE
Event that forces mouse move event in the dispatcher (eg.
This file is part of the common library.
double GetWorldScale() const
Get the world scale.
double GetLegacyZoom() const
Function GetLegacyZoom() Returns current view scale converted to zoom value used by the legacy canvas...
virtual bool SwitchBackend(GAL_TYPE aGalType)
Function SwitchBackend Switches method of rendering graphics.
KIGFX::GAL_DISPLAY_OPTIONS & m_options
virtual void OnShow()
Function OnShow() Called when the window is shown for the first time.
Class OpenGL_GAL is the OpenGL implementation of the Graphics Abstraction Layer.
Definition: opengl_gal.h:64
void onLostFocus(wxFocusEvent &aEvent)
int m_currentCursor
Current mouse cursor shape id.
virtual bool IsVisible() const
Returns true if the GAL canvas is visible on the screen.
void UpdateAllLayersOrder()
Function UpdateLayerOrder() Does everything that is needed to apply the rendering order of layers...
Definition: view.cpp:870
void onShowTimer(wxTimerEvent &aEvent)
Class EDA_DRAW_FRAME is the base class for create windows for drawing purpose.
Definition: draw_frame.h:55
bool IsDirty() const
Function IsDirty() Returns true if any of the VIEW layers needs to be refreshened.
Definition: view.h:548
virtual bool IsInitialized() const
Returns the initalization status for the canvas.
The class PROF_COUNTER is a small class to help profiling.
Definition: profile.h:45
int m_defaultCursor
The default mouse cursor shape id.
const COLOR4D & GetLayerColor(int aLayer) const
Function GetLayerColor Returns the color used to draw a layer.
Definition: painter.h:206
virtual void UseGalCanvas(bool aEnable)
Function UseGalCanvas used to switch between standard and GAL-based canvas.
static const int delta[8][2]
Definition: solve.cpp:112
WX_VIEW_CONTROLS class definition.
void SetActiveLayer(int aLayerId, bool aEnabled=true)
Function SetActiveLayer Sets the specified layer as active - it means that it can be drawn in a speci...
Definition: painter.h:76
Auxiliary rendering target (noncached)
Definition: definitions.h:42
void onRefreshTimer(wxTimerEvent &aEvent)
void UpdateScrollbars()
Adjusts the scrollbars position to match the current viewport.
bool m_drawing
True if GAL is currently redrawing the view.
wxWindow * m_parent
Pointer to the parent window.
void onPaint(wxPaintEvent &WXUNUSED(aEvent))
void ClearActiveLayers()
Function ClearActiveLayers Clears the list of active layers.
Definition: painter.h:98
virtual void Redraw()
Function Redraw() Immediately redraws the whole view.
Definition: view.cpp:1085
void MarkTargetDirty(int aTarget)
Function MarkTargetDirty() Sets or clears target &#39;dirty&#39; flag.
Definition: view.h:577
static const int MinRefreshPeriod
60 FPS.
std::unique_ptr< KIGFX::PAINTER > m_painter
Contains information about how to draw items using GAL.
virtual void DispatchWxEvent(wxEvent &aEvent)
Function DispatchWxEvent() Processes wxEvents (mostly UI events), translates them to TOOL_EVENTs...
virtual void SetTopLayer(int aLayer)
Function SetTopLayer Moves the selected layer to the top, so it is displayed above all others...
bool IsTargetDirty(int aTarget) const
Function IsTargetDirty() Returns true if any of layers belonging to the target or the target itself s...
Definition: view.h:565
virtual void DrawCursor(const VECTOR2D &aCursorPosition)
Draw the cursor.
EDA_DRAW_PANEL_GAL(wxWindow *aParentWindow, wxWindowID aWindowId, const wxPoint &aPosition, const wxSize &aSize, KIGFX::GAL_DISPLAY_OPTIONS &aOptions, GAL_TYPE aGalType=GAL_TYPE_OPENGL)
bool m_lostFocus
Flag to indicate that focus should be regained on the next mouse event.
virtual void SetFocus() override
virtual bool SwitchCanvas(EDA_DRAW_PANEL_GAL::GAL_TYPE aCanvasType)
Changes the current rendering backend.
void UpdateItems()
Function UpdateItems() Iterates through the list of items that asked for updating and updates them...
Definition: view.cpp:1354
EDA_DRAW_FRAME * m_edaFrame
Parent EDA_DRAW_FRAME (if available)
PAINTER * GetPainter() const
Function GetPainter() Returns the painter object used by the view for drawing VIEW_ITEMS.
Definition: view.h:196
void SetCursorColor(const COLOR4D &aCursorColor)
Set the cursor color.
string & err
Definition: json11.cpp:598
virtual void ResizeScreen(int aWidth, int aHeight)
Resizes the canvas.
void onSize(wxSizeEvent &aEvent)
void StartDrawing()
Function StartDrawing() Begins drawing if it was stopped previously.
void Refresh(bool aEraseBackground=true, const wxRect *aRect=NULL) override
Update the board display after modifying it bu a python script (note: it is automatically called by a...
void ClearTopLayers()
Function ClearTopLayers() Removes all layers from the on-the-top set (they are no longer displayed ov...
Definition: view.cpp:855
Class TOOL_DISPATCHER.
virtual void DrawGrid()
>
virtual void ClearScreen()
Clear the screen.
TOOL_DISPATCHER * m_eventDispatcher
Processes and forwards events to tools.
double msecs() const
Definition: profile.h:124
virtual RENDER_SETTINGS * GetSettings()=0
Function GetSettings Returns pointer to current settings that are going to be used when drawing items...
wxTimer m_onShowTimer
Timer used to execute OnShow() when the window finally appears on the screen.
Main rendering target (cached)
Definition: definitions.h:41
KIGFX::VIEW * m_view
Stores view settings (scale, center, etc.) and items to be drawn.
virtual void DispatchWxCommand(wxCommandEvent &aEvent)
Function DispatchWxCommand() Processes wxCommands (mostly menu related events) and runs appropriate a...
void SetEventDispatcher(TOOL_DISPATCHER *aEventDispatcher)
Function SetEventDispatcher() Sets a dispatcher that processes events and forwards them to tools...
bool m_drawingEnabled
Flag that determines if VIEW may use GAL for redrawing the screen.
VECTOR2D GetCursorPosition(bool aSnappingEnabled) const override
Returns the current cursor position in world coordinates.
void ClearTargets()
Function ClearTargets() Clears targets that are marked as dirty.
Definition: view.cpp:1066
double GetZoomFactor() const
Get the zoom factor.
double GetScale() const
Function GetScale()
Definition: view.h:265
wxLongLong m_lastRefresh
Last timestamp when the panel was refreshed.
virtual void BeginDrawing()
Begin the drawing, needs to be called for every new frame.
void onEnter(wxEvent &aEvent)
void onEvent(wxEvent &aEvent)
void UpdateAllLayersColor()
Function UpdateAllLayersColor() Applies the new coloring scheme to all layers.
Definition: view.cpp:743
bool m_stealsFocus
Flag to indicate whether the panel should take focus at certain times (when moused over...
void SetGAL(GAL *aGal)
Function SetGAL() Assigns a rendering device for the VIEW.
Definition: view.cpp:507
virtual void EndDrawing()
End the drawing, needs to be called for every new frame.
void DisplayError(wxWindow *parent, const wxString &text, int displaytime)
Function DisplayError displays an error or warning message box with aMessage.
Definition: confirm.cpp:185
void StopDrawing()
Function StopDrawing() Prevents the GAL canvas from further drawing till it is recreated or StartDraw...
const COLOR4D & GetBackgroundColor() const
Function GetBackgroundColor Returns current background color settings.
Definition: painter.h:186
Class GAL is the abstract interface for drawing on a 2D-surface.