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#include <base_set.h>
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 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
441 inline 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
806 void redrawRect( const BOX2I& aRect );
807
808 inline void markTargetClean( int aTarget )
809 {
810 wxCHECK( aTarget < TARGETS_NUMBER, /* void */ );
811 m_dirtyTargets[aTarget] = false;
812 }
813
825 void draw( VIEW_ITEM* aItem, int aLayer, bool aImmediate = false );
826
834 void draw( VIEW_ITEM* aItem, bool aImmediate = false );
835
843 void draw( VIEW_GROUP* aGroup, bool aImmediate = false );
844
847 void clearGroupCache();
848
855 void invalidateItem( VIEW_ITEM* aItem, int aUpdateFlags );
856
858 void updateItemColor( VIEW_ITEM* aItem, int aLayer );
859
861 void updateItemGeometry( VIEW_ITEM* aItem, int aLayer );
862
864 void updateBbox( VIEW_ITEM* aItem );
865
867 void updateLayers( VIEW_ITEM* aItem );
868
871 {
872 return aI->renderingOrder > aJ->renderingOrder;
873 }
874
876 bool areRequiredLayersEnabled( int aLayerId ) const;
877
878 void syncLayerVisibilityCache();
879
880 // Function objects that need to access VIEW/VIEW_ITEM private/protected members
881 struct CLEAR_LAYER_CACHE_VISITOR;
882 struct RECACHE_ITEM_VISITOR;
883 struct DRAW_ITEM_VISITOR;
884 struct UPDATE_COLOR_VISITOR;
885 struct UPDATE_DEPTH_VISITOR;
886
887 std::unique_ptr<KIGFX::VIEW_GROUP> m_preview;
888 std::vector<VIEW_ITEM*> m_ownedItems;
889
892
894 std::map<int, VIEW_LAYER> m_layers;
895
897 std::vector<VIEW_LAYER*> m_orderedLayers;
898
900 std::shared_ptr<std::vector<VIEW_ITEM*>> m_allItems;
901
903 std::set<unsigned int> m_topLayers;
904
907
908 double m_scale;
912
917
920
923
926
929
932
935
938};
939} // 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:911
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:355
void CopySettings(const VIEW *aOtherView)
Copy layers and visibility settings from another view.
Definition view.cpp:576
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:934
void SetRequired(int aLayerId, int aRequiredId, bool aRequired=true)
Mark the aRequiredId layer as required for the aLayerId layer.
Definition view.cpp:479
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:870
virtual void Add(VIEW_ITEM *aItem, int aDrawPriority=-1)
Add a VIEW_ITEM to the view.
Definition view.cpp:304
virtual void Remove(VIEW_ITEM *aItem)
Remove a VIEW_ITEM from the view.
Definition view.cpp:408
bool m_mirrorX
Definition view.h:913
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:704
bool m_mirrorY
Definition view.h:914
std::shared_ptr< std::vector< VIEW_ITEM * > > m_allItems
Flat list of all items.
Definition view.h:900
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:582
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:491
std::vector< VIEW_ITEM * > m_ownedItems
Definition view.h:888
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:1828
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:1814
PAINTER * m_painter
PAINTER contains information how do draw items.
Definition view.h:919
std::set< unsigned int > m_topLayers
The set of layers that are displayed on the top.
Definition view.h:903
bool m_enableOrderModifier
Whether to use rendering order modifier or not.
Definition view.h:891
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:211
bool IsHiddenOnOverlay(const VIEW_ITEM *aItem) const
Definition view.cpp:1806
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:925
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:910
double m_scale
Definition view.h:908
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:650
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:931
bool m_useDrawPriority
Flag to respect draw priority when drawing items.
Definition view.h:928
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:347
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
BASE_SET m_layerVisibilityCache
Definition view.h:915
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:937
std::unique_ptr< KIGFX::VIEW_GROUP > m_preview
Definition view.h:887
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:229
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:922
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:894
void Hide(VIEW_ITEM *aItem, bool aHide=true, bool aHideOverlay=false)
Temporarily hide the item in the view (e.g.
Definition view.cpp:1776
void markTargetClean(int aTarget)
Definition view.h:808
void MarkDirty()
Force redraw of view on the next rendering.
Definition view.h:681
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:909
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:897
VECTOR2D m_center
Center point of the VIEW (the point at which we are looking at).
Definition view.h:906
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:1798
BASE_SET m_layerCachedFlagCache
Definition view.h:916
void SetVisible(VIEW_ITEM *aItem, bool aIsVisible=true)
Set the item visibility.
Definition view.cpp:1755
#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: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:687
VECTOR2< double > VECTOR2D
Definition vector2d.h:686