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 (C) 2020 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#include <view/view.h>
41
42namespace KIGFX
43{
44class PAINTER;
45class GAL;
46class VIEW_ITEM;
47class VIEW_GROUP;
48class VIEW_RTREE;
49
68{
69public:
70 friend class VIEW_ITEM;
71
72 typedef std::pair<VIEW_ITEM*, int> LAYER_ITEM_PAIR;
73
77 VIEW( bool aIsDynamic = true );
78
79 virtual ~VIEW();
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
117 virtual int Query( const BOX2I& aRect, std::vector<LAYER_ITEM_PAIR>& aResult ) const;
118
125 void SetVisible( VIEW_ITEM* aItem, bool aIsVisible = true );
126
134 void Hide( VIEW_ITEM* aItem, bool aHide = true, bool aHideOverlay = false );
135
143 bool IsVisible( const VIEW_ITEM* aItem ) const;
144
145 bool IsHiddenOnOverlay( const VIEW_ITEM* aItem ) const;
146
150 bool HasItem( const VIEW_ITEM* aItem ) const;
151
159 virtual void Update( const VIEW_ITEM* aItem, int aUpdateFlags ) const;
160 virtual void Update( const VIEW_ITEM* aItem ) const;
161
170 void SetRequired( int aLayerId, int aRequiredId, bool aRequired = true );
171
177 void CopySettings( const VIEW* aOtherView );
178
179 /*
180 * Convenience wrappers for adding multiple items
181 * template <class T> void AddItems( const T& aItems );
182 * template <class T> void RemoveItems( const T& aItems );
183 */
184
190 void SetGAL( GAL* aGal );
191
197 inline GAL* GetGAL() const
198 {
199 return m_gal;
200 }
201
205 inline void SetPainter( PAINTER* aPainter )
206 {
207 m_painter = aPainter;
208 }
209
215 inline PAINTER* GetPainter() const
216 {
217 return m_painter;
218 }
219
225 void SetViewport( const BOX2D& aViewport );
226
232 BOX2D GetViewport() const;
233
240 void SetMirror( bool aMirrorX, bool aMirrorY );
241
245 bool IsMirroredX() const
246 {
247 return m_mirrorX;
248 }
249
253 bool IsMirroredY() const
254 {
255 return m_mirrorY;
256 }
257
266 virtual void SetScale( double aScale, VECTOR2D aAnchor = { 0, 0 } );
267
271 inline double GetScale() const
272 {
273 return m_scale;
274 }
275
281 inline void SetBoundary( const BOX2D& aBoundary )
282 {
283 m_boundary = aBoundary;
284 }
285
291 inline void SetBoundary( const BOX2I& aBoundary )
292 {
293 m_boundary.SetOrigin( aBoundary.GetOrigin() );
294 m_boundary.SetEnd( aBoundary.GetEnd() );
295 }
296
300 inline const BOX2D& GetBoundary() const
301 {
302 return m_boundary;
303 }
304
311 void SetScaleLimits( double aMaximum, double aMinimum )
312 {
313 wxASSERT_MSG( aMaximum > aMinimum, wxT( "I guess you passed parameters in wrong order" ) );
314
315 m_minScale = aMinimum;
316 m_maxScale = aMaximum;
317 }
318
325 void SetCenter( const VECTOR2D& aCenter );
326
334 void SetCenter( const VECTOR2D& aCenter, const std::vector<BOX2D>& obscuringScreenRects );
335
341 const VECTOR2D& GetCenter() const
342 {
343 return m_center;
344 }
345
352 VECTOR2D ToWorld( const VECTOR2D& aCoord, bool aAbsolute = true ) const;
353
360 double ToWorld( double aSize ) const;
361
368 VECTOR2D ToScreen( const VECTOR2D& aCoord, bool aAbsolute = true ) const;
369
375 double ToScreen( double aSize ) const;
376
382 const VECTOR2I& GetScreenPixelSize() const;
383
387 void Clear();
388
395 inline void SetLayerVisible( int aLayer, bool aVisible = true )
396 {
397 wxCHECK( aLayer < (int) m_layers.size(), /*void*/ );
398
399 if( m_layers[aLayer].visible != aVisible )
400 {
401 // Target has to be redrawn after changing its visibility
402 MarkTargetDirty( m_layers[aLayer].target );
403 m_layers[aLayer].visible = aVisible;
404 }
405 }
406
412 inline bool IsLayerVisible( int aLayer ) const
413 {
414 wxCHECK( aLayer >= 0, false);
415 wxCHECK( aLayer < (int) m_layers.size(), false );
416
417 return m_layers.at( aLayer ).visible;
418 }
419
426 inline void SetLayerDiff( int aLayer, bool aDiff = true )
427 {
428 wxCHECK( aLayer < (int) m_layers.size(), /*void*/ );
429
430 if( m_layers[aLayer].diffLayer != aDiff )
431 {
432 // Target has to be redrawn after changing its layers' diff status
433 MarkTargetDirty( m_layers[aLayer].target );
434 m_layers[aLayer].diffLayer = aDiff;
435 }
436 }
437
444 inline void SetLayerHasNegatives( int aLayer, bool aNegatives = true )
445 {
446 wxCHECK( aLayer < (int) m_layers.size(), /*void*/ );
447
448 if( m_layers[aLayer].hasNegatives != aNegatives )
449 {
450 // Target has to be redrawn after changing a layers' negatives
451 MarkTargetDirty( m_layers[aLayer].target );
452 m_layers[aLayer].hasNegatives = aNegatives;
453 }
454 }
455
459 inline void SetLayerDisplayOnly( int aLayer, bool aDisplayOnly = true )
460 {
461 wxCHECK( aLayer < (int) m_layers.size(), /*void*/ );
462 m_layers[aLayer].displayOnly = aDisplayOnly;
463 }
464
471 inline void SetLayerTarget( int aLayer, RENDER_TARGET aTarget )
472 {
473 wxCHECK( aLayer < (int) m_layers.size(), /*void*/ );
474 m_layers[aLayer].target = aTarget;
475 }
476
483 void SetLayerOrder( int aLayer, int aRenderingOrder );
484
491 int GetLayerOrder( int aLayer ) const;
492
501 void SortLayers( int aLayers[], int& aCount ) const;
502
510 void ReorderLayerData( std::unordered_map<int, int> aReorderMap );
511
518 void UpdateLayerColor( int aLayer );
519
525 void UpdateAllLayersColor();
526
534 virtual void SetTopLayer( int aLayer, bool aEnabled = true );
535
545 virtual void EnableTopLayer( bool aEnable );
546
547 virtual int GetTopLayer() const;
548
553 void ClearTopLayers();
554
560 void UpdateAllLayersOrder();
561
565 void ClearTargets();
566
570 virtual void Redraw();
571
575 void RecacheAllItems();
576
581 bool IsDynamic() const
582 {
583 return m_dynamic;
584 }
585
591 bool IsDirty() const
592 {
593 for( int i = 0; i < TARGETS_NUMBER; ++i )
594 {
595 if( IsTargetDirty( i ) )
596 return true;
597 }
598
599 return false;
600 }
601
608 bool IsTargetDirty( int aTarget ) const
609 {
610 wxCHECK( aTarget < TARGETS_NUMBER, false );
611 return m_dirtyTargets[aTarget];
612 }
613
619 inline void MarkTargetDirty( int aTarget )
620 {
621 wxCHECK( aTarget < TARGETS_NUMBER, /* void */ );
622 m_dirtyTargets[aTarget] = true;
623 }
624
626 inline bool IsCached( int aLayer ) const
627 {
628 wxCHECK( aLayer < (int) m_layers.size(), false );
629
630 try
631 {
632 return m_layers.at( aLayer ).target == TARGET_CACHED;
633 }
634 catch( const std::out_of_range& )
635 {
636 return false;
637 }
638 }
639
644 {
645 for( int i = 0; i < TARGETS_NUMBER; ++i )
646 m_dirtyTargets[i] = true;
647 }
648
653 {
654 for( int i = 0; i < TARGETS_NUMBER; ++i )
655 m_dirtyTargets[i] = false;
656 }
657
661 void UpdateItems();
662
668 void UpdateAllItems( int aUpdateFlags );
669
676 void UpdateAllItemsConditionally( int aUpdateFlags,
677 std::function<bool( VIEW_ITEM* )> aCondition );
678
684 void UpdateAllItemsConditionally( std::function<int( VIEW_ITEM* )> aItemFlagsProvider );
685
690 {
691 return m_useDrawPriority;
692 }
693
697 void UseDrawPriority( bool aFlag )
698 {
699 m_useDrawPriority = aFlag;
700 }
701
707 void ReverseDrawOrder( bool aFlag )
708 {
709 m_reverseDrawOrder = aFlag;
710 }
711
712 std::shared_ptr<VIEW_OVERLAY> MakeOverlay();
713
714 void InitPreview();
715
716 void ClearPreview();
717 void AddToPreview( VIEW_ITEM* aItem, bool aTakeOwnership = true );
718
719 void ShowPreview( bool aShow = true );
720
726 std::unique_ptr<VIEW> DataReference() const;
727
729 static constexpr int VIEW_MAX_LAYERS = 512;
730
732 static constexpr int TOP_LAYER_MODIFIER = -VIEW_MAX_LAYERS;
733
734protected:
736 {
737 bool visible;
741 std::shared_ptr<VIEW_RTREE> items;
743 int id;
745 std::set<int> requiredLayers;
747 };
748
749
750
751 VIEW( const VIEW& ) = delete;
752
754 void redrawRect( const BOX2I& aRect );
755
756 inline void markTargetClean( int aTarget )
757 {
758 wxCHECK( aTarget < TARGETS_NUMBER, /* void */ );
759 m_dirtyTargets[aTarget] = false;
760 }
761
773 void draw( VIEW_ITEM* aItem, int aLayer, bool aImmediate = false );
774
782 void draw( VIEW_ITEM* aItem, bool aImmediate = false );
783
791 void draw( VIEW_GROUP* aGroup, bool aImmediate = false );
792
794 void sortLayers();
795
798 void clearGroupCache();
799
806 void invalidateItem( VIEW_ITEM* aItem, int aUpdateFlags );
807
809 void updateItemColor( VIEW_ITEM* aItem, int aLayer );
810
812 void updateItemGeometry( VIEW_ITEM* aItem, int aLayer );
813
815 void updateBbox( VIEW_ITEM* aItem );
816
818 void updateLayers( VIEW_ITEM* aItem );
819
822 {
823 return aI->renderingOrder > aJ->renderingOrder;
824 }
825
827 bool areRequiredLayersEnabled( int aLayerId ) const;
828
829 // Function objects that need to access VIEW/VIEW_ITEM private/protected members
830 struct CLEAR_LAYER_CACHE_VISITOR;
831 struct RECACHE_ITEM_VISITOR;
832 struct DRAW_ITEM_VISITOR;
833 struct UPDATE_COLOR_VISITOR;
834 struct UPDATE_DEPTH_VISITOR;
835
836 std::unique_ptr<KIGFX::VIEW_GROUP> m_preview;
837 std::vector<VIEW_ITEM *> m_ownedItems;
838
841
843 std::vector<VIEW_LAYER> m_layers;
844
846 std::vector<VIEW_LAYER*> m_orderedLayers;
847
849 std::shared_ptr<std::vector<VIEW_ITEM*>> m_allItems;
850
852 std::set<unsigned int> m_topLayers;
853
856
857 double m_scale;
861
864
867
870
874
876 bool m_dirtyTargets[TARGETS_NUMBER];
877
880
883
886};
887} // namespace KIGFX
888
889#endif
const Vec & GetOrigin() const
Definition: box2.h:184
const Vec GetEnd() const
Definition: box2.h:186
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:84
Hold a (potentially large) number of VIEW_ITEMs and renders them on a graphics device provided by the...
Definition: view.h:68
double GetScale() const
Definition: view.h:271
bool m_dynamic
Flag to mark targets as dirty so they have to be redrawn on the next refresh event.
Definition: view.h:873
bool IsDynamic() const
Tell if the VIEW is dynamic (ie.
Definition: view.h:581
double m_maxScale
Definition: view.h:860
void ReverseDrawOrder(bool aFlag)
Only takes effect if UseDrawPriority is true.
Definition: view.h:707
std::vector< VIEW_LAYER > m_layers
Sorted list of pointers to members of m_layers.
Definition: view.h:843
const VECTOR2D & GetCenter() const
Return the center point of this VIEW (in world space coordinates).
Definition: view.h:341
VIEW(const VIEW &)=delete
std::vector< VIEW_ITEM * > m_ownedItems
Whether to use rendering order modifier or not.
Definition: view.h:837
bool m_reverseDrawOrder
Definition: view.h:885
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:459
static bool compareRenderingOrder(VIEW_LAYER *aI, VIEW_LAYER *aJ)
Check if every layer required by the aLayerId layer is enabled.
Definition: view.h:821
bool m_mirrorX
Definition: view.h:862
void SetPainter(PAINTER *aPainter)
Set the painter object used by the view for drawing #VIEW_ITEMS.
Definition: view.h:205
void SetBoundary(const BOX2D &aBoundary)
Set limits for view area.
Definition: view.h:281
bool m_mirrorY
PAINTER contains information how do draw items.
Definition: view.h:863
std::shared_ptr< std::vector< VIEW_ITEM * > > m_allItems
The set of layers that are displayed on the top.
Definition: view.h:849
void SetLayerDiff(int aLayer, bool aDiff=true)
Set the whether the layer should drawn differentially.
Definition: view.h:426
void SetLayerTarget(int aLayer, RENDER_TARGET aTarget)
Change the rendering target for a particular layer.
Definition: view.h:471
void SetLayerVisible(int aLayer, bool aVisible=true)
Control the visibility of a particular layer.
Definition: view.h:395
PAINTER * m_painter
Interface to #PAINTER that is used to draw items.
Definition: view.h:866
std::set< unsigned int > m_topLayers
Center point of the VIEW (the point at which we are looking at).
Definition: view.h:852
bool m_enableOrderModifier
The set of possible displayed layers and its properties.
Definition: view.h:840
void SetLayerHasNegatives(int aLayer, bool aNegatives=true)
Set the status of negatives presense in a particular layer.
Definition: view.h:444
GAL * GetGAL() const
Return the #GAL this view is using to draw graphical primitives.
Definition: view.h:197
void MarkClean()
Force redraw of view on the next rendering.
Definition: view.h:652
double m_minScale
Definition: view.h:859
double m_scale
Definition: view.h:857
bool IsMirroredX() const
Return true if view is flipped across the X axis.
Definition: view.h:245
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:608
void SetBoundary(const BOX2I &aBoundary)
Set limits for view area.
Definition: view.h:291
int m_nextDrawPriority
Flag to reverse the draw order when using draw priority.
Definition: view.h:882
bool m_useDrawPriority
The next sequential drawing priority.
Definition: view.h:879
bool IsCached(int aLayer) const
Return true if the layer is cached.
Definition: view.h:626
std::pair< VIEW_ITEM *, int > LAYER_ITEM_PAIR
Definition: view.h:72
bool IsMirroredY() const
Return true if view is flipped across the Y axis.
Definition: view.h:253
void UseDrawPriority(bool aFlag)
Definition: view.h:697
std::unique_ptr< KIGFX::VIEW_GROUP > m_preview
Definition: view.h:836
bool IsUsingDrawPriority() const
Definition: view.h:689
GAL * m_gal
Dynamic VIEW (eg.
Definition: view.h:869
bool IsDirty() const
Return true if any of the VIEW layers needs to be refreshened.
Definition: view.h:591
void markTargetClean(int aTarget)
Definition: view.h:756
void MarkDirty()
Force redraw of view on the next rendering.
Definition: view.h:643
bool IsLayerVisible(int aLayer) const
Return information about visibility of a particular layer.
Definition: view.h:412
BOX2D m_boundary
Definition: view.h:858
const BOX2D & GetBoundary() const
Definition: view.h:300
PAINTER * GetPainter() const
Return the painter object used by the view for drawing #VIEW_ITEMS.
Definition: view.h:215
void SetScaleLimits(double aMaximum, double aMinimum)
Set minimum and maximum values for scale.
Definition: view.h:311
std::vector< VIEW_LAYER * > m_orderedLayers
Flat list of all items.
Definition: view.h:846
VECTOR2D m_center
Definition: view.h:855
void MarkTargetDirty(int aTarget)
Set or clear target 'dirty' flag.
Definition: view.h:619
#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:47
@ TARGET_CACHED
Main rendering target (cached)
Definition: definitions.h:48
@ TARGETS_NUMBER
Number of available rendering targets.
Definition: definitions.h:52
std::set< int > requiredLayers
Layers that have to be enabled to show the layer.
Definition: view.h:745
bool diffLayer
Layer should be drawn differentially over lower layers.
Definition: view.h:739
int renderingOrder
Rendering order of this layer.
Definition: view.h:742
bool hasNegatives
Layer should be drawn separately to not delete lower layers.
Definition: view.h:740
bool visible
Is the layer to be rendered?
Definition: view.h:737
bool displayOnly
Is the layer display only?
Definition: view.h:738
std::shared_ptr< VIEW_RTREE > items
R-tree indexing all items on this layer.
Definition: view.h:741
RENDER_TARGET target
Where the layer should be rendered.
Definition: view.h:744
int id
Layer ID.
Definition: view.h:743