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#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
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
83 static void OnDestroy( VIEW_ITEM* aItem );
84
93 virtual void Add( VIEW_ITEM* aItem, int aDrawPriority = -1 );
94
100 virtual void Remove( VIEW_ITEM* aItem );
101
102
111 int Query( const BOX2I& aRect, std::vector<LAYER_ITEM_PAIR>& aResult ) const;
112
118 void Query( const BOX2I& aRect, const std::function<bool( VIEW_ITEM* )>& aFunc ) const;
119
126 void SetVisible( VIEW_ITEM* aItem, bool aIsVisible = true );
127
135 void Hide( VIEW_ITEM* aItem, bool aHide = true, bool aHideOverlay = false );
136
144 bool IsVisible( const VIEW_ITEM* aItem ) const;
145
146 bool IsHiddenOnOverlay( const VIEW_ITEM* aItem ) const;
147
151 bool HasItem( const VIEW_ITEM* aItem ) const;
152
160 virtual void Update( const VIEW_ITEM* aItem, int aUpdateFlags ) const;
161 virtual void Update( const VIEW_ITEM* aItem ) const;
162
171 void SetRequired( int aLayerId, int aRequiredId, bool aRequired = true );
172
178 void CopySettings( const VIEW* aOtherView );
179
180 /*
181 * Convenience wrappers for adding multiple items
182 * template <class T> void AddItems( const T& aItems );
183 * template <class T> void RemoveItems( const T& aItems );
184 */
185
191 void SetGAL( GAL* aGal );
192
198 inline GAL* GetGAL() const
199 {
200 return m_gal;
201 }
202
206 inline void SetPainter( PAINTER* aPainter )
207 {
208 m_painter = aPainter;
209 }
210
216 inline PAINTER* GetPainter() const
217 {
218 return m_painter;
219 }
220
226 void SetViewport( const BOX2D& aViewport );
227
233 BOX2D GetViewport() const;
234
241 void SetMirror( bool aMirrorX, bool aMirrorY );
242
246 bool IsMirroredX() const
247 {
248 return m_mirrorX;
249 }
250
254 bool IsMirroredY() const
255 {
256 return m_mirrorY;
257 }
258
267 virtual void SetScale( double aScale, VECTOR2D aAnchor = { 0, 0 } );
268
272 inline double GetScale() const
273 {
274 return m_scale;
275 }
276
282 inline void SetBoundary( const BOX2D& aBoundary )
283 {
284 m_boundary = aBoundary;
285 }
286
292 inline void SetBoundary( const BOX2I& aBoundary )
293 {
294 m_boundary.SetOrigin( aBoundary.GetOrigin() );
295 m_boundary.SetEnd( aBoundary.GetEnd() );
296 }
297
301 inline const BOX2D& GetBoundary() const
302 {
303 return m_boundary;
304 }
305
312 void SetScaleLimits( double aMaximum, double aMinimum )
313 {
314 wxASSERT_MSG( aMaximum > aMinimum, wxT( "I guess you passed parameters in wrong order" ) );
315
316 m_minScale = aMinimum;
317 m_maxScale = aMaximum;
318 }
319
326 void SetCenter( const VECTOR2D& aCenter );
327
335 void SetCenter( const VECTOR2D& aCenter, const std::vector<BOX2D>& obscuringScreenRects );
336
342 const VECTOR2D& GetCenter() const
343 {
344 return m_center;
345 }
346
353 VECTOR2D ToWorld( const VECTOR2D& aCoord, bool aAbsolute = true ) const;
354
361 double ToWorld( double aSize ) const;
362
369 VECTOR2D ToScreen( const VECTOR2D& aCoord, bool aAbsolute = true ) const;
370
376 double ToScreen( double aSize ) const;
377
383 const VECTOR2I& GetScreenPixelSize() const;
384
388 void Clear();
389
396 inline void SetLayerVisible( int aLayer, bool aVisible = true )
397 {
398 auto it = m_layers.find( aLayer );
399
400 if( it == m_layers.end() )
401 return;
402
403 VIEW_LAYER& layer = it->second;
404
405 if( layer.visible != aVisible )
406 {
407 // Target has to be redrawn after changing its visibility
408 MarkTargetDirty( layer.target );
409 layer.visible = aVisible;
410 }
411 }
412
418 inline bool IsLayerVisible( int aLayer ) const
419 {
420 auto it = m_layers.find( aLayer );
421
422 if( it == m_layers.end() )
423 return false;
424
425 return it->second.visible;
426 }
427
434 inline void SetLayerDiff( int aLayer, bool aDiff = true )
435 {
436 auto it = m_layers.find( aLayer );
437
438 if( it == m_layers.end() )
439 return;
440
441 VIEW_LAYER& layer = it->second;
442
443 if( layer.diffLayer != aDiff )
444 {
445 // Target has to be redrawn after changing its layers' diff status
446 MarkTargetDirty( layer.target );
447 layer.diffLayer = aDiff;
448 }
449 }
450
457 inline void SetLayerHasNegatives( int aLayer, bool aNegatives = true )
458 {
459 auto it = m_layers.find( aLayer );
460
461 if( it == m_layers.end() )
462 return;
463
464 VIEW_LAYER& layer = it->second;
465
466 if( layer.hasNegatives != aNegatives )
467 {
468 // Target has to be redrawn after changing a layers' negatives
469 MarkTargetDirty( layer.target );
470 layer.hasNegatives = aNegatives;
471 }
472 }
473
477 inline void SetLayerDisplayOnly( int aLayer, bool aDisplayOnly = true )
478 {
479 auto it = m_layers.find( aLayer );
480
481 if( it == m_layers.end() )
482 return;
483
484 it->second.displayOnly = aDisplayOnly;
485 }
486
493 inline void SetLayerTarget( int aLayer, RENDER_TARGET aTarget )
494 {
495 auto it = m_layers.find( aLayer );
496
497 if( it == m_layers.end() )
498 return;
499
500 it->second.target = aTarget;
501 }
502
509 void SetLayerOrder( int aLayer, int aRenderingOrder );
510
517 int GetLayerOrder( int aLayer ) const;
518
526 void SortLayers( std::vector<int>& aLayers ) const;
527
535 void ReorderLayerData( std::unordered_map<int, int> aReorderMap );
536
543 void UpdateLayerColor( int aLayer );
544
550 void UpdateAllLayersColor();
551
559 virtual void SetTopLayer( int aLayer, bool aEnabled = true );
560
570 virtual void EnableTopLayer( bool aEnable );
571
572 virtual int GetTopLayer() const;
573
578 void ClearTopLayers();
579
585 void UpdateAllLayersOrder();
586
590 void ClearTargets();
591
595 virtual void Redraw();
596
600 void RecacheAllItems();
601
607 bool IsDirty() const
608 {
609 for( int i = 0; i < TARGETS_NUMBER; ++i )
610 {
611 if( IsTargetDirty( i ) )
612 return true;
613 }
614
615 return false;
616 }
617
624 bool IsTargetDirty( int aTarget ) const
625 {
626 wxCHECK( aTarget < TARGETS_NUMBER, false );
627 return m_dirtyTargets[aTarget];
628 }
629
635 inline void MarkTargetDirty( int aTarget )
636 {
637 wxCHECK( aTarget < TARGETS_NUMBER, /* void */ );
638 m_dirtyTargets[aTarget] = true;
639 }
640
642 inline bool IsCached( int aLayer ) const
643 {
644 auto it = m_layers.find( aLayer );
645
646 if( it == m_layers.end() )
647 return false;
648
649 return it->second.target == TARGET_CACHED;
650 }
651
656 {
657 for( int i = 0; i < TARGETS_NUMBER; ++i )
658 m_dirtyTargets[i] = true;
659 }
660
665 {
666 for( int i = 0; i < TARGETS_NUMBER; ++i )
667 m_dirtyTargets[i] = false;
668 }
669
673 void UpdateItems();
674
680 void UpdateAllItems( int aUpdateFlags );
681
688 void UpdateAllItemsConditionally( int aUpdateFlags,
689 std::function<bool( VIEW_ITEM* )> aCondition );
690
696 void UpdateAllItemsConditionally( std::function<int( VIEW_ITEM* )> aItemFlagsProvider );
697
702 {
703 return m_useDrawPriority;
704 }
705
709 void UseDrawPriority( bool aFlag )
710 {
711 m_useDrawPriority = aFlag;
712 }
713
719 void ReverseDrawOrder( bool aFlag )
720 {
721 m_reverseDrawOrder = aFlag;
722 }
723
724 std::shared_ptr<VIEW_OVERLAY> MakeOverlay();
725
726 void InitPreview();
727
728 void ClearPreview();
729 void AddToPreview( VIEW_ITEM* aItem, bool aTakeOwnership = true );
730
731 void ShowPreview( bool aShow = true );
732
738 std::unique_ptr<VIEW> DataReference() const;
739
741 static constexpr int VIEW_MAX_LAYERS = MAX_LAYERS_FOR_VIEW;
742
744 static constexpr int TOP_LAYER_MODIFIER = -MAX_LAYERS_FOR_VIEW;
745
746protected:
748 {
749 bool visible;
751
754
757 std::shared_ptr<VIEW_RTREE> items;
759 int id;
761
763 std::set<int> requiredLayers;
764
765 bool operator< ( const VIEW_LAYER& aOther ) const
766 {
767 return id < aOther.id;
768 }
769 };
770
771
772
773 VIEW( const VIEW& ) = delete;
774
776 void redrawRect( const BOX2I& aRect );
777
778 inline void markTargetClean( int aTarget )
779 {
780 wxCHECK( aTarget < TARGETS_NUMBER, /* void */ );
781 m_dirtyTargets[aTarget] = false;
782 }
783
795 void draw( VIEW_ITEM* aItem, int aLayer, bool aImmediate = false );
796
804 void draw( VIEW_ITEM* aItem, bool aImmediate = false );
805
813 void draw( VIEW_GROUP* aGroup, bool aImmediate = false );
814
816 void sortOrderedLayers();
817
820 void clearGroupCache();
821
828 void invalidateItem( VIEW_ITEM* aItem, int aUpdateFlags );
829
831 void updateItemColor( VIEW_ITEM* aItem, int aLayer );
832
834 void updateItemGeometry( VIEW_ITEM* aItem, int aLayer );
835
837 void updateBbox( VIEW_ITEM* aItem );
838
840 void updateLayers( VIEW_ITEM* aItem );
841
844 {
845 return aI->renderingOrder > aJ->renderingOrder;
846 }
847
849 bool areRequiredLayersEnabled( int aLayerId ) const;
850
851 // Function objects that need to access VIEW/VIEW_ITEM private/protected members
852 struct CLEAR_LAYER_CACHE_VISITOR;
853 struct RECACHE_ITEM_VISITOR;
854 struct DRAW_ITEM_VISITOR;
855 struct UPDATE_COLOR_VISITOR;
856 struct UPDATE_DEPTH_VISITOR;
857
858 std::unique_ptr<KIGFX::VIEW_GROUP> m_preview;
859 std::vector<VIEW_ITEM *> m_ownedItems;
860
863
865 std::map<int, VIEW_LAYER> m_layers;
866
868 std::vector<VIEW_LAYER*> m_orderedLayers;
869
871 std::shared_ptr<std::vector<VIEW_ITEM*>> m_allItems;
872
874 std::set<unsigned int> m_topLayers;
875
878
879 double m_scale;
883
886
889
892
894 bool m_dirtyTargets[TARGETS_NUMBER];
895
898
901
904};
905} // namespace KIGFX
906
907#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:86
Hold a (potentially large) number of VIEW_ITEMs and renders them on a graphics device provided by the...
Definition: view.h:67
double GetScale() const
Definition: view.h:272
double m_maxScale
Definition: view.h:882
void ReverseDrawOrder(bool aFlag)
Only takes effect if UseDrawPriority is true.
Definition: view.h:719
const VECTOR2D & GetCenter() const
Return the center point of this VIEW (in world space coordinates).
Definition: view.h:342
VIEW(const VIEW &)=delete
std::vector< VIEW_ITEM * > m_ownedItems
Definition: view.h:859
bool m_reverseDrawOrder
Flag to reverse the draw order when using draw priority.
Definition: view.h:903
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:477
static bool compareRenderingOrder(VIEW_LAYER *aI, VIEW_LAYER *aJ)
Determine rendering order of layers. Used in display order sorting function.
Definition: view.h:843
bool m_mirrorX
Definition: view.h:884
void SetPainter(PAINTER *aPainter)
Set the painter object used by the view for drawing #VIEW_ITEMS.
Definition: view.h:206
void SetBoundary(const BOX2D &aBoundary)
Set limits for view area.
Definition: view.h:282
bool m_mirrorY
Definition: view.h:885
std::shared_ptr< std::vector< VIEW_ITEM * > > m_allItems
Flat list of all items.
Definition: view.h:871
void SetLayerDiff(int aLayer, bool aDiff=true)
Set the whether the layer should drawn differentially.
Definition: view.h:434
void SetLayerTarget(int aLayer, RENDER_TARGET aTarget)
Change the rendering target for a particular layer.
Definition: view.h:493
void SetLayerVisible(int aLayer, bool aVisible=true)
Control the visibility of a particular layer.
Definition: view.h:396
PAINTER * m_painter
PAINTER contains information how do draw items.
Definition: view.h:888
std::set< unsigned int > m_topLayers
The set of layers that are displayed on the top.
Definition: view.h:874
bool m_enableOrderModifier
Whether to use rendering order modifier or not.
Definition: view.h:862
void SetLayerHasNegatives(int aLayer, bool aNegatives=true)
Set the status of negatives presense in a particular layer.
Definition: view.h:457
GAL * GetGAL() const
Return the #GAL this view is using to draw graphical primitives.
Definition: view.h:198
void MarkClean()
Force redraw of view on the next rendering.
Definition: view.h:664
double m_minScale
Definition: view.h:881
double m_scale
Definition: view.h:879
bool IsMirroredX() const
Return true if view is flipped across the X axis.
Definition: view.h:246
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:624
void SetBoundary(const BOX2I &aBoundary)
Set limits for view area.
Definition: view.h:292
int m_nextDrawPriority
The next sequential drawing priority.
Definition: view.h:900
bool m_useDrawPriority
Flag to respect draw priority when drawing items.
Definition: view.h:897
bool IsCached(int aLayer) const
Return true if the layer is cached.
Definition: view.h:642
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:254
void UseDrawPriority(bool aFlag)
Definition: view.h:709
std::unique_ptr< KIGFX::VIEW_GROUP > m_preview
Definition: view.h:858
bool IsUsingDrawPriority() const
Definition: view.h:701
GAL * m_gal
Interface to #PAINTER that is used to draw items.
Definition: view.h:891
bool IsDirty() const
Return true if any of the VIEW layers needs to be refreshened.
Definition: view.h:607
std::map< int, VIEW_LAYER > m_layers
The set of possible displayed layers and its properties.
Definition: view.h:865
void markTargetClean(int aTarget)
Definition: view.h:778
void MarkDirty()
Force redraw of view on the next rendering.
Definition: view.h:655
bool IsLayerVisible(int aLayer) const
Return information about visibility of a particular layer.
Definition: view.h:418
BOX2D m_boundary
Definition: view.h:880
const BOX2D & GetBoundary() const
Definition: view.h:301
PAINTER * GetPainter() const
Return the painter object used by the view for drawing #VIEW_ITEMS.
Definition: view.h:216
void SetScaleLimits(double aMaximum, double aMinimum)
Set minimum and maximum values for scale.
Definition: view.h:312
std::vector< VIEW_LAYER * > m_orderedLayers
Sorted list of pointers to members of m_layers.
Definition: view.h:868
VECTOR2D m_center
Center point of the VIEW (the point at which we are looking at).
Definition: view.h:877
void MarkTargetDirty(int aTarget)
Set or clear target 'dirty' flag.
Definition: view.h:635
#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: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
Definition: view.h:763
bool diffLayer
Layer should be drawn differentially over lower layers.
Definition: view.h:753
int renderingOrder
Rendering order of this layer.
Definition: view.h:758
bool hasNegatives
Layer should be drawn separately to not delete lower layers.
Definition: view.h:756
bool visible
Is the layer to be rendered?
Definition: view.h:749
bool displayOnly
Is the layer display only?
Definition: view.h:750
std::shared_ptr< VIEW_RTREE > items
R-tree indexing all items on this layer.
Definition: view.h:757
RENDER_TARGET target
Where the layer should be rendered.
Definition: view.h:760
int id
Layer ID.
Definition: view.h:759