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 (C) 2021 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
32#include <view/view.h>
33#include <view/view_group.h>
34#include <view/view_item.h>
35#include <view/view_rtree.h>
36#include <view/view_overlay.h>
37
38#include <gal/definitions.h>
40#include <gal/painter.h>
41
42#include <core/profile.h>
43
44#ifdef KICAD_GAL_PROFILE
45#include <wx/log.h>
46#endif
47
48namespace KIGFX {
49
50class VIEW;
51
53{
54public:
56 m_view( nullptr ),
59 m_drawPriority( 0 ),
60 m_cachedIndex( -1 ),
61 m_groups( nullptr ),
62 m_groupsSize( 0 ) {}
63
65 {
67 }
68
69 int GetFlags() const
70 {
71 return m_flags;
72 }
73
74private:
75 friend class VIEW;
76
83 int getGroup( int aLayer ) const
84 {
85 for( int i = 0; i < m_groupsSize; ++i )
86 {
87 if( m_groups[i].first == aLayer )
88 return m_groups[i].second;
89 }
90
91 return -1;
92 }
93
100 void setGroup( int aLayer, int aGroup )
101 {
102 // Look if there is already an entry for the layer
103 for( int i = 0; i < m_groupsSize; ++i )
104 {
105 if( m_groups[i].first == aLayer )
106 {
107 m_groups[i].second = aGroup;
108 return;
109 }
110 }
111
112 // If there was no entry for the given layer - create one
113 std::pair<int, int>* newGroups = new std::pair<int, int>[m_groupsSize + 1];
114
115 if( m_groupsSize > 0 )
116 {
117 std::copy( m_groups, m_groups + m_groupsSize, newGroups );
118 delete[] m_groups;
119 }
120
121 m_groups = newGroups;
122 newGroups[m_groupsSize++] = { aLayer, aGroup };
123 }
124
125
130 {
131 delete[] m_groups;
132 m_groups = nullptr;
133 m_groupsSize = 0;
134 }
135
136
142 inline bool storesGroups() const
143 {
144 return m_groupsSize > 0;
145 }
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 wxT( "Invalid layer number" ) );
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 ),
248 m_nextDrawPriority( 0 ),
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
308 || aItem->m_viewPrivData->m_view == this,
309 wxS( "Already in a different view!" ) );
310
311 aItem->m_viewPrivData->m_view = this;
312 aItem->m_viewPrivData->m_drawPriority = aDrawPriority;
313 const BOX2I bbox = aItem->ViewBBox();
314 aItem->m_viewPrivData->m_bbox = bbox;
315 aItem->m_viewPrivData->m_cachedIndex = m_allItems->size();
316
317 std::vector<int> layers = aItem->ViewGetLayers();
318 aItem->viewPrivData()->saveLayers( layers );
319
320 m_allItems->push_back( aItem );
321
322 for( int layer : layers )
323 {
324 VIEW_LAYER& l = m_layers[layer];
325 l.items->Insert( aItem, bbox );
327 }
328
329 SetVisible( aItem, true );
330 Update( aItem, KIGFX::INITIAL_ADD );
331}
332
333
335{
336 static int s_gcCounter = 0;
337
338 if( aItem && aItem->m_viewPrivData )
339 {
340 wxCHECK( aItem->m_viewPrivData->m_view == this, /*void*/ );
341
342 std::vector<VIEW_ITEM*>::iterator item = m_allItems->end();
343 int cachedIndex = aItem->m_viewPrivData->m_cachedIndex;
344
345 if( cachedIndex >= 0 && cachedIndex < static_cast<ssize_t>( m_allItems->size() )
346 && ( *m_allItems )[cachedIndex] == aItem )
347 {
348 item = m_allItems->begin() + cachedIndex;
349 }
350 else
351 {
352 item = std::find( m_allItems->begin(), m_allItems->end(), aItem );
353 }
354
355 if( item != m_allItems->end() )
356 {
357 *item = nullptr;
359
360 s_gcCounter++;
361
362 if( s_gcCounter > 4096 )
363 {
364 // Perform defragmentation
366 []( VIEW_ITEM* it )
367 {
368 return it == nullptr;
369 } );
370
371 // Update cached indices
372 for( size_t idx = 0; idx < m_allItems->size(); idx++ )
373 ( *m_allItems )[idx]->m_viewPrivData->m_cachedIndex = idx;
374
375 s_gcCounter = 0;
376 }
377 }
378
379 const BOX2I* bbox = &aItem->m_viewPrivData->m_bbox;
380
381 for( int layer : aItem->m_viewPrivData->m_layers )
382 {
383 VIEW_LAYER& l = m_layers[layer];
384 l.items->Remove( aItem, bbox );
386
387 // Clear the GAL cache
388 int prevGroup = aItem->m_viewPrivData->getGroup( layer );
389
390 if( prevGroup >= 0 )
391 m_gal->DeleteGroup( prevGroup );
392 }
393
395 aItem->m_viewPrivData->m_view = nullptr;
396 }
397}
398
399
400void VIEW::SetRequired( int aLayerId, int aRequiredId, bool aRequired )
401{
402 wxCHECK( (unsigned) aLayerId < m_layers.size(), /*void*/ );
403 wxCHECK( (unsigned) aRequiredId < m_layers.size(), /*void*/ );
404
405 if( aRequired )
406 m_layers[aLayerId].requiredLayers.insert( aRequiredId );
407 else
408 m_layers[aLayerId].requiredLayers.erase( aRequired );
409}
410
411
412int VIEW::Query( const BOX2I& aRect, std::vector<LAYER_ITEM_PAIR>& aResult ) const
413{
414 if( m_orderedLayers.empty() )
415 return 0;
416
417 int layer = UNDEFINED_LAYER;
418 auto visitor =
419 [&]( VIEW_ITEM* item ) -> bool
420 {
421 aResult.push_back( VIEW::LAYER_ITEM_PAIR( item, layer ) );
422 return true;
423 };
424
425 std::vector<VIEW_LAYER*>::const_reverse_iterator i;
426
427 // execute queries in reverse direction, so that items that are on the top of
428 // the rendering stack are returned first.
429 for( i = m_orderedLayers.rbegin(); i != m_orderedLayers.rend(); ++i )
430 {
431 // ignore layers that do not contain actual items (i.e. the selection box, menus, floats)
432 if( ( *i )->displayOnly || !( *i )->visible )
433 continue;
434
435 layer = ( *i )->id;
436 ( *i )->items->Query( aRect, visitor );
437 }
438
439 return aResult.size();
440}
441
442
443void VIEW::Query( const BOX2I& aRect, const std::function<bool( VIEW_ITEM* )>& aFunc ) const
444{
445 if( m_orderedLayers.empty() )
446 return;
447
448 for( const auto& i : m_orderedLayers )
449 {
450 // ignore layers that do not contain actual items (i.e. the selection box, menus, floats)
451 if( i->displayOnly || !i->visible )
452 continue;
453
454 i->items->Query( aRect, aFunc );
455 }
456}
457
458
459VECTOR2D VIEW::ToWorld( const VECTOR2D& aCoord, bool aAbsolute ) const
460{
461 const MATRIX3x3D& matrix = m_gal->GetScreenWorldMatrix();
462
463 if( aAbsolute )
464 return VECTOR2D( matrix * aCoord );
465 else
466 return VECTOR2D( matrix.GetScale().x * aCoord.x, matrix.GetScale().y * aCoord.y );
467}
468
469
470double VIEW::ToWorld( double aSize ) const
471{
472 const MATRIX3x3D& matrix = m_gal->GetScreenWorldMatrix();
473
474 return fabs( matrix.GetScale().x * aSize );
475}
476
477
478VECTOR2D VIEW::ToScreen( const VECTOR2D& aCoord, bool aAbsolute ) const
479{
480 const MATRIX3x3D& matrix = m_gal->GetWorldScreenMatrix();
481
482 if( aAbsolute )
483 return VECTOR2D( matrix * aCoord );
484 else
485 return VECTOR2D( matrix.GetScale().x * aCoord.x, matrix.GetScale().y * aCoord.y );
486}
487
488
489double VIEW::ToScreen( double aSize ) const
490{
491 const MATRIX3x3D& matrix = m_gal->GetWorldScreenMatrix();
492
493 return matrix.GetScale().x * aSize;
494}
495
496
497void VIEW::CopySettings( const VIEW* aOtherView )
498{
499 wxASSERT_MSG( false, wxT( "This is not implemented" ) );
500}
501
502
503void VIEW::SetGAL( GAL* aGal )
504{
505 bool recacheGroups = ( m_gal != nullptr ); // recache groups only if GAL is reassigned
506 m_gal = aGal;
507
508 // clear group numbers, so everything is going to be recached
509 if( recacheGroups )
511
512 // every target has to be refreshed
513 MarkDirty();
514
515 // force the new GAL to display the current viewport.
517 SetScale( m_scale );
519}
520
521
523{
524 BOX2D rect;
525 VECTOR2D screenSize = m_gal->GetScreenPixelSize();
526
527 rect.SetOrigin( ToWorld( VECTOR2D( 0, 0 ) ) );
528 rect.SetEnd( ToWorld( screenSize ) );
529
530 return rect.Normalize();
531}
532
533
534void VIEW::SetViewport( const BOX2D& aViewport )
535{
536 VECTOR2D ssize = ToWorld( m_gal->GetScreenPixelSize(), false );
537
538 wxCHECK( fabs(ssize.x) > 0 && fabs(ssize.y) > 0, /*void*/ );
539
540 VECTOR2D centre = aViewport.Centre();
541 VECTOR2D vsize = aViewport.GetSize();
542 double zoom = 1.0 / std::max( fabs( vsize.x / ssize.x ), fabs( vsize.y / ssize.y ) );
543
544 SetCenter( centre );
545 SetScale( GetScale() * zoom );
546}
547
548
549void VIEW::SetMirror( bool aMirrorX, bool aMirrorY )
550{
551 wxASSERT_MSG( !aMirrorY, _( "Mirroring for Y axis is not supported yet" ) );
552
553 m_mirrorX = aMirrorX;
554 m_mirrorY = aMirrorY;
555 m_gal->SetFlip( aMirrorX, aMirrorY );
556
557 // Redraw everything
558 MarkDirty();
559}
560
561
562void VIEW::SetScale( double aScale, VECTOR2D aAnchor )
563{
564 if( aAnchor == VECTOR2D( 0, 0 ) )
565 aAnchor = m_center;
566
567 VECTOR2D a = ToScreen( aAnchor );
568
569 if( aScale < m_minScale )
571 else if( aScale > m_maxScale )
573 else
574 m_scale = aScale;
575
578
579 VECTOR2D delta = ToWorld( a ) - aAnchor;
580
582
583 // Redraw everything after the viewport has changed
584 MarkDirty();
585}
586
587
588void VIEW::SetCenter( const VECTOR2D& aCenter )
589{
590 m_center = aCenter;
591
592 if( !m_boundary.Contains( aCenter ) )
593 {
594 if( m_center.x < m_boundary.GetLeft() )
596 else if( aCenter.x > m_boundary.GetRight() )
598
599 if( m_center.y < m_boundary.GetTop() )
601 else if( m_center.y > m_boundary.GetBottom() )
603 }
604
607
608 // Redraw everything after the viewport has changed
609 MarkDirty();
610}
611
612
613void VIEW::SetCenter( const VECTOR2D& aCenter, const std::vector<BOX2D>& obscuringScreenRects )
614{
615 if( obscuringScreenRects.empty() )
616 return SetCenter( aCenter );
617
618 BOX2D screenRect( { 0, 0 }, m_gal->GetScreenPixelSize() );
619 SHAPE_POLY_SET unobscuredPoly( screenRect );
620 VECTOR2D unobscuredCenter = screenRect.Centre();
621
622 for( const BOX2D& obscuringScreenRect : obscuringScreenRects )
623 {
624 SHAPE_POLY_SET obscuringPoly( obscuringScreenRect );
625 unobscuredPoly.BooleanSubtract( obscuringPoly, SHAPE_POLY_SET::PM_FAST );
626 }
627
628 /*
629 * Perform a step-wise deflate to find the center of the largest unobscured area
630 */
631
632 BOX2I bbox = unobscuredPoly.BBox();
633 int step = std::min( bbox.GetWidth(), bbox.GetHeight() ) / 10;
634
635 if( step < 20 )
636 step = 20;
637
638 while( !unobscuredPoly.IsEmpty() )
639 {
640 unobscuredCenter = unobscuredPoly.BBox().Centre();
641 unobscuredPoly.Deflate( step, CORNER_STRATEGY::ALLOW_ACUTE_CORNERS, ARC_LOW_DEF );
642 }
643
644 SetCenter( aCenter - ToWorld( unobscuredCenter - screenRect.Centre(), false ) );
645}
646
647
648void VIEW::SetLayerOrder( int aLayer, int aRenderingOrder )
649{
650 m_layers[aLayer].renderingOrder = aRenderingOrder;
651
653}
654
655
656int VIEW::GetLayerOrder( int aLayer ) const
657{
658 return m_layers.at( aLayer ).renderingOrder;
659}
660
661
662void VIEW::SortLayers( std::vector<int> aLayers ) const
663{
664 std::sort( aLayers.begin(), aLayers.end(),
665 [this]( int a, int b )
666 {
667 return GetLayerOrder( a ) < GetLayerOrder( b );
668 } );
669}
670
671
672void VIEW::ReorderLayerData( std::unordered_map<int, int> aReorderMap )
673{
674 std::map<int,VIEW_LAYER> new_map;
675
676 for( auto& [_, layer] : m_layers )
677 {
678 auto reorder_it = aReorderMap.find( layer.id );
679
680 // If the layer is not in the reorder map or if it is mapped to itself,
681 // just copy the layer to the new map.
682 if( reorder_it == aReorderMap.end() || reorder_it->second == layer.id )
683 {
684 new_map.emplace( layer.id, layer );
685 continue;
686 }
687
688 auto [new_it,__] = new_map.emplace( reorder_it->second, layer );
689 new_it->second.id = reorder_it->second;
690 }
691
692 // Transfer reordered data (using the copy assignment operator ):
693 m_layers = new_map;
694
695 for( VIEW_ITEM* item : *m_allItems )
696 {
697 if( !item )
698 continue;
699
700 VIEW_ITEM_DATA* viewData = item->viewPrivData();
701
702 if( !viewData )
703 continue;
704
705 std::vector<int> layers = item->ViewGetLayers();
706 viewData->saveLayers( layers );
707
708 viewData->reorderGroups( aReorderMap );
709
710 viewData->m_requiredUpdate |= COLOR;
711 }
712
713 UpdateItems();
714}
715
716
718{
719 UPDATE_COLOR_VISITOR( int aLayer, PAINTER* aPainter, GAL* aGal ) :
720 layer( aLayer ),
721 painter( aPainter ),
722 gal( aGal )
723 {
724 }
725
726 bool operator()( VIEW_ITEM* aItem )
727 {
728 // Obtain the color that should be used for coloring the item
729 const COLOR4D color = painter->GetSettings()->GetColor( aItem, layer );
730 int group = aItem->viewPrivData()->getGroup( layer );
731
732 if( group >= 0 )
734
735 return true;
736 }
737
738 int layer;
741};
742
743
744void VIEW::UpdateLayerColor( int aLayer )
745{
746 // There is no point in updating non-cached layers
747 if( !IsCached( aLayer ) )
748 return;
749
750 BOX2I r;
751
752 r.SetMaximum();
753
754 if( m_gal->IsVisible() )
755 {
757
758 UPDATE_COLOR_VISITOR visitor( aLayer, m_painter, m_gal );
759 m_layers[aLayer].items->Query( r, visitor );
760 MarkTargetDirty( m_layers[aLayer].target );
761 }
762}
763
764
766{
767 if( m_gal->IsVisible() )
768 {
770
771 for( VIEW_ITEM* item : *m_allItems )
772 {
773 if( !item )
774 continue;
775
776 VIEW_ITEM_DATA* viewData = item->viewPrivData();
777
778 if( !viewData )
779 continue;
780
781 for( int layer : viewData->m_layers )
782 {
783 const COLOR4D color = m_painter->GetSettings()->GetColor( item, layer );
784 int group = viewData->getGroup( layer );
785
786 if( group >= 0 )
788 }
789 }
790 }
791
792 MarkDirty();
793}
794
795
797{
798 UPDATE_DEPTH_VISITOR( int aLayer, int aDepth, GAL* aGal ) :
799 layer( aLayer ),
800 depth( aDepth ),
801 gal( aGal )
802 {
803 }
804
805 bool operator()( VIEW_ITEM* aItem )
806 {
807 int group = aItem->viewPrivData()->getGroup( layer );
808
809 if( group >= 0 )
811
812 return true;
813 }
814
817};
818
819
821{
822 if( m_topLayers.size() == 0 )
823 return 0;
824
825 return *m_topLayers.begin();
826}
827
828
829void VIEW::SetTopLayer( int aLayer, bool aEnabled )
830{
831 if( aEnabled )
832 {
833 if( m_topLayers.count( aLayer ) == 1 )
834 return;
835
836 m_topLayers.insert( aLayer );
837
838 // Move the layer closer to front
840 m_layers[aLayer].renderingOrder += TOP_LAYER_MODIFIER;
841 }
842 else
843 {
844 if( m_topLayers.count( aLayer ) == 0 )
845 return;
846
847 m_topLayers.erase( aLayer );
848
849 // Restore the previous rendering order
851 m_layers[aLayer].renderingOrder -= TOP_LAYER_MODIFIER;
852 }
853}
854
855
856void VIEW::EnableTopLayer( bool aEnable )
857{
858 if( aEnable == m_enableOrderModifier )
859 return;
860
861 m_enableOrderModifier = aEnable;
862
863 std::set<unsigned int>::iterator it;
864
865 if( aEnable )
866 {
867 for( it = m_topLayers.begin(); it != m_topLayers.end(); ++it )
868 m_layers[*it].renderingOrder += TOP_LAYER_MODIFIER;
869 }
870 else
871 {
872 for( it = m_topLayers.begin(); it != m_topLayers.end(); ++it )
873 m_layers[*it].renderingOrder -= TOP_LAYER_MODIFIER;
874 }
875
878}
879
880
882{
883 std::set<unsigned int>::iterator it;
884
886 {
887 // Restore the previous rendering order for layers that were marked as top
888 for( it = m_topLayers.begin(); it != m_topLayers.end(); ++it )
889 m_layers[*it].renderingOrder -= TOP_LAYER_MODIFIER;
890 }
891
892 m_topLayers.clear();
893}
894
895
897{
899
900 if( m_gal->IsVisible() )
901 {
903
904 for( VIEW_ITEM* item : *m_allItems )
905 {
906 if( !item )
907 continue;
908
909 VIEW_ITEM_DATA* viewData = item->viewPrivData();
910
911 if( !viewData )
912 continue;
913
914 for( int layer : viewData->m_layers )
915 {
916 int group = viewData->getGroup( layer );
917
918 if( group >= 0 )
919 m_gal->ChangeGroupDepth( group, m_layers[layer].renderingOrder );
920 }
921 }
922 }
923
924 MarkDirty();
925}
926
927
929{
930 DRAW_ITEM_VISITOR( VIEW* aView, int aLayer, bool aUseDrawPriority, bool aReverseDrawOrder ) :
931 view( aView ),
932 layer( aLayer ),
933 useDrawPriority( aUseDrawPriority ),
934 reverseDrawOrder( aReverseDrawOrder ),
935 drawForcedTransparent( false ),
937 {
938 }
939
940 bool operator()( VIEW_ITEM* aItem )
941 {
942 wxCHECK( aItem->viewPrivData(), false );
943
945 {
947 return true;
948 }
949
950 // Conditions that have to be fulfilled for an item to be drawn
951 bool drawCondition = aItem->viewPrivData()->isRenderable()
952 && aItem->ViewGetLOD( layer, view ) < view->m_scale;
953 if( !drawCondition )
954 return true;
955
956 if( useDrawPriority )
957 drawItems.push_back( aItem );
958 else
959 view->draw( aItem, layer );
960
961 return true;
962 }
963
965 {
966 if( reverseDrawOrder )
967 {
968 std::sort( drawItems.begin(), drawItems.end(),
969 []( VIEW_ITEM* a, VIEW_ITEM* b ) -> bool
970 {
971 return b->viewPrivData()->m_drawPriority < a->viewPrivData()->m_drawPriority;
972 } );
973 }
974 else
975 {
976 std::sort( drawItems.begin(), drawItems.end(),
977 []( VIEW_ITEM* a, VIEW_ITEM* b ) -> bool
978 {
979 return a->viewPrivData()->m_drawPriority < b->viewPrivData()->m_drawPriority;
980 } );
981 }
982
983 for( VIEW_ITEM* item : drawItems )
984 view->draw( item, layer );
985 }
986
990 std::vector<VIEW_ITEM*> drawItems;
993};
994
995
996void VIEW::redrawRect( const BOX2I& aRect )
997{
998 for( VIEW_LAYER* l : m_orderedLayers )
999 {
1000 if( l->visible && IsTargetDirty( l->target ) && areRequiredLayersEnabled( l->id ) )
1001 {
1002 DRAW_ITEM_VISITOR drawFunc( this, l->id, m_useDrawPriority, m_reverseDrawOrder );
1003
1004 m_gal->SetTarget( l->target );
1005 m_gal->SetLayerDepth( l->renderingOrder );
1006
1007 // Differential layer also work for the negatives, since both special layer types
1008 // will composite on separate layers (at least in Cairo)
1009 if( l->diffLayer )
1011 else if( l->hasNegatives )
1013
1014 l->items->Query( aRect, drawFunc );
1015
1016 if( m_useDrawPriority )
1017 drawFunc.deferredDraw();
1018
1019 if( l->diffLayer )
1021 else if( l->hasNegatives )
1023
1024 if( drawFunc.foundForcedTransparent )
1025 {
1026 drawFunc.drawForcedTransparent = true;
1027
1029 m_gal->EnableDepthTest( true );
1030 m_gal->SetLayerDepth( l->renderingOrder );
1031
1032 l->items->Query( aRect, drawFunc );
1033 }
1034 }
1035 }
1036}
1037
1038
1039void VIEW::draw( VIEW_ITEM* aItem, int aLayer, bool aImmediate )
1040{
1041 VIEW_ITEM_DATA* viewData = aItem->viewPrivData();
1042
1043 if( !viewData )
1044 return;
1045
1046 if( IsCached( aLayer ) && !aImmediate )
1047 {
1048 // Draw using cached information or create one
1049 int group = viewData->getGroup( aLayer );
1050
1051 if( group >= 0 )
1052 m_gal->DrawGroup( group );
1053 else
1054 Update( aItem );
1055 }
1056 else
1057 {
1058 // Immediate mode
1059 if( !m_painter->Draw( aItem, aLayer ) )
1060 aItem->ViewDraw( aLayer, this ); // Alternative drawing method
1061 }
1062}
1063
1064
1065void VIEW::draw( VIEW_ITEM* aItem, bool aImmediate )
1066{
1067 std::vector<int> layers = aItem->ViewGetLayers();
1068
1069 // Sorting is needed for drawing order dependent GALs (like Cairo)
1070 if( !m_gal || !m_gal->IsOpenGlEngine())
1071 SortLayers( layers );
1072
1073 for( int layer : layers )
1074 {
1075 auto it = m_layers.find( layer );
1076
1077 if( it == m_layers.end() )
1078 continue;
1079
1080 m_gal->SetLayerDepth( it->second.renderingOrder );
1081 draw( aItem, layer, aImmediate );
1082 }
1083}
1084
1085
1086void VIEW::draw( VIEW_GROUP* aGroup, bool aImmediate )
1087{
1088 for( unsigned int i = 0; i < aGroup->GetSize(); i++)
1089 draw( aGroup->GetItem(i), aImmediate );
1090}
1091
1092
1094{
1095 RECACHE_ITEM_VISITOR( VIEW* aView, GAL* aGal, int aLayer ) :
1096 view( aView ),
1097 gal( aGal ),
1098 layer( aLayer )
1099 {
1100 }
1101
1102 bool operator()( VIEW_ITEM* aItem )
1103 {
1104 VIEW_ITEM_DATA* viewData = aItem->viewPrivData();
1105
1106 if( !viewData )
1107 return false;
1108
1109 // Remove previously cached group
1110 int group = viewData->getGroup( layer );
1111
1112 if( group >= 0 )
1113 gal->DeleteGroup( group );
1114
1115 viewData->setGroup( layer, -1 );
1116 view->Update( aItem );
1117
1118 return true;
1119 }
1120
1124};
1125
1126
1128{
1129 BOX2I r;
1130 r.SetMaximum();
1131 m_allItems->clear();
1132
1133 for( auto& [_, layer] : m_layers )
1134 layer.items->RemoveAll();
1135
1137
1138 m_gal->ClearCache();
1139}
1140
1141
1143{
1145 {
1146 // TARGET_CACHED and TARGET_NONCACHED have to be redrawn together, as they contain
1147 // layers that rely on each other (eg. netnames are noncached, but tracks - are cached)
1150
1151 MarkDirty();
1152 }
1153
1155 {
1157 }
1158}
1159
1160
1162{
1163#ifdef KICAD_GAL_PROFILE
1164 PROF_TIMER totalRealTime;
1165#endif /* KICAD_GAL_PROFILE */
1166
1167 VECTOR2D screenSize = m_gal->GetScreenPixelSize();
1168 BOX2D rect( ToWorld( VECTOR2D( 0, 0 ) ),
1169 ToWorld( screenSize ) - ToWorld( VECTOR2D( 0, 0 ) ) );
1170
1171 rect.Normalize();
1172 BOX2I recti = BOX2ISafe( rect );
1173
1174 redrawRect( recti );
1175
1176 // All targets were redrawn, so nothing is dirty
1177 MarkClean();
1178
1179#ifdef KICAD_GAL_PROFILE
1180 totalRealTime.Stop();
1181 wxLogTrace( traceGalProfile, wxS( "VIEW::Redraw(): %.1f ms" ), totalRealTime.msecs() );
1182#endif /* KICAD_GAL_PROFILE */
1183}
1184
1185
1187{
1188 return m_gal->GetScreenPixelSize();
1189}
1190
1191
1193{
1195 view( aView )
1196 {
1197 }
1198
1199 bool operator()( VIEW_ITEM* aItem )
1200 {
1201 aItem->viewPrivData()->deleteGroups();
1202
1203 return true;
1204 }
1205
1207};
1208
1209
1211{
1212 BOX2I r;
1213
1214 r.SetMaximum();
1215 CLEAR_LAYER_CACHE_VISITOR visitor( this );
1216
1217 for( auto& [_, layer] : m_layers )
1218 layer.items->Query( r, visitor );
1219}
1220
1221
1222void VIEW::invalidateItem( VIEW_ITEM* aItem, int aUpdateFlags )
1223{
1224 if( aUpdateFlags & INITIAL_ADD )
1225 {
1226 // Don't update layers or bbox, since it was done in VIEW::Add()
1227 // Now that we have initialized, set flags to ALL for the code below
1228 aUpdateFlags = ALL;
1229 }
1230 else
1231 {
1232 // updateLayers updates geometry too, so we do not have to update both of them at the
1233 // same time
1234 if( aUpdateFlags & LAYERS )
1235 updateLayers( aItem );
1236 else if( aUpdateFlags & GEOMETRY )
1237 updateBbox( aItem );
1238 }
1239
1240 std::vector<int> layers = aItem->ViewGetLayers();
1241
1242 // Iterate through layers used by the item and recache it immediately
1243 for( int layer : layers )
1244 {
1245 if( IsCached( layer ) )
1246 {
1247 if( aUpdateFlags & ( GEOMETRY | LAYERS | REPAINT ) )
1248 updateItemGeometry( aItem, layer );
1249 else if( aUpdateFlags & COLOR )
1250 updateItemColor( aItem, layer );
1251 }
1252
1253 // Mark those layers as dirty, so the VIEW will be refreshed
1254 MarkTargetDirty( m_layers[layer].target );
1255 }
1256
1257 aItem->viewPrivData()->clearUpdateFlags();
1258}
1259
1260
1262{
1263 int n = 0;
1264
1265 m_orderedLayers.resize( m_layers.size() );
1266
1267 for( auto& [layer_id, layer] : m_layers )
1268 m_orderedLayers[n++] = &layer;
1269
1271
1272 MarkDirty();
1273}
1274
1275
1276void VIEW::updateItemColor( VIEW_ITEM* aItem, int aLayer )
1277{
1278 VIEW_ITEM_DATA* viewData = aItem->viewPrivData();
1279 wxCHECK( IsCached( aLayer ), /*void*/ ); // This will check if the layer exists
1280
1281 if( !viewData )
1282 return;
1283
1284 // Obtain the color that should be used for coloring the item on the specific layerId
1285 const COLOR4D color = m_painter->GetSettings()->GetColor( aItem, aLayer );
1286 int group = viewData->getGroup( aLayer );
1287
1288 // Change the color, only if it has group assigned
1289 if( group >= 0 )
1291}
1292
1293
1294void VIEW::updateItemGeometry( VIEW_ITEM* aItem, int aLayer )
1295{
1296 VIEW_ITEM_DATA* viewData = aItem->viewPrivData();
1297
1298 if( !viewData )
1299 return;
1300
1301 auto it = m_layers.find( aLayer );
1302
1303 if( it == m_layers.end() )
1304 return;
1305
1306 VIEW_LAYER& l = it->second;
1307
1308 // Save the extra map lookup in IsCached by open coding here
1309 if( l.target != TARGET_CACHED )
1310 return;
1311
1312 m_gal->SetTarget( l.target );
1314
1315 // Redraw the item from scratch
1316 int group = viewData->getGroup( aLayer );
1317
1318 if( group >= 0 )
1320
1321 group = m_gal->BeginGroup();
1322 viewData->setGroup( aLayer, group );
1323
1324 if( !m_painter->Draw( aItem, aLayer ) )
1325 aItem->ViewDraw( aLayer, this ); // Alternative drawing method
1326
1327 m_gal->EndGroup();
1328}
1329
1330
1332{
1333 std::vector<int> layers = aItem->ViewGetLayers();
1334
1335 wxASSERT( aItem->m_viewPrivData ); //must have a viewPrivData
1336
1337 const BOX2I new_bbox = aItem->ViewBBox();
1338 const BOX2I* old_bbox = &aItem->m_viewPrivData->m_bbox;
1339 aItem->m_viewPrivData->m_bbox = new_bbox;
1340
1341 for( int layer : layers )
1342 {
1343 auto it = m_layers.find( layer );
1344
1345 if( it == m_layers.end() )
1346 continue;
1347
1348 VIEW_LAYER& l = it->second;
1349 l.items->Remove( aItem, old_bbox );
1350 l.items->Insert( aItem, new_bbox );
1352 }
1353}
1354
1355
1357{
1358 VIEW_ITEM_DATA* viewData = aItem->viewPrivData();
1359
1360 if( !viewData )
1361 return;
1362
1363 // Remove the item from previous layer set
1364 const BOX2I* old_bbox = &aItem->m_viewPrivData->m_bbox;
1365
1366 for( int layer : aItem->m_viewPrivData->m_layers )
1367 {
1368 auto it = m_layers.find( layer );
1369
1370 if( it == m_layers.end() )
1371 continue;
1372
1373 VIEW_LAYER& l = it->second;
1374 l.items->Remove( aItem, old_bbox );
1376
1377 if( IsCached( l.id ) )
1378 {
1379 // Redraw the item from scratch
1380 int prevGroup = viewData->getGroup( layer );
1381
1382 if( prevGroup >= 0 )
1383 {
1384 m_gal->DeleteGroup( prevGroup );
1385 viewData->setGroup( l.id, -1 );
1386 }
1387 }
1388 }
1389
1390 const BOX2I new_bbox = aItem->ViewBBox();
1391 aItem->m_viewPrivData->m_bbox = new_bbox;
1392
1393 // Add the item to new layer set
1394 std::vector<int> layers = aItem->ViewGetLayers();
1395 viewData->saveLayers( layers );
1396
1397 for( int layer : 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->Insert( aItem, new_bbox );
1407 }
1408}
1409
1410
1411bool VIEW::areRequiredLayersEnabled( int aLayerId ) const
1412{
1413 auto it = m_layers.find( aLayerId );
1414
1415 if( it == m_layers.end() )
1416 return false;
1417
1418 for( int layer : it->second.requiredLayers )
1419 {
1420 // That is enough if just one layer is not enabled
1421
1422 auto it2 = m_layers.find( layer );
1423
1424 if( it2 == m_layers.end() || !it2->second.visible )
1425 return false;
1426
1427 if( !areRequiredLayersEnabled( layer ) )
1428 return false;
1429 }
1430
1431 return true;
1432}
1433
1434
1436{
1437 BOX2I r;
1438
1439 r.SetMaximum();
1440
1441 for( const auto& [_, l] : m_layers )
1442 {
1443 if( IsCached( l.id ) )
1444 {
1445 RECACHE_ITEM_VISITOR visitor( this, m_gal, l.id );
1446 l.items->Query( r, visitor );
1447 }
1448 }
1449}
1450
1451
1453{
1454 if( !m_gal->IsVisible() || !m_gal->IsInitialized() )
1455 return;
1456
1457 unsigned int cntGeomUpdate = 0;
1458 bool anyUpdated = false;
1459
1460 for( VIEW_ITEM* item : *m_allItems )
1461 {
1462 if( !item )
1463 continue;
1464
1465 auto vpd = item->viewPrivData();
1466
1467 if( !vpd )
1468 continue;
1469
1470 if( vpd->m_requiredUpdate != NONE )
1471 {
1472 anyUpdated = true;
1473
1474 if( vpd->m_requiredUpdate & ( GEOMETRY | LAYERS ) )
1475 {
1476 cntGeomUpdate++;
1477 }
1478 }
1479 }
1480
1481 unsigned int cntTotal = m_allItems->size();
1482
1483 double ratio = (double) cntGeomUpdate / (double) cntTotal;
1484
1485 // Optimization to improve view update time. If a lot of items (say, 30%) have their
1486 // bboxes/geometry changed it's way faster (around 10 times) to rebuild the R-Trees
1487 // from scratch rather than update the bbox of each changed item. Pcbnew does multiple
1488 // full geometry updates during file load, this can save a solid 30 seconds on load time
1489 // for larger designs...
1490
1491 if( ratio > 0.3 )
1492 {
1493 auto allItems = *m_allItems;
1494
1495 // kill all Rtrees
1496 for( auto& [_, layer] : m_layers )
1497 layer.items->RemoveAll();
1498
1499 // and re-insert items from scratch
1500 for( VIEW_ITEM* item : allItems )
1501 {
1502 if( !item )
1503 continue;
1504
1505 const BOX2I bbox = item->ViewBBox();
1506 item->m_viewPrivData->m_bbox = bbox;
1507
1508 std::vector<int> layers = item->ViewGetLayers();
1509 item->viewPrivData()->saveLayers( layers );
1510
1511 for( int layer : layers )
1512 {
1513 auto it = m_layers.find( layer );
1514
1515 wxCHECK2_MSG( it != m_layers.end(), continue, wxS( "Invalid layer" ) );
1516
1517 VIEW_LAYER& l = it->second;
1518 l.items->Insert( item, bbox );
1520 }
1521
1522 item->viewPrivData()->m_requiredUpdate &= ~( LAYERS | GEOMETRY );
1523 }
1524 }
1525
1526 if( anyUpdated )
1527 {
1529
1530 for( VIEW_ITEM* item : *m_allItems.get() )
1531 {
1532 if( item && item->viewPrivData() && item->viewPrivData()->m_requiredUpdate != NONE )
1533 {
1534 invalidateItem( item, item->viewPrivData()->m_requiredUpdate );
1535 item->viewPrivData()->m_requiredUpdate = NONE;
1536 }
1537 }
1538 }
1539
1540 KI_TRACE( traceGalProfile, wxS( "View update: total items %u, geom %u anyUpdated %u\n" ), cntTotal,
1541 cntGeomUpdate, (unsigned) anyUpdated );
1542}
1543
1544
1545void VIEW::UpdateAllItems( int aUpdateFlags )
1546{
1547 for( VIEW_ITEM* item : *m_allItems )
1548 {
1549 if( item && item->viewPrivData() )
1550 item->viewPrivData()->m_requiredUpdate |= aUpdateFlags;
1551 }
1552}
1553
1554
1556 std::function<bool( VIEW_ITEM* )> aCondition )
1557{
1558 for( VIEW_ITEM* item : *m_allItems )
1559 {
1560 if( !item )
1561 continue;
1562
1563 if( aCondition( item ) )
1564 {
1565 if( item->viewPrivData() )
1566 item->viewPrivData()->m_requiredUpdate |= aUpdateFlags;
1567 }
1568 }
1569}
1570
1571
1572void VIEW::UpdateAllItemsConditionally( std::function<int( VIEW_ITEM* )> aItemFlagsProvider )
1573{
1574 for( VIEW_ITEM* item : *m_allItems )
1575 {
1576 if( !item )
1577 continue;
1578
1579 if( item->viewPrivData() )
1580 item->viewPrivData()->m_requiredUpdate |= aItemFlagsProvider( item );
1581 }
1582}
1583
1584
1585
1586std::unique_ptr<VIEW> VIEW::DataReference() const
1587{
1588 std::unique_ptr<VIEW> ret = std::make_unique<VIEW>();
1589 ret->m_allItems = m_allItems;
1590 ret->m_layers = m_layers;
1591 ret->sortOrderedLayers();
1592 return ret;
1593}
1594
1595
1596void VIEW::SetVisible( VIEW_ITEM* aItem, bool aIsVisible )
1597{
1598 VIEW_ITEM_DATA* viewData = aItem->viewPrivData();
1599
1600 if( !viewData )
1601 return;
1602
1603 bool cur_visible = viewData->m_flags & VISIBLE;
1604
1605 if( cur_visible != aIsVisible )
1606 {
1607 if( aIsVisible )
1608 viewData->m_flags |= VISIBLE;
1609 else
1610 viewData->m_flags &= ~VISIBLE;
1611
1612 Update( aItem, APPEARANCE | COLOR );
1613 }
1614}
1615
1616
1617void VIEW::Hide( VIEW_ITEM* aItem, bool aHide, bool aHideOverlay )
1618{
1619 VIEW_ITEM_DATA* viewData = aItem->viewPrivData();
1620
1621 if( !viewData )
1622 return;
1623
1624 if( !( viewData->m_flags & VISIBLE ) )
1625 return;
1626
1627 if( aHideOverlay )
1628 viewData->m_flags |= OVERLAY_HIDDEN;
1629
1630 if( aHide )
1631 viewData->m_flags |= HIDDEN;
1632 else
1633 viewData->m_flags &= ~( HIDDEN | OVERLAY_HIDDEN );
1634
1635 Update( aItem, APPEARANCE );
1636}
1637
1638
1639bool VIEW::IsVisible( const VIEW_ITEM* aItem ) const
1640{
1641 const VIEW_ITEM_DATA* viewData = aItem->viewPrivData();
1642
1643 return viewData && ( viewData->m_flags & VISIBLE );
1644}
1645
1646
1647bool VIEW::IsHiddenOnOverlay( const VIEW_ITEM* aItem ) const
1648{
1649 const VIEW_ITEM_DATA* viewData = aItem->viewPrivData();
1650
1651 return viewData && ( viewData->m_flags & OVERLAY_HIDDEN );
1652}
1653
1654
1655bool VIEW::HasItem( const VIEW_ITEM* aItem ) const
1656{
1657 const VIEW_ITEM_DATA* viewData = aItem->viewPrivData();
1658
1659 return viewData && viewData->m_view == this;
1660}
1661
1662
1663void VIEW::Update( const VIEW_ITEM* aItem ) const
1664{
1665 Update( aItem, ALL );
1666}
1667
1668
1669void VIEW::Update( const VIEW_ITEM* aItem, int aUpdateFlags ) const
1670{
1671 VIEW_ITEM_DATA* viewData = aItem->viewPrivData();
1672
1673 if( !viewData )
1674 return;
1675
1676 assert( aUpdateFlags != NONE );
1677
1678 viewData->m_requiredUpdate |= aUpdateFlags;
1679}
1680
1681
1682std::shared_ptr<VIEW_OVERLAY> VIEW::MakeOverlay()
1683{
1684 std::shared_ptr<VIEW_OVERLAY> overlay = std::make_shared<VIEW_OVERLAY>();
1685
1686 Add( overlay.get() );
1687 return overlay;
1688}
1689
1690
1692{
1693 if( !m_preview )
1694 return;
1695
1696 m_preview->Clear();
1697
1698 for( VIEW_ITEM* item : m_ownedItems )
1699 delete item;
1700
1701 m_ownedItems.clear();
1702 Update( m_preview.get() );
1703}
1704
1705
1707{
1708 m_preview.reset( new KIGFX::VIEW_GROUP() );
1709 Add( m_preview.get() );
1710}
1711
1712
1713void VIEW::AddToPreview( VIEW_ITEM* aItem, bool aTakeOwnership )
1714{
1715 Hide( aItem, false );
1716 m_preview->Add( aItem );
1717
1718 if( aTakeOwnership )
1719 m_ownedItems.push_back( aItem );
1720
1721 SetVisible( m_preview.get(), true );
1722 Hide( m_preview.get(), false );
1723 Update( m_preview.get() );
1724}
1725
1726
1727void VIEW::ShowPreview( bool aShow )
1728{
1729 SetVisible( m_preview.get(), aShow );
1730}
1731
1732
1733} // namespace KIGFX
int color
Definition: DXF_plotter.cpp:58
constexpr int ARC_LOW_DEF
Definition: base_units.h:119
constexpr BOX2I BOX2ISafe(const BOX2D &aInput)
Definition: box2.h:929
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 void SetSize(const SizeVec &size)
Definition: box2.h:248
constexpr size_type GetHeight() const
Definition: box2.h:215
constexpr coord_type GetLeft() const
Definition: box2.h:228
constexpr bool Contains(const Vec &aPoint) const
Definition: box2.h:168
constexpr coord_type GetRight() const
Definition: box2.h:217
constexpr const SizeVec & GetSize() const
Definition: box2.h:206
constexpr void SetEnd(coord_type x, coord_type y)
Definition: box2.h:297
constexpr coord_type GetTop() const
Definition: box2.h:229
constexpr coord_type GetBottom() const
Definition: box2.h:222
A color representation with 4 components: red, green, blue, alpha.
Definition: color4d.h:104
Abstract interface for drawing on a 2D-surface.
virtual void SetLayerDepth(double aLayerDepth)
Set the depth of the layer (position on the z-axis)
const MATRIX3x3D & GetWorldScreenMatrix() const
Get the world <-> screen transformation matrix.
virtual void StartDiffLayer()
Begins rendering of a differential layer.
virtual void EndDiffLayer()
Ends rendering of a differential layer.
void SetFlip(bool xAxis, bool yAxis)
Sets flipping of the screen.
virtual void DeleteGroup(int aGroupNumber)
Delete the group from the memory.
const MATRIX3x3D & GetScreenWorldMatrix() const
Get the screen <-> world transformation matrix.
virtual void ClearTarget(RENDER_TARGET aTarget)
Clear the target for rendering.
void SetZoomFactor(double aZoomFactor)
virtual bool IsOpenGlEngine()
Return true if the GAL engine is a OpenGL based type.
virtual int BeginGroup()
Begin a group.
virtual void ChangeGroupDepth(int aGroupNumber, int aDepth)
Change the depth (Z-axis position) of the group.
virtual void EndNegativesLayer()
Ends rendering of a negatives layer and draws it to the main layer.
virtual void StartNegativesLayer()
Begins rendering in a new layer that will be copied to the main layer in EndNegativesLayer().
virtual void ChangeGroupColor(int aGroupNumber, const COLOR4D &aNewColor)
Change the color used to draw the group.
virtual void DrawGroup(int aGroupNumber)
Draw the stored group.
void SetLookAtPoint(const VECTOR2D &aPoint)
Get/set the Point in world space to look at.
virtual bool IsInitialized() const
Return the initialization status for the canvas.
virtual void ComputeWorldScreenMatrix()
Compute the world <-> screen transformation matrix.
virtual void EndGroup()
End the group.
virtual void ClearCache()
Delete all data created during caching of graphic items.
virtual void SetTarget(RENDER_TARGET aTarget)
Set the target for rendering.
const VECTOR2I & GetScreenPixelSize() const
Return GAL canvas size in pixels.
virtual bool IsVisible() const
Return true if the GAL canvas is visible on the screen.
virtual void EnableDepthTest(bool aEnabled=false)
Contains all the knowledge about how to draw graphical object onto any particular output device.
Definition: painter.h:59
virtual bool Draw(const VIEW_ITEM *aItem, int aLayer)=0
Takes an instance of VIEW_ITEM and passes it to a function that knows how to draw the item.
virtual RENDER_SETTINGS * GetSettings()=0
Return a pointer to current settings that are going to be used when drawing items.
virtual COLOR4D GetColor(const VIEW_ITEM *aItem, int aLayer) const =0
Returns the color that should be used to draw the specific VIEW_ITEM on the specific layer using curr...
Extend VIEW_ITEM by possibility of grouping items into a single object.
Definition: view_group.h:48
virtual unsigned int GetSize() const
Return the number of stored items.
Definition: view_group.cpp:76
virtual VIEW_ITEM * GetItem(unsigned int aIdx) const
Definition: view_group.cpp:82
bool storesGroups() const
Return information if the item uses at least one group id (ie.
Definition: view.cpp:142
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:129
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
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:83
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:100
void saveLayers(const std::vector< int > &aLayers)
Save layers used by the item.
Definition: view.cpp:175
int GetFlags() const
Definition: view.cpp:69
void clearUpdateFlags()
Mark an item as already updated, so it is not going to be redrawn.
Definition: view.cpp:198
An abstract base class for deriving all objects that can be added to a VIEW.
Definition: view_item.h:84
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:169
VIEW_ITEM_DATA * viewPrivData() const
Definition: view_item.h:148
virtual void ViewDraw(int aLayer, VIEW *aView) const
Draw the parts of the object belonging to layer aLayer.
Definition: view_item.h:119
virtual std::vector< int > ViewGetLayers() const =0
Return the all the layers within the VIEW the object is painted on.
virtual double ViewGetLOD(int aLayer, VIEW *aView) const
Return the level of detail (LOD) of the item.
Definition: view_item.h:142
VIEW_ITEM_DATA * m_viewPrivData
Definition: view_item.h:168
Hold a (potentially large) number of VIEW_ITEMs and renders them on a graphics device provided by the...
Definition: view.h:68
double GetScale() const
Definition: view.h:273
void SetMirror(bool aMirrorX, bool aMirrorY)
Control the mirroring of the VIEW.
Definition: view.cpp:549
void ShowPreview(bool aShow=true)
Definition: view.cpp:1727
double m_maxScale
Definition: view.h:878
BOX2D GetViewport() const
Return the current viewport visible area rectangle.
Definition: view.cpp:522
void CopySettings(const VIEW *aOtherView)
Copy layers and visibility settings from another view.
Definition: view.cpp:497
virtual void SetScale(double aScale, VECTOR2D aAnchor={ 0, 0 })
Set the scaling factor, zooming around a given anchor point.
Definition: view.cpp:562
static constexpr int TOP_LAYER_MODIFIER
Definition: view.h:745
void draw(VIEW_ITEM *aItem, int aLayer, bool aImmediate=false)
Draw an item, but on a specified layers.
Definition: view.cpp:1039
std::vector< VIEW_ITEM * > m_ownedItems
Whether to use rendering order modifier or not.
Definition: view.h:855
bool m_reverseDrawOrder
Definition: view.h:899
void UpdateAllLayersOrder()
Do everything that is needed to apply the rendering order of layers.
Definition: view.cpp:896
void updateItemColor(VIEW_ITEM *aItem, int aLayer)
Update all information needed to draw an item.
Definition: view.cpp:1276
void SetViewport(const BOX2D &aViewport)
Set the visible area of the VIEW.
Definition: view.cpp:534
void SetRequired(int aLayerId, int aRequiredId, bool aRequired=true)
Mark the aRequiredId layer as required for the aLayerId layer.
Definition: view.cpp:400
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:478
static bool compareRenderingOrder(VIEW_LAYER *aI, VIEW_LAYER *aJ)
Check if every layer required by the aLayerId layer is enabled.
Definition: view.h:839
int GetLayerOrder(int aLayer) const
Return rendering order of a particular layer.
Definition: view.cpp:656
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:334
void ClearTargets()
Clear targets that are marked as dirty.
Definition: view.cpp:1142
virtual void EnableTopLayer(bool aEnable)
Enable or disable display of the top layer.
Definition: view.cpp:856
bool m_mirrorX
Definition: view.h:880
void UpdateAllLayersColor()
Apply the new coloring scheme to all layers.
Definition: view.cpp:765
bool m_mirrorY
PAINTER contains information how do draw items.
Definition: view.h:881
std::shared_ptr< std::vector< VIEW_ITEM * > > m_allItems
The set of layers that are displayed on the top.
Definition: view.h:867
std::shared_ptr< VIEW_OVERLAY > MakeOverlay()
Definition: view.cpp:1682
void SetGAL(GAL *aGal)
Assign a rendering device for the VIEW.
Definition: view.cpp:503
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:412
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:1669
void invalidateItem(VIEW_ITEM *aItem, int aUpdateFlags)
Manage dirty flags & redraw queuing when updating an item.
Definition: view.cpp:1222
const VECTOR2I & GetScreenPixelSize() const
Return the size of the our rendering area in pixels.
Definition: view.cpp:1186
bool HasItem(const VIEW_ITEM *aItem) const
Indicates whether or not the given item has been added to the view.
Definition: view.cpp:1655
virtual int GetTopLayer() const
Definition: view.cpp:820
PAINTER * m_painter
Interface to #PAINTER that is used to draw items.
Definition: view.h:884
virtual void Redraw()
Immediately redraws the whole view.
Definition: view.cpp:1161
void Clear()
Remove all items from the view.
Definition: view.cpp:1127
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:662
std::set< unsigned int > m_topLayers
Center point of the VIEW (the point at which we are looking at).
Definition: view.h:870
bool m_enableOrderModifier
The set of possible displayed layers and its properties.
Definition: view.h:858
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:459
bool IsHiddenOnOverlay(const VIEW_ITEM *aItem) const
Definition: view.cpp:1647
void ClearTopLayers()
Remove all layers from the on-the-top set (they are no longer displayed over the rest of layers).
Definition: view.cpp:881
void InitPreview()
Definition: view.cpp:1706
void ClearPreview()
Definition: view.cpp:1691
void MarkClean()
Force redraw of view on the next rendering.
Definition: view.h:665
void updateItemGeometry(VIEW_ITEM *aItem, int aLayer)
Update bounding box of an item.
Definition: view.cpp:1294
static constexpr int VIEW_MAX_LAYERS
Rendering order modifier for layers that are marked as top layers.
Definition: view.h:742
double m_minScale
Definition: view.h:877
double m_scale
Definition: view.h:875
void updateLayers(VIEW_ITEM *aItem)
Determine rendering order of layers. Used in display order sorting function.
Definition: view.cpp:1356
void RecacheAllItems()
Rebuild GAL display lists.
Definition: view.cpp:1435
bool areRequiredLayersEnabled(int aLayerId) const
Definition: view.cpp:1411
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:625
int m_nextDrawPriority
Flag to reverse the draw order when using draw priority.
Definition: view.h:896
void sortOrderedLayers()
Clear cached GAL group numbers (ONLY numbers stored in VIEW_ITEMs, not group objects used by GAL)
Definition: view.cpp:1261
bool m_useDrawPriority
The next sequential drawing priority.
Definition: view.h:893
void UpdateItems()
Iterate through the list of items that asked for updating and updates them.
Definition: view.cpp:1452
bool IsCached(int aLayer) const
Return true if the layer is cached.
Definition: view.h:643
std::pair< VIEW_ITEM *, int > LAYER_ITEM_PAIR
Definition: view.h:72
void UpdateAllItems(int aUpdateFlags)
Update all items in the view according to the given flags.
Definition: view.cpp:1545
std::unique_ptr< KIGFX::VIEW_GROUP > m_preview
Definition: view.h:854
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
Flag to mark targets as dirty so they have to be redrawn on the next refresh event.
Definition: view.h:887
std::map< int, VIEW_LAYER > m_layers
Sorted list of pointers to members of m_layers.
Definition: view.h:861
std::unique_ptr< VIEW > DataReference() const
Return a new VIEW object that shares the same set of VIEW_ITEMs and LAYERs.
Definition: view.cpp:1586
void Hide(VIEW_ITEM *aItem, bool aHide=true, bool aHideOverlay=false)
Temporarily hide the item in the view (e.g.
Definition: view.cpp:1617
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:829
void AddToPreview(VIEW_ITEM *aItem, bool aTakeOwnership=true)
Definition: view.cpp:1713
void UpdateLayerColor(int aLayer)
Apply the new coloring scheme held by RENDER_SETTINGS in case that it has changed.
Definition: view.cpp:744
void MarkDirty()
Force redraw of view on the next rendering.
Definition: view.h:656
BOX2D m_boundary
Definition: view.h:876
void SetCenter(const VECTOR2D &aCenter)
Set the center point of the VIEW (i.e.
Definition: view.cpp:588
std::vector< VIEW_LAYER * > m_orderedLayers
Flat list of all items.
Definition: view.h:864
void clearGroupCache()
Definition: view.cpp:1210
VECTOR2D m_center
Definition: view.h:873
void SetLayerOrder(int aLayer, int aRenderingOrder)
Set rendering order of a particular layer.
Definition: view.cpp:648
void MarkTargetDirty(int aTarget)
Set or clear target 'dirty' flag.
Definition: view.h:636
void updateBbox(VIEW_ITEM *aItem)
Update set of layers that an item occupies.
Definition: view.cpp:1331
bool IsVisible(const VIEW_ITEM *aItem) const
Return information if the item is visible (or not).
Definition: view.cpp:1639
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:1555
void SetVisible(VIEW_ITEM *aItem, bool aIsVisible=true)
Set the item visibility.
Definition: view.cpp:1596
void ReorderLayerData(std::unordered_map< int, int > aReorderMap)
Remap the data between layer ids without invalidating that data.
Definition: view.cpp:672
void redrawRect(const BOX2I &aRect)
Definition: view.cpp:996
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.
void BooleanSubtract(const SHAPE_POLY_SET &b, POLYGON_MODE aFastMode)
Perform boolean polyset difference For aFastMode meaning, see function booleanOp.
bool IsEmpty() const
Return true if the set is empty (no polygons at all)
void Deflate(int aAmount, CORNER_STRATEGY aCornerStrategy, int aMaxError)
const BOX2I BBox(int aClearance=0) const override
Compute a bounding box of the shape, with a margin of aClearance a collision.
#define _(s)
const wxChar *const traceGalProfile
Flag to enable debug output of GAL performance profiling.
@ UNDEFINED_LAYER
Definition: layer_ids.h:61
The Cairo implementation of the graphics abstraction layer.
Definition: color4d.cpp:247
@ COLOR
Color has changed.
Definition: view_item.h:53
@ INITIAL_ADD
Item is being added to the view.
Definition: view_item.h:56
@ NONE
No updates are required.
Definition: view_item.h:51
@ REPAINT
Item needs to be redrawn.
Definition: view_item.h:57
@ APPEARANCE
Visibility flag has changed.
Definition: view_item.h:52
@ GEOMETRY
Position or shape has changed.
Definition: view_item.h:54
@ LAYERS
Layers have changed.
Definition: view_item.h:55
@ ALL
All except INITIAL_ADD.
Definition: view_item.h:58
@ 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:66
@ OVERLAY_HIDDEN
Item is temporarily hidden from being drawn on an overlay.
Definition: view_item.h:69
@ VISIBLE
Item is visible (in general)
Definition: view_item.h:65
void delete_if(_Container &__c, _Function &&__f)
Deletes all values from __c for which __f returns true.
Definition: kicad_algo.h:174
std::shared_ptr< PNS_LOG_VIEWER_OVERLAY > overlay
Definition: playground.cpp:46
bool operator()(VIEW_ITEM *aItem)
Definition: view.cpp:1199
bool operator()(VIEW_ITEM *aItem)
Definition: view.cpp:940
DRAW_ITEM_VISITOR(VIEW *aView, int aLayer, bool aUseDrawPriority, bool aReverseDrawOrder)
Definition: view.cpp:930
int layers[VIEW_MAX_LAYERS]
Definition: view.cpp:988
std::vector< VIEW_ITEM * > drawItems
Definition: view.cpp:990
bool operator()(VIEW_ITEM *aItem)
Definition: view.cpp:1102
RECACHE_ITEM_VISITOR(VIEW *aView, GAL *aGal, int aLayer)
Definition: view.cpp:1095
bool operator()(VIEW_ITEM *aItem)
Definition: view.cpp:726
UPDATE_COLOR_VISITOR(int aLayer, PAINTER *aPainter, GAL *aGal)
Definition: view.cpp:719
UPDATE_DEPTH_VISITOR(int aLayer, int aDepth, GAL *aGal)
Definition: view.cpp:798
bool operator()(VIEW_ITEM *aItem)
Definition: view.cpp:805
bool diffLayer
Layer should be drawn differentially over lower layers.
Definition: view.h:752
int renderingOrder
Rendering order of this layer.
Definition: view.h:755
bool hasNegatives
Layer should be drawn separately to not delete lower layers.
Definition: view.h:753
bool visible
Is the layer to be rendered?
Definition: view.h:750
bool displayOnly
Is the layer display only?
Definition: view.h:751
std::shared_ptr< VIEW_RTREE > items
R-tree indexing all items on this layer.
Definition: view.h:754
RENDER_TARGET target
Where the layer should be rendered.
Definition: view.h:757
int id
Layer ID.
Definition: view.h:756
constexpr int delta
wxLogTrace helper definitions.
#define KI_TRACE(aWhat,...)
VECTOR2< double > VECTOR2D
Definition: vector2d.h:690