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 inline bool IsLayerVisibleCached( int aLayer ) const
438 {
439 return m_layerVisibilityCache[ aLayer ];
440 }
441
448 inline void SetLayerDiff( int aLayer, bool aDiff = true )
449 {
450 auto it = m_layers.find( aLayer );
451
452 if( it == m_layers.end() )
453 return;
454
455 VIEW_LAYER& layer = it->second;
456
457 if( layer.diffLayer != aDiff )
458 {
459 // Target has to be redrawn after changing its layers' diff status
460 MarkTargetDirty( layer.target );
461 layer.diffLayer = aDiff;
462 }
463 }
464
471 inline void SetLayerHasNegatives( int aLayer, bool aNegatives = true )
472 {
473 auto it = m_layers.find( aLayer );
474
475 if( it == m_layers.end() )
476 return;
477
478 VIEW_LAYER& layer = it->second;
479
480 if( layer.hasNegatives != aNegatives )
481 {
482 // Target has to be redrawn after changing a layers' negatives
483 MarkTargetDirty( layer.target );
484 layer.hasNegatives = aNegatives;
485 }
486 }
487
491 inline void SetLayerDisplayOnly( int aLayer, bool aDisplayOnly = true )
492 {
493 auto it = m_layers.find( aLayer );
494
495 if( it == m_layers.end() )
496 return;
497
498 it->second.displayOnly = aDisplayOnly;
499 }
500
507 inline void SetLayerTarget( int aLayer, RENDER_TARGET aTarget )
508 {
509 auto it = m_layers.find( aLayer );
510
511 if( it == m_layers.end() )
512 return;
513
514 it->second.target = aTarget;
515 }
516
525 void SetLayerOrder( int aLayer, int aRenderingOrder, bool aAutoSort = true );
526
533 int GetLayerOrder( int aLayer ) const;
534
539 void SortOrderedLayers();
540
548 void SortLayers( std::vector<int>& aLayers ) const;
549
557 void ReorderLayerData( std::unordered_map<int, int> aReorderMap );
558
565 void UpdateLayerColor( int aLayer );
566
572 void UpdateAllLayersColor();
573
581 virtual void SetTopLayer( int aLayer, bool aEnabled = true );
582
592 virtual void EnableTopLayer( bool aEnable );
593
594 virtual int GetTopLayer() const;
595
600 void ClearTopLayers();
601
607 void UpdateAllLayersOrder();
608
612 void ClearTargets();
613
617 virtual void Redraw();
618
622 void RecacheAllItems();
623
629 bool IsDirty() const
630 {
631 for( int i = 0; i < TARGETS_NUMBER; ++i )
632 {
633 if( IsTargetDirty( i ) )
634 return true;
635 }
636
637 return false;
638 }
639
646 bool IsTargetDirty( int aTarget ) const
647 {
648 wxCHECK( aTarget < TARGETS_NUMBER, false );
649 return m_dirtyTargets[aTarget];
650 }
651
657 inline void MarkTargetDirty( int aTarget )
658 {
659 wxCHECK( aTarget < TARGETS_NUMBER, /* void */ );
660 m_dirtyTargets[aTarget] = true;
661 }
662
664 inline bool IsCached( int aLayer ) const
665 {
666 auto it = m_layers.find( aLayer );
667
668 if( it == m_layers.end() )
669 return false;
670
671 return it->second.target == TARGET_CACHED;
672 }
673
678 {
679 for( int i = 0; i < TARGETS_NUMBER; ++i )
680 m_dirtyTargets[i] = true;
681 }
682
687 {
688 for( int i = 0; i < TARGETS_NUMBER; ++i )
689 m_dirtyTargets[i] = false;
690 }
691
695 void UpdateItems();
696
701 {
703 }
704
710 void UpdateAllItems( int aUpdateFlags );
711
718 void UpdateAllItemsConditionally( int aUpdateFlags,
719 std::function<bool( VIEW_ITEM* )> aCondition );
720
726 void UpdateAllItemsConditionally( std::function<int( VIEW_ITEM* )> aItemFlagsProvider );
727
732 {
733 return m_useDrawPriority;
734 }
735
739 void UseDrawPriority( bool aFlag )
740 {
741 m_useDrawPriority = aFlag;
742 }
743
749 void ReverseDrawOrder( bool aFlag )
750 {
751 m_reverseDrawOrder = aFlag;
752 }
753
754 std::shared_ptr<VIEW_OVERLAY> MakeOverlay();
755
756 void InitPreview();
757
758 void ClearPreview();
759 void AddToPreview( VIEW_ITEM* aItem, bool aTakeOwnership = true );
760
761 void ShowPreview( bool aShow = true );
762
768 std::unique_ptr<VIEW> DataReference() const;
769
771 static constexpr int VIEW_MAX_LAYERS = MAX_LAYERS_FOR_VIEW;
772
775
776protected:
778 {
779 bool visible;
781
784
787 std::shared_ptr<VIEW_RTREE> items;
789 int id;
791
793 std::set<int> requiredLayers;
794
795 bool operator< ( const VIEW_LAYER& aOther ) const
796 {
797 return id < aOther.id;
798 }
799 };
800
802 void redrawRect( const BOX2I& aRect );
803
804 inline void markTargetClean( int aTarget )
805 {
806 wxCHECK( aTarget < TARGETS_NUMBER, /* void */ );
807 m_dirtyTargets[aTarget] = false;
808 }
809
821 void draw( VIEW_ITEM* aItem, int aLayer, bool aImmediate = false );
822
830 void draw( VIEW_ITEM* aItem, bool aImmediate = false );
831
839 void draw( VIEW_GROUP* aGroup, bool aImmediate = false );
840
843 void clearGroupCache();
844
851 void invalidateItem( VIEW_ITEM* aItem, int aUpdateFlags );
852
854 void updateItemColor( VIEW_ITEM* aItem, int aLayer );
855
857 void updateItemGeometry( VIEW_ITEM* aItem, int aLayer );
858
860 void updateBbox( VIEW_ITEM* aItem );
861
863 void updateLayers( VIEW_ITEM* aItem );
864
867 {
868 return aI->renderingOrder > aJ->renderingOrder;
869 }
870
872 bool areRequiredLayersEnabled( int aLayerId ) const;
873
874 void syncLayerVisibilityCache();
875
876 // Function objects that need to access VIEW/VIEW_ITEM private/protected members
877 struct CLEAR_LAYER_CACHE_VISITOR;
878 struct RECACHE_ITEM_VISITOR;
879 struct DRAW_ITEM_VISITOR;
880 struct UPDATE_COLOR_VISITOR;
881 struct UPDATE_DEPTH_VISITOR;
882
883 std::unique_ptr<KIGFX::VIEW_GROUP> m_preview;
884 std::vector<VIEW_ITEM*> m_ownedItems;
885
888
890 std::map<int, VIEW_LAYER> m_layers;
891
893 std::vector<VIEW_LAYER*> m_orderedLayers;
894
896 std::shared_ptr<std::vector<VIEW_ITEM*>> m_allItems;
897
899 std::set<unsigned int> m_topLayers;
900
903
904 double m_scale;
908
913
916
919
922
925
928
931
934};
935} // namespace KIGFX
BOX2< VECTOR2I > BOX2I
Definition box2.h:918
BOX2< VECTOR2D > BOX2D
Definition box2.h:919
constexpr const Vec GetEnd() const
Definition box2.h:208
constexpr const Vec & GetOrigin() const
Definition box2.h:206
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:907
void ReverseDrawOrder(bool aFlag)
Only takes effect if UseDrawPriority is true.
Definition view.h:749
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:572
static constexpr int TOP_LAYER_MODIFIER
Rendering order modifier for layers that are marked as top layers.
Definition view.h:774
VIEW(const VIEW &)=delete
bool m_reverseDrawOrder
Flag to reverse the draw order when using draw priority.
Definition view.h:930
void SetRequired(int aLayerId, int aRequiredId, bool aRequired=true)
Mark the aRequiredId layer as required for the aLayerId layer.
Definition view.cpp:475
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:491
static bool compareRenderingOrder(VIEW_LAYER *aI, VIEW_LAYER *aJ)
Determine rendering order of layers. Used in display order sorting function.
Definition view.h:866
virtual void Add(VIEW_ITEM *aItem, int aDrawPriority=-1)
Add a VIEW_ITEM to the view.
Definition view.cpp:300
virtual void Remove(VIEW_ITEM *aItem)
Remove a VIEW_ITEM from the view.
Definition view.cpp:404
bool m_mirrorX
Definition view.h:909
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:700
bool m_mirrorY
Definition view.h:910
std::shared_ptr< std::vector< VIEW_ITEM * > > m_allItems
Flat list of all items.
Definition view.h:896
void SetLayerDiff(int aLayer, bool aDiff=true)
Set the whether the layer should drawn differentially.
Definition view.h:448
void SetGAL(GAL *aGal)
Assign a rendering device for the VIEW.
Definition view.cpp:578
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:487
std::vector< VIEW_ITEM * > m_ownedItems
Definition view.h:884
void SetLayerTarget(int aLayer, RENDER_TARGET aTarget)
Change the rendering target for a particular layer.
Definition view.h:507
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:1835
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:1821
PAINTER * m_painter
PAINTER contains information how do draw items.
Definition view.h:915
std::set< unsigned int > m_topLayers
The set of layers that are displayed on the top.
Definition view.h:899
bool m_enableOrderModifier
Whether to use rendering order modifier or not.
Definition view.h:887
void SetLayerHasNegatives(int aLayer, bool aNegatives=true)
Set the status of negatives presense in a particular layer.
Definition view.h:471
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:1813
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:921
void MarkClean()
Force redraw of view on the next rendering.
Definition view.h:686
static constexpr int VIEW_MAX_LAYERS
Maximum number of layers that may be shown.
Definition view.h:771
double m_minScale
Definition view.h:906
double m_scale
Definition view.h:904
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:646
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:927
bool m_useDrawPriority
Flag to respect draw priority when drawing items.
Definition view.h:924
bool IsLayerVisibleCached(int aLayer) const
Definition view.h:437
bool IsCached(int aLayer) const
Return true if the layer is cached.
Definition view.h:664
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:343
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:911
void UseDrawPriority(bool aFlag)
Definition view.h:739
bool m_hasPendingItemUpdates
True when at least one item has deferred update flags that still need processing.
Definition view.h:933
std::unique_ptr< KIGFX::VIEW_GROUP > m_preview
Definition view.h:883
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:225
bool IsUsingDrawPriority() const
Definition view.h:731
VIEW & operator=(const VIEW &)=delete
GAL * m_gal
Interface to PAINTER that is used to draw items.
Definition view.h:918
bool IsDirty() const
Return true if any of the VIEW layers needs to be refreshened.
Definition view.h:629
std::map< int, VIEW_LAYER > m_layers
The set of possible displayed layers and its properties.
Definition view.h:890
void Hide(VIEW_ITEM *aItem, bool aHide=true, bool aHideOverlay=false)
Temporarily hide the item in the view (e.g.
Definition view.cpp:1780
void markTargetClean(int aTarget)
Definition view.h:804
void MarkDirty()
Force redraw of view on the next rendering.
Definition view.h:677
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:905
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:893
VECTOR2D m_center
Center point of the VIEW (the point at which we are looking at).
Definition view.h:902
void MarkTargetDirty(int aTarget)
Set or clear target 'dirty' flag.
Definition view.h:657
bool IsVisible(const VIEW_ITEM *aItem) const
Return information if the item is visible (or not).
Definition view.cpp:1805
BASE_SET m_layerCachedFlagCache
Definition view.h:912
void SetVisible(VIEW_ITEM *aItem, bool aIsVisible=true)
Set the item visibility.
Definition view.cpp:1756
#define MAX_LAYERS_FOR_VIEW
Definition definitions.h:41
bool operator<(const DESIGN_BLOCK_INFO &lhs, const DESIGN_BLOCK_INFO &rhs)
#define GAL_API
Definition gal.h:27
The Cairo implementation of the graphics abstraction layer.
Definition eda_group.h:29
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:793
bool diffLayer
Layer should be drawn differentially over lower layers.
Definition view.h:783
int renderingOrder
Rendering order of this layer.
Definition view.h:788
bool hasNegatives
Layer should be drawn separately to not delete lower layers.
Definition view.h:786
bool visible
Is the layer to be rendered?
Definition view.h:779
bool displayOnly
Is the layer display only?
Definition view.h:780
std::shared_ptr< VIEW_RTREE > items
R-tree indexing all items on this layer.
Definition view.h:787
RENDER_TARGET target
Where the layer should be rendered.
Definition view.h:790
int id
Layer ID.
Definition view.h:789
VECTOR2< int32_t > VECTOR2I
Definition vector2d.h:683
VECTOR2< double > VECTOR2D
Definition vector2d.h:682