KiCad PCB EDA Suite
Loading...
Searching...
No Matches
view.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 The KiCad Developers, see AUTHORS.txt for contributors.
6 *
7 * @author Tomasz Wlostowski <[email protected]>
8 * @author Maciej Suminski <[email protected]>
9 *
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License
12 * as published by the Free Software Foundation; either version 2
13 * of the License, or (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, you may find one here:
22 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
23 * or you may search the http://www.gnu.org website for the version 2 license,
24 * or you may write to the Free Software Foundation, Inc.,
25 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
26 */
27
28
29#include <layer_ids.h>
30#include <trace_helpers.h>
31#include <wx/log.h>
32
33#include <view/view.h>
34#include <view/view_group.h>
35#include <view/view_item.h>
36#include <view/view_rtree.h>
37#include <view/view_overlay.h>
38
39#include <gal/definitions.h>
41#include <gal/painter.h>
42#include <algorithm>
43
44#include <core/profile.h>
45
46#ifdef KICAD_GAL_PROFILE
47#include <wx/log.h>
48#endif
49
50namespace KIGFX {
51
52class VIEW;
53
55{
56public:
58 m_view( nullptr ),
61 m_drawPriority( 0 ),
62 m_cachedIndex( -1 ),
63 m_groups( nullptr ),
64 m_groupsSize( 0 ) {}
65
67 {
69 }
70
71 int GetFlags() const
72 {
73 return m_flags;
74 }
75
76private:
77 friend class VIEW;
78
85 int getGroup( int aLayer ) const
86 {
87 for( int i = 0; i < m_groupsSize; ++i )
88 {
89 if( m_groups[i].first == aLayer )
90 return m_groups[i].second;
91 }
92
93 return -1;
94 }
95
102 void setGroup( int aLayer, int aGroup )
103 {
104 // Look if there is already an entry for the layer
105 for( int i = 0; i < m_groupsSize; ++i )
106 {
107 if( m_groups[i].first == aLayer )
108 {
109 m_groups[i].second = aGroup;
110 return;
111 }
112 }
113
114 // If there was no entry for the given layer - create one
115 std::pair<int, int>* newGroups = new std::pair<int, int>[m_groupsSize + 1];
116
117 if( m_groupsSize > 0 )
118 {
119 std::copy( m_groups, m_groups + m_groupsSize, newGroups );
120 delete[] m_groups;
121 }
122
123 m_groups = newGroups;
124 newGroups[m_groupsSize++] = { aLayer, aGroup };
125 }
126
127
132 {
133 delete[] m_groups;
134 m_groups = nullptr;
135 m_groupsSize = 0;
136 }
137
143 inline bool storesGroups() const
144 {
145 return m_groupsSize > 0;
146 }
147
155 void reorderGroups( std::unordered_map<int, int> aReorderMap )
156 {
157 for( int i = 0; i < m_groupsSize; ++i )
158 {
159 int orig_layer = m_groups[i].first;
160 int new_layer = orig_layer;
161
162 if( aReorderMap.count( orig_layer ) )
163 new_layer = aReorderMap.at( orig_layer );
164
165 m_groups[i].first = new_layer;
166 }
167 }
168
175 void saveLayers( const std::vector<int>& aLayers )
176 {
177 m_layers.clear();
178
179 for( int layer : aLayers )
180 {
181 wxCHECK2_MSG( layer >= 0 && layer < VIEW::VIEW_MAX_LAYERS, continue,
182 wxString::Format( wxT( "Invalid layer number: %d" ), layer ) );
183 m_layers.push_back( layer );
184 }
185 }
186
190 int requiredUpdate() const
191 {
192 return m_requiredUpdate;
193 }
194
199 {
201 }
202
206 bool isRenderable() const
207 {
208 return m_flags == VISIBLE;
209 }
210
216
217 std::pair<int, int>* m_groups;
220
221 std::vector<int> m_layers;
222
224};
225
226
228{
229 if( aItem->m_viewPrivData )
230 {
231 if( aItem->m_viewPrivData->m_view )
232 aItem->m_viewPrivData->m_view->VIEW::Remove( aItem );
233
234 delete aItem->m_viewPrivData;
235 aItem->m_viewPrivData = nullptr;
236 }
237}
238
239
241 m_enableOrderModifier( true ),
242 m_scale( 4.0 ),
243 m_minScale( 0.2 ), m_maxScale( 50000.0 ),
244 m_mirrorX( false ), m_mirrorY( false ),
245 m_painter( nullptr ),
246 m_gal( nullptr ),
247 m_useDrawPriority( false ),
249 m_reverseDrawOrder( false )
250{
251 // Set m_boundary to define the max area size. The default area size
252 // is defined here as the max value of a int.
253 // this is a default value acceptable for Pcbnew and Gerbview, but too large for Eeschema.
254 // So in eeschema a call to SetBoundary() with a smaller value will be needed.
255 typedef std::numeric_limits<int> coord_limits;
256 double pos = coord_limits::lowest() / 2 + coord_limits::epsilon();
257 double size = coord_limits::max() - coord_limits::epsilon();
258 m_boundary.SetOrigin( pos, pos );
259 m_boundary.SetSize( size, size );
260
261 m_allItems.reset( new std::vector<VIEW_ITEM*> );
262 m_allItems->reserve( 32768 );
263
264 // Redraw everything at the beginning
265 MarkDirty();
266
267 // View uses layers to display EDA_ITEMs (item may be displayed on several layers, for example
268 // pad may be shown on pad, pad hole and solder paste layers). There are usual copper layers
269 // (eg. F.Cu, B.Cu, internal and so on) and layers for displaying objects such as texts,
270 // silkscreen, pads, vias, etc.
271 for( int ii = 0; ii < VIEW_MAX_LAYERS; ++ii )
272 {
273 auto [it, _] = m_layers.emplace( ii, VIEW_LAYER() );
274 VIEW_LAYER& l = it->second;
275
276 l.items = std::make_shared<VIEW_RTREE>();
277 l.id = ii;
278 l.renderingOrder = ii;
279 l.visible = true;
280 l.displayOnly = false;
281 l.diffLayer = false;
282 l.hasNegatives = false;
284 }
285
287
288 m_preview.reset( new KIGFX::VIEW_GROUP() );
289 Add( m_preview.get() );
290}
291
292
294{
295 Remove( m_preview.get() );
296}
297
298
299void VIEW::Add( VIEW_ITEM* aItem, int aDrawPriority )
300{
301 if( aDrawPriority < 0 )
302 aDrawPriority = m_nextDrawPriority++;
303
304 if( !aItem->m_viewPrivData )
305 aItem->m_viewPrivData = new VIEW_ITEM_DATA;
306
307 wxASSERT_MSG( aItem->m_viewPrivData->m_view == nullptr || aItem->m_viewPrivData->m_view == this,
308 wxS( "Already in a different view!" ) );
309
310 aItem->m_viewPrivData->m_view = this;
311 aItem->m_viewPrivData->m_drawPriority = aDrawPriority;
312 const BOX2I bbox = aItem->ViewBBox();
313 aItem->m_viewPrivData->m_bbox = bbox;
314 aItem->m_viewPrivData->m_cachedIndex = m_allItems->size();
315
316 std::vector<int> layers = aItem->ViewGetLayers();
317
318 std::erase_if( layers, []( int layer )
319 {
320 return layer < 0 || layer >= VIEW_MAX_LAYERS;
321 } );
322
323 if( layers.empty() )
324 return;
325
326 aItem->viewPrivData()->saveLayers( layers );
327
328 m_allItems->push_back( aItem );
329
330 for( int layer : layers )
331 {
332 VIEW_LAYER& l = m_layers[layer];
333 l.items->Insert( aItem, bbox );
335 }
336
337 SetVisible( aItem, true );
338 Update( aItem, KIGFX::INITIAL_ADD );
339}
340
341
343{
344 static int s_gcCounter = 0;
345
346 if( aItem && aItem->m_viewPrivData )
347 {
348 if( aItem->m_viewPrivData->m_view != nullptr && aItem->m_viewPrivData->m_view != this )
349 {
350 wxLogDebug( wxT( "VIEW::Remove: item %s belongs to a different view" ), aItem->GetClass() );
351 return;
352 }
353
354 std::vector<VIEW_ITEM*>::iterator item = m_allItems->end();
355 int cachedIndex = aItem->m_viewPrivData->m_cachedIndex;
356
357 if( cachedIndex >= 0
358 && cachedIndex < static_cast<ssize_t>( m_allItems->size() )
359 && ( *m_allItems )[cachedIndex] == aItem )
360 {
361 item = m_allItems->begin() + cachedIndex;
362 }
363 else
364 {
365 item = std::find( m_allItems->begin(), m_allItems->end(), aItem );
366 }
367
368 if( item != m_allItems->end() )
369 {
370 *item = nullptr;
372
373 s_gcCounter++;
374
375 if( s_gcCounter > 4096 )
376 {
377 // Perform defragmentation
378 std::erase_if( *m_allItems,
379 []( VIEW_ITEM* it )
380 {
381 return it == nullptr;
382 } );
383
384 // Update cached indices
385 for( size_t idx = 0; idx < m_allItems->size(); idx++ )
386 ( *m_allItems )[idx]->m_viewPrivData->m_cachedIndex = idx;
387
388 s_gcCounter = 0;
389 }
390 }
391
392 const BOX2I* bbox = &aItem->m_viewPrivData->m_bbox;
393
394 for( int layer : aItem->m_viewPrivData->m_layers )
395 {
396 VIEW_LAYER& l = m_layers[layer];
397 l.items->Remove( aItem, bbox );
399
400 // Clear the GAL cache
401 int prevGroup = aItem->m_viewPrivData->getGroup( layer );
402
403 if( prevGroup >= 0 )
404 m_gal->DeleteGroup( prevGroup );
405 }
406
408 aItem->m_viewPrivData->m_view = nullptr;
409 }
410}
411
412
413void VIEW::SetRequired( int aLayerId, int aRequiredId, bool aRequired )
414{
415 wxCHECK( (unsigned) aLayerId < m_layers.size(), /*void*/ );
416 wxCHECK( (unsigned) aRequiredId < m_layers.size(), /*void*/ );
417
418 if( aRequired )
419 m_layers[aLayerId].requiredLayers.insert( aRequiredId );
420 else
421 m_layers[aLayerId].requiredLayers.erase( aRequired );
422}
423
424
425int VIEW::Query( const BOX2I& aRect, std::vector<LAYER_ITEM_PAIR>& aResult ) const
426{
427 if( m_orderedLayers.empty() )
428 return 0;
429
430 int layer = UNDEFINED_LAYER;
431 auto visitor =
432 [&]( VIEW_ITEM* item ) -> bool
433 {
434 aResult.push_back( VIEW::LAYER_ITEM_PAIR( item, layer ) );
435 return true;
436 };
437
438 std::vector<VIEW_LAYER*>::const_reverse_iterator i;
439
440 // execute queries in reverse direction, so that items that are on the top of
441 // the rendering stack are returned first.
442 for( i = m_orderedLayers.rbegin(); i != m_orderedLayers.rend(); ++i )
443 {
444 // ignore layers that do not contain actual items (i.e. the selection box, menus, floats)
445 if( ( *i )->displayOnly || !( *i )->visible )
446 continue;
447
448 layer = ( *i )->id;
449 ( *i )->items->Query( aRect, visitor );
450 }
451
452 return aResult.size();
453}
454
455
456void VIEW::Query( const BOX2I& aRect, const std::function<bool( VIEW_ITEM* )>& aFunc ) const
457{
458 if( m_orderedLayers.empty() )
459 return;
460
461 for( const auto& i : m_orderedLayers )
462 {
463 // ignore layers that do not contain actual items (i.e. the selection box, menus, floats)
464 if( i->displayOnly || !i->visible )
465 continue;
466
467 i->items->Query( aRect, aFunc );
468 }
469}
470
471
472VECTOR2D VIEW::ToWorld( const VECTOR2D& aCoord, bool aAbsolute ) const
473{
474 const MATRIX3x3D& matrix = m_gal->GetScreenWorldMatrix();
475
476 if( aAbsolute )
477 return VECTOR2D( matrix * aCoord );
478 else
479 return VECTOR2D( matrix.GetScale().x * aCoord.x, matrix.GetScale().y * aCoord.y );
480}
481
482
483double VIEW::ToWorld( double aSize ) const
484{
485 const MATRIX3x3D& matrix = m_gal->GetScreenWorldMatrix();
486
487 return fabs( matrix.GetScale().x * aSize );
488}
489
490
491VECTOR2D VIEW::ToScreen( const VECTOR2D& aCoord, bool aAbsolute ) const
492{
493 const MATRIX3x3D& matrix = m_gal->GetWorldScreenMatrix();
494
495 if( aAbsolute )
496 return VECTOR2D( matrix * aCoord );
497 else
498 return VECTOR2D( matrix.GetScale().x * aCoord.x, matrix.GetScale().y * aCoord.y );
499}
500
501
502double VIEW::ToScreen( double aSize ) const
503{
504 const MATRIX3x3D& matrix = m_gal->GetWorldScreenMatrix();
505
506 return matrix.GetScale().x * aSize;
507}
508
509
510void VIEW::CopySettings( const VIEW* aOtherView )
511{
512 wxASSERT_MSG( false, wxT( "This is not implemented" ) );
513}
514
515
516void VIEW::SetGAL( GAL* aGal )
517{
518 bool recacheGroups = ( m_gal != nullptr ); // recache groups only if GAL is reassigned
519 m_gal = aGal;
520
521 // clear group numbers, so everything is going to be recached
522 if( recacheGroups )
524
525 // every target has to be refreshed
526 MarkDirty();
527
528 // force the new GAL to display the current viewport.
530 SetScale( m_scale );
532}
533
534
536{
537 BOX2D rect;
538 VECTOR2D screenSize = m_gal->GetScreenPixelSize();
539
540 rect.SetOrigin( ToWorld( VECTOR2D( 0, 0 ) ) );
541 rect.SetEnd( ToWorld( screenSize ) );
542
543 return rect.Normalize();
544}
545
546
547void VIEW::SetViewport( const BOX2D& aViewport )
548{
549 VECTOR2D ssize = ToWorld( m_gal->GetScreenPixelSize(), false );
550
551 wxCHECK( fabs(ssize.x) > 0 && fabs(ssize.y) > 0, /*void*/ );
552
553 VECTOR2D centre = aViewport.Centre();
554 VECTOR2D vsize = aViewport.GetSize();
555 double zoom = 1.0 / std::max( fabs( vsize.x / ssize.x ), fabs( vsize.y / ssize.y ) );
556
557 SetCenter( centre );
558 SetScale( GetScale() * zoom );
559}
560
561
562void VIEW::SetMirror( bool aMirrorX, bool aMirrorY )
563{
564 wxASSERT_MSG( !aMirrorY, _( "Mirroring for Y axis is not supported yet" ) );
565
566 m_mirrorX = aMirrorX;
567 m_mirrorY = aMirrorY;
568 m_gal->SetFlip( aMirrorX, aMirrorY );
569
570 // Redraw everything
571 MarkDirty();
572}
573
574
575void VIEW::SetScale( double aScale, VECTOR2D aAnchor )
576{
577 if( aAnchor == VECTOR2D( 0, 0 ) )
578 aAnchor = m_center;
579
580 VECTOR2D a = ToScreen( aAnchor );
581
582 if( aScale < m_minScale )
584 else if( aScale > m_maxScale )
586 else
587 m_scale = aScale;
588
589 m_gal->SetZoomFactor( m_scale );
590 m_gal->ComputeWorldScreenMatrix();
591
592 VECTOR2D delta = ToWorld( a ) - aAnchor;
593
595
596 // Redraw everything after the viewport has changed
597 MarkDirty();
598}
599
600
601void VIEW::SetCenter( const VECTOR2D& aCenter )
602{
603 m_center = aCenter;
604
605 if( !m_boundary.Contains( aCenter ) )
606 {
607 if( m_center.x < m_boundary.GetLeft() )
608 m_center.x = m_boundary.GetLeft();
609 else if( aCenter.x > m_boundary.GetRight() )
610 m_center.x = m_boundary.GetRight();
611
612 if( m_center.y < m_boundary.GetTop() )
613 m_center.y = m_boundary.GetTop();
614 else if( m_center.y > m_boundary.GetBottom() )
615 m_center.y = m_boundary.GetBottom();
616 }
617
618 m_gal->SetLookAtPoint( m_center );
619 m_gal->ComputeWorldScreenMatrix();
620
621 // Redraw everything after the viewport has changed
622 MarkDirty();
623}
624
625
626void VIEW::SetCenter( const VECTOR2D& aCenter, const std::vector<BOX2D>& obscuringScreenRects )
627{
628 if( obscuringScreenRects.empty() )
629 return SetCenter( aCenter );
630
631 BOX2D screenRect( { 0, 0 }, m_gal->GetScreenPixelSize() );
632 SHAPE_POLY_SET unobscuredPoly( screenRect );
633 VECTOR2D unobscuredCenter = screenRect.Centre();
634
635 for( const BOX2D& obscuringScreenRect : obscuringScreenRects )
636 {
637 SHAPE_POLY_SET obscuringPoly( obscuringScreenRect );
638 unobscuredPoly.BooleanSubtract( obscuringPoly );
639 }
640
641 /*
642 * Perform a step-wise deflate to find the center of the largest unobscured area
643 */
644
645 BOX2I bbox = unobscuredPoly.BBox();
646 int step = std::min( bbox.GetWidth(), bbox.GetHeight() ) / 10;
647
648 if( step < 20 )
649 step = 20;
650
651 while( !unobscuredPoly.IsEmpty() )
652 {
653 unobscuredCenter = unobscuredPoly.BBox().Centre();
655 }
656
657 SetCenter( aCenter - ToWorld( unobscuredCenter - screenRect.Centre(), false ) );
658}
659
660
661void VIEW::SetLayerOrder( int aLayer, int aRenderingOrder )
662{
663 m_layers[aLayer].renderingOrder = aRenderingOrder;
664
666}
667
668
669int VIEW::GetLayerOrder( int aLayer ) const
670{
671 return m_layers.at( aLayer ).renderingOrder;
672}
673
674
675void VIEW::SortLayers( std::vector<int>& aLayers ) const
676{
677 std::sort( aLayers.begin(), aLayers.end(),
678 [this]( int a, int b )
679 {
680 return GetLayerOrder( a ) > GetLayerOrder( b );
681 } );
682}
683
684
685void VIEW::ReorderLayerData( std::unordered_map<int, int> aReorderMap )
686{
687 std::map<int,VIEW_LAYER> new_map;
688
689 for( auto& [_, layer] : m_layers )
690 {
691 auto reorder_it = aReorderMap.find( layer.id );
692
693 // If the layer is not in the reorder map or if it is mapped to itself,
694 // just copy the layer to the new map.
695 if( reorder_it == aReorderMap.end() || reorder_it->second == layer.id )
696 {
697 new_map.emplace( layer.id, layer );
698 continue;
699 }
700
701 auto [new_it,__] = new_map.emplace( reorder_it->second, layer );
702 new_it->second.id = reorder_it->second;
703 }
704
705 // Transfer reordered data (using the copy assignment operator ):
706 m_layers = new_map;
707
709
710 for( VIEW_ITEM* item : *m_allItems )
711 {
712 if( !item )
713 continue;
714
715 VIEW_ITEM_DATA* viewData = item->viewPrivData();
716
717 if( !viewData )
718 continue;
719
720 std::vector<int> layers = item->ViewGetLayers();
721 viewData->saveLayers( layers );
722
723 viewData->reorderGroups( aReorderMap );
724
725 viewData->m_requiredUpdate |= COLOR;
726 }
727
728 UpdateItems();
729}
730
731
733{
734 UPDATE_COLOR_VISITOR( int aLayer, PAINTER* aPainter, GAL* aGal ) :
735 layer( aLayer ),
736 painter( aPainter ),
737 gal( aGal )
738 {
739 }
740
741 bool operator()( VIEW_ITEM* aItem )
742 {
743 // Obtain the color that should be used for coloring the item
744 const COLOR4D color = painter->GetSettings()->GetColor( aItem, layer );
745 int group = aItem->viewPrivData()->getGroup( layer );
746
747 if( group >= 0 )
748 gal->ChangeGroupColor( group, color );
749
750 return true;
751 }
752
753 int layer;
756};
757
758
759void VIEW::UpdateLayerColor( int aLayer )
760{
761 // There is no point in updating non-cached layers
762 if( !IsCached( aLayer ) )
763 return;
764
765 BOX2I r;
766
767 r.SetMaximum();
768
769 if( m_gal->IsVisible() )
770 {
772
773 UPDATE_COLOR_VISITOR visitor( aLayer, m_painter, m_gal );
774 m_layers[aLayer].items->Query( r, visitor );
775 MarkTargetDirty( m_layers[aLayer].target );
776 }
777}
778
779
781{
782 if( m_gal->IsVisible() )
783 {
785
786 for( VIEW_ITEM* item : *m_allItems )
787 {
788 if( !item )
789 continue;
790
791 VIEW_ITEM_DATA* viewData = item->viewPrivData();
792
793 if( !viewData )
794 continue;
795
796 for( int layer : viewData->m_layers )
797 {
798 const COLOR4D color = m_painter->GetSettings()->GetColor( item, layer );
799 int group = viewData->getGroup( layer );
800
801 if( group >= 0 )
802 m_gal->ChangeGroupColor( group, color );
803 }
804 }
805 }
806
807 MarkDirty();
808}
809
810
812{
813 UPDATE_DEPTH_VISITOR( int aLayer, int aDepth, GAL* aGal ) :
814 layer( aLayer ),
815 depth( aDepth ),
816 gal( aGal )
817 {
818 }
819
820 bool operator()( VIEW_ITEM* aItem )
821 {
822 int group = aItem->viewPrivData()->getGroup( layer );
823
824 if( group >= 0 )
825 gal->ChangeGroupDepth( group, depth );
826
827 return true;
828 }
829
832};
833
834
836{
837 if( m_topLayers.size() == 0 )
838 return 0;
839
840 return *m_topLayers.begin();
841}
842
843
844void VIEW::SetTopLayer( int aLayer, bool aEnabled )
845{
846 if( aEnabled )
847 {
848 if( m_topLayers.count( aLayer ) == 1 )
849 return;
850
851 m_topLayers.insert( aLayer );
852
853 // Move the layer closer to front
855 m_layers[aLayer].renderingOrder += TOP_LAYER_MODIFIER;
856 }
857 else
858 {
859 if( m_topLayers.count( aLayer ) == 0 )
860 return;
861
862 m_topLayers.erase( aLayer );
863
864 // Restore the previous rendering order
866 m_layers[aLayer].renderingOrder -= TOP_LAYER_MODIFIER;
867 }
868}
869
870
871void VIEW::EnableTopLayer( bool aEnable )
872{
873 if( aEnable == m_enableOrderModifier )
874 return;
875
876 m_enableOrderModifier = aEnable;
877
878 std::set<unsigned int>::iterator it;
879
880 if( aEnable )
881 {
882 for( it = m_topLayers.begin(); it != m_topLayers.end(); ++it )
883 m_layers[*it].renderingOrder += TOP_LAYER_MODIFIER;
884 }
885 else
886 {
887 for( it = m_topLayers.begin(); it != m_topLayers.end(); ++it )
888 m_layers[*it].renderingOrder -= TOP_LAYER_MODIFIER;
889 }
890
893}
894
895
897{
898 std::set<unsigned int>::iterator it;
899
901 {
902 // Restore the previous rendering order for layers that were marked as top
903 for( it = m_topLayers.begin(); it != m_topLayers.end(); ++it )
904 m_layers[*it].renderingOrder -= TOP_LAYER_MODIFIER;
905 }
906
907 m_topLayers.clear();
908}
909
910
912{
914
915 if( m_gal->IsVisible() )
916 {
918
919 for( VIEW_ITEM* item : *m_allItems )
920 {
921 if( !item )
922 continue;
923
924 VIEW_ITEM_DATA* viewData = item->viewPrivData();
925
926 if( !viewData )
927 continue;
928
929 for( int layer : viewData->m_layers )
930 {
931 int group = viewData->getGroup( layer );
932
933 if( group >= 0 )
934 m_gal->ChangeGroupDepth( group, m_layers[layer].renderingOrder );
935 }
936 }
937 }
938
939 MarkDirty();
940}
941
942
944{
945 DRAW_ITEM_VISITOR( VIEW* aView, int aLayer, bool aUseDrawPriority, bool aReverseDrawOrder ) :
946 view( aView ),
947 layer( aLayer ),
948 useDrawPriority( aUseDrawPriority ),
949 reverseDrawOrder( aReverseDrawOrder ),
950 drawForcedTransparent( false ),
952 {
953 }
954
955 bool operator()( VIEW_ITEM* aItem )
956 {
957 wxCHECK( aItem->viewPrivData(), false );
958
960 {
962 return true;
963 }
964
965 const double itemLOD = aItem->ViewGetLOD( layer, view );
966
967 // Conditions that have to be fulfilled for an item to be drawn
968 bool drawCondition = aItem->viewPrivData()->isRenderable() && itemLOD < view->m_scale;
969
970 if( !drawCondition )
971 return true;
972
973 if( useDrawPriority )
974 drawItems.push_back( aItem );
975 else
976 view->draw( aItem, layer );
977
978 return true;
979 }
980
982 {
983 if( reverseDrawOrder )
984 {
985 std::sort( drawItems.begin(), drawItems.end(),
986 []( VIEW_ITEM* a, VIEW_ITEM* b ) -> bool
987 {
988 return b->viewPrivData()->m_drawPriority
989 < a->viewPrivData()->m_drawPriority;
990 } );
991 }
992 else
993 {
994 std::sort( drawItems.begin(), drawItems.end(),
995 []( VIEW_ITEM* a, VIEW_ITEM* b ) -> bool
996 {
997 return a->viewPrivData()->m_drawPriority
998 < b->viewPrivData()->m_drawPriority;
999 } );
1000 }
1001
1002 for( VIEW_ITEM* item : drawItems )
1003 view->draw( item, layer );
1004 }
1005
1009 std::vector<VIEW_ITEM*> drawItems;
1012};
1013
1014
1015void VIEW::redrawRect( const BOX2I& aRect )
1016{
1017 for( VIEW_LAYER* l : m_orderedLayers )
1018 {
1019 if( l->visible && IsTargetDirty( l->target ) && areRequiredLayersEnabled( l->id ) )
1020 {
1021 DRAW_ITEM_VISITOR drawFunc( this, l->id, m_useDrawPriority, m_reverseDrawOrder );
1022
1023 m_gal->SetTarget( l->target );
1024 m_gal->SetLayerDepth( l->renderingOrder );
1025
1026 // Differential layer also work for the negatives, since both special layer types
1027 // will composite on separate layers (at least in Cairo)
1028 if( l->diffLayer )
1029 m_gal->StartDiffLayer();
1030 else if( l->hasNegatives )
1031 m_gal->StartNegativesLayer();
1032
1033 l->items->Query( aRect, drawFunc );
1034
1035 if( m_useDrawPriority )
1036 drawFunc.deferredDraw();
1037
1038 if( l->diffLayer )
1039 m_gal->EndDiffLayer();
1040 else if( l->hasNegatives )
1041 m_gal->EndNegativesLayer();
1042
1043 if( drawFunc.foundForcedTransparent )
1044 {
1045 drawFunc.drawForcedTransparent = true;
1046
1047 m_gal->SetTarget( TARGET_NONCACHED );
1048 m_gal->EnableDepthTest( true );
1049 m_gal->SetLayerDepth( l->renderingOrder );
1050
1051 l->items->Query( aRect, drawFunc );
1052 }
1053 }
1054 }
1055}
1056
1057
1058void VIEW::draw( VIEW_ITEM* aItem, int aLayer, bool aImmediate )
1059{
1060 VIEW_ITEM_DATA* viewData = aItem->viewPrivData();
1061
1062 if( !viewData )
1063 return;
1064
1065 if( IsCached( aLayer ) && !aImmediate )
1066 {
1067 // Draw using cached information or create one
1068 int group = viewData->getGroup( aLayer );
1069
1070 if( group >= 0 )
1071 m_gal->DrawGroup( group );
1072 else
1073 Update( aItem );
1074 }
1075 else
1076 {
1077 // Immediate mode
1078 if( !m_painter->Draw( aItem, aLayer ) )
1079 aItem->ViewDraw( aLayer, this ); // Alternative drawing method
1080 }
1081}
1082
1083
1084void VIEW::draw( VIEW_ITEM* aItem, bool aImmediate )
1085{
1086 std::vector<int> layers = aItem->ViewGetLayers();
1087
1088 // Sorting is needed for drawing order dependent GALs (like Cairo)
1089 if( !m_gal || !m_gal->IsOpenGlEngine())
1090 SortLayers( layers );
1091
1092 for( int layer : layers )
1093 {
1094 auto it = m_layers.find( layer );
1095
1096 if( it == m_layers.end() )
1097 continue;
1098
1099 if( m_gal )
1100 m_gal->SetLayerDepth( it->second.renderingOrder );
1101
1102 draw( aItem, layer, aImmediate );
1103 }
1104}
1105
1106
1107void VIEW::draw( VIEW_GROUP* aGroup, bool aImmediate )
1108{
1109 for( unsigned int i = 0; i < aGroup->GetSize(); i++ )
1110 draw( aGroup->GetItem(i), aImmediate );
1111}
1112
1113
1115{
1116 RECACHE_ITEM_VISITOR( VIEW* aView, GAL* aGal, int aLayer ) :
1117 view( aView ),
1118 gal( aGal ),
1119 layer( aLayer )
1120 {
1121 }
1122
1123 bool operator()( VIEW_ITEM* aItem )
1124 {
1125 VIEW_ITEM_DATA* viewData = aItem->viewPrivData();
1126
1127 if( !viewData )
1128 return false;
1129
1130 // Remove previously cached group
1131 int group = viewData->getGroup( layer );
1132
1133 if( group >= 0 )
1134 gal->DeleteGroup( group );
1135
1136 viewData->setGroup( layer, -1 );
1137 view->Update( aItem );
1138
1139 return true;
1140 }
1141
1145};
1146
1147
1149{
1150 BOX2I r;
1151 r.SetMaximum();
1152
1153 // Invalidate viewPrivData for all items before clearing. This ensures that items
1154 // which persist outside the view (like selection groups) won't have stale references
1155 // to this view, which could cause issues if they're later removed and re-added.
1156 for( VIEW_ITEM* item : *m_allItems )
1157 {
1158 if( item && item->m_viewPrivData )
1159 item->m_viewPrivData->m_view = nullptr;
1160 }
1161
1162 m_allItems->clear();
1163
1164 for( auto& [_, layer] : m_layers )
1165 layer.items->RemoveAll();
1166
1168
1169 m_gal->ClearCache();
1170}
1171
1172
1174{
1176 {
1177 // TARGET_CACHED and TARGET_NONCACHED have to be redrawn together, as they contain
1178 // layers that rely on each other (eg. netnames are noncached, but tracks - are cached)
1179 m_gal->ClearTarget( TARGET_NONCACHED );
1180 m_gal->ClearTarget( TARGET_CACHED );
1181
1182 MarkDirty();
1183 }
1184
1186 {
1187 m_gal->ClearTarget( TARGET_OVERLAY );
1188 }
1189}
1190
1191
1193{
1194#ifdef KICAD_GAL_PROFILE
1195 PROF_TIMER totalRealTime;
1196#endif /* KICAD_GAL_PROFILE */
1197
1198 VECTOR2D screenSize = m_gal->GetScreenPixelSize();
1199 BOX2D rect( ToWorld( VECTOR2D( 0, 0 ) ),
1200 ToWorld( screenSize ) - ToWorld( VECTOR2D( 0, 0 ) ) );
1201
1202 rect.Normalize();
1203 BOX2I recti = BOX2ISafe( rect );
1204
1205 redrawRect( recti );
1206
1207 // All targets were redrawn, so nothing is dirty
1208 MarkClean();
1209
1210#ifdef KICAD_GAL_PROFILE
1211 totalRealTime.Stop();
1212 wxLogTrace( traceGalProfile, wxS( "VIEW::Redraw(): %.1f ms" ), totalRealTime.msecs() );
1213#endif /* KICAD_GAL_PROFILE */
1214}
1215
1216
1218{
1219 return m_gal->GetScreenPixelSize();
1220}
1221
1222
1224{
1226 view( aView )
1227 {
1228 }
1229
1230 bool operator()( VIEW_ITEM* aItem )
1231 {
1232 aItem->viewPrivData()->deleteGroups();
1233
1234 return true;
1235 }
1236
1238};
1239
1240
1242{
1243 BOX2I r;
1244
1245 r.SetMaximum();
1246 CLEAR_LAYER_CACHE_VISITOR visitor( this );
1247
1248 for( auto& [_, layer] : m_layers )
1249 layer.items->Query( r, visitor );
1250}
1251
1252
1253void VIEW::invalidateItem( VIEW_ITEM* aItem, int aUpdateFlags )
1254{
1255 if( aUpdateFlags & INITIAL_ADD )
1256 {
1257 // Don't update layers or bbox, since it was done in VIEW::Add()
1258 // Now that we have initialized, set flags to ALL for the code below
1259 aUpdateFlags = ALL;
1260 }
1261 else
1262 {
1263 // updateLayers updates geometry too, so we do not have to update both of them at the
1264 // same time
1265 if( aUpdateFlags & LAYERS )
1266 updateLayers( aItem );
1267 else if( aUpdateFlags & GEOMETRY )
1268 updateBbox( aItem );
1269 }
1270
1271 std::vector<int> layers = aItem->ViewGetLayers();
1272
1273 // Iterate through layers used by the item and recache it immediately
1274 for( int layer : layers )
1275 {
1276 if( IsCached( layer ) )
1277 {
1278 if( aUpdateFlags & ( GEOMETRY | LAYERS | REPAINT ) )
1279 updateItemGeometry( aItem, layer );
1280 else if( aUpdateFlags & COLOR )
1281 updateItemColor( aItem, layer );
1282 }
1283
1284 // Mark those layers as dirty, so the VIEW will be refreshed
1285 MarkTargetDirty( m_layers[layer].target );
1286 }
1287
1288 aItem->viewPrivData()->clearUpdateFlags();
1289}
1290
1291
1293{
1294 int n = 0;
1295
1296 m_orderedLayers.resize( m_layers.size() );
1297
1298 for( auto& [layer_id, layer] : m_layers )
1299 m_orderedLayers[n++] = &layer;
1300
1302
1303 MarkDirty();
1304}
1305
1306
1307void VIEW::updateItemColor( VIEW_ITEM* aItem, int aLayer )
1308{
1309 VIEW_ITEM_DATA* viewData = aItem->viewPrivData();
1310 wxCHECK( IsCached( aLayer ), /*void*/ ); // This will check if the layer exists
1311
1312 if( !viewData )
1313 return;
1314
1315 // Obtain the color that should be used for coloring the item on the specific layerId
1316 const COLOR4D color = m_painter->GetSettings()->GetColor( aItem, aLayer );
1317 int group = viewData->getGroup( aLayer );
1318
1319 // Change the color, only if it has group assigned
1320 if( group >= 0 )
1321 m_gal->ChangeGroupColor( group, color );
1322}
1323
1324
1325void VIEW::updateItemGeometry( VIEW_ITEM* aItem, int aLayer )
1326{
1327 VIEW_ITEM_DATA* viewData = aItem->viewPrivData();
1328
1329 if( !viewData )
1330 return;
1331
1332 auto it = m_layers.find( aLayer );
1333
1334 if( it == m_layers.end() )
1335 return;
1336
1337 VIEW_LAYER& l = it->second;
1338
1339 // Save the extra map lookup in IsCached by open coding here
1340 if( l.target != TARGET_CACHED )
1341 return;
1342
1343 m_gal->SetTarget( l.target );
1344 m_gal->SetLayerDepth( l.renderingOrder );
1345
1346 // Redraw the item from scratch
1347 int group = viewData->getGroup( aLayer );
1348
1349 if( group >= 0 )
1350 m_gal->DeleteGroup( group );
1351
1352 group = m_gal->BeginGroup();
1353 viewData->setGroup( aLayer, group );
1354
1355 if( !m_painter->Draw( aItem, aLayer ) )
1356 aItem->ViewDraw( aLayer, this ); // Alternative drawing method
1357
1358 m_gal->EndGroup();
1359}
1360
1361
1363{
1364 std::vector<int> layers = aItem->ViewGetLayers();
1365
1366 wxASSERT( aItem->m_viewPrivData ); //must have a viewPrivData
1367
1368 const BOX2I new_bbox = aItem->ViewBBox();
1369 const BOX2I* old_bbox = &aItem->m_viewPrivData->m_bbox;
1370 aItem->m_viewPrivData->m_bbox = new_bbox;
1371
1372 for( int layer : layers )
1373 {
1374 auto it = m_layers.find( layer );
1375
1376 if( it == m_layers.end() )
1377 continue;
1378
1379 VIEW_LAYER& l = it->second;
1380 l.items->Remove( aItem, old_bbox );
1381 l.items->Insert( aItem, new_bbox );
1383 }
1384}
1385
1386
1388{
1389 VIEW_ITEM_DATA* viewData = aItem->viewPrivData();
1390
1391 if( !viewData )
1392 return;
1393
1394 // Remove the item from previous layer set
1395 const BOX2I* old_bbox = &aItem->m_viewPrivData->m_bbox;
1396
1397 for( int layer : aItem->m_viewPrivData->m_layers )
1398 {
1399 auto it = m_layers.find( layer );
1400
1401 if( it == m_layers.end() )
1402 continue;
1403
1404 VIEW_LAYER& l = it->second;
1405 l.items->Remove( aItem, old_bbox );
1407
1408 if( IsCached( l.id ) )
1409 {
1410 // Redraw the item from scratch
1411 int prevGroup = viewData->getGroup( layer );
1412
1413 if( prevGroup >= 0 )
1414 {
1415 m_gal->DeleteGroup( prevGroup );
1416 viewData->setGroup( l.id, -1 );
1417 }
1418 }
1419 }
1420
1421 const BOX2I new_bbox = aItem->ViewBBox();
1422 aItem->m_viewPrivData->m_bbox = new_bbox;
1423
1424 // Add the item to new layer set
1425 std::vector<int> layers = aItem->ViewGetLayers();
1426 viewData->saveLayers( layers );
1427
1428 for( int layer : layers )
1429 {
1430 auto it = m_layers.find( layer );
1431
1432 if( it == m_layers.end() )
1433 continue;
1434
1435 VIEW_LAYER& l = it->second;
1436 l.items->Insert( aItem, new_bbox );
1438 }
1439}
1440
1441
1442bool VIEW::areRequiredLayersEnabled( int aLayerId ) const
1443{
1444 auto it = m_layers.find( aLayerId );
1445
1446 if( it == m_layers.end() )
1447 return false;
1448
1449 for( int layer : it->second.requiredLayers )
1450 {
1451 // That is enough if just one layer is not enabled
1452
1453 auto it2 = m_layers.find( layer );
1454
1455 if( it2 == m_layers.end() || !it2->second.visible )
1456 return false;
1457
1458 if( !areRequiredLayersEnabled( layer ) )
1459 return false;
1460 }
1461
1462 return true;
1463}
1464
1465
1467{
1468 BOX2I r;
1469
1470 r.SetMaximum();
1471
1472 for( const auto& [_, l] : m_layers )
1473 {
1474 if( IsCached( l.id ) )
1475 {
1476 RECACHE_ITEM_VISITOR visitor( this, m_gal, l.id );
1477 l.items->Query( r, visitor );
1478 }
1479 }
1480}
1481
1482
1484{
1485 if( !m_gal->IsVisible() || !m_gal->IsInitialized() )
1486 return;
1487
1488 unsigned int cntGeomUpdate = 0;
1489 bool anyUpdated = false;
1490
1491 for( VIEW_ITEM* item : *m_allItems )
1492 {
1493 if( !item )
1494 continue;
1495
1496 auto vpd = item->viewPrivData();
1497
1498 if( !vpd )
1499 continue;
1500
1501 if( vpd->m_requiredUpdate != NONE )
1502 {
1503 anyUpdated = true;
1504
1505 if( vpd->m_requiredUpdate & ( GEOMETRY | LAYERS ) )
1506 {
1507 cntGeomUpdate++;
1508 }
1509 }
1510 }
1511
1512 unsigned int cntTotal = m_allItems->size();
1513
1514 double ratio = (double) cntGeomUpdate / (double) cntTotal;
1515
1516 // Optimization to improve view update time. If a lot of items (say, 30%) have their
1517 // bboxes/geometry changed it's way faster (around 10 times) to rebuild the R-Trees
1518 // from scratch rather than update the bbox of each changed item. Pcbnew does multiple
1519 // full geometry updates during file load, this can save a solid 30 seconds on load time
1520 // for larger designs...
1521
1522 if( ratio > 0.3 )
1523 {
1524 auto allItems = *m_allItems;
1525
1526 // kill all Rtrees
1527 for( auto& [_, layer] : m_layers )
1528 layer.items->RemoveAll();
1529
1530 // and re-insert items from scratch
1531 for( VIEW_ITEM* item : allItems )
1532 {
1533 if( !item )
1534 continue;
1535
1536 const BOX2I bbox = item->ViewBBox();
1537 item->m_viewPrivData->m_bbox = bbox;
1538
1539 std::vector<int> layers = item->ViewGetLayers();
1540 item->viewPrivData()->saveLayers( layers );
1541
1542 for( int layer : layers )
1543 {
1544 auto it = m_layers.find( layer );
1545
1546 wxCHECK2_MSG( it != m_layers.end(), continue, wxS( "Invalid layer" ) );
1547
1548 VIEW_LAYER& l = it->second;
1549 l.items->Insert( item, bbox );
1551 }
1552
1553 item->viewPrivData()->m_requiredUpdate &= ~( LAYERS | GEOMETRY );
1554 }
1555 }
1556
1557 if( anyUpdated )
1558 {
1560
1561 for( VIEW_ITEM* item : *m_allItems.get() )
1562 {
1563 if( item && item->viewPrivData() && item->viewPrivData()->m_requiredUpdate != NONE )
1564 {
1565 invalidateItem( item, item->viewPrivData()->m_requiredUpdate );
1566 item->viewPrivData()->m_requiredUpdate = NONE;
1567 }
1568 }
1569 }
1570
1571 KI_TRACE( traceGalProfile, wxS( "View update: total items %u, geom %u anyUpdated %u\n" ),
1572 cntTotal, cntGeomUpdate, (unsigned) anyUpdated );
1573}
1574
1575
1576void VIEW::UpdateAllItems( int aUpdateFlags )
1577{
1578 for( VIEW_ITEM* item : *m_allItems )
1579 {
1580 if( item && item->viewPrivData() )
1581 item->viewPrivData()->m_requiredUpdate |= aUpdateFlags;
1582 }
1583}
1584
1585
1587 std::function<bool( VIEW_ITEM* )> aCondition )
1588{
1589 for( VIEW_ITEM* item : *m_allItems )
1590 {
1591 if( !item )
1592 continue;
1593
1594 if( aCondition( item ) )
1595 {
1596 if( item->viewPrivData() )
1597 item->viewPrivData()->m_requiredUpdate |= aUpdateFlags;
1598 }
1599 }
1600}
1601
1602
1603void VIEW::UpdateAllItemsConditionally( std::function<int( VIEW_ITEM* )> aItemFlagsProvider )
1604{
1605 for( VIEW_ITEM* item : *m_allItems )
1606 {
1607 if( !item )
1608 continue;
1609
1610 if( item->viewPrivData() )
1611 item->viewPrivData()->m_requiredUpdate |= aItemFlagsProvider( item );
1612 }
1613}
1614
1615
1616
1617std::unique_ptr<VIEW> VIEW::DataReference() const
1618{
1619 std::unique_ptr<VIEW> ret = std::make_unique<VIEW>();
1620 ret->m_allItems = m_allItems;
1621 ret->m_layers = m_layers;
1622 ret->sortOrderedLayers();
1623 return ret;
1624}
1625
1626
1627void VIEW::SetVisible( VIEW_ITEM* aItem, bool aIsVisible )
1628{
1629 VIEW_ITEM_DATA* viewData = aItem->viewPrivData();
1630
1631 if( !viewData )
1632 return;
1633
1634 bool cur_visible = viewData->m_flags & VISIBLE;
1635
1636 if( cur_visible != aIsVisible )
1637 {
1638 if( aIsVisible )
1639 viewData->m_flags |= VISIBLE;
1640 else
1641 viewData->m_flags &= ~VISIBLE;
1642
1643 Update( aItem, APPEARANCE | COLOR );
1644 }
1645}
1646
1647
1648void VIEW::Hide( VIEW_ITEM* aItem, bool aHide, bool aHideOverlay )
1649{
1650 VIEW_ITEM_DATA* viewData = aItem->viewPrivData();
1651
1652 if( !viewData )
1653 return;
1654
1655 if( !( viewData->m_flags & VISIBLE ) )
1656 return;
1657
1658 if( aHideOverlay )
1659 viewData->m_flags |= OVERLAY_HIDDEN;
1660
1661 if( aHide )
1662 viewData->m_flags |= HIDDEN;
1663 else
1664 viewData->m_flags &= ~( HIDDEN | OVERLAY_HIDDEN );
1665
1666 Update( aItem, APPEARANCE );
1667}
1668
1669
1670bool VIEW::IsVisible( const VIEW_ITEM* aItem ) const
1671{
1672 const VIEW_ITEM_DATA* viewData = aItem->viewPrivData();
1673
1674 return viewData && ( viewData->m_flags & VISIBLE );
1675}
1676
1677
1678bool VIEW::IsHiddenOnOverlay( const VIEW_ITEM* aItem ) const
1679{
1680 const VIEW_ITEM_DATA* viewData = aItem->viewPrivData();
1681
1682 return viewData && ( viewData->m_flags & OVERLAY_HIDDEN );
1683}
1684
1685
1686bool VIEW::HasItem( const VIEW_ITEM* aItem ) const
1687{
1688 const VIEW_ITEM_DATA* viewData = aItem->viewPrivData();
1689
1690 return viewData && viewData->m_view == this;
1691}
1692
1693
1694void VIEW::Update( const VIEW_ITEM* aItem ) const
1695{
1696 Update( aItem, ALL );
1697}
1698
1699
1700void VIEW::Update( const VIEW_ITEM* aItem, int aUpdateFlags ) const
1701{
1702 VIEW_ITEM_DATA* viewData = aItem->viewPrivData();
1703
1704 if( !viewData )
1705 return;
1706
1707 assert( aUpdateFlags != NONE );
1708
1709 viewData->m_requiredUpdate |= aUpdateFlags;
1710}
1711
1712
1713std::shared_ptr<VIEW_OVERLAY> VIEW::MakeOverlay()
1714{
1715 std::shared_ptr<VIEW_OVERLAY> overlay = std::make_shared<VIEW_OVERLAY>();
1716
1717 Add( overlay.get() );
1718 return overlay;
1719}
1720
1721
1723{
1724 if( !m_preview )
1725 return;
1726
1727 m_preview->Clear();
1728
1729 for( VIEW_ITEM* item : m_ownedItems )
1730 delete item;
1731
1732 m_ownedItems.clear();
1733 Update( m_preview.get() );
1734}
1735
1736
1738{
1739 m_preview.reset( new KIGFX::VIEW_GROUP() );
1740 Add( m_preview.get() );
1741}
1742
1743
1744void VIEW::AddToPreview( VIEW_ITEM* aItem, bool aTakeOwnership )
1745{
1746 Hide( aItem, false );
1747 m_preview->Add( aItem );
1748
1749 if( aTakeOwnership )
1750 m_ownedItems.push_back( aItem );
1751
1752 SetVisible( m_preview.get(), true );
1753 Hide( m_preview.get(), false );
1754 Update( m_preview.get() );
1755}
1756
1757
1758void VIEW::ShowPreview( bool aShow )
1759{
1760 SetVisible( m_preview.get(), aShow );
1761}
1762
1763
1764} // namespace KIGFX
constexpr int ARC_LOW_DEF
Definition base_units.h:128
constexpr BOX2I BOX2ISafe(const BOX2D &aInput)
Definition box2.h:929
BOX2< VECTOR2I > BOX2I
Definition box2.h:922
BOX2< VECTOR2D > BOX2D
Definition box2.h:923
constexpr void SetMaximum()
Definition box2.h:80
constexpr void SetOrigin(const Vec &pos)
Definition box2.h:237
constexpr BOX2< Vec > & Normalize()
Ensure that the height and width are positive.
Definition box2.h:146
constexpr size_type GetWidth() const
Definition box2.h:214
constexpr Vec Centre() const
Definition box2.h:97
constexpr size_type GetHeight() const
Definition box2.h:215
constexpr const SizeVec & GetSize() const
Definition box2.h:206
constexpr void SetEnd(coord_type x, coord_type y)
Definition box2.h:297
A color representation with 4 components: red, green, blue, alpha.
Definition color4d.h:105
Abstract interface for drawing on a 2D-surface.
Contains all the knowledge about how to draw graphical object onto any particular output device.
Definition painter.h:59
Extend VIEW_ITEM by possibility of grouping items into a single object.
Definition view_group.h:43
virtual unsigned int GetSize() const
Return the number of stored items.
virtual VIEW_ITEM * GetItem(unsigned int aIdx) const
bool storesGroups() const
Return information if the item uses at least one group id (ie.
Definition view.cpp:143
std::vector< int > m_layers
Definition view.cpp:221
int requiredUpdate() const
Return current update flag for an item.
Definition view.cpp:190
int m_requiredUpdate
Flag required for updating.
Definition view.cpp:213
int m_flags
Visibility flags.
Definition view.cpp:212
void reorderGroups(std::unordered_map< int, int > aReorderMap)
Reorder the stored groups (to facilitate reordering of layers).
Definition view.cpp:155
int m_cachedIndex
Cached index in m_allItems.
Definition view.cpp:215
void deleteGroups()
Remove all of the stored group ids.
Definition view.cpp:131
bool isRenderable() const
Return if the item should be drawn or not.
Definition view.cpp:206
int m_drawPriority
Order to draw this item in a layer, lowest first.
Definition view.cpp:214
friend class VIEW
Definition view.cpp:77
VIEW * m_view
Current dynamic view the item is assigned to.
Definition view.cpp:211
std::pair< int, int > * m_groups
layer_number:group_id pairs for each layer the item occupies.
Definition view.cpp:217
int getGroup(int aLayer) const
Return number of the group id for the given layer, or -1 in case it was not cached before.
Definition view.cpp:85
BOX2I m_bbox
Stores layer numbers used by the item.
Definition view.cpp:223
void setGroup(int aLayer, int aGroup)
Set a group id for the item and the layer combination.
Definition view.cpp:102
void saveLayers(const std::vector< int > &aLayers)
Save layers used by the item.
Definition view.cpp:175
int GetFlags() const
Definition view.cpp:71
void clearUpdateFlags()
Mark an item as already updated, so it is not going to be redrawn.
Definition view.cpp:198
virtual const BOX2I ViewBBox() const =0
Return the bounding box of the item covering all its layers.
double m_forcedTransparency
Additional transparency for diff'ing items.
Definition view_item.h:210
VIEW_ITEM_DATA * viewPrivData() const
Definition view_item.h:161
virtual void ViewDraw(int aLayer, VIEW *aView) const
Draw the parts of the object belonging to layer aLayer.
Definition view_item.h:127
virtual std::vector< int > ViewGetLayers() const =0
Return the all the layers within the VIEW the object is painted on.
virtual wxString GetClass() const =0
Return the class name.
virtual double ViewGetLOD(int aLayer, const VIEW *aView) const
Return the level of detail (LOD) of the item.
Definition view_item.h:155
VIEW_ITEM_DATA * m_viewPrivData
Definition view_item.h:209
Hold a (potentially large) number of VIEW_ITEMs and renders them on a graphics device provided by the...
Definition view.h:67
double GetScale() const
Definition view.h:277
void SetMirror(bool aMirrorX, bool aMirrorY)
Control the mirroring of the VIEW.
Definition view.cpp:562
void ShowPreview(bool aShow=true)
Definition view.cpp:1758
double m_maxScale
Definition view.h:883
BOX2D GetViewport() const
Return the current viewport visible area rectangle.
Definition view.cpp:535
void CopySettings(const VIEW *aOtherView)
Copy layers and visibility settings from another view.
Definition view.cpp:510
virtual void SetScale(double aScale, VECTOR2D aAnchor={ 0, 0 })
Set the scaling factor, zooming around a given anchor point.
Definition view.cpp:575
static constexpr int TOP_LAYER_MODIFIER
Rendering order modifier for layers that are marked as top layers.
Definition view.h:749
void draw(VIEW_ITEM *aItem, int aLayer, bool aImmediate=false)
Draw an item, but on a specified layers.
Definition view.cpp:1058
bool m_reverseDrawOrder
Flag to reverse the draw order when using draw priority.
Definition view.h:904
void UpdateAllLayersOrder()
Do everything that is needed to apply the rendering order of layers.
Definition view.cpp:911
void updateItemColor(VIEW_ITEM *aItem, int aLayer)
Update colors that are used for an item to be drawn.
Definition view.cpp:1307
void SetViewport(const BOX2D &aViewport)
Set the visible area of the VIEW.
Definition view.cpp:547
void SetRequired(int aLayerId, int aRequiredId, bool aRequired=true)
Mark the aRequiredId layer as required for the aLayerId layer.
Definition view.cpp:413
VECTOR2D ToScreen(const VECTOR2D &aCoord, bool aAbsolute=true) const
Convert a world space point/vector to a point/vector in screen space coordinates.
Definition view.cpp:491
static bool compareRenderingOrder(VIEW_LAYER *aI, VIEW_LAYER *aJ)
Determine rendering order of layers. Used in display order sorting function.
Definition view.h:844
int GetLayerOrder(int aLayer) const
Return rendering order of a particular layer.
Definition view.cpp:669
virtual void Add(VIEW_ITEM *aItem, int aDrawPriority=-1)
Add a VIEW_ITEM to the view.
Definition view.cpp:299
virtual void Remove(VIEW_ITEM *aItem)
Remove a VIEW_ITEM from the view.
Definition view.cpp:342
void ClearTargets()
Clear targets that are marked as dirty.
Definition view.cpp:1173
virtual void EnableTopLayer(bool aEnable)
Enable or disable display of the top layer.
Definition view.cpp:871
bool m_mirrorX
Definition view.h:885
void UpdateAllLayersColor()
Apply the new coloring scheme to all layers.
Definition view.cpp:780
bool m_mirrorY
Definition view.h:886
std::shared_ptr< std::vector< VIEW_ITEM * > > m_allItems
Flat list of all items.
Definition view.h:872
std::shared_ptr< VIEW_OVERLAY > MakeOverlay()
Definition view.cpp:1713
void SetGAL(GAL *aGal)
Assign a rendering device for the VIEW.
Definition view.cpp:516
int Query(const BOX2I &aRect, std::vector< LAYER_ITEM_PAIR > &aResult) const
Find all visible items that touch or are within the rectangle aRect.
Definition view.cpp:425
std::vector< VIEW_ITEM * > m_ownedItems
Definition view.h:860
virtual void Update(const VIEW_ITEM *aItem, int aUpdateFlags) const
For dynamic VIEWs, inform the associated VIEW that the graphical representation of this item has chan...
Definition view.cpp:1700
void invalidateItem(VIEW_ITEM *aItem, int aUpdateFlags)
Manage dirty flags & redraw queuing when updating an item.
Definition view.cpp:1253
const VECTOR2I & GetScreenPixelSize() const
Return the size of the our rendering area in pixels.
Definition view.cpp:1217
bool HasItem(const VIEW_ITEM *aItem) const
Indicates whether or not the given item has been added to the view.
Definition view.cpp:1686
virtual int GetTopLayer() const
Definition view.cpp:835
PAINTER * m_painter
PAINTER contains information how do draw items.
Definition view.h:889
virtual void Redraw()
Immediately redraws the whole view.
Definition view.cpp:1192
void Clear()
Remove all items from the view.
Definition view.cpp:1148
std::set< unsigned int > m_topLayers
The set of layers that are displayed on the top.
Definition view.h:875
bool m_enableOrderModifier
Whether to use rendering order modifier or not.
Definition view.h:863
VECTOR2D ToWorld(const VECTOR2D &aCoord, bool aAbsolute=true) const
Converts a screen space point/vector to a point/vector in world space coordinates.
Definition view.cpp:472
bool IsHiddenOnOverlay(const VIEW_ITEM *aItem) const
Definition view.cpp:1678
void ClearTopLayers()
Remove all layers from the on-the-top set (they are no longer displayed over the rest of layers).
Definition view.cpp:896
void InitPreview()
Definition view.cpp:1737
void ClearPreview()
Definition view.cpp:1722
void MarkClean()
Force redraw of view on the next rendering.
Definition view.h:669
void updateItemGeometry(VIEW_ITEM *aItem, int aLayer)
Update all information needed to draw an item.
Definition view.cpp:1325
static constexpr int VIEW_MAX_LAYERS
Maximum number of layers that may be shown.
Definition view.h:746
double m_minScale
Definition view.h:882
double m_scale
Definition view.h:880
void updateLayers(VIEW_ITEM *aItem)
Update set of layers that an item occupies.
Definition view.cpp:1387
void RecacheAllItems()
Rebuild GAL display lists.
Definition view.cpp:1466
bool areRequiredLayersEnabled(int aLayerId) const
Check if every layer required by the aLayerId layer is enabled.
Definition view.cpp:1442
bool IsTargetDirty(int aTarget) const
Return true if any of layers belonging to the target or the target itself should be redrawn.
Definition view.h:629
int m_nextDrawPriority
The next sequential drawing priority.
Definition view.h:901
void sortOrderedLayers()
Sort m_orderedLayers when layer rendering order has changed.
Definition view.cpp:1292
bool m_useDrawPriority
Flag to respect draw priority when drawing items.
Definition view.h:898
void UpdateItems()
Iterate through the list of items that asked for updating and updates them.
Definition view.cpp:1483
bool IsCached(int aLayer) const
Return true if the layer is cached.
Definition view.h:647
std::pair< VIEW_ITEM *, int > LAYER_ITEM_PAIR
Definition view.h:71
void SortLayers(std::vector< int > &aLayers) const
Change the order of given layer ids, so after sorting the order corresponds to layers rendering order...
Definition view.cpp:675
void UpdateAllItems(int aUpdateFlags)
Update all items in the view according to the given flags.
Definition view.cpp:1576
std::unique_ptr< KIGFX::VIEW_GROUP > m_preview
Definition view.h:859
static void OnDestroy(VIEW_ITEM *aItem)
Nasty hack, invoked by the destructor of VIEW_ITEM to auto-remove the item from the owning VIEW if th...
Definition view.cpp:227
virtual ~VIEW()
Definition view.cpp:293
GAL * m_gal
Interface to PAINTER that is used to draw items.
Definition view.h:892
std::map< int, VIEW_LAYER > m_layers
The set of possible displayed layers and its properties.
Definition view.h:866
std::unique_ptr< VIEW > DataReference() const
Return a new VIEW object that shares the same set of VIEW_ITEMs and LAYERs.
Definition view.cpp:1617
void Hide(VIEW_ITEM *aItem, bool aHide=true, bool aHideOverlay=false)
Temporarily hide the item in the view (e.g.
Definition view.cpp:1648
virtual void SetTopLayer(int aLayer, bool aEnabled=true)
Set given layer to be displayed on the top or sets back the default order of layers.
Definition view.cpp:844
void AddToPreview(VIEW_ITEM *aItem, bool aTakeOwnership=true)
Definition view.cpp:1744
void UpdateLayerColor(int aLayer)
Apply the new coloring scheme held by RENDER_SETTINGS in case that it has changed.
Definition view.cpp:759
void MarkDirty()
Force redraw of view on the next rendering.
Definition view.h:660
friend class VIEW_ITEM
Definition view.h:69
BOX2D m_boundary
Definition view.h:881
void SetCenter(const VECTOR2D &aCenter)
Set the center point of the VIEW (i.e.
Definition view.cpp:601
std::vector< VIEW_LAYER * > m_orderedLayers
Sorted list of pointers to members of m_layers.
Definition view.h:869
void clearGroupCache()
Clear cached GAL group numbers (ONLY numbers stored in VIEW_ITEMs, not group objects used by GAL).
Definition view.cpp:1241
VECTOR2D m_center
Center point of the VIEW (the point at which we are looking at).
Definition view.h:878
void SetLayerOrder(int aLayer, int aRenderingOrder)
Set rendering order of a particular layer.
Definition view.cpp:661
void MarkTargetDirty(int aTarget)
Set or clear target 'dirty' flag.
Definition view.h:640
void updateBbox(VIEW_ITEM *aItem)
Update bounding box of an item.
Definition view.cpp:1362
bool IsVisible(const VIEW_ITEM *aItem) const
Return information if the item is visible (or not).
Definition view.cpp:1670
void UpdateAllItemsConditionally(int aUpdateFlags, std::function< bool(VIEW_ITEM *)> aCondition)
Update items in the view according to the given flags and condition.
Definition view.cpp:1586
void SetVisible(VIEW_ITEM *aItem, bool aIsVisible=true)
Set the item visibility.
Definition view.cpp:1627
void ReorderLayerData(std::unordered_map< int, int > aReorderMap)
Remap the data between layer ids without invalidating that data.
Definition view.cpp:685
void redrawRect(const BOX2I &aRect)
Redraw contents within rectangle aRect.
Definition view.cpp:1015
VECTOR2< T > GetScale() const
Get the scale components of the matrix.
Definition matrix3x3.h:295
A small class to help profiling.
Definition profile.h:49
void Stop()
Save the time when this function was called, and set the counter stane to stop.
Definition profile.h:88
double msecs(bool aSinceLast=false)
Definition profile.h:149
Represent a set of closed polygons.
bool IsEmpty() const
Return true if the set is empty (no polygons at all)
void Deflate(int aAmount, CORNER_STRATEGY aCornerStrategy, int aMaxError)
void BooleanSubtract(const SHAPE_POLY_SET &b)
Perform boolean polyset difference.
const BOX2I BBox(int aClearance=0) const override
Compute a bounding box of the shape, with a margin of aClearance a collision.
@ ALLOW_ACUTE_CORNERS
just inflate the polygon. Acute angles create spikes
#define _(s)
const wxChar *const traceGalProfile
Flag to enable debug output of GAL performance profiling.
@ UNDEFINED_LAYER
Definition layer_ids.h:61
MATRIX3x3< double > MATRIX3x3D
Definition matrix3x3.h:473
The Cairo implementation of the graphics abstraction layer.
Definition eda_group.h:33
@ COLOR
Color has changed.
Definition view_item.h:54
@ INITIAL_ADD
Item is being added to the view.
Definition view_item.h:57
@ NONE
No updates are required.
Definition view_item.h:52
@ REPAINT
Item needs to be redrawn.
Definition view_item.h:58
@ APPEARANCE
Visibility flag has changed.
Definition view_item.h:53
@ GEOMETRY
Position or shape has changed.
Definition view_item.h:55
@ LAYERS
Layers have changed.
Definition view_item.h:56
@ ALL
All except INITIAL_ADD.
Definition view_item.h:59
@ TARGET_NONCACHED
Auxiliary rendering target (noncached)
Definition definitions.h:38
@ TARGET_CACHED
Main rendering target (cached)
Definition definitions.h:37
@ TARGET_OVERLAY
Items that may change while the view stays the same (noncached)
Definition definitions.h:39
@ HIDDEN
Item is temporarily hidden (usually in favor of a being drawn from an overlay, such as a SELECTION).
Definition view_item.h:70
@ OVERLAY_HIDDEN
Item is temporarily hidden from being drawn on an overlay.
Definition view_item.h:71
@ VISIBLE
Item is visible (in general)
Definition view_item.h:66
std::shared_ptr< PNS_LOG_VIEWER_OVERLAY > overlay
bool operator()(VIEW_ITEM *aItem)
Definition view.cpp:1230
bool operator()(VIEW_ITEM *aItem)
Definition view.cpp:955
DRAW_ITEM_VISITOR(VIEW *aView, int aLayer, bool aUseDrawPriority, bool aReverseDrawOrder)
Definition view.cpp:945
int layers[VIEW_MAX_LAYERS]
Definition view.cpp:1007
std::vector< VIEW_ITEM * > drawItems
Definition view.cpp:1009
bool operator()(VIEW_ITEM *aItem)
Definition view.cpp:1123
RECACHE_ITEM_VISITOR(VIEW *aView, GAL *aGal, int aLayer)
Definition view.cpp:1116
bool operator()(VIEW_ITEM *aItem)
Definition view.cpp:741
UPDATE_COLOR_VISITOR(int aLayer, PAINTER *aPainter, GAL *aGal)
Definition view.cpp:734
UPDATE_DEPTH_VISITOR(int aLayer, int aDepth, GAL *aGal)
Definition view.cpp:813
bool operator()(VIEW_ITEM *aItem)
Definition view.cpp:820
bool diffLayer
Layer should be drawn differentially over lower layers.
Definition view.h:758
int renderingOrder
Rendering order of this layer.
Definition view.h:763
bool hasNegatives
Layer should be drawn separately to not delete lower layers.
Definition view.h:761
bool visible
Is the layer to be rendered?
Definition view.h:754
bool displayOnly
Is the layer display only?
Definition view.h:755
std::shared_ptr< VIEW_RTREE > items
R-tree indexing all items on this layer.
Definition view.h:762
RENDER_TARGET target
Where the layer should be rendered.
Definition view.h:765
int id
Layer ID.
Definition view.h:764
int delta
wxLogTrace helper definitions.
#define KI_TRACE(aWhat,...)
VECTOR2< int32_t > VECTOR2I
Definition vector2d.h:695
VECTOR2< double > VECTOR2D
Definition vector2d.h:694