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
105 virtual void Remove( VIEW_ITEM* aItem );
106
107
116 int Query( const BOX2I& aRect, std::vector<LAYER_ITEM_PAIR>& aResult ) const;
117
123 void Query( const BOX2I& aRect, const std::function<bool( VIEW_ITEM* )>& aFunc ) const;
124
131 void SetVisible( VIEW_ITEM* aItem, bool aIsVisible = true );
132
140 void Hide( VIEW_ITEM* aItem, bool aHide = true, bool aHideOverlay = false );
141
149 bool IsVisible( const VIEW_ITEM* aItem ) const;
150
151 bool IsHiddenOnOverlay( const VIEW_ITEM* aItem ) const;
152
156 bool HasItem( const VIEW_ITEM* aItem ) const;
157
165 virtual void Update( const VIEW_ITEM* aItem, int aUpdateFlags ) const;
166 virtual void Update( const VIEW_ITEM* aItem ) const;
167
176 void SetRequired( int aLayerId, int aRequiredId, bool aRequired = true );
177
183 void CopySettings( const VIEW* aOtherView );
184
185 /*
186 * Convenience wrappers for adding multiple items
187 * template <class T> void AddItems( const T& aItems );
188 * template <class T> void RemoveItems( const T& aItems );
189 */
190
196 void SetGAL( GAL* aGal );
197
203 inline GAL* GetGAL() const
204 {
205 return m_gal;
206 }
207
211 inline void SetPainter( PAINTER* aPainter )
212 {
213 m_painter = aPainter;
214 }
215
221 inline PAINTER* GetPainter() const
222 {
223 return m_painter;
224 }
225
231 void SetViewport( const BOX2D& aViewport );
232
238 BOX2D GetViewport() const;
239
246 void SetMirror( bool aMirrorX, bool aMirrorY );
247
251 bool IsMirroredX() const
252 {
253 return m_mirrorX;
254 }
255
259 bool IsMirroredY() const
260 {
261 return m_mirrorY;
262 }
263
272 virtual void SetScale( double aScale, VECTOR2D aAnchor = { 0, 0 } );
273
277 inline double GetScale() const
278 {
279 return m_scale;
280 }
281
287 inline void SetBoundary( const BOX2D& aBoundary )
288 {
289 m_boundary = aBoundary;
290 }
291
297 inline void SetBoundary( const BOX2I& aBoundary )
298 {
299 m_boundary.SetOrigin( aBoundary.GetOrigin() );
300 m_boundary.SetEnd( aBoundary.GetEnd() );
301 }
302
306 inline const BOX2D& GetBoundary() const
307 {
308 return m_boundary;
309 }
310
317 void SetScaleLimits( double aMaximum, double aMinimum )
318 {
319 wxASSERT_MSG( aMaximum > aMinimum, wxT( "I guess you passed parameters in wrong order" ) );
320
321 m_minScale = aMinimum;
322 m_maxScale = aMaximum;
323 }
324
331 void SetCenter( const VECTOR2D& aCenter );
332
340 void SetCenter( const VECTOR2D& aCenter, const std::vector<BOX2D>& obscuringScreenRects );
341
347 const VECTOR2D& GetCenter() const
348 {
349 return m_center;
350 }
351
358 VECTOR2D ToWorld( const VECTOR2D& aCoord, bool aAbsolute = true ) const;
359
366 double ToWorld( double aSize ) const;
367
374 VECTOR2D ToScreen( const VECTOR2D& aCoord, bool aAbsolute = true ) const;
375
381 double ToScreen( double aSize ) const;
382
388 const VECTOR2I& GetScreenPixelSize() const;
389
393 void Clear();
394
401 inline void SetLayerVisible( int aLayer, bool aVisible = true )
402 {
403 auto it = m_layers.find( aLayer );
404
405 if( it == m_layers.end() )
406 return;
407
408 VIEW_LAYER& layer = it->second;
409
410 if( layer.visible != aVisible )
411 {
412 // Target has to be redrawn after changing its visibility
413 MarkTargetDirty( layer.target );
414 layer.visible = aVisible;
415 }
416 }
417
423 inline bool IsLayerVisible( int aLayer ) const
424 {
425 auto it = m_layers.find( aLayer );
426
427 if( it == m_layers.end() )
428 return false;
429
430 return it->second.visible;
431 }
432
439 inline void SetLayerDiff( int aLayer, bool aDiff = true )
440 {
441 auto it = m_layers.find( aLayer );
442
443 if( it == m_layers.end() )
444 return;
445
446 VIEW_LAYER& layer = it->second;
447
448 if( layer.diffLayer != aDiff )
449 {
450 // Target has to be redrawn after changing its layers' diff status
451 MarkTargetDirty( layer.target );
452 layer.diffLayer = aDiff;
453 }
454 }
455
462 inline void SetLayerHasNegatives( int aLayer, bool aNegatives = true )
463 {
464 auto it = m_layers.find( aLayer );
465
466 if( it == m_layers.end() )
467 return;
468
469 VIEW_LAYER& layer = it->second;
470
471 if( layer.hasNegatives != aNegatives )
472 {
473 // Target has to be redrawn after changing a layers' negatives
474 MarkTargetDirty( layer.target );
475 layer.hasNegatives = aNegatives;
476 }
477 }
478
482 inline void SetLayerDisplayOnly( int aLayer, bool aDisplayOnly = true )
483 {
484 auto it = m_layers.find( aLayer );
485
486 if( it == m_layers.end() )
487 return;
488
489 it->second.displayOnly = aDisplayOnly;
490 }
491
498 inline void SetLayerTarget( int aLayer, RENDER_TARGET aTarget )
499 {
500 auto it = m_layers.find( aLayer );
501
502 if( it == m_layers.end() )
503 return;
504
505 it->second.target = aTarget;
506 }
507
516 void SetLayerOrder( int aLayer, int aRenderingOrder, bool aAutoSort = true );
517
524 int GetLayerOrder( int aLayer ) const;
525
530 void SortOrderedLayers();
531
539 void SortLayers( std::vector<int>& aLayers ) const;
540
548 void ReorderLayerData( std::unordered_map<int, int> aReorderMap );
549
556 void UpdateLayerColor( int aLayer );
557
563 void UpdateAllLayersColor();
564
572 virtual void SetTopLayer( int aLayer, bool aEnabled = true );
573
583 virtual void EnableTopLayer( bool aEnable );
584
585 virtual int GetTopLayer() const;
586
591 void ClearTopLayers();
592
598 void UpdateAllLayersOrder();
599
603 void ClearTargets();
604
608 virtual void Redraw();
609
613 void RecacheAllItems();
614
620 bool IsDirty() const
621 {
622 for( int i = 0; i < TARGETS_NUMBER; ++i )
623 {
624 if( IsTargetDirty( i ) )
625 return true;
626 }
627
628 return false;
629 }
630
637 bool IsTargetDirty( int aTarget ) const
638 {
639 wxCHECK( aTarget < TARGETS_NUMBER, false );
640 return m_dirtyTargets[aTarget];
641 }
642
648 inline void MarkTargetDirty( int aTarget )
649 {
650 wxCHECK( aTarget < TARGETS_NUMBER, /* void */ );
651 m_dirtyTargets[aTarget] = true;
652 }
653
655 inline bool IsCached( int aLayer ) const
656 {
657 auto it = m_layers.find( aLayer );
658
659 if( it == m_layers.end() )
660 return false;
661
662 return it->second.target == TARGET_CACHED;
663 }
664
669 {
670 for( int i = 0; i < TARGETS_NUMBER; ++i )
671 m_dirtyTargets[i] = true;
672 }
673
678 {
679 for( int i = 0; i < TARGETS_NUMBER; ++i )
680 m_dirtyTargets[i] = false;
681 }
682
686 void UpdateItems();
687
692 {
694 }
695
701 void UpdateAllItems( int aUpdateFlags );
702
709 void UpdateAllItemsConditionally( int aUpdateFlags,
710 std::function<bool( VIEW_ITEM* )> aCondition );
711
717 void UpdateAllItemsConditionally( std::function<int( VIEW_ITEM* )> aItemFlagsProvider );
718
723 {
724 return m_useDrawPriority;
725 }
726
730 void UseDrawPriority( bool aFlag )
731 {
732 m_useDrawPriority = aFlag;
733 }
734
740 void ReverseDrawOrder( bool aFlag )
741 {
742 m_reverseDrawOrder = aFlag;
743 }
744
745 std::shared_ptr<VIEW_OVERLAY> MakeOverlay();
746
747 void InitPreview();
748
749 void ClearPreview();
750 void AddToPreview( VIEW_ITEM* aItem, bool aTakeOwnership = true );
751
752 void ShowPreview( bool aShow = true );
753
759 std::unique_ptr<VIEW> DataReference() const;
760
762 static constexpr int VIEW_MAX_LAYERS = MAX_LAYERS_FOR_VIEW;
763
766
767protected:
769 {
770 bool visible;
772
775
778 std::shared_ptr<VIEW_RTREE> items;
780 int id;
782
784 std::set<int> requiredLayers;
785
786 bool operator< ( const VIEW_LAYER& aOther ) const
787 {
788 return id < aOther.id;
789 }
790 };
791
793 void redrawRect( const BOX2I& aRect );
794
795 inline void markTargetClean( int aTarget )
796 {
797 wxCHECK( aTarget < TARGETS_NUMBER, /* void */ );
798 m_dirtyTargets[aTarget] = false;
799 }
800
812 void draw( VIEW_ITEM* aItem, int aLayer, bool aImmediate = false );
813
821 void draw( VIEW_ITEM* aItem, bool aImmediate = false );
822
830 void draw( VIEW_GROUP* aGroup, bool aImmediate = false );
831
834 void clearGroupCache();
835
842 void invalidateItem( VIEW_ITEM* aItem, int aUpdateFlags );
843
845 void updateItemColor( VIEW_ITEM* aItem, int aLayer );
846
848 void updateItemGeometry( VIEW_ITEM* aItem, int aLayer );
849
851 void updateBbox( VIEW_ITEM* aItem );
852
854 void updateLayers( VIEW_ITEM* aItem );
855
858 {
859 return aI->renderingOrder > aJ->renderingOrder;
860 }
861
863 bool areRequiredLayersEnabled( int aLayerId ) const;
864
865 // Function objects that need to access VIEW/VIEW_ITEM private/protected members
866 struct CLEAR_LAYER_CACHE_VISITOR;
867 struct RECACHE_ITEM_VISITOR;
868 struct DRAW_ITEM_VISITOR;
869 struct UPDATE_COLOR_VISITOR;
870 struct UPDATE_DEPTH_VISITOR;
871
872 std::unique_ptr<KIGFX::VIEW_GROUP> m_preview;
873 std::vector<VIEW_ITEM*> m_ownedItems;
874
877
879 std::map<int, VIEW_LAYER> m_layers;
880
882 std::vector<VIEW_LAYER*> m_orderedLayers;
883
885 std::shared_ptr<std::vector<VIEW_ITEM*>> m_allItems;
886
888 std::set<unsigned int> m_topLayers;
889
892
893 double m_scale;
897
900
903
906
909
912
915
918
921};
922} // 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 an non-owning R-tree for fast spatial indexing of VIEW items.
Definition view_rtree.h:42
double GetScale() const
Definition view.h:277
double m_maxScale
Definition view.h:896
void ReverseDrawOrder(bool aFlag)
Only takes effect if UseDrawPriority is true.
Definition view.h:740
const VECTOR2D & GetCenter() const
Return the center point of this VIEW (in world space coordinates).
Definition view.h:347
void CopySettings(const VIEW *aOtherView)
Copy layers and visibility settings from another view.
Definition view.cpp:511
static constexpr int TOP_LAYER_MODIFIER
Rendering order modifier for layers that are marked as top layers.
Definition view.h:765
VIEW(const VIEW &)=delete
bool m_reverseDrawOrder
Flag to reverse the draw order when using draw priority.
Definition view.h:917
void SetRequired(int aLayerId, int aRequiredId, bool aRequired=true)
Mark the aRequiredId layer as required for the aLayerId layer.
Definition view.cpp:414
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:482
static bool compareRenderingOrder(VIEW_LAYER *aI, VIEW_LAYER *aJ)
Determine rendering order of layers. Used in display order sorting function.
Definition view.h:857
virtual void Add(VIEW_ITEM *aItem, int aDrawPriority=-1)
Add a VIEW_ITEM to the view.
Definition view.cpp:300
virtual void Remove(VIEW_ITEM *aItem)
Remove a VIEW_ITEM from the view.
Definition view.cpp:343
bool m_mirrorX
Definition view.h:898
void SetPainter(PAINTER *aPainter)
Set the painter object used by the view for drawing #VIEW_ITEMS.
Definition view.h:211
void SetBoundary(const BOX2D &aBoundary)
Set limits for view area.
Definition view.h:287
bool HasPendingItemUpdates() const
Definition view.h:691
bool m_mirrorY
Definition view.h:899
std::shared_ptr< std::vector< VIEW_ITEM * > > m_allItems
Flat list of all items.
Definition view.h:885
void SetLayerDiff(int aLayer, bool aDiff=true)
Set the whether the layer should drawn differentially.
Definition view.h:439
void SetGAL(GAL *aGal)
Assign a rendering device for the VIEW.
Definition view.cpp:517
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:426
std::vector< VIEW_ITEM * > m_ownedItems
Definition view.h:873
void SetLayerTarget(int aLayer, RENDER_TARGET aTarget)
Change the rendering target for a particular layer.
Definition view.h:498
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:1727
void SetLayerVisible(int aLayer, bool aVisible=true)
Control the visibility of a particular layer.
Definition view.h:401
bool HasItem(const VIEW_ITEM *aItem) const
Indicates whether or not the given item has been added to the view.
Definition view.cpp:1713
PAINTER * m_painter
PAINTER contains information how do draw items.
Definition view.h:902
std::set< unsigned int > m_topLayers
The set of layers that are displayed on the top.
Definition view.h:888
bool m_enableOrderModifier
Whether to use rendering order modifier or not.
Definition view.h:876
void SetLayerHasNegatives(int aLayer, bool aNegatives=true)
Set the status of negatives presense in a particular layer.
Definition view.h:462
GAL * GetGAL() const
Return the GAL this view is using to draw graphical primitives.
Definition view.h:203
bool IsHiddenOnOverlay(const VIEW_ITEM *aItem) const
Definition view.cpp:1705
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:908
void MarkClean()
Force redraw of view on the next rendering.
Definition view.h:677
static constexpr int VIEW_MAX_LAYERS
Maximum number of layers that may be shown.
Definition view.h:762
double m_minScale
Definition view.h:895
double m_scale
Definition view.h:893
bool IsMirroredX() const
Return true if view is flipped across the X axis.
Definition view.h:251
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:637
void SetBoundary(const BOX2I &aBoundary)
Set limits for view area.
Definition view.h:297
int m_nextDrawPriority
The next sequential drawing priority.
Definition view.h:914
bool m_useDrawPriority
Flag to respect draw priority when drawing items.
Definition view.h:911
bool IsCached(int aLayer) const
Return true if the layer is cached.
Definition view.h:655
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:259
void UseDrawPriority(bool aFlag)
Definition view.h:730
bool m_hasPendingItemUpdates
True when at least one item has deferred update flags that still need processing.
Definition view.h:920
std::unique_ptr< KIGFX::VIEW_GROUP > m_preview
Definition view.h:872
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:227
bool IsUsingDrawPriority() const
Definition view.h:722
VIEW & operator=(const VIEW &)=delete
GAL * m_gal
Interface to PAINTER that is used to draw items.
Definition view.h:905
bool IsDirty() const
Return true if any of the VIEW layers needs to be refreshened.
Definition view.h:620
std::map< int, VIEW_LAYER > m_layers
The set of possible displayed layers and its properties.
Definition view.h:879
void Hide(VIEW_ITEM *aItem, bool aHide=true, bool aHideOverlay=false)
Temporarily hide the item in the view (e.g.
Definition view.cpp:1675
void markTargetClean(int aTarget)
Definition view.h:795
void MarkDirty()
Force redraw of view on the next rendering.
Definition view.h:668
friend class VIEW_ITEM
Definition view.h:69
bool IsLayerVisible(int aLayer) const
Return information about visibility of a particular layer.
Definition view.h:423
BOX2D m_boundary
Definition view.h:894
const BOX2D & GetBoundary() const
Definition view.h:306
PAINTER * GetPainter() const
Return the painter object used by the view for drawing #VIEW_ITEMS.
Definition view.h:221
void SetScaleLimits(double aMaximum, double aMinimum)
Set minimum and maximum values for scale.
Definition view.h:317
std::vector< VIEW_LAYER * > m_orderedLayers
Sorted list of pointers to members of m_layers.
Definition view.h:882
VECTOR2D m_center
Center point of the VIEW (the point at which we are looking at).
Definition view.h:891
void MarkTargetDirty(int aTarget)
Set or clear target 'dirty' flag.
Definition view.h:648
bool IsVisible(const VIEW_ITEM *aItem) const
Return information if the item is visible (or not).
Definition view.cpp:1697
void SetVisible(VIEW_ITEM *aItem, bool aIsVisible=true)
Set the item visibility.
Definition view.cpp:1654
#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:784
bool diffLayer
Layer should be drawn differentially over lower layers.
Definition view.h:774
int renderingOrder
Rendering order of this layer.
Definition view.h:779
bool hasNegatives
Layer should be drawn separately to not delete lower layers.
Definition view.h:777
bool visible
Is the layer to be rendered?
Definition view.h:770
bool displayOnly
Is the layer display only?
Definition view.h:771
std::shared_ptr< VIEW_RTREE > items
R-tree indexing all items on this layer.
Definition view.h:778
RENDER_TARGET target
Where the layer should be rendered.
Definition view.h:781
int id
Layer ID.
Definition view.h:780
VECTOR2< int32_t > VECTOR2I
Definition vector2d.h:695
VECTOR2< double > VECTOR2D
Definition vector2d.h:694