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, you may find one here:
21 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
22 * or you may search the http://www.gnu.org website for the version 2 license,
23 * or you may write to the Free Software Foundation, Inc.,
24 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
25 */
26
27#pragma once
28
29#include <gal/gal.h>
30#include <map>
31#include <vector>
32#include <set>
33#include <unordered_map>
34#include <memory>
35
36#include <math/box2.h>
37#include <gal/definitions.h>
38
39#include <view/view_overlay.h>
40
41namespace KIGFX
42{
43class PAINTER;
44class GAL;
45class VIEW_ITEM;
46class VIEW_GROUP;
47class VIEW_RTREE;
48
67{
68public:
69 friend class VIEW_ITEM;
70
71 typedef std::pair<VIEW_ITEM*, int> LAYER_ITEM_PAIR;
72
73 VIEW();
74 virtual ~VIEW();
75
76 // We own at least one list of raw pointers. Don't let the compiler fill in copy c'tors that
77 // will only land us in trouble.
78 VIEW( const VIEW& ) = delete;
79 VIEW& operator=( const VIEW& ) = delete;
80
88 static void OnDestroy( VIEW_ITEM* aItem );
89
98 virtual void Add( VIEW_ITEM* aItem, int aDrawPriority = -1 );
99
106 void AddBatch( const std::vector<VIEW_ITEM*>& aItems );
107
113 virtual void Remove( VIEW_ITEM* aItem );
114
115
124 int Query( const BOX2I& aRect, std::vector<LAYER_ITEM_PAIR>& aResult ) const;
125
131 void Query( const BOX2I& aRect, const std::function<bool( VIEW_ITEM* )>& aFunc ) const;
132
139 void SetVisible( VIEW_ITEM* aItem, bool aIsVisible = true );
140
148 void Hide( VIEW_ITEM* aItem, bool aHide = true, bool aHideOverlay = false );
149
157 bool IsVisible( const VIEW_ITEM* aItem ) const;
158
159 bool IsHiddenOnOverlay( const VIEW_ITEM* aItem ) const;
160
164 bool HasItem( const VIEW_ITEM* aItem ) const;
165
173 virtual void Update( const VIEW_ITEM* aItem, int aUpdateFlags ) const;
174 virtual void Update( const VIEW_ITEM* aItem ) const;
175
184 void SetRequired( int aLayerId, int aRequiredId, bool aRequired = true );
185
191 void CopySettings( const VIEW* aOtherView );
192
193 /*
194 * Convenience wrappers for adding multiple items
195 * template <class T> void AddItems( const T& aItems );
196 * template <class T> void RemoveItems( const T& aItems );
197 */
198
204 void SetGAL( GAL* aGal );
205
211 inline GAL* GetGAL() const
212 {
213 return m_gal;
214 }
215
219 inline void SetPainter( PAINTER* aPainter )
220 {
221 m_painter = aPainter;
222 }
223
229 inline PAINTER* GetPainter() const
230 {
231 return m_painter;
232 }
233
239 void SetViewport( const BOX2D& aViewport );
240
246 BOX2D GetViewport() const;
247
254 void SetMirror( bool aMirrorX, bool aMirrorY );
255
259 bool IsMirroredX() const
260 {
261 return m_mirrorX;
262 }
263
267 bool IsMirroredY() const
268 {
269 return m_mirrorY;
270 }
271
280 virtual void SetScale( double aScale, VECTOR2D aAnchor = { 0, 0 } );
281
285 inline double GetScale() const
286 {
287 return m_scale;
288 }
289
295 inline void SetBoundary( const BOX2D& aBoundary )
296 {
297 m_boundary = aBoundary;
298 }
299
305 inline void SetBoundary( const BOX2I& aBoundary )
306 {
307 m_boundary.SetOrigin( aBoundary.GetOrigin() );
308 m_boundary.SetEnd( aBoundary.GetEnd() );
309 }
310
314 inline const BOX2D& GetBoundary() const
315 {
316 return m_boundary;
317 }
318
325 void SetScaleLimits( double aMaximum, double aMinimum )
326 {
327 wxASSERT_MSG( aMaximum > aMinimum, wxT( "I guess you passed parameters in wrong order" ) );
328
329 m_minScale = aMinimum;
330 m_maxScale = aMaximum;
331 }
332
339 void SetCenter( const VECTOR2D& aCenter );
340
348 void SetCenter( const VECTOR2D& aCenter, const std::vector<BOX2D>& obscuringScreenRects );
349
355 const VECTOR2D& GetCenter() const
356 {
357 return m_center;
358 }
359
366 VECTOR2D ToWorld( const VECTOR2D& aCoord, bool aAbsolute = true ) const;
367
374 double ToWorld( double aSize ) const;
375
382 VECTOR2D ToScreen( const VECTOR2D& aCoord, bool aAbsolute = true ) const;
383
389 double ToScreen( double aSize ) const;
390
396 const VECTOR2I& GetScreenPixelSize() const;
397
401 void Clear();
402
409 inline void SetLayerVisible( int aLayer, bool aVisible = true )
410 {
411 auto it = m_layers.find( aLayer );
412
413 if( it == m_layers.end() )
414 return;
415
416 VIEW_LAYER& layer = it->second;
417
418 if( layer.visible != aVisible )
419 {
420 // Target has to be redrawn after changing its visibility
421 MarkTargetDirty( layer.target );
422 layer.visible = aVisible;
423 }
424 }
425
431 inline bool IsLayerVisible( int aLayer ) const
432 {
433 auto it = m_layers.find( aLayer );
434
435 if( it == m_layers.end() )
436 return false;
437
438 return it->second.visible;
439 }
440
447 inline void SetLayerDiff( int aLayer, bool aDiff = true )
448 {
449 auto it = m_layers.find( aLayer );
450
451 if( it == m_layers.end() )
452 return;
453
454 VIEW_LAYER& layer = it->second;
455
456 if( layer.diffLayer != aDiff )
457 {
458 // Target has to be redrawn after changing its layers' diff status
459 MarkTargetDirty( layer.target );
460 layer.diffLayer = aDiff;
461 }
462 }
463
470 inline void SetLayerHasNegatives( int aLayer, bool aNegatives = true )
471 {
472 auto it = m_layers.find( aLayer );
473
474 if( it == m_layers.end() )
475 return;
476
477 VIEW_LAYER& layer = it->second;
478
479 if( layer.hasNegatives != aNegatives )
480 {
481 // Target has to be redrawn after changing a layers' negatives
482 MarkTargetDirty( layer.target );
483 layer.hasNegatives = aNegatives;
484 }
485 }
486
490 inline void SetLayerDisplayOnly( int aLayer, bool aDisplayOnly = true )
491 {
492 auto it = m_layers.find( aLayer );
493
494 if( it == m_layers.end() )
495 return;
496
497 it->second.displayOnly = aDisplayOnly;
498 }
499
506 inline void SetLayerTarget( int aLayer, RENDER_TARGET aTarget )
507 {
508 auto it = m_layers.find( aLayer );
509
510 if( it == m_layers.end() )
511 return;
512
513 it->second.target = aTarget;
514 }
515
524 void SetLayerOrder( int aLayer, int aRenderingOrder, bool aAutoSort = true );
525
532 int GetLayerOrder( int aLayer ) const;
533
538 void SortOrderedLayers();
539
547 void SortLayers( std::vector<int>& aLayers ) const;
548
556 void ReorderLayerData( std::unordered_map<int, int> aReorderMap );
557
564 void UpdateLayerColor( int aLayer );
565
571 void UpdateAllLayersColor();
572
580 virtual void SetTopLayer( int aLayer, bool aEnabled = true );
581
591 virtual void EnableTopLayer( bool aEnable );
592
593 virtual int GetTopLayer() const;
594
599 void ClearTopLayers();
600
606 void UpdateAllLayersOrder();
607
611 void ClearTargets();
612
616 virtual void Redraw();
617
621 void RecacheAllItems();
622
628 bool IsDirty() const
629 {
630 for( int i = 0; i < TARGETS_NUMBER; ++i )
631 {
632 if( IsTargetDirty( i ) )
633 return true;
634 }
635
636 return false;
637 }
638
645 bool IsTargetDirty( int aTarget ) const
646 {
647 wxCHECK( aTarget < TARGETS_NUMBER, false );
648 return m_dirtyTargets[aTarget];
649 }
650
656 inline void MarkTargetDirty( int aTarget )
657 {
658 wxCHECK( aTarget < TARGETS_NUMBER, /* void */ );
659 m_dirtyTargets[aTarget] = true;
660 }
661
663 inline bool IsCached( int aLayer ) const
664 {
665 auto it = m_layers.find( aLayer );
666
667 if( it == m_layers.end() )
668 return false;
669
670 return it->second.target == TARGET_CACHED;
671 }
672
677 {
678 for( int i = 0; i < TARGETS_NUMBER; ++i )
679 m_dirtyTargets[i] = true;
680 }
681
686 {
687 for( int i = 0; i < TARGETS_NUMBER; ++i )
688 m_dirtyTargets[i] = false;
689 }
690
694 void UpdateItems();
695
700 {
702 }
703
709 void UpdateAllItems( int aUpdateFlags );
710
717 void UpdateAllItemsConditionally( int aUpdateFlags,
718 std::function<bool( VIEW_ITEM* )> aCondition );
719
725 void UpdateAllItemsConditionally( std::function<int( VIEW_ITEM* )> aItemFlagsProvider );
726
731 {
732 return m_useDrawPriority;
733 }
734
738 void UseDrawPriority( bool aFlag )
739 {
740 m_useDrawPriority = aFlag;
741 }
742
748 void ReverseDrawOrder( bool aFlag )
749 {
750 m_reverseDrawOrder = aFlag;
751 }
752
753 std::shared_ptr<VIEW_OVERLAY> MakeOverlay();
754
755 void InitPreview();
756
757 void ClearPreview();
758 void AddToPreview( VIEW_ITEM* aItem, bool aTakeOwnership = true );
759
760 void ShowPreview( bool aShow = true );
761
767 std::unique_ptr<VIEW> DataReference() const;
768
770 static constexpr int VIEW_MAX_LAYERS = MAX_LAYERS_FOR_VIEW;
771
774
775protected:
777 {
778 bool visible;
780
783
786 std::shared_ptr<VIEW_RTREE> items;
788 int id;
790
792 std::set<int> requiredLayers;
793
794 bool operator< ( const VIEW_LAYER& aOther ) const
795 {
796 return id < aOther.id;
797 }
798 };
799
801 void redrawRect( const BOX2I& aRect );
802
803 inline void markTargetClean( int aTarget )
804 {
805 wxCHECK( aTarget < TARGETS_NUMBER, /* void */ );
806 m_dirtyTargets[aTarget] = false;
807 }
808
820 void draw( VIEW_ITEM* aItem, int aLayer, bool aImmediate = false );
821
829 void draw( VIEW_ITEM* aItem, bool aImmediate = false );
830
838 void draw( VIEW_GROUP* aGroup, bool aImmediate = false );
839
842 void clearGroupCache();
843
850 void invalidateItem( VIEW_ITEM* aItem, int aUpdateFlags );
851
853 void updateItemColor( VIEW_ITEM* aItem, int aLayer );
854
856 void updateItemGeometry( VIEW_ITEM* aItem, int aLayer );
857
859 void updateBbox( VIEW_ITEM* aItem );
860
862 void updateLayers( VIEW_ITEM* aItem );
863
866 {
867 return aI->renderingOrder > aJ->renderingOrder;
868 }
869
871 bool areRequiredLayersEnabled( int aLayerId ) const;
872
873 // Function objects that need to access VIEW/VIEW_ITEM private/protected members
874 struct CLEAR_LAYER_CACHE_VISITOR;
875 struct RECACHE_ITEM_VISITOR;
876 struct DRAW_ITEM_VISITOR;
877 struct UPDATE_COLOR_VISITOR;
878 struct UPDATE_DEPTH_VISITOR;
879
880 std::unique_ptr<KIGFX::VIEW_GROUP> m_preview;
881 std::vector<VIEW_ITEM*> m_ownedItems;
882
885
887 std::map<int, VIEW_LAYER> m_layers;
888
890 std::vector<VIEW_LAYER*> m_orderedLayers;
891
893 std::shared_ptr<std::vector<VIEW_ITEM*>> m_allItems;
894
896 std::set<unsigned int> m_topLayers;
897
900
901 double m_scale;
905
908
911
914
917
920
923
926
929};
930} // namespace KIGFX
BOX2< VECTOR2I > BOX2I
Definition box2.h:922
BOX2< VECTOR2D > BOX2D
Definition box2.h:923
constexpr const Vec GetEnd() const
Definition box2.h:212
constexpr const Vec & GetOrigin() const
Definition box2.h:210
Abstract interface for drawing on a 2D-surface.
Contains all the knowledge about how to draw graphical object onto any particular output device.
Definition painter.h:59
Extend VIEW_ITEM by possibility of grouping items into a single object.
Definition view_group.h:43
An abstract base class for deriving all objects that can be added to a VIEW.
Definition view_item.h:86
Implement a non-owning R-tree for fast spatial indexing of VIEW items.
Definition view_rtree.h:46
double GetScale() const
Definition view.h:285
double m_maxScale
Definition view.h:904
void ReverseDrawOrder(bool aFlag)
Only takes effect if UseDrawPriority is true.
Definition view.h:748
const VECTOR2D & GetCenter() const
Return the center point of this VIEW (in world space coordinates).
Definition view.h:355
void CopySettings(const VIEW *aOtherView)
Copy layers and visibility settings from another view.
Definition view.cpp:573
static constexpr int TOP_LAYER_MODIFIER
Rendering order modifier for layers that are marked as top layers.
Definition view.h:773
VIEW(const VIEW &)=delete
bool m_reverseDrawOrder
Flag to reverse the draw order when using draw priority.
Definition view.h:925
void SetRequired(int aLayerId, int aRequiredId, bool aRequired=true)
Mark the aRequiredId layer as required for the aLayerId layer.
Definition view.cpp:476
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:490
static bool compareRenderingOrder(VIEW_LAYER *aI, VIEW_LAYER *aJ)
Determine rendering order of layers. Used in display order sorting function.
Definition view.h:865
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:405
bool m_mirrorX
Definition view.h:906
void SetPainter(PAINTER *aPainter)
Set the painter object used by the view for drawing #VIEW_ITEMS.
Definition view.h:219
void SetBoundary(const BOX2D &aBoundary)
Set limits for view area.
Definition view.h:295
bool HasPendingItemUpdates() const
Definition view.h:699
bool m_mirrorY
Definition view.h:907
std::shared_ptr< std::vector< VIEW_ITEM * > > m_allItems
Flat list of all items.
Definition view.h:893
void SetLayerDiff(int aLayer, bool aDiff=true)
Set the whether the layer should drawn differentially.
Definition view.h:447
void SetGAL(GAL *aGal)
Assign a rendering device for the VIEW.
Definition view.cpp:579
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:488
std::vector< VIEW_ITEM * > m_ownedItems
Definition view.h:881
void SetLayerTarget(int aLayer, RENDER_TARGET aTarget)
Change the rendering target for a particular layer.
Definition view.h:506
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:1809
void SetLayerVisible(int aLayer, bool aVisible=true)
Control the visibility of a particular layer.
Definition view.h:409
bool HasItem(const VIEW_ITEM *aItem) const
Indicates whether or not the given item has been added to the view.
Definition view.cpp:1795
PAINTER * m_painter
PAINTER contains information how do draw items.
Definition view.h:910
std::set< unsigned int > m_topLayers
The set of layers that are displayed on the top.
Definition view.h:896
bool m_enableOrderModifier
Whether to use rendering order modifier or not.
Definition view.h:884
void SetLayerHasNegatives(int aLayer, bool aNegatives=true)
Set the status of negatives presense in a particular layer.
Definition view.h:470
GAL * GetGAL() const
Return the GAL this view is using to draw graphical primitives.
Definition view.h:211
bool IsHiddenOnOverlay(const VIEW_ITEM *aItem) const
Definition view.cpp:1787
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:916
void MarkClean()
Force redraw of view on the next rendering.
Definition view.h:685
static constexpr int VIEW_MAX_LAYERS
Maximum number of layers that may be shown.
Definition view.h:770
double m_minScale
Definition view.h:903
double m_scale
Definition view.h:901
bool IsMirroredX() const
Return true if view is flipped across the X axis.
Definition view.h:259
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:645
void SetBoundary(const BOX2I &aBoundary)
Set limits for view area.
Definition view.h:305
int m_nextDrawPriority
The next sequential drawing priority.
Definition view.h:922
bool m_useDrawPriority
Flag to respect draw priority when drawing items.
Definition view.h:919
bool IsCached(int aLayer) const
Return true if the layer is cached.
Definition view.h:663
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:344
std::pair< VIEW_ITEM *, int > LAYER_ITEM_PAIR
Definition view.h:71
bool IsMirroredY() const
Return true if view is flipped across the Y axis.
Definition view.h:267
void UseDrawPriority(bool aFlag)
Definition view.h:738
bool m_hasPendingItemUpdates
True when at least one item has deferred update flags that still need processing.
Definition view.h:928
std::unique_ptr< KIGFX::VIEW_GROUP > m_preview
Definition view.h:880
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:228
bool IsUsingDrawPriority() const
Definition view.h:730
VIEW & operator=(const VIEW &)=delete
GAL * m_gal
Interface to PAINTER that is used to draw items.
Definition view.h:913
bool IsDirty() const
Return true if any of the VIEW layers needs to be refreshened.
Definition view.h:628
std::map< int, VIEW_LAYER > m_layers
The set of possible displayed layers and its properties.
Definition view.h:887
void Hide(VIEW_ITEM *aItem, bool aHide=true, bool aHideOverlay=false)
Temporarily hide the item in the view (e.g.
Definition view.cpp:1757
void markTargetClean(int aTarget)
Definition view.h:803
void MarkDirty()
Force redraw of view on the next rendering.
Definition view.h:676
friend class VIEW_ITEM
Definition view.h:69
bool IsLayerVisible(int aLayer) const
Return information about visibility of a particular layer.
Definition view.h:431
BOX2D m_boundary
Definition view.h:902
const BOX2D & GetBoundary() const
Definition view.h:314
PAINTER * GetPainter() const
Return the painter object used by the view for drawing #VIEW_ITEMS.
Definition view.h:229
void SetScaleLimits(double aMaximum, double aMinimum)
Set minimum and maximum values for scale.
Definition view.h:325
std::vector< VIEW_LAYER * > m_orderedLayers
Sorted list of pointers to members of m_layers.
Definition view.h:890
VECTOR2D m_center
Center point of the VIEW (the point at which we are looking at).
Definition view.h:899
void MarkTargetDirty(int aTarget)
Set or clear target 'dirty' flag.
Definition view.h:656
bool IsVisible(const VIEW_ITEM *aItem) const
Return information if the item is visible (or not).
Definition view.cpp:1779
void SetVisible(VIEW_ITEM *aItem, bool aIsVisible=true)
Set the item visibility.
Definition view.cpp:1736
#define MAX_LAYERS_FOR_VIEW
Definition definitions.h:45
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:33
RENDER_TARGET
RENDER_TARGET: Possible rendering targets.
Definition definitions.h:36
@ TARGET_CACHED
Main rendering target (cached)
Definition definitions.h:37
@ TARGETS_NUMBER
Number of available rendering targets.
Definition definitions.h:41
std::set< int > requiredLayers
Definition view.h:792
bool diffLayer
Layer should be drawn differentially over lower layers.
Definition view.h:782
int renderingOrder
Rendering order of this layer.
Definition view.h:787
bool hasNegatives
Layer should be drawn separately to not delete lower layers.
Definition view.h:785
bool visible
Is the layer to be rendered?
Definition view.h:778
bool displayOnly
Is the layer display only?
Definition view.h:779
std::shared_ptr< VIEW_RTREE > items
R-tree indexing all items on this layer.
Definition view.h:786
RENDER_TARGET target
Where the layer should be rendered.
Definition view.h:789
int id
Layer ID.
Definition view.h:788
VECTOR2< int32_t > VECTOR2I
Definition vector2d.h:687
VECTOR2< double > VECTOR2D
Definition vector2d.h:686