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
514 void SetLayerOrder( int aLayer, int aRenderingOrder );
515
522 int GetLayerOrder( int aLayer ) const;
523
531 void SortLayers( std::vector<int>& aLayers ) const;
532
540 void ReorderLayerData( std::unordered_map<int, int> aReorderMap );
541
548 void UpdateLayerColor( int aLayer );
549
555 void UpdateAllLayersColor();
556
564 virtual void SetTopLayer( int aLayer, bool aEnabled = true );
565
575 virtual void EnableTopLayer( bool aEnable );
576
577 virtual int GetTopLayer() const;
578
583 void ClearTopLayers();
584
590 void UpdateAllLayersOrder();
591
595 void ClearTargets();
596
600 virtual void Redraw();
601
605 void RecacheAllItems();
606
612 bool IsDirty() const
613 {
614 for( int i = 0; i < TARGETS_NUMBER; ++i )
615 {
616 if( IsTargetDirty( i ) )
617 return true;
618 }
619
620 return false;
621 }
622
629 bool IsTargetDirty( int aTarget ) const
630 {
631 wxCHECK( aTarget < TARGETS_NUMBER, false );
632 return m_dirtyTargets[aTarget];
633 }
634
640 inline void MarkTargetDirty( int aTarget )
641 {
642 wxCHECK( aTarget < TARGETS_NUMBER, /* void */ );
643 m_dirtyTargets[aTarget] = true;
644 }
645
647 inline bool IsCached( int aLayer ) const
648 {
649 auto it = m_layers.find( aLayer );
650
651 if( it == m_layers.end() )
652 return false;
653
654 return it->second.target == TARGET_CACHED;
655 }
656
661 {
662 for( int i = 0; i < TARGETS_NUMBER; ++i )
663 m_dirtyTargets[i] = true;
664 }
665
670 {
671 for( int i = 0; i < TARGETS_NUMBER; ++i )
672 m_dirtyTargets[i] = false;
673 }
674
678 void UpdateItems();
679
685 void UpdateAllItems( int aUpdateFlags );
686
693 void UpdateAllItemsConditionally( int aUpdateFlags,
694 std::function<bool( VIEW_ITEM* )> aCondition );
695
701 void UpdateAllItemsConditionally( std::function<int( VIEW_ITEM* )> aItemFlagsProvider );
702
707 {
708 return m_useDrawPriority;
709 }
710
714 void UseDrawPriority( bool aFlag )
715 {
716 m_useDrawPriority = aFlag;
717 }
718
724 void ReverseDrawOrder( bool aFlag )
725 {
726 m_reverseDrawOrder = aFlag;
727 }
728
729 std::shared_ptr<VIEW_OVERLAY> MakeOverlay();
730
731 void InitPreview();
732
733 void ClearPreview();
734 void AddToPreview( VIEW_ITEM* aItem, bool aTakeOwnership = true );
735
736 void ShowPreview( bool aShow = true );
737
743 std::unique_ptr<VIEW> DataReference() const;
744
746 static constexpr int VIEW_MAX_LAYERS = MAX_LAYERS_FOR_VIEW;
747
750
751protected:
753 {
754 bool visible;
756
759
762 std::shared_ptr<VIEW_RTREE> items;
764 int id;
766
768 std::set<int> requiredLayers;
769
770 bool operator< ( const VIEW_LAYER& aOther ) const
771 {
772 return id < aOther.id;
773 }
774 };
775
777 void redrawRect( const BOX2I& aRect );
778
779 inline void markTargetClean( int aTarget )
780 {
781 wxCHECK( aTarget < TARGETS_NUMBER, /* void */ );
782 m_dirtyTargets[aTarget] = false;
783 }
784
796 void draw( VIEW_ITEM* aItem, int aLayer, bool aImmediate = false );
797
805 void draw( VIEW_ITEM* aItem, bool aImmediate = false );
806
814 void draw( VIEW_GROUP* aGroup, bool aImmediate = false );
815
817 void sortOrderedLayers();
818
821 void clearGroupCache();
822
829 void invalidateItem( VIEW_ITEM* aItem, int aUpdateFlags );
830
832 void updateItemColor( VIEW_ITEM* aItem, int aLayer );
833
835 void updateItemGeometry( VIEW_ITEM* aItem, int aLayer );
836
838 void updateBbox( VIEW_ITEM* aItem );
839
841 void updateLayers( VIEW_ITEM* aItem );
842
845 {
846 return aI->renderingOrder > aJ->renderingOrder;
847 }
848
850 bool areRequiredLayersEnabled( int aLayerId ) const;
851
852 // Function objects that need to access VIEW/VIEW_ITEM private/protected members
853 struct CLEAR_LAYER_CACHE_VISITOR;
854 struct RECACHE_ITEM_VISITOR;
855 struct DRAW_ITEM_VISITOR;
856 struct UPDATE_COLOR_VISITOR;
857 struct UPDATE_DEPTH_VISITOR;
858
859 std::unique_ptr<KIGFX::VIEW_GROUP> m_preview;
860 std::vector<VIEW_ITEM*> m_ownedItems;
861
864
866 std::map<int, VIEW_LAYER> m_layers;
867
869 std::vector<VIEW_LAYER*> m_orderedLayers;
870
872 std::shared_ptr<std::vector<VIEW_ITEM*>> m_allItems;
873
875 std::set<unsigned int> m_topLayers;
876
879
880 double m_scale;
884
887
890
893
896
899
902
905};
906} // namespace KIGFX
907
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:883
void ReverseDrawOrder(bool aFlag)
Only takes effect if UseDrawPriority is true.
Definition view.h:724
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:510
static constexpr int TOP_LAYER_MODIFIER
Rendering order modifier for layers that are marked as top layers.
Definition view.h:749
VIEW(const VIEW &)=delete
bool m_reverseDrawOrder
Flag to reverse the draw order when using draw priority.
Definition view.h:904
void SetRequired(int aLayerId, int aRequiredId, bool aRequired=true)
Mark the aRequiredId layer as required for the aLayerId layer.
Definition view.cpp:413
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:844
virtual void Add(VIEW_ITEM *aItem, int aDrawPriority=-1)
Add a VIEW_ITEM to the view.
Definition view.cpp:299
virtual void Remove(VIEW_ITEM *aItem)
Remove a VIEW_ITEM from the view.
Definition view.cpp:342
bool m_mirrorX
Definition view.h:885
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 m_mirrorY
Definition view.h:886
std::shared_ptr< std::vector< VIEW_ITEM * > > m_allItems
Flat list of all items.
Definition view.h:872
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:516
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:425
std::vector< VIEW_ITEM * > m_ownedItems
Definition view.h:860
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:1700
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:1686
PAINTER * m_painter
PAINTER contains information how do draw items.
Definition view.h:889
std::set< unsigned int > m_topLayers
The set of layers that are displayed on the top.
Definition view.h:875
bool m_enableOrderModifier
Whether to use rendering order modifier or not.
Definition view.h:863
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:1678
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:895
void MarkClean()
Force redraw of view on the next rendering.
Definition view.h:669
static constexpr int VIEW_MAX_LAYERS
Maximum number of layers that may be shown.
Definition view.h:746
double m_minScale
Definition view.h:882
double m_scale
Definition view.h:880
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:629
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:901
bool m_useDrawPriority
Flag to respect draw priority when drawing items.
Definition view.h:898
bool IsCached(int aLayer) const
Return true if the layer is cached.
Definition view.h:647
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:714
std::unique_ptr< KIGFX::VIEW_GROUP > m_preview
Definition view.h:859
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:706
VIEW & operator=(const VIEW &)=delete
GAL * m_gal
Interface to PAINTER that is used to draw items.
Definition view.h:892
bool IsDirty() const
Return true if any of the VIEW layers needs to be refreshened.
Definition view.h:612
std::map< int, VIEW_LAYER > m_layers
The set of possible displayed layers and its properties.
Definition view.h:866
void Hide(VIEW_ITEM *aItem, bool aHide=true, bool aHideOverlay=false)
Temporarily hide the item in the view (e.g.
Definition view.cpp:1648
void markTargetClean(int aTarget)
Definition view.h:779
void MarkDirty()
Force redraw of view on the next rendering.
Definition view.h:660
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:881
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:869
VECTOR2D m_center
Center point of the VIEW (the point at which we are looking at).
Definition view.h:878
void MarkTargetDirty(int aTarget)
Set or clear target 'dirty' flag.
Definition view.h:640
bool IsVisible(const VIEW_ITEM *aItem) const
Return information if the item is visible (or not).
Definition view.cpp:1670
void SetVisible(VIEW_ITEM *aItem, bool aIsVisible=true)
Set the item visibility.
Definition view.cpp:1627
#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:768
bool diffLayer
Layer should be drawn differentially over lower layers.
Definition view.h:758
int renderingOrder
Rendering order of this layer.
Definition view.h:763
bool hasNegatives
Layer should be drawn separately to not delete lower layers.
Definition view.h:761
bool visible
Is the layer to be rendered?
Definition view.h:754
bool displayOnly
Is the layer display only?
Definition view.h:755
std::shared_ptr< VIEW_RTREE > items
R-tree indexing all items on this layer.
Definition view.h:762
RENDER_TARGET target
Where the layer should be rendered.
Definition view.h:765
int id
Layer ID.
Definition view.h:764
VECTOR2< int32_t > VECTOR2I
Definition vector2d.h:695
VECTOR2< double > VECTOR2D
Definition vector2d.h:694