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 (C) 2020 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#ifndef __VIEW_H
28#define __VIEW_H
29
30#include <gal/gal.h>
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#include <view/view.h>
41
42namespace KIGFX
43{
44class PAINTER;
45class GAL;
46class VIEW_ITEM;
47class VIEW_GROUP;
48class VIEW_RTREE;
49
68{
69public:
70 friend class VIEW_ITEM;
71
72 typedef std::pair<VIEW_ITEM*, int> LAYER_ITEM_PAIR;
73
74 VIEW();
75 virtual ~VIEW();
76
84 static void OnDestroy( VIEW_ITEM* aItem );
85
94 virtual void Add( VIEW_ITEM* aItem, int aDrawPriority = -1 );
95
101 virtual void Remove( VIEW_ITEM* aItem );
102
103
112 int Query( const BOX2I& aRect, std::vector<LAYER_ITEM_PAIR>& aResult ) const;
113
119 void Query( const BOX2I& aRect, const std::function<bool( VIEW_ITEM* )>& aFunc ) const;
120
127 void SetVisible( VIEW_ITEM* aItem, bool aIsVisible = true );
128
136 void Hide( VIEW_ITEM* aItem, bool aHide = true, bool aHideOverlay = false );
137
145 bool IsVisible( const VIEW_ITEM* aItem ) const;
146
147 bool IsHiddenOnOverlay( const VIEW_ITEM* aItem ) const;
148
152 bool HasItem( const VIEW_ITEM* aItem ) const;
153
161 virtual void Update( const VIEW_ITEM* aItem, int aUpdateFlags ) const;
162 virtual void Update( const VIEW_ITEM* aItem ) const;
163
172 void SetRequired( int aLayerId, int aRequiredId, bool aRequired = true );
173
179 void CopySettings( const VIEW* aOtherView );
180
181 /*
182 * Convenience wrappers for adding multiple items
183 * template <class T> void AddItems( const T& aItems );
184 * template <class T> void RemoveItems( const T& aItems );
185 */
186
192 void SetGAL( GAL* aGal );
193
199 inline GAL* GetGAL() const
200 {
201 return m_gal;
202 }
203
207 inline void SetPainter( PAINTER* aPainter )
208 {
209 m_painter = aPainter;
210 }
211
217 inline PAINTER* GetPainter() const
218 {
219 return m_painter;
220 }
221
227 void SetViewport( const BOX2D& aViewport );
228
234 BOX2D GetViewport() const;
235
242 void SetMirror( bool aMirrorX, bool aMirrorY );
243
247 bool IsMirroredX() const
248 {
249 return m_mirrorX;
250 }
251
255 bool IsMirroredY() const
256 {
257 return m_mirrorY;
258 }
259
268 virtual void SetScale( double aScale, VECTOR2D aAnchor = { 0, 0 } );
269
273 inline double GetScale() const
274 {
275 return m_scale;
276 }
277
283 inline void SetBoundary( const BOX2D& aBoundary )
284 {
285 m_boundary = aBoundary;
286 }
287
293 inline void SetBoundary( const BOX2I& aBoundary )
294 {
295 m_boundary.SetOrigin( aBoundary.GetOrigin() );
296 m_boundary.SetEnd( aBoundary.GetEnd() );
297 }
298
302 inline const BOX2D& GetBoundary() const
303 {
304 return m_boundary;
305 }
306
313 void SetScaleLimits( double aMaximum, double aMinimum )
314 {
315 wxASSERT_MSG( aMaximum > aMinimum, wxT( "I guess you passed parameters in wrong order" ) );
316
317 m_minScale = aMinimum;
318 m_maxScale = aMaximum;
319 }
320
327 void SetCenter( const VECTOR2D& aCenter );
328
336 void SetCenter( const VECTOR2D& aCenter, const std::vector<BOX2D>& obscuringScreenRects );
337
343 const VECTOR2D& GetCenter() const
344 {
345 return m_center;
346 }
347
354 VECTOR2D ToWorld( const VECTOR2D& aCoord, bool aAbsolute = true ) const;
355
362 double ToWorld( double aSize ) const;
363
370 VECTOR2D ToScreen( const VECTOR2D& aCoord, bool aAbsolute = true ) const;
371
377 double ToScreen( double aSize ) const;
378
384 const VECTOR2I& GetScreenPixelSize() const;
385
389 void Clear();
390
397 inline void SetLayerVisible( int aLayer, bool aVisible = true )
398 {
399 auto it = m_layers.find( aLayer );
400
401 if( it == m_layers.end() )
402 return;
403
404 VIEW_LAYER& layer = it->second;
405
406 if( layer.visible != aVisible )
407 {
408 // Target has to be redrawn after changing its visibility
409 MarkTargetDirty( layer.target );
410 layer.visible = aVisible;
411 }
412 }
413
419 inline bool IsLayerVisible( int aLayer ) const
420 {
421 auto it = m_layers.find( aLayer );
422
423 if( it == m_layers.end() )
424 return false;
425
426 return it->second.visible;
427 }
428
435 inline void SetLayerDiff( int aLayer, bool aDiff = true )
436 {
437 auto it = m_layers.find( aLayer );
438
439 if( it == m_layers.end() )
440 return;
441
442 VIEW_LAYER& layer = it->second;
443
444 if( layer.diffLayer != aDiff )
445 {
446 // Target has to be redrawn after changing its layers' diff status
447 MarkTargetDirty( layer.target );
448 layer.diffLayer = aDiff;
449 }
450 }
451
458 inline void SetLayerHasNegatives( int aLayer, bool aNegatives = true )
459 {
460 auto it = m_layers.find( aLayer );
461
462 if( it == m_layers.end() )
463 return;
464
465 VIEW_LAYER& layer = it->second;
466
467 if( layer.hasNegatives != aNegatives )
468 {
469 // Target has to be redrawn after changing a layers' negatives
470 MarkTargetDirty( layer.target );
471 layer.hasNegatives = aNegatives;
472 }
473 }
474
478 inline void SetLayerDisplayOnly( int aLayer, bool aDisplayOnly = true )
479 {
480 auto it = m_layers.find( aLayer );
481
482 if( it == m_layers.end() )
483 return;
484
485 it->second.displayOnly = aDisplayOnly;
486 }
487
494 inline void SetLayerTarget( int aLayer, RENDER_TARGET aTarget )
495 {
496 auto it = m_layers.find( aLayer );
497
498 if( it == m_layers.end() )
499 return;
500
501 it->second.target = aTarget;
502 }
503
510 void SetLayerOrder( int aLayer, int aRenderingOrder );
511
518 int GetLayerOrder( int aLayer ) const;
519
527 void SortLayers( std::vector<int> aLayers ) const;
528
536 void ReorderLayerData( std::unordered_map<int, int> aReorderMap );
537
544 void UpdateLayerColor( int aLayer );
545
551 void UpdateAllLayersColor();
552
560 virtual void SetTopLayer( int aLayer, bool aEnabled = true );
561
571 virtual void EnableTopLayer( bool aEnable );
572
573 virtual int GetTopLayer() const;
574
579 void ClearTopLayers();
580
586 void UpdateAllLayersOrder();
587
591 void ClearTargets();
592
596 virtual void Redraw();
597
601 void RecacheAllItems();
602
608 bool IsDirty() const
609 {
610 for( int i = 0; i < TARGETS_NUMBER; ++i )
611 {
612 if( IsTargetDirty( i ) )
613 return true;
614 }
615
616 return false;
617 }
618
625 bool IsTargetDirty( int aTarget ) const
626 {
627 wxCHECK( aTarget < TARGETS_NUMBER, false );
628 return m_dirtyTargets[aTarget];
629 }
630
636 inline void MarkTargetDirty( int aTarget )
637 {
638 wxCHECK( aTarget < TARGETS_NUMBER, /* void */ );
639 m_dirtyTargets[aTarget] = true;
640 }
641
643 inline bool IsCached( int aLayer ) const
644 {
645 auto it = m_layers.find( aLayer );
646
647 if( it == m_layers.end() )
648 return false;
649
650 return it->second.target == TARGET_CACHED;
651 }
652
657 {
658 for( int i = 0; i < TARGETS_NUMBER; ++i )
659 m_dirtyTargets[i] = true;
660 }
661
666 {
667 for( int i = 0; i < TARGETS_NUMBER; ++i )
668 m_dirtyTargets[i] = false;
669 }
670
674 void UpdateItems();
675
681 void UpdateAllItems( int aUpdateFlags );
682
689 void UpdateAllItemsConditionally( int aUpdateFlags,
690 std::function<bool( VIEW_ITEM* )> aCondition );
691
697 void UpdateAllItemsConditionally( std::function<int( VIEW_ITEM* )> aItemFlagsProvider );
698
703 {
704 return m_useDrawPriority;
705 }
706
710 void UseDrawPriority( bool aFlag )
711 {
712 m_useDrawPriority = aFlag;
713 }
714
720 void ReverseDrawOrder( bool aFlag )
721 {
722 m_reverseDrawOrder = aFlag;
723 }
724
725 std::shared_ptr<VIEW_OVERLAY> MakeOverlay();
726
727 void InitPreview();
728
729 void ClearPreview();
730 void AddToPreview( VIEW_ITEM* aItem, bool aTakeOwnership = true );
731
732 void ShowPreview( bool aShow = true );
733
739 std::unique_ptr<VIEW> DataReference() const;
740
742 static constexpr int VIEW_MAX_LAYERS = 512;
743
745 static constexpr int TOP_LAYER_MODIFIER = -VIEW_MAX_LAYERS;
746
747protected:
749 {
750 bool visible;
754 std::shared_ptr<VIEW_RTREE> items;
756 int id;
758 std::set<int> requiredLayers;
760
761 bool operator< ( const VIEW_LAYER& aOther ) const
762 {
763 return id < aOther.id;
764 }
765 };
766
767
768
769 VIEW( const VIEW& ) = delete;
770
772 void redrawRect( const BOX2I& aRect );
773
774 inline void markTargetClean( int aTarget )
775 {
776 wxCHECK( aTarget < TARGETS_NUMBER, /* void */ );
777 m_dirtyTargets[aTarget] = false;
778 }
779
791 void draw( VIEW_ITEM* aItem, int aLayer, bool aImmediate = false );
792
800 void draw( VIEW_ITEM* aItem, bool aImmediate = false );
801
809 void draw( VIEW_GROUP* aGroup, bool aImmediate = false );
810
812 void sortOrderedLayers();
813
816 void clearGroupCache();
817
824 void invalidateItem( VIEW_ITEM* aItem, int aUpdateFlags );
825
827 void updateItemColor( VIEW_ITEM* aItem, int aLayer );
828
830 void updateItemGeometry( VIEW_ITEM* aItem, int aLayer );
831
833 void updateBbox( VIEW_ITEM* aItem );
834
836 void updateLayers( VIEW_ITEM* aItem );
837
840 {
841 return aI->renderingOrder > aJ->renderingOrder;
842 }
843
845 bool areRequiredLayersEnabled( int aLayerId ) const;
846
847 // Function objects that need to access VIEW/VIEW_ITEM private/protected members
848 struct CLEAR_LAYER_CACHE_VISITOR;
849 struct RECACHE_ITEM_VISITOR;
850 struct DRAW_ITEM_VISITOR;
851 struct UPDATE_COLOR_VISITOR;
852 struct UPDATE_DEPTH_VISITOR;
853
854 std::unique_ptr<KIGFX::VIEW_GROUP> m_preview;
855 std::vector<VIEW_ITEM *> m_ownedItems;
856
859
861 std::map<int, VIEW_LAYER> m_layers;
862
864 std::vector<VIEW_LAYER*> m_orderedLayers;
865
867 std::shared_ptr<std::vector<VIEW_ITEM*>> m_allItems;
868
870 std::set<unsigned int> m_topLayers;
871
874
875 double m_scale;
879
882
885
888
890 bool m_dirtyTargets[TARGETS_NUMBER];
891
894
897
900};
901} // namespace KIGFX
902
903#endif
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:48
An abstract base class for deriving all objects that can be added to a VIEW.
Definition: view_item.h:84
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
double m_maxScale
Definition: view.h:878
void ReverseDrawOrder(bool aFlag)
Only takes effect if UseDrawPriority is true.
Definition: view.h:720
const VECTOR2D & GetCenter() const
Return the center point of this VIEW (in world space coordinates).
Definition: view.h:343
VIEW(const VIEW &)=delete
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 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: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
bool m_mirrorX
Definition: view.h:880
void SetPainter(PAINTER *aPainter)
Set the painter object used by the view for drawing #VIEW_ITEMS.
Definition: view.h:207
void SetBoundary(const BOX2D &aBoundary)
Set limits for view area.
Definition: view.h:283
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
void SetLayerDiff(int aLayer, bool aDiff=true)
Set the whether the layer should drawn differentially.
Definition: view.h:435
void SetLayerTarget(int aLayer, RENDER_TARGET aTarget)
Change the rendering target for a particular layer.
Definition: view.h:494
void SetLayerVisible(int aLayer, bool aVisible=true)
Control the visibility of a particular layer.
Definition: view.h:397
PAINTER * m_painter
Interface to #PAINTER that is used to draw items.
Definition: view.h:884
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
void SetLayerHasNegatives(int aLayer, bool aNegatives=true)
Set the status of negatives presense in a particular layer.
Definition: view.h:458
GAL * GetGAL() const
Return the #GAL this view is using to draw graphical primitives.
Definition: view.h:199
void MarkClean()
Force redraw of view on the next rendering.
Definition: view.h:665
double m_minScale
Definition: view.h:877
double m_scale
Definition: view.h:875
bool IsMirroredX() const
Return true if view is flipped across the X axis.
Definition: view.h:247
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
void SetBoundary(const BOX2I &aBoundary)
Set limits for view area.
Definition: view.h:293
int m_nextDrawPriority
Flag to reverse the draw order when using draw priority.
Definition: view.h:896
bool m_useDrawPriority
The next sequential drawing priority.
Definition: view.h:893
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
bool IsMirroredY() const
Return true if view is flipped across the Y axis.
Definition: view.h:255
void UseDrawPriority(bool aFlag)
Definition: view.h:710
std::unique_ptr< KIGFX::VIEW_GROUP > m_preview
Definition: view.h:854
bool IsUsingDrawPriority() const
Definition: view.h:702
GAL * m_gal
Flag to mark targets as dirty so they have to be redrawn on the next refresh event.
Definition: view.h:887
bool IsDirty() const
Return true if any of the VIEW layers needs to be refreshened.
Definition: view.h:608
std::map< int, VIEW_LAYER > m_layers
Sorted list of pointers to members of m_layers.
Definition: view.h:861
void markTargetClean(int aTarget)
Definition: view.h:774
void MarkDirty()
Force redraw of view on the next rendering.
Definition: view.h:656
bool IsLayerVisible(int aLayer) const
Return information about visibility of a particular layer.
Definition: view.h:419
BOX2D m_boundary
Definition: view.h:876
const BOX2D & GetBoundary() const
Definition: view.h:302
PAINTER * GetPainter() const
Return the painter object used by the view for drawing #VIEW_ITEMS.
Definition: view.h:217
void SetScaleLimits(double aMaximum, double aMinimum)
Set minimum and maximum values for scale.
Definition: view.h:313
std::vector< VIEW_LAYER * > m_orderedLayers
Flat list of all items.
Definition: view.h:864
VECTOR2D m_center
Definition: view.h:873
void MarkTargetDirty(int aTarget)
Set or clear target 'dirty' flag.
Definition: view.h:636
bool operator<(const DESIGN_BLOCK_INFO &lhs, const DESIGN_BLOCK_INFO &rhs)
#define GAL_API
Definition: gal.h:28
The Cairo implementation of the graphics abstraction layer.
Definition: color4d.cpp:247
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
Layers that have to be enabled to show the layer.
Definition: view.h:758
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