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
121 int Query( const BOX2I& aRect, std::vector<LAYER_ITEM_PAIR>& aResult ) const;
122
129 void Query( const BOX2I& aRect, const std::function<bool( VIEW_ITEM* )>& aFunc ) const;
130
137 void SetVisible( VIEW_ITEM* aItem, bool aIsVisible = true );
138
146 void Hide( VIEW_ITEM* aItem, bool aHide = true, bool aHideOverlay = false );
147
155 bool IsVisible( const VIEW_ITEM* aItem ) const;
156
157 bool IsHiddenOnOverlay( const VIEW_ITEM* aItem ) const;
158
162 bool HasItem( const VIEW_ITEM* aItem ) const;
163
171 virtual void Update( const VIEW_ITEM* aItem, int aUpdateFlags ) const;
172 virtual void Update( const VIEW_ITEM* aItem ) const;
173
182 void SetRequired( int aLayerId, int aRequiredId, bool aRequired = true );
183
189 void CopySettings( const VIEW* aOtherView );
190
191 /*
192 * Convenience wrappers for adding multiple items
193 * template <class T> void AddItems( const T& aItems );
194 * template <class T> void RemoveItems( const T& aItems );
195 */
196
202 void SetGAL( GAL* aGal );
203
209 inline GAL* GetGAL() const
210 {
211 return m_gal;
212 }
213
217 inline void SetPainter( PAINTER* aPainter )
218 {
219 m_painter = aPainter;
220 }
221
227 inline PAINTER* GetPainter() const
228 {
229 return m_painter;
230 }
231
237 void SetViewport( const BOX2D& aViewport );
238
244 BOX2D GetViewport() const;
245
252 void SetMirror( bool aMirrorX, bool aMirrorY );
253
257 bool IsMirroredX() const
258 {
259 return m_mirrorX;
260 }
261
265 bool IsMirroredY() const
266 {
267 return m_mirrorY;
268 }
269
278 virtual void SetScale( double aScale, VECTOR2D aAnchor = { 0, 0 } );
279
283 inline double GetScale() const
284 {
285 return m_scale;
286 }
287
293 inline void SetBoundary( const BOX2D& aBoundary )
294 {
295 m_boundary = aBoundary;
296 }
297
303 inline void SetBoundary( const BOX2I& aBoundary )
304 {
305 m_boundary.SetOrigin( aBoundary.GetOrigin() );
306 m_boundary.SetEnd( aBoundary.GetEnd() );
307 }
308
312 inline const BOX2D& GetBoundary() const
313 {
314 return m_boundary;
315 }
316
323 void SetScaleLimits( double aMaximum, double aMinimum )
324 {
325 wxASSERT_MSG( aMaximum > aMinimum, wxT( "I guess you passed parameters in wrong order" ) );
326
327 m_minScale = aMinimum;
328 m_maxScale = aMaximum;
329 }
330
337 void SetCenter( const VECTOR2D& aCenter );
338
346 void SetCenter( const VECTOR2D& aCenter, const std::vector<BOX2D>& obscuringScreenRects );
347
353 const VECTOR2D& GetCenter() const
354 {
355 return m_center;
356 }
357
364 VECTOR2D ToWorld( const VECTOR2D& aCoord, bool aAbsolute = true ) const;
365
372 double ToWorld( double aSize ) const;
373
380 VECTOR2D ToScreen( const VECTOR2D& aCoord, bool aAbsolute = true ) const;
381
387 double ToScreen( double aSize ) const;
388
394 const VECTOR2I& GetScreenPixelSize() const;
395
399 void Clear();
400
407 inline void SetLayerVisible( int aLayer, bool aVisible = true )
408 {
409 auto it = m_layers.find( aLayer );
410
411 if( it == m_layers.end() )
412 return;
413
414 VIEW_LAYER& layer = it->second;
415
416 if( layer.visible != aVisible )
417 {
418 // Target has to be redrawn after changing its visibility
419 MarkTargetDirty( layer.target );
420 layer.visible = aVisible;
421 }
422 }
423
429 bool IsLayerVisible( int aLayer ) const
430 {
431 auto it = m_layers.find( aLayer );
432
433 if( it == m_layers.end() )
434 return false;
435
436 return it->second.visible;
437 }
438
439 void SyncLayerVisibilityCache();
440
441 bool IsLayerVisibleCached( int aLayer ) const
442 {
443 return m_layerVisibilityCache[ aLayer ];
444 }
445
452 inline void SetLayerDiff( int aLayer, bool aDiff = true )
453 {
454 auto it = m_layers.find( aLayer );
455
456 if( it == m_layers.end() )
457 return;
458
459 VIEW_LAYER& layer = it->second;
460
461 if( layer.diffLayer != aDiff )
462 {
463 // Target has to be redrawn after changing its layers' diff status
464 MarkTargetDirty( layer.target );
465 layer.diffLayer = aDiff;
466 }
467 }
468
475 inline void SetLayerHasNegatives( int aLayer, bool aNegatives = true )
476 {
477 auto it = m_layers.find( aLayer );
478
479 if( it == m_layers.end() )
480 return;
481
482 VIEW_LAYER& layer = it->second;
483
484 if( layer.hasNegatives != aNegatives )
485 {
486 // Target has to be redrawn after changing a layers' negatives
487 MarkTargetDirty( layer.target );
488 layer.hasNegatives = aNegatives;
489 }
490 }
491
495 inline void SetLayerDisplayOnly( int aLayer, bool aDisplayOnly = true )
496 {
497 auto it = m_layers.find( aLayer );
498
499 if( it == m_layers.end() )
500 return;
501
502 it->second.displayOnly = aDisplayOnly;
503 }
504
511 inline void SetLayerTarget( int aLayer, RENDER_TARGET aTarget )
512 {
513 auto it = m_layers.find( aLayer );
514
515 if( it == m_layers.end() )
516 return;
517
518 it->second.target = aTarget;
519 }
520
529 void SetLayerOrder( int aLayer, int aRenderingOrder, bool aAutoSort = true );
530
537 int GetLayerOrder( int aLayer ) const;
538
543 void SortOrderedLayers();
544
552 void SortLayers( std::vector<int>& aLayers ) const;
553
561 void ReorderLayerData( std::unordered_map<int, int> aReorderMap );
562
569 void UpdateLayerColor( int aLayer );
570
576 void UpdateAllLayersColor();
577
585 virtual void SetTopLayer( int aLayer, bool aEnabled = true );
586
596 virtual void EnableTopLayer( bool aEnable );
597
598 virtual int GetTopLayer() const;
599
604 void ClearTopLayers();
605
611 void UpdateAllLayersOrder();
612
616 void ClearTargets();
617
621 virtual void Redraw();
622
626 void RecacheAllItems();
627
633 bool IsDirty() const
634 {
635 for( int i = 0; i < TARGETS_NUMBER; ++i )
636 {
637 if( IsTargetDirty( i ) )
638 return true;
639 }
640
641 return false;
642 }
643
650 bool IsTargetDirty( int aTarget ) const
651 {
652 wxCHECK( aTarget < TARGETS_NUMBER, false );
653 return m_dirtyTargets[aTarget];
654 }
655
661 inline void MarkTargetDirty( int aTarget )
662 {
663 wxCHECK( aTarget < TARGETS_NUMBER, /* void */ );
664 m_dirtyTargets[aTarget] = true;
665 }
666
668 inline bool IsCached( int aLayer ) const
669 {
670 auto it = m_layers.find( aLayer );
671
672 if( it == m_layers.end() )
673 return false;
674
675 return it->second.target == TARGET_CACHED;
676 }
677
682 {
683 for( int i = 0; i < TARGETS_NUMBER; ++i )
684 m_dirtyTargets[i] = true;
685 }
686
691 {
692 for( int i = 0; i < TARGETS_NUMBER; ++i )
693 m_dirtyTargets[i] = false;
694 }
695
699 void UpdateItems();
700
705 {
707 }
708
714 void UpdateAllItems( int aUpdateFlags );
715
722 void UpdateAllItemsConditionally( int aUpdateFlags,
723 std::function<bool( VIEW_ITEM* )> aCondition );
724
730 void UpdateAllItemsConditionally( std::function<int( VIEW_ITEM* )> aItemFlagsProvider );
731
736 {
737 return m_useDrawPriority;
738 }
739
743 void UseDrawPriority( bool aFlag )
744 {
745 m_useDrawPriority = aFlag;
746 }
747
753 void ReverseDrawOrder( bool aFlag )
754 {
755 m_reverseDrawOrder = aFlag;
756 }
757
758 std::shared_ptr<VIEW_OVERLAY> MakeOverlay();
759
760 void InitPreview();
761
762 void ClearPreview();
763 void AddToPreview( VIEW_ITEM* aItem, bool aTakeOwnership = true );
764
765 void ShowPreview( bool aShow = true );
766
772 std::unique_ptr<VIEW> DataReference() const;
773
775 static constexpr int VIEW_MAX_LAYERS = MAX_LAYERS_FOR_VIEW;
776
779
780protected:
782 {
783 bool visible;
785
788
791 std::shared_ptr<VIEW_RTREE> items;
793 int id;
795
797 std::set<int> requiredLayers;
798
799 bool operator< ( const VIEW_LAYER& aOther ) const
800 {
801 return id < aOther.id;
802 }
803 };
804
814 void unlinkItem( VIEW_ITEM* aItem );
815
817 void redrawRect( const BOX2I& aRect );
818
819 inline void markTargetClean( int aTarget )
820 {
821 wxCHECK( aTarget < TARGETS_NUMBER, /* void */ );
822 m_dirtyTargets[aTarget] = false;
823 }
824
836 void draw( VIEW_ITEM* aItem, int aLayer, bool aImmediate = false );
837
845 void draw( VIEW_ITEM* aItem, bool aImmediate = false );
846
854 void draw( VIEW_GROUP* aGroup, bool aImmediate = false );
855
858 void clearGroupCache();
859
866 void invalidateItem( VIEW_ITEM* aItem, int aUpdateFlags );
867
869 void updateItemColor( VIEW_ITEM* aItem, int aLayer );
870
871 void recolorGroup( VIEW_ITEM* aItem, int aLayer, int aGroup );
872
874 void updateItemGeometry( VIEW_ITEM* aItem, int aLayer );
875
877 void updateBbox( VIEW_ITEM* aItem );
878
880 void updateLayers( VIEW_ITEM* aItem );
881
884 {
885 return aI->renderingOrder > aJ->renderingOrder;
886 }
887
889 bool areRequiredLayersEnabled( int aLayerId ) const;
890
891 // Function objects that need to access VIEW/VIEW_ITEM private/protected members
892 struct CLEAR_LAYER_CACHE_VISITOR;
893 struct RECACHE_ITEM_VISITOR;
894 struct DRAW_ITEM_VISITOR;
895 struct UPDATE_COLOR_VISITOR;
896 struct UPDATE_DEPTH_VISITOR;
897
898 std::unique_ptr<KIGFX::VIEW_GROUP> m_preview;
899 std::vector<VIEW_ITEM*> m_ownedItems;
900
903
905 std::map<int, VIEW_LAYER> m_layers;
906
908 std::vector<VIEW_LAYER*> m_orderedLayers;
909
911 std::shared_ptr<std::vector<VIEW_ITEM*>> m_allItems;
912
914 std::set<unsigned int> m_topLayers;
915
918
919 double m_scale;
923
928
931
934
937
940
943
946
949};
950} // namespace KIGFX
BOX2< VECTOR2I > BOX2I
Definition box2.h:914
BOX2< VECTOR2D > BOX2D
Definition box2.h:915
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:283
double m_maxScale
Definition view.h:922
void ReverseDrawOrder(bool aFlag)
Only takes effect if UseDrawPriority is true.
Definition view.h:753
const VECTOR2D & GetCenter() const
Return the center point of this VIEW (in world space coordinates).
Definition view.h:353
void CopySettings(const VIEW *aOtherView)
Copy layers and visibility settings from another view.
Definition view.cpp:586
static constexpr int TOP_LAYER_MODIFIER
Rendering order modifier for layers that are marked as top layers.
Definition view.h:778
VIEW(const VIEW &)=delete
bool m_reverseDrawOrder
Flag to reverse the draw order when using draw priority.
Definition view.h:945
void SetRequired(int aLayerId, int aRequiredId, bool aRequired=true)
Mark the aRequiredId layer as required for the aLayerId layer.
Definition view.cpp:489
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:495
static bool compareRenderingOrder(VIEW_LAYER *aI, VIEW_LAYER *aJ)
Determine rendering order of layers. Used in display order sorting function.
Definition view.h:883
virtual void Add(VIEW_ITEM *aItem, int aDrawPriority=-1)
Add a VIEW_ITEM to the view.
Definition view.cpp:297
virtual void Remove(VIEW_ITEM *aItem)
Remove a VIEW_ITEM from the view.
Definition view.cpp:412
bool m_mirrorX
Definition view.h:924
void SetPainter(PAINTER *aPainter)
Set the painter object used by the view for drawing VIEW_ITEM objects.
Definition view.h:217
void SetBoundary(const BOX2D &aBoundary)
Set limits for view area.
Definition view.h:293
bool HasPendingItemUpdates() const
Definition view.h:704
bool m_mirrorY
Definition view.h:925
std::shared_ptr< std::vector< VIEW_ITEM * > > m_allItems
Flat list of all items.
Definition view.h:911
void SetLayerDiff(int aLayer, bool aDiff=true)
Set the whether the layer should drawn differentially.
Definition view.h:452
void SetGAL(GAL *aGal)
Assign a rendering device for the VIEW.
Definition view.cpp:592
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:501
std::vector< VIEW_ITEM * > m_ownedItems
Definition view.h:899
void SetLayerTarget(int aLayer, RENDER_TARGET aTarget)
Change the rendering target for a particular layer.
Definition view.h:511
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:1892
void SetLayerVisible(int aLayer, bool aVisible=true)
Control the visibility of a particular layer.
Definition view.h:407
bool HasItem(const VIEW_ITEM *aItem) const
Indicates whether or not the given item has been added to the view.
Definition view.cpp:1878
PAINTER * m_painter
PAINTER contains information how do draw items.
Definition view.h:930
std::set< unsigned int > m_topLayers
The set of layers that are displayed on the top.
Definition view.h:914
bool m_enableOrderModifier
Whether to use rendering order modifier or not.
Definition view.h:902
void SetLayerHasNegatives(int aLayer, bool aNegatives=true)
Set the status of negatives presense in a particular layer.
Definition view.h:475
GAL * GetGAL() const
Return the GAL this view is using to draw graphical primitives.
Definition view.h:209
bool IsHiddenOnOverlay(const VIEW_ITEM *aItem) const
Definition view.cpp:1870
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:936
void MarkClean()
Force redraw of view on the next rendering.
Definition view.h:690
static constexpr int VIEW_MAX_LAYERS
Maximum number of layers that may be shown.
Definition view.h:775
double m_minScale
Definition view.h:921
double m_scale
Definition view.h:919
bool IsMirroredX() const
Return true if view is flipped across the X axis.
Definition view.h:257
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:650
void SetBoundary(const BOX2I &aBoundary)
Set limits for view area.
Definition view.h:303
int m_nextDrawPriority
The next sequential drawing priority.
Definition view.h:942
bool m_useDrawPriority
Flag to respect draw priority when drawing items.
Definition view.h:939
bool IsLayerVisibleCached(int aLayer) const
Definition view.h:441
bool IsCached(int aLayer) const
Return true if the layer is cached.
Definition view.h:668
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:342
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:265
BASE_SET m_layerVisibilityCache
Definition view.h:926
void UseDrawPriority(bool aFlag)
Definition view.h:743
bool m_hasPendingItemUpdates
True when at least one item has deferred update flags that still need processing.
Definition view.h:948
std::unique_ptr< KIGFX::VIEW_GROUP > m_preview
Definition view.h:898
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:222
bool IsUsingDrawPriority() const
Definition view.h:735
VIEW & operator=(const VIEW &)=delete
GAL * m_gal
Interface to PAINTER that is used to draw items.
Definition view.h:933
bool IsDirty() const
Return true if any of the VIEW layers needs to be refreshened.
Definition view.h:633
std::map< int, VIEW_LAYER > m_layers
The set of possible displayed layers and its properties.
Definition view.h:905
void Hide(VIEW_ITEM *aItem, bool aHide=true, bool aHideOverlay=false)
Temporarily hide the item in the view (e.g.
Definition view.cpp:1837
void markTargetClean(int aTarget)
Definition view.h:819
void MarkDirty()
Force redraw of view on the next rendering.
Definition view.h:681
friend class VIEW_ITEM
Definition view.h:65
bool IsLayerVisible(int aLayer) const
Return information about visibility of a particular layer.
Definition view.h:429
BOX2D m_boundary
Definition view.h:920
const BOX2D & GetBoundary() const
Definition view.h:312
PAINTER * GetPainter() const
Return the painter object used by the view for drawing VIEW_ITEM objects.
Definition view.h:227
void SetScaleLimits(double aMaximum, double aMinimum)
Set minimum and maximum values for scale.
Definition view.h:323
std::vector< VIEW_LAYER * > m_orderedLayers
Sorted list of pointers to members of m_layers.
Definition view.h:908
VECTOR2D m_center
Center point of the VIEW (the point at which we are looking at).
Definition view.h:917
void MarkTargetDirty(int aTarget)
Set or clear target 'dirty' flag.
Definition view.h:661
bool IsVisible(const VIEW_ITEM *aItem) const
Return information if the item is visible (or not).
Definition view.cpp:1862
BASE_SET m_layerCachedFlagCache
Definition view.h:927
void SetVisible(VIEW_ITEM *aItem, bool aIsVisible=true)
Set the item visibility.
Definition view.cpp:1813
#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
Layers that have to be enabled to show the layer.
Definition view.h:797
bool diffLayer
Layer should be drawn differentially over lower layers.
Definition view.h:787
int renderingOrder
Rendering order of this layer.
Definition view.h:792
bool hasNegatives
Layer should be drawn separately to not delete lower layers.
Definition view.h:790
bool visible
Is the layer to be rendered?
Definition view.h:783
bool displayOnly
Is the layer display only?
Definition view.h:784
std::shared_ptr< VIEW_RTREE > items
R-tree indexing all items on this layer.
Definition view.h:791
RENDER_TARGET target
Where the layer should be rendered.
Definition view.h:794
int id
Layer ID.
Definition view.h:793
VECTOR2< int32_t > VECTOR2I
Definition vector2d.h:708
VECTOR2< double > VECTOR2D
Definition vector2d.h:707