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 <vector>
31#include <set>
32#include <unordered_map>
33#include <memory>
34
35#include <math/box2.h>
36#include <gal/definitions.h>
37
38#include <view/view_overlay.h>
39
40namespace KIGFX
41{
42class PAINTER;
43class GAL;
44class VIEW_ITEM;
45class VIEW_GROUP;
46class VIEW_RTREE;
47
66{
67public:
68 friend class VIEW_ITEM;
69
70 typedef std::pair<VIEW_ITEM*, int> LAYER_ITEM_PAIR;
71
72 VIEW();
73 virtual ~VIEW();
74
75 // We own at least one list of raw pointers. Don't let the compiler fill in copy c'tors that
76 // will only land us in trouble.
77 VIEW( const VIEW& ) = delete;
78 VIEW& operator=( const VIEW& ) = delete;
79
87 static void OnDestroy( VIEW_ITEM* aItem );
88
97 virtual void Add( VIEW_ITEM* aItem, int aDrawPriority = -1 );
98
104 virtual void Remove( VIEW_ITEM* aItem );
105
106
115 int Query( const BOX2I& aRect, std::vector<LAYER_ITEM_PAIR>& aResult ) const;
116
122 void Query( const BOX2I& aRect, const std::function<bool( VIEW_ITEM* )>& aFunc ) const;
123
130 void SetVisible( VIEW_ITEM* aItem, bool aIsVisible = true );
131
139 void Hide( VIEW_ITEM* aItem, bool aHide = true, bool aHideOverlay = false );
140
148 bool IsVisible( const VIEW_ITEM* aItem ) const;
149
150 bool IsHiddenOnOverlay( const VIEW_ITEM* aItem ) const;
151
155 bool HasItem( const VIEW_ITEM* aItem ) const;
156
164 virtual void Update( const VIEW_ITEM* aItem, int aUpdateFlags ) const;
165 virtual void Update( const VIEW_ITEM* aItem ) const;
166
175 void SetRequired( int aLayerId, int aRequiredId, bool aRequired = true );
176
182 void CopySettings( const VIEW* aOtherView );
183
184 /*
185 * Convenience wrappers for adding multiple items
186 * template <class T> void AddItems( const T& aItems );
187 * template <class T> void RemoveItems( const T& aItems );
188 */
189
195 void SetGAL( GAL* aGal );
196
202 inline GAL* GetGAL() const
203 {
204 return m_gal;
205 }
206
210 inline void SetPainter( PAINTER* aPainter )
211 {
212 m_painter = aPainter;
213 }
214
220 inline PAINTER* GetPainter() const
221 {
222 return m_painter;
223 }
224
230 void SetViewport( const BOX2D& aViewport );
231
237 BOX2D GetViewport() const;
238
245 void SetMirror( bool aMirrorX, bool aMirrorY );
246
250 bool IsMirroredX() const
251 {
252 return m_mirrorX;
253 }
254
258 bool IsMirroredY() const
259 {
260 return m_mirrorY;
261 }
262
271 virtual void SetScale( double aScale, VECTOR2D aAnchor = { 0, 0 } );
272
276 inline double GetScale() const
277 {
278 return m_scale;
279 }
280
286 inline void SetBoundary( const BOX2D& aBoundary )
287 {
288 m_boundary = aBoundary;
289 }
290
296 inline void SetBoundary( const BOX2I& aBoundary )
297 {
298 m_boundary.SetOrigin( aBoundary.GetOrigin() );
299 m_boundary.SetEnd( aBoundary.GetEnd() );
300 }
301
305 inline const BOX2D& GetBoundary() const
306 {
307 return m_boundary;
308 }
309
316 void SetScaleLimits( double aMaximum, double aMinimum )
317 {
318 wxASSERT_MSG( aMaximum > aMinimum, wxT( "I guess you passed parameters in wrong order" ) );
319
320 m_minScale = aMinimum;
321 m_maxScale = aMaximum;
322 }
323
330 void SetCenter( const VECTOR2D& aCenter );
331
339 void SetCenter( const VECTOR2D& aCenter, const std::vector<BOX2D>& obscuringScreenRects );
340
346 const VECTOR2D& GetCenter() const
347 {
348 return m_center;
349 }
350
357 VECTOR2D ToWorld( const VECTOR2D& aCoord, bool aAbsolute = true ) const;
358
365 double ToWorld( double aSize ) const;
366
373 VECTOR2D ToScreen( const VECTOR2D& aCoord, bool aAbsolute = true ) const;
374
380 double ToScreen( double aSize ) const;
381
387 const VECTOR2I& GetScreenPixelSize() const;
388
392 void Clear();
393
400 inline void SetLayerVisible( int aLayer, bool aVisible = true )
401 {
402 auto it = m_layers.find( aLayer );
403
404 if( it == m_layers.end() )
405 return;
406
407 VIEW_LAYER& layer = it->second;
408
409 if( layer.visible != aVisible )
410 {
411 // Target has to be redrawn after changing its visibility
412 MarkTargetDirty( layer.target );
413 layer.visible = aVisible;
414 }
415 }
416
422 inline bool IsLayerVisible( int aLayer ) const
423 {
424 auto it = m_layers.find( aLayer );
425
426 if( it == m_layers.end() )
427 return false;
428
429 return it->second.visible;
430 }
431
438 inline void SetLayerDiff( int aLayer, bool aDiff = true )
439 {
440 auto it = m_layers.find( aLayer );
441
442 if( it == m_layers.end() )
443 return;
444
445 VIEW_LAYER& layer = it->second;
446
447 if( layer.diffLayer != aDiff )
448 {
449 // Target has to be redrawn after changing its layers' diff status
450 MarkTargetDirty( layer.target );
451 layer.diffLayer = aDiff;
452 }
453 }
454
461 inline void SetLayerHasNegatives( int aLayer, bool aNegatives = true )
462 {
463 auto it = m_layers.find( aLayer );
464
465 if( it == m_layers.end() )
466 return;
467
468 VIEW_LAYER& layer = it->second;
469
470 if( layer.hasNegatives != aNegatives )
471 {
472 // Target has to be redrawn after changing a layers' negatives
473 MarkTargetDirty( layer.target );
474 layer.hasNegatives = aNegatives;
475 }
476 }
477
481 inline void SetLayerDisplayOnly( int aLayer, bool aDisplayOnly = true )
482 {
483 auto it = m_layers.find( aLayer );
484
485 if( it == m_layers.end() )
486 return;
487
488 it->second.displayOnly = aDisplayOnly;
489 }
490
497 inline void SetLayerTarget( int aLayer, RENDER_TARGET aTarget )
498 {
499 auto it = m_layers.find( aLayer );
500
501 if( it == m_layers.end() )
502 return;
503
504 it->second.target = aTarget;
505 }
506
513 void SetLayerOrder( int aLayer, int aRenderingOrder );
514
521 int GetLayerOrder( int aLayer ) const;
522
530 void SortLayers( std::vector<int>& aLayers ) const;
531
539 void ReorderLayerData( std::unordered_map<int, int> aReorderMap );
540
547 void UpdateLayerColor( int aLayer );
548
554 void UpdateAllLayersColor();
555
563 virtual void SetTopLayer( int aLayer, bool aEnabled = true );
564
574 virtual void EnableTopLayer( bool aEnable );
575
576 virtual int GetTopLayer() const;
577
582 void ClearTopLayers();
583
589 void UpdateAllLayersOrder();
590
594 void ClearTargets();
595
599 virtual void Redraw();
600
604 void RecacheAllItems();
605
611 bool IsDirty() const
612 {
613 for( int i = 0; i < TARGETS_NUMBER; ++i )
614 {
615 if( IsTargetDirty( i ) )
616 return true;
617 }
618
619 return false;
620 }
621
628 bool IsTargetDirty( int aTarget ) const
629 {
630 wxCHECK( aTarget < TARGETS_NUMBER, false );
631 return m_dirtyTargets[aTarget];
632 }
633
639 inline void MarkTargetDirty( int aTarget )
640 {
641 wxCHECK( aTarget < TARGETS_NUMBER, /* void */ );
642 m_dirtyTargets[aTarget] = true;
643 }
644
646 inline bool IsCached( int aLayer ) const
647 {
648 auto it = m_layers.find( aLayer );
649
650 if( it == m_layers.end() )
651 return false;
652
653 return it->second.target == TARGET_CACHED;
654 }
655
660 {
661 for( int i = 0; i < TARGETS_NUMBER; ++i )
662 m_dirtyTargets[i] = true;
663 }
664
669 {
670 for( int i = 0; i < TARGETS_NUMBER; ++i )
671 m_dirtyTargets[i] = false;
672 }
673
677 void UpdateItems();
678
684 void UpdateAllItems( int aUpdateFlags );
685
692 void UpdateAllItemsConditionally( int aUpdateFlags,
693 std::function<bool( VIEW_ITEM* )> aCondition );
694
700 void UpdateAllItemsConditionally( std::function<int( VIEW_ITEM* )> aItemFlagsProvider );
701
706 {
707 return m_useDrawPriority;
708 }
709
713 void UseDrawPriority( bool aFlag )
714 {
715 m_useDrawPriority = aFlag;
716 }
717
723 void ReverseDrawOrder( bool aFlag )
724 {
725 m_reverseDrawOrder = aFlag;
726 }
727
728 std::shared_ptr<VIEW_OVERLAY> MakeOverlay();
729
730 void InitPreview();
731
732 void ClearPreview();
733 void AddToPreview( VIEW_ITEM* aItem, bool aTakeOwnership = true );
734
735 void ShowPreview( bool aShow = true );
736
742 std::unique_ptr<VIEW> DataReference() const;
743
745 static constexpr int VIEW_MAX_LAYERS = MAX_LAYERS_FOR_VIEW;
746
748 static constexpr int TOP_LAYER_MODIFIER = -MAX_LAYERS_FOR_VIEW;
749
750protected:
752 {
753 bool visible;
755
758
761 std::shared_ptr<VIEW_RTREE> items;
763 int id;
765
767 std::set<int> requiredLayers;
768
769 bool operator< ( const VIEW_LAYER& aOther ) const
770 {
771 return id < aOther.id;
772 }
773 };
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
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
Hold a (potentially large) number of VIEW_ITEMs and renders them on a graphics device provided by the...
Definition: view.h:66
double GetScale() const
Definition: view.h:276
double m_maxScale
Definition: view.h:882
void ReverseDrawOrder(bool aFlag)
Only takes effect if UseDrawPriority is true.
Definition: view.h:723
const VECTOR2D & GetCenter() const
Return the center point of this VIEW (in world space coordinates).
Definition: view.h:346
VIEW(const VIEW &)=delete
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:481
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:210
void SetBoundary(const BOX2D &aBoundary)
Set limits for view area.
Definition: view.h:286
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:438
std::vector< VIEW_ITEM * > m_ownedItems
Definition: view.h:859
void SetLayerTarget(int aLayer, RENDER_TARGET aTarget)
Change the rendering target for a particular layer.
Definition: view.h:497
void SetLayerVisible(int aLayer, bool aVisible=true)
Control the visibility of a particular layer.
Definition: view.h:400
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:461
GAL * GetGAL() const
Return the #GAL this view is using to draw graphical primitives.
Definition: view.h:202
void MarkClean()
Force redraw of view on the next rendering.
Definition: view.h:668
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:250
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:628
void SetBoundary(const BOX2I &aBoundary)
Set limits for view area.
Definition: view.h:296
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:646
std::pair< VIEW_ITEM *, int > LAYER_ITEM_PAIR
Definition: view.h:70
bool IsMirroredY() const
Return true if view is flipped across the Y axis.
Definition: view.h:258
void UseDrawPriority(bool aFlag)
Definition: view.h:713
std::unique_ptr< KIGFX::VIEW_GROUP > m_preview
Definition: view.h:858
bool IsUsingDrawPriority() const
Definition: view.h:705
VIEW & operator=(const VIEW &)=delete
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:611
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:659
bool IsLayerVisible(int aLayer) const
Return information about visibility of a particular layer.
Definition: view.h:422
BOX2D m_boundary
Definition: view.h:880
const BOX2D & GetBoundary() const
Definition: view.h:305
PAINTER * GetPainter() const
Return the painter object used by the view for drawing #VIEW_ITEMS.
Definition: view.h:220
void SetScaleLimits(double aMaximum, double aMinimum)
Set minimum and maximum values for scale.
Definition: view.h:316
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:639
#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: 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:767
bool diffLayer
Layer should be drawn differentially over lower layers.
Definition: view.h:757
int renderingOrder
Rendering order of this layer.
Definition: view.h:762
bool hasNegatives
Layer should be drawn separately to not delete lower layers.
Definition: view.h:760
bool visible
Is the layer to be rendered?
Definition: view.h:753
bool displayOnly
Is the layer display only?
Definition: view.h:754
std::shared_ptr< VIEW_RTREE > items
R-tree indexing all items on this layer.
Definition: view.h:761
RENDER_TARGET target
Where the layer should be rendered.
Definition: view.h:764
int id
Layer ID.
Definition: view.h:763