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
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 wxCHECK( aLayer < (int) m_layers.size(), /*void*/ );
404
405 if( m_layers[aLayer].visible != aVisible )
406 {
407 // Target has to be redrawn after changing its visibility
408 MarkTargetDirty( m_layers[aLayer].target );
409 m_layers[aLayer].visible = aVisible;
410 }
411 }
412
418 inline bool IsLayerVisible( int aLayer ) const
419 {
420 wxCHECK( aLayer >= 0, false);
421 wxCHECK( aLayer < (int) m_layers.size(), false );
422
423 return m_layers.at( aLayer ).visible;
424 }
425
432 inline void SetLayerDiff( int aLayer, bool aDiff = true )
433 {
434 wxCHECK( aLayer < (int) m_layers.size(), /*void*/ );
435
436 if( m_layers[aLayer].diffLayer != aDiff )
437 {
438 // Target has to be redrawn after changing its layers' diff status
439 MarkTargetDirty( m_layers[aLayer].target );
440 m_layers[aLayer].diffLayer = aDiff;
441 }
442 }
443
450 inline void SetLayerHasNegatives( int aLayer, bool aNegatives = true )
451 {
452 wxCHECK( aLayer < (int) m_layers.size(), /*void*/ );
453
454 if( m_layers[aLayer].hasNegatives != aNegatives )
455 {
456 // Target has to be redrawn after changing a layers' negatives
457 MarkTargetDirty( m_layers[aLayer].target );
458 m_layers[aLayer].hasNegatives = aNegatives;
459 }
460 }
461
465 inline void SetLayerDisplayOnly( int aLayer, bool aDisplayOnly = true )
466 {
467 wxCHECK( aLayer < (int) m_layers.size(), /*void*/ );
468 m_layers[aLayer].displayOnly = aDisplayOnly;
469 }
470
477 inline void SetLayerTarget( int aLayer, RENDER_TARGET aTarget )
478 {
479 wxCHECK( aLayer < (int) m_layers.size(), /*void*/ );
480 m_layers[aLayer].target = aTarget;
481 }
482
489 void SetLayerOrder( int aLayer, int aRenderingOrder );
490
497 int GetLayerOrder( int aLayer ) const;
498
507 void SortLayers( int aLayers[], int& aCount ) const;
508
516 void ReorderLayerData( std::unordered_map<int, int> aReorderMap );
517
524 void UpdateLayerColor( int aLayer );
525
531 void UpdateAllLayersColor();
532
540 virtual void SetTopLayer( int aLayer, bool aEnabled = true );
541
551 virtual void EnableTopLayer( bool aEnable );
552
553 virtual int GetTopLayer() const;
554
559 void ClearTopLayers();
560
566 void UpdateAllLayersOrder();
567
571 void ClearTargets();
572
576 virtual void Redraw();
577
581 void RecacheAllItems();
582
587 bool IsDynamic() const
588 {
589 return m_dynamic;
590 }
591
597 bool IsDirty() const
598 {
599 for( int i = 0; i < TARGETS_NUMBER; ++i )
600 {
601 if( IsTargetDirty( i ) )
602 return true;
603 }
604
605 return false;
606 }
607
614 bool IsTargetDirty( int aTarget ) const
615 {
616 wxCHECK( aTarget < TARGETS_NUMBER, false );
617 return m_dirtyTargets[aTarget];
618 }
619
625 inline void MarkTargetDirty( int aTarget )
626 {
627 wxCHECK( aTarget < TARGETS_NUMBER, /* void */ );
628 m_dirtyTargets[aTarget] = true;
629 }
630
632 inline bool IsCached( int aLayer ) const
633 {
634 wxCHECK( aLayer < (int) m_layers.size(), false );
635
636 try
637 {
638 return m_layers.at( aLayer ).target == TARGET_CACHED;
639 }
640 catch( const std::out_of_range& )
641 {
642 return false;
643 }
644 }
645
650 {
651 for( int i = 0; i < TARGETS_NUMBER; ++i )
652 m_dirtyTargets[i] = true;
653 }
654
659 {
660 for( int i = 0; i < TARGETS_NUMBER; ++i )
661 m_dirtyTargets[i] = false;
662 }
663
667 void UpdateItems();
668
674 void UpdateAllItems( int aUpdateFlags );
675
682 void UpdateAllItemsConditionally( int aUpdateFlags,
683 std::function<bool( VIEW_ITEM* )> aCondition );
684
690 void UpdateAllItemsConditionally( std::function<int( VIEW_ITEM* )> aItemFlagsProvider );
691
696 {
697 return m_useDrawPriority;
698 }
699
703 void UseDrawPriority( bool aFlag )
704 {
705 m_useDrawPriority = aFlag;
706 }
707
713 void ReverseDrawOrder( bool aFlag )
714 {
715 m_reverseDrawOrder = aFlag;
716 }
717
718 std::shared_ptr<VIEW_OVERLAY> MakeOverlay();
719
720 void InitPreview();
721
722 void ClearPreview();
723 void AddToPreview( VIEW_ITEM* aItem, bool aTakeOwnership = true );
724
725 void ShowPreview( bool aShow = true );
726
732 std::unique_ptr<VIEW> DataReference() const;
733
735 static constexpr int VIEW_MAX_LAYERS = 512;
736
738 static constexpr int TOP_LAYER_MODIFIER = -VIEW_MAX_LAYERS;
739
740protected:
742 {
743 bool visible;
747 std::shared_ptr<VIEW_RTREE> items;
749 int id;
751 std::set<int> requiredLayers;
753 };
754
755
756
757 VIEW( const VIEW& ) = delete;
758
760 void redrawRect( const BOX2I& aRect );
761
762 inline void markTargetClean( int aTarget )
763 {
764 wxCHECK( aTarget < TARGETS_NUMBER, /* void */ );
765 m_dirtyTargets[aTarget] = false;
766 }
767
779 void draw( VIEW_ITEM* aItem, int aLayer, bool aImmediate = false );
780
788 void draw( VIEW_ITEM* aItem, bool aImmediate = false );
789
797 void draw( VIEW_GROUP* aGroup, bool aImmediate = false );
798
800 void sortLayers();
801
804 void clearGroupCache();
805
812 void invalidateItem( VIEW_ITEM* aItem, int aUpdateFlags );
813
815 void updateItemColor( VIEW_ITEM* aItem, int aLayer );
816
818 void updateItemGeometry( VIEW_ITEM* aItem, int aLayer );
819
821 void updateBbox( VIEW_ITEM* aItem );
822
824 void updateLayers( VIEW_ITEM* aItem );
825
828 {
829 return aI->renderingOrder > aJ->renderingOrder;
830 }
831
833 bool areRequiredLayersEnabled( int aLayerId ) const;
834
835 // Function objects that need to access VIEW/VIEW_ITEM private/protected members
836 struct CLEAR_LAYER_CACHE_VISITOR;
837 struct RECACHE_ITEM_VISITOR;
838 struct DRAW_ITEM_VISITOR;
839 struct UPDATE_COLOR_VISITOR;
840 struct UPDATE_DEPTH_VISITOR;
841
842 std::unique_ptr<KIGFX::VIEW_GROUP> m_preview;
843 std::vector<VIEW_ITEM *> m_ownedItems;
844
847
849 std::vector<VIEW_LAYER> m_layers;
850
852 std::vector<VIEW_LAYER*> m_orderedLayers;
853
855 std::shared_ptr<std::vector<VIEW_ITEM*>> m_allItems;
856
858 std::set<unsigned int> m_topLayers;
859
862
863 double m_scale;
867
870
873
876
880
882 bool m_dirtyTargets[TARGETS_NUMBER];
883
886
889
892};
893} // namespace KIGFX
894
895#endif
const Vec & GetOrigin() const
Definition: box2.h:200
const Vec GetEnd() const
Definition: box2.h:202
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:277
bool m_dynamic
Flag to mark targets as dirty so they have to be redrawn on the next refresh event.
Definition: view.h:879
bool IsDynamic() const
Tell if the VIEW is dynamic (ie.
Definition: view.h:587
double m_maxScale
Definition: view.h:866
void ReverseDrawOrder(bool aFlag)
Only takes effect if UseDrawPriority is true.
Definition: view.h:713
std::vector< VIEW_LAYER > m_layers
Sorted list of pointers to members of m_layers.
Definition: view.h:849
const VECTOR2D & GetCenter() const
Return the center point of this VIEW (in world space coordinates).
Definition: view.h:347
VIEW(const VIEW &)=delete
std::vector< VIEW_ITEM * > m_ownedItems
Whether to use rendering order modifier or not.
Definition: view.h:843
bool m_reverseDrawOrder
Definition: view.h:891
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:465
static bool compareRenderingOrder(VIEW_LAYER *aI, VIEW_LAYER *aJ)
Check if every layer required by the aLayerId layer is enabled.
Definition: view.h:827
bool m_mirrorX
Definition: view.h:868
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
PAINTER contains information how do draw items.
Definition: view.h:869
std::shared_ptr< std::vector< VIEW_ITEM * > > m_allItems
The set of layers that are displayed on the top.
Definition: view.h:855
void SetLayerDiff(int aLayer, bool aDiff=true)
Set the whether the layer should drawn differentially.
Definition: view.h:432
void SetLayerTarget(int aLayer, RENDER_TARGET aTarget)
Change the rendering target for a particular layer.
Definition: view.h:477
void SetLayerVisible(int aLayer, bool aVisible=true)
Control the visibility of a particular layer.
Definition: view.h:401
PAINTER * m_painter
Interface to #PAINTER that is used to draw items.
Definition: view.h:872
std::set< unsigned int > m_topLayers
Center point of the VIEW (the point at which we are looking at).
Definition: view.h:858
bool m_enableOrderModifier
The set of possible displayed layers and its properties.
Definition: view.h:846
void SetLayerHasNegatives(int aLayer, bool aNegatives=true)
Set the status of negatives presense in a particular layer.
Definition: view.h:450
GAL * GetGAL() const
Return the #GAL this view is using to draw graphical primitives.
Definition: view.h:203
void MarkClean()
Force redraw of view on the next rendering.
Definition: view.h:658
double m_minScale
Definition: view.h:865
double m_scale
Definition: view.h:863
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:614
void SetBoundary(const BOX2I &aBoundary)
Set limits for view area.
Definition: view.h:297
int m_nextDrawPriority
Flag to reverse the draw order when using draw priority.
Definition: view.h:888
bool m_useDrawPriority
The next sequential drawing priority.
Definition: view.h:885
bool IsCached(int aLayer) const
Return true if the layer is cached.
Definition: view.h:632
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:259
void UseDrawPriority(bool aFlag)
Definition: view.h:703
std::unique_ptr< KIGFX::VIEW_GROUP > m_preview
Definition: view.h:842
bool IsUsingDrawPriority() const
Definition: view.h:695
GAL * m_gal
Dynamic VIEW (eg.
Definition: view.h:875
bool IsDirty() const
Return true if any of the VIEW layers needs to be refreshened.
Definition: view.h:597
void markTargetClean(int aTarget)
Definition: view.h:762
void MarkDirty()
Force redraw of view on the next rendering.
Definition: view.h:649
bool IsLayerVisible(int aLayer) const
Return information about visibility of a particular layer.
Definition: view.h:418
BOX2D m_boundary
Definition: view.h:864
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
Flat list of all items.
Definition: view.h:852
VECTOR2D m_center
Definition: view.h:861
void MarkTargetDirty(int aTarget)
Set or clear target 'dirty' flag.
Definition: view.h:625
#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:751
bool diffLayer
Layer should be drawn differentially over lower layers.
Definition: view.h:745
int renderingOrder
Rendering order of this layer.
Definition: view.h:748
bool hasNegatives
Layer should be drawn separately to not delete lower layers.
Definition: view.h:746
bool visible
Is the layer to be rendered?
Definition: view.h:743
bool displayOnly
Is the layer display only?
Definition: view.h:744
std::shared_ptr< VIEW_RTREE > items
R-tree indexing all items on this layer.
Definition: view.h:747
RENDER_TARGET target
Where the layer should be rendered.
Definition: view.h:750
int id
Layer ID.
Definition: view.h:749