KiCad PCB EDA Suite
Loading...
Searching...
No Matches
view.h
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-2016 CERN
5 * Copyright The KiCad Developers, see AUTHORS.txt for contributors.
6 *
7 * @author Tomasz Wlostowski <[email protected]>
8 *
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License
11 * as published by the Free Software Foundation; either version 2
12 * of the License, or (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program. If not, see <https://www.gnu.org/licenses/>.
21 */
22
23#pragma once
24
25#include <gal/gal.h>
26#include <map>
27#include <vector>
28#include <set>
29#include <unordered_map>
30#include <memory>
31
32#include <math/box2.h>
33#include <gal/definitions.h>
34#include <base_set.h>
35#include <view/view_overlay.h>
36
37namespace KIGFX
38{
39class PAINTER;
40class GAL;
41class VIEW_ITEM;
42class VIEW_GROUP;
43class VIEW_RTREE;
44
63{
64public:
65 friend class VIEW_ITEM;
66
67 typedef std::pair<VIEW_ITEM*, int> LAYER_ITEM_PAIR;
68
69 VIEW();
70 virtual ~VIEW();
71
72 // We own at least one list of raw pointers. Don't let the compiler fill in copy c'tors that
73 // will only land us in trouble.
74 VIEW( const VIEW& ) = delete;
75 VIEW& operator=( const VIEW& ) = delete;
76
84 static void OnDestroy( VIEW_ITEM* aItem );
85
94 virtual void Add( VIEW_ITEM* aItem, int aDrawPriority = -1 );
95
102 void AddBatch( const std::vector<VIEW_ITEM*>& aItems );
103
109 virtual void Remove( VIEW_ITEM* aItem );
110
111
120 int Query( const BOX2I& aRect, std::vector<LAYER_ITEM_PAIR>& aResult ) const;
121
127 void Query( const BOX2I& aRect, const std::function<bool( VIEW_ITEM* )>& aFunc ) const;
128
135 void SetVisible( VIEW_ITEM* aItem, bool aIsVisible = true );
136
144 void Hide( VIEW_ITEM* aItem, bool aHide = true, bool aHideOverlay = false );
145
153 bool IsVisible( const VIEW_ITEM* aItem ) const;
154
155 bool IsHiddenOnOverlay( const VIEW_ITEM* aItem ) const;
156
160 bool HasItem( const VIEW_ITEM* aItem ) const;
161
169 virtual void Update( const VIEW_ITEM* aItem, int aUpdateFlags ) const;
170 virtual void Update( const VIEW_ITEM* aItem ) const;
171
180 void SetRequired( int aLayerId, int aRequiredId, bool aRequired = true );
181
187 void CopySettings( const VIEW* aOtherView );
188
189 /*
190 * Convenience wrappers for adding multiple items
191 * template <class T> void AddItems( const T& aItems );
192 * template <class T> void RemoveItems( const T& aItems );
193 */
194
200 void SetGAL( GAL* aGal );
201
207 inline GAL* GetGAL() const
208 {
209 return m_gal;
210 }
211
215 inline void SetPainter( PAINTER* aPainter )
216 {
217 m_painter = aPainter;
218 }
219
225 inline PAINTER* GetPainter() const
226 {
227 return m_painter;
228 }
229
235 void SetViewport( const BOX2D& aViewport );
236
242 BOX2D GetViewport() const;
243
250 void SetMirror( bool aMirrorX, bool aMirrorY );
251
255 bool IsMirroredX() const
256 {
257 return m_mirrorX;
258 }
259
263 bool IsMirroredY() const
264 {
265 return m_mirrorY;
266 }
267
276 virtual void SetScale( double aScale, VECTOR2D aAnchor = { 0, 0 } );
277
281 inline double GetScale() const
282 {
283 return m_scale;
284 }
285
291 inline void SetBoundary( const BOX2D& aBoundary )
292 {
293 m_boundary = aBoundary;
294 }
295
301 inline void SetBoundary( const BOX2I& aBoundary )
302 {
303 m_boundary.SetOrigin( aBoundary.GetOrigin() );
304 m_boundary.SetEnd( aBoundary.GetEnd() );
305 }
306
310 inline const BOX2D& GetBoundary() const
311 {
312 return m_boundary;
313 }
314
321 void SetScaleLimits( double aMaximum, double aMinimum )
322 {
323 wxASSERT_MSG( aMaximum > aMinimum, wxT( "I guess you passed parameters in wrong order" ) );
324
325 m_minScale = aMinimum;
326 m_maxScale = aMaximum;
327 }
328
335 void SetCenter( const VECTOR2D& aCenter );
336
344 void SetCenter( const VECTOR2D& aCenter, const std::vector<BOX2D>& obscuringScreenRects );
345
351 const VECTOR2D& GetCenter() const
352 {
353 return m_center;
354 }
355
362 VECTOR2D ToWorld( const VECTOR2D& aCoord, bool aAbsolute = true ) const;
363
370 double ToWorld( double aSize ) const;
371
378 VECTOR2D ToScreen( const VECTOR2D& aCoord, bool aAbsolute = true ) const;
379
385 double ToScreen( double aSize ) const;
386
392 const VECTOR2I& GetScreenPixelSize() const;
393
397 void Clear();
398
405 inline void SetLayerVisible( int aLayer, bool aVisible = true )
406 {
407 auto it = m_layers.find( aLayer );
408
409 if( it == m_layers.end() )
410 return;
411
412 VIEW_LAYER& layer = it->second;
413
414 if( layer.visible != aVisible )
415 {
416 // Target has to be redrawn after changing its visibility
417 MarkTargetDirty( layer.target );
418 layer.visible = aVisible;
419 }
420 }
421
427 bool IsLayerVisible( int aLayer ) const
428 {
429 auto it = m_layers.find( aLayer );
430
431 if( it == m_layers.end() )
432 return false;
433
434 return it->second.visible;
435 }
436
437 void SyncLayerVisibilityCache();
438
439 bool IsLayerVisibleCached( int aLayer ) const
440 {
441 return m_layerVisibilityCache[ aLayer ];
442 }
443
450 inline void SetLayerDiff( int aLayer, bool aDiff = true )
451 {
452 auto it = m_layers.find( aLayer );
453
454 if( it == m_layers.end() )
455 return;
456
457 VIEW_LAYER& layer = it->second;
458
459 if( layer.diffLayer != aDiff )
460 {
461 // Target has to be redrawn after changing its layers' diff status
462 MarkTargetDirty( layer.target );
463 layer.diffLayer = aDiff;
464 }
465 }
466
473 inline void SetLayerHasNegatives( int aLayer, bool aNegatives = true )
474 {
475 auto it = m_layers.find( aLayer );
476
477 if( it == m_layers.end() )
478 return;
479
480 VIEW_LAYER& layer = it->second;
481
482 if( layer.hasNegatives != aNegatives )
483 {
484 // Target has to be redrawn after changing a layers' negatives
485 MarkTargetDirty( layer.target );
486 layer.hasNegatives = aNegatives;
487 }
488 }
489
493 inline void SetLayerDisplayOnly( int aLayer, bool aDisplayOnly = true )
494 {
495 auto it = m_layers.find( aLayer );
496
497 if( it == m_layers.end() )
498 return;
499
500 it->second.displayOnly = aDisplayOnly;
501 }
502
509 inline void SetLayerTarget( int aLayer, RENDER_TARGET aTarget )
510 {
511 auto it = m_layers.find( aLayer );
512
513 if( it == m_layers.end() )
514 return;
515
516 it->second.target = aTarget;
517 }
518
527 void SetLayerOrder( int aLayer, int aRenderingOrder, bool aAutoSort = true );
528
535 int GetLayerOrder( int aLayer ) const;
536
541 void SortOrderedLayers();
542
550 void SortLayers( std::vector<int>& aLayers ) const;
551
559 void ReorderLayerData( std::unordered_map<int, int> aReorderMap );
560
567 void UpdateLayerColor( int aLayer );
568
574 void UpdateAllLayersColor();
575
583 virtual void SetTopLayer( int aLayer, bool aEnabled = true );
584
594 virtual void EnableTopLayer( bool aEnable );
595
596 virtual int GetTopLayer() const;
597
602 void ClearTopLayers();
603
609 void UpdateAllLayersOrder();
610
614 void ClearTargets();
615
619 virtual void Redraw();
620
624 void RecacheAllItems();
625
631 bool IsDirty() const
632 {
633 for( int i = 0; i < TARGETS_NUMBER; ++i )
634 {
635 if( IsTargetDirty( i ) )
636 return true;
637 }
638
639 return false;
640 }
641
648 bool IsTargetDirty( int aTarget ) const
649 {
650 wxCHECK( aTarget < TARGETS_NUMBER, false );
651 return m_dirtyTargets[aTarget];
652 }
653
659 inline void MarkTargetDirty( int aTarget )
660 {
661 wxCHECK( aTarget < TARGETS_NUMBER, /* void */ );
662 m_dirtyTargets[aTarget] = true;
663 }
664
666 inline bool IsCached( int aLayer ) const
667 {
668 auto it = m_layers.find( aLayer );
669
670 if( it == m_layers.end() )
671 return false;
672
673 return it->second.target == TARGET_CACHED;
674 }
675
680 {
681 for( int i = 0; i < TARGETS_NUMBER; ++i )
682 m_dirtyTargets[i] = true;
683 }
684
689 {
690 for( int i = 0; i < TARGETS_NUMBER; ++i )
691 m_dirtyTargets[i] = false;
692 }
693
697 void UpdateItems();
698
703 {
705 }
706
712 void UpdateAllItems( int aUpdateFlags );
713
720 void UpdateAllItemsConditionally( int aUpdateFlags,
721 std::function<bool( VIEW_ITEM* )> aCondition );
722
728 void UpdateAllItemsConditionally( std::function<int( VIEW_ITEM* )> aItemFlagsProvider );
729
734 {
735 return m_useDrawPriority;
736 }
737
741 void UseDrawPriority( bool aFlag )
742 {
743 m_useDrawPriority = aFlag;
744 }
745
751 void ReverseDrawOrder( bool aFlag )
752 {
753 m_reverseDrawOrder = aFlag;
754 }
755
756 std::shared_ptr<VIEW_OVERLAY> MakeOverlay();
757
758 void InitPreview();
759
760 void ClearPreview();
761 void AddToPreview( VIEW_ITEM* aItem, bool aTakeOwnership = true );
762
763 void ShowPreview( bool aShow = true );
764
770 std::unique_ptr<VIEW> DataReference() const;
771
773 static constexpr int VIEW_MAX_LAYERS = MAX_LAYERS_FOR_VIEW;
774
777
778protected:
780 {
781 bool visible;
783
786
789 std::shared_ptr<VIEW_RTREE> items;
791 int id;
793
795 std::set<int> requiredLayers;
796
797 bool operator< ( const VIEW_LAYER& aOther ) const
798 {
799 return id < aOther.id;
800 }
801 };
802
812 void unlinkItem( VIEW_ITEM* aItem );
813
815 void redrawRect( const BOX2I& aRect );
816
817 inline void markTargetClean( int aTarget )
818 {
819 wxCHECK( aTarget < TARGETS_NUMBER, /* void */ );
820 m_dirtyTargets[aTarget] = false;
821 }
822
834 void draw( VIEW_ITEM* aItem, int aLayer, bool aImmediate = false );
835
843 void draw( VIEW_ITEM* aItem, bool aImmediate = false );
844
852 void draw( VIEW_GROUP* aGroup, bool aImmediate = false );
853
856 void clearGroupCache();
857
864 void invalidateItem( VIEW_ITEM* aItem, int aUpdateFlags );
865
867 void updateItemColor( VIEW_ITEM* aItem, int aLayer );
868
869 void recolorGroup( VIEW_ITEM* aItem, int aLayer, int aGroup );
870
872 void updateItemGeometry( VIEW_ITEM* aItem, int aLayer );
873
875 void updateBbox( VIEW_ITEM* aItem );
876
878 void updateLayers( VIEW_ITEM* aItem );
879
882 {
883 return aI->renderingOrder > aJ->renderingOrder;
884 }
885
887 bool areRequiredLayersEnabled( int aLayerId ) const;
888
889 // Function objects that need to access VIEW/VIEW_ITEM private/protected members
890 struct CLEAR_LAYER_CACHE_VISITOR;
891 struct RECACHE_ITEM_VISITOR;
892 struct DRAW_ITEM_VISITOR;
893 struct UPDATE_COLOR_VISITOR;
894 struct UPDATE_DEPTH_VISITOR;
895
896 std::unique_ptr<KIGFX::VIEW_GROUP> m_preview;
897 std::vector<VIEW_ITEM*> m_ownedItems;
898
901
903 std::map<int, VIEW_LAYER> m_layers;
904
906 std::vector<VIEW_LAYER*> m_orderedLayers;
907
909 std::shared_ptr<std::vector<VIEW_ITEM*>> m_allItems;
910
912 std::set<unsigned int> m_topLayers;
913
916
917 double m_scale;
921
926
929
932
935
938
941
944
947};
948} // namespace KIGFX
BOX2< VECTOR2I > BOX2I
Definition box2.h:927
BOX2< VECTOR2D > BOX2D
Definition box2.h:928
constexpr const Vec GetEnd() const
Definition box2.h:209
constexpr const Vec & GetOrigin() const
Definition box2.h:207
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:55
Extend VIEW_ITEM by possibility of grouping items into a single object.
Definition view_group.h:39
An abstract base class for deriving all objects that can be added to a VIEW.
Definition view_item.h:82
Implement a non-owning R-tree for fast spatial indexing of VIEW items.
Definition view_rtree.h:42
double GetScale() const
Definition view.h:281
double m_maxScale
Definition view.h:920
void ReverseDrawOrder(bool aFlag)
Only takes effect if UseDrawPriority is true.
Definition view.h:751
const VECTOR2D & GetCenter() const
Return the center point of this VIEW (in world space coordinates).
Definition view.h:351
void CopySettings(const VIEW *aOtherView)
Copy layers and visibility settings from another view.
Definition view.cpp:590
static constexpr int TOP_LAYER_MODIFIER
Rendering order modifier for layers that are marked as top layers.
Definition view.h:776
VIEW(const VIEW &)=delete
bool m_reverseDrawOrder
Flag to reverse the draw order when using draw priority.
Definition view.h:943
void SetRequired(int aLayerId, int aRequiredId, bool aRequired=true)
Mark the aRequiredId layer as required for the aLayerId layer.
Definition view.cpp:493
void SetLayerDisplayOnly(int aLayer, bool aDisplayOnly=true)
Set a layer display-only (ie: to be rendered but not returned by hit test queries).
Definition view.h:493
static bool compareRenderingOrder(VIEW_LAYER *aI, VIEW_LAYER *aJ)
Determine rendering order of layers. Used in display order sorting function.
Definition view.h:881
virtual void Add(VIEW_ITEM *aItem, int aDrawPriority=-1)
Add a VIEW_ITEM to the view.
Definition view.cpp:301
virtual void Remove(VIEW_ITEM *aItem)
Remove a VIEW_ITEM from the view.
Definition view.cpp:416
bool m_mirrorX
Definition view.h:922
void SetPainter(PAINTER *aPainter)
Set the painter object used by the view for drawing #VIEW_ITEMS.
Definition view.h:215
void SetBoundary(const BOX2D &aBoundary)
Set limits for view area.
Definition view.h:291
bool HasPendingItemUpdates() const
Definition view.h:702
bool m_mirrorY
Definition view.h:923
std::shared_ptr< std::vector< VIEW_ITEM * > > m_allItems
Flat list of all items.
Definition view.h:909
void SetLayerDiff(int aLayer, bool aDiff=true)
Set the whether the layer should drawn differentially.
Definition view.h:450
void SetGAL(GAL *aGal)
Assign a rendering device for the VIEW.
Definition view.cpp:596
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:505
std::vector< VIEW_ITEM * > m_ownedItems
Definition view.h:897
void SetLayerTarget(int aLayer, RENDER_TARGET aTarget)
Change the rendering target for a particular layer.
Definition view.h:509
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:1852
void SetLayerVisible(int aLayer, bool aVisible=true)
Control the visibility of a particular layer.
Definition view.h:405
bool HasItem(const VIEW_ITEM *aItem) const
Indicates whether or not the given item has been added to the view.
Definition view.cpp:1838
PAINTER * m_painter
PAINTER contains information how do draw items.
Definition view.h:928
std::set< unsigned int > m_topLayers
The set of layers that are displayed on the top.
Definition view.h:912
bool m_enableOrderModifier
Whether to use rendering order modifier or not.
Definition view.h:900
void SetLayerHasNegatives(int aLayer, bool aNegatives=true)
Set the status of negatives presense in a particular layer.
Definition view.h:473
GAL * GetGAL() const
Return the GAL this view is using to draw graphical primitives.
Definition view.h:207
bool IsHiddenOnOverlay(const VIEW_ITEM *aItem) const
Definition view.cpp:1830
bool m_dirtyTargets[TARGETS_NUMBER]
Flag to mark targets as dirty so they have to be redrawn on the next refresh event.
Definition view.h:934
void MarkClean()
Force redraw of view on the next rendering.
Definition view.h:688
static constexpr int VIEW_MAX_LAYERS
Maximum number of layers that may be shown.
Definition view.h:773
double m_minScale
Definition view.h:919
double m_scale
Definition view.h:917
bool IsMirroredX() const
Return true if view is flipped across the X axis.
Definition view.h:255
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:648
void SetBoundary(const BOX2I &aBoundary)
Set limits for view area.
Definition view.h:301
int m_nextDrawPriority
The next sequential drawing priority.
Definition view.h:940
bool m_useDrawPriority
Flag to respect draw priority when drawing items.
Definition view.h:937
bool IsLayerVisibleCached(int aLayer) const
Definition view.h:439
bool IsCached(int aLayer) const
Return true if the layer is cached.
Definition view.h:666
void AddBatch(const std::vector< VIEW_ITEM * > &aItems)
Add a batch of items to the view, using bulk-loaded R-trees for initial population.
Definition view.cpp:346
std::pair< VIEW_ITEM *, int > LAYER_ITEM_PAIR
Definition view.h:67
bool IsMirroredY() const
Return true if view is flipped across the Y axis.
Definition view.h:263
BASE_SET m_layerVisibilityCache
Definition view.h:924
void UseDrawPriority(bool aFlag)
Definition view.h:741
bool m_hasPendingItemUpdates
True when at least one item has deferred update flags that still need processing.
Definition view.h:946
std::unique_ptr< KIGFX::VIEW_GROUP > m_preview
Definition view.h:896
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:226
bool IsUsingDrawPriority() const
Definition view.h:733
VIEW & operator=(const VIEW &)=delete
GAL * m_gal
Interface to PAINTER that is used to draw items.
Definition view.h:931
bool IsDirty() const
Return true if any of the VIEW layers needs to be refreshened.
Definition view.h:631
std::map< int, VIEW_LAYER > m_layers
The set of possible displayed layers and its properties.
Definition view.h:903
void Hide(VIEW_ITEM *aItem, bool aHide=true, bool aHideOverlay=false)
Temporarily hide the item in the view (e.g.
Definition view.cpp:1797
void markTargetClean(int aTarget)
Definition view.h:817
void MarkDirty()
Force redraw of view on the next rendering.
Definition view.h:679
friend class VIEW_ITEM
Definition view.h:65
bool IsLayerVisible(int aLayer) const
Return information about visibility of a particular layer.
Definition view.h:427
BOX2D m_boundary
Definition view.h:918
const BOX2D & GetBoundary() const
Definition view.h:310
PAINTER * GetPainter() const
Return the painter object used by the view for drawing #VIEW_ITEMS.
Definition view.h:225
void SetScaleLimits(double aMaximum, double aMinimum)
Set minimum and maximum values for scale.
Definition view.h:321
std::vector< VIEW_LAYER * > m_orderedLayers
Sorted list of pointers to members of m_layers.
Definition view.h:906
VECTOR2D m_center
Center point of the VIEW (the point at which we are looking at).
Definition view.h:915
void MarkTargetDirty(int aTarget)
Set or clear target 'dirty' flag.
Definition view.h:659
bool IsVisible(const VIEW_ITEM *aItem) const
Return information if the item is visible (or not).
Definition view.cpp:1822
BASE_SET m_layerCachedFlagCache
Definition view.h:925
void SetVisible(VIEW_ITEM *aItem, bool aIsVisible=true)
Set the item visibility.
Definition view.cpp:1773
#define MAX_LAYERS_FOR_VIEW
Definition definitions.h:41
bool operator<(const FOOTPRINT_INFO &lhs, const FOOTPRINT_INFO &rhs)
#define GAL_API
Definition gal.h:27
The Cairo implementation of the graphics abstraction layer.
Definition eda_group.h:30
RENDER_TARGET
RENDER_TARGET: Possible rendering targets.
Definition definitions.h:32
@ TARGET_CACHED
Main rendering target (cached)
Definition definitions.h:33
@ TARGETS_NUMBER
Number of available rendering targets.
Definition definitions.h:37
std::set< int > requiredLayers
Definition view.h:795
bool diffLayer
Layer should be drawn differentially over lower layers.
Definition view.h:785
int renderingOrder
Rendering order of this layer.
Definition view.h:790
bool hasNegatives
Layer should be drawn separately to not delete lower layers.
Definition view.h:788
bool visible
Is the layer to be rendered?
Definition view.h:781
bool displayOnly
Is the layer display only?
Definition view.h:782
std::shared_ptr< VIEW_RTREE > items
R-tree indexing all items on this layer.
Definition view.h:789
RENDER_TARGET target
Where the layer should be rendered.
Definition view.h:792
int id
Layer ID.
Definition view.h:791
VECTOR2< int32_t > VECTOR2I
Definition vector2d.h:683
VECTOR2< double > VECTOR2D
Definition vector2d.h:682