KiCad PCB EDA Suite
Loading...
Searching...
No Matches
view_group.cpp
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 CERN
5 * Copyright The KiCad Developers, see AUTHORS.txt for contributors.
6 * @author Maciej Suminski <[email protected]>
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version 2
11 * of the License, or (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <https://www.gnu.org/licenses/>.
20 *
21 */
22
29
30#include <set>
31#include <core/kicad_algo.h>
32#include <algorithm>
33#include <view/view_group.h>
34#include <view/view.h>
35#include <view/view_item.h>
36#include <gal/painter.h>
38#include <layer_ids.h>
39
40using namespace KIGFX;
41
47
48
50{
51 // VIEW_ITEM destructor removes the object from its parent view
52}
53
54
55wxString VIEW_GROUP::GetClass() const
56{
57 return wxT( "VIEW_GROUP" );
58}
59
60
62{
63 if( !aItem )
64 return;
65
66 m_groupItems.push_back( aItem );
67}
68
69
71{
72 std::erase( m_groupItems, aItem );
73}
74
75
77{
78 m_groupItems.clear();
79}
80
81
82unsigned int VIEW_GROUP::GetSize() const
83{
84 return m_groupItems.size();
85}
86
87
88VIEW_ITEM *VIEW_GROUP::GetItem( unsigned int idx ) const
89{
90 return m_groupItems[idx];
91}
92
93
95{
96 BOX2I bb;
97
98 if( !m_groupItems.size() )
99 {
100 bb.SetMaximum();
101 }
102 else
103 {
104 bb = m_groupItems[0]->ViewBBox();
105
106 for( VIEW_ITEM* item : m_groupItems )
107 bb.Merge( item->ViewBBox() );
108 }
109
110 return bb;
111}
112
113
114void VIEW_GROUP::ViewDraw( int aLayer, VIEW* aView ) const
115{
116 KIGFX::GAL* gal = aView->GetGAL();
117 PAINTER* painter = aView->GetPainter();
118 bool isSelection = m_layer == LAYER_SELECT_OVERLAY;
119
120 const std::vector<VIEW_ITEM*> drawList = updateDrawList();
121
122 std::map<int, std::vector<VIEW_ITEM*>> layer_item_map;
123
124 // Build a list of layers used by the items in the group
125 for( VIEW_ITEM* item : drawList )
126 {
127 if( aView->IsHiddenOnOverlay( item ) )
128 continue;
129
130 std::vector<int> layers = item->ViewGetLayers();
131
132 for( auto layer : layers )
133 {
134 wxCHECK2_MSG( layer <= LAYER_ID_COUNT, continue, wxT( "Invalid item layer" ) );
135 layer_item_map[ layer ].push_back( item );
136 }
137 }
138
139 if( layer_item_map.empty() )
140 return;
141
142 std::vector<int> layers;
143 layers.reserve( layer_item_map.size() );
144
145 for( const std::pair<const int, std::vector<VIEW_ITEM*>>& entry : layer_item_map )
146 layers.push_back( entry.first );
147
148 aView->SortLayers( layers );
149
150 // Now draw the layers in sorted order
151
153
154 for( int layer : layers )
155 {
156 bool draw = aView->IsLayerVisible( layer );
157
158 if( IsZoneFillLayer( layer ) )
159 {
160 // The visibility of solid areas must follow the visiblility of the zone layer
161 int zone_main_layer = layer - LAYER_ZONE_START;
162 draw = aView->IsLayerVisible( zone_main_layer );
163 }
164 else if( IsPadCopperLayer( layer ) )
165 {
166 draw = aView->IsLayerVisible( layer - LAYER_PAD_COPPER_START );
167 }
168 else if( IsViaCopperLayer( layer ) )
169 {
170 draw = aView->IsLayerVisible( layer - LAYER_VIA_COPPER_START );
171 }
172 else if( IsClearanceLayer( layer ) )
173 {
174 draw = aView->IsLayerVisible( layer - LAYER_CLEARANCE_START );
175 }
176 else if( IsPointsLayer( layer ) )
177 {
178 draw = aView->IsLayerVisible( layer - LAYER_POINT_START );
179 }
180
181 if( isSelection )
182 {
183 switch( layer )
184 {
187 draw = true;
188 break;
189
190 default:
191 break;
192 }
193 }
194
195 if( draw )
196 {
197 gal->AdvanceDepth();
198
199 for( VIEW_ITEM* item : layer_item_map[ layer ] )
200 {
201 // Ignore LOD scale for selected items, but don't ignore things explicitly
202 // hidden.
203 if( item->ViewGetLOD( layer, aView ) == LOD_HIDE )
204 continue;
205
206 if( !painter->Draw( item, layer ) )
207 item->ViewDraw( layer, aView ); // Alternative drawing method
208 }
209 }
210 }
211}
212
213
214std::vector<int> VIEW_GROUP::ViewGetLayers() const
215{
216 // Everything is displayed on a single layer
217 return { m_layer };
218}
219
220
222{
223 for( unsigned int i = 0 ; i < GetSize(); i++ )
224 delete GetItem( i );
225
226 Clear();
227}
228
229
230const std::vector<VIEW_ITEM*> VIEW_GROUP::updateDrawList() const
231{
232 return m_groupItems;
233}
234
235
236/*void VIEW_GROUP::ItemsSetVisibility( bool aVisible )
237{
238 for(unsigned int i = 0 ; i < GetSize(); i++)
239 GetItem(i)->ViewSetVisible( aVisible );
240}
241
242
243void VIEW_GROUP::ItemsViewUpdate( VIEW_ITEM::VIEW_UPDATE_FLAGS aFlags )
244{
245 for(unsigned int i = 0 ; i < GetSize(); i++)
246 GetItem(i)->ViewUpdate( aFlags );
247}*/
BOX2< VECTOR2I > BOX2I
Definition box2.h:918
constexpr void SetMaximum()
Definition box2.h:76
constexpr BOX2< Vec > & Merge(const BOX2< Vec > &aRect)
Modify the position and size of the rectangle in order to contain aRect.
Definition box2.h:654
Attribute save/restore for GAL attributes.
Abstract interface for drawing on a 2D-surface.
void AdvanceDepth()
Change the current depth to deeper, so it is possible to draw objects right beneath other.
Contains all the knowledge about how to draw graphical object onto any particular output device.
Definition painter.h:55
virtual bool Draw(const VIEW_ITEM *aItem, int aLayer)=0
Takes an instance of VIEW_ITEM and passes it to a function that knows how to draw the item.
virtual void Clear()
Remove all the stored items from the group.
virtual unsigned int GetSize() const
Return the number of stored items.
virtual void ViewDraw(int aLayer, VIEW *aView) const override
Draw all the stored items in the group on the given layer.
virtual VIEW_ITEM * GetItem(unsigned int aIdx) const
void FreeItems()
Free all the items that were added to the group.
std::vector< int > ViewGetLayers() const override
Return the all the layers within the VIEW the object is painted on.
std::vector< VIEW_ITEM * > m_groupItems
Definition view_group.h:107
wxString GetClass() const override
Return the class name.
virtual void Remove(VIEW_ITEM *aItem)
Remove an item from the group.
VIEW_GROUP(VIEW *aView=nullptr)
virtual ~VIEW_GROUP()
virtual void Add(VIEW_ITEM *aItem)
Add an item to the group.
virtual const std::vector< VIEW_ITEM * > updateDrawList() const
virtual const BOX2I ViewBBox() const override
Return the bounding box for all stored items covering all its layers.
VIEW_ITEM(bool isSCH_ITEM=false, bool isBOARD_ITEM=false)
Definition view_item.h:84
friend class VIEW
Definition view_item.h:201
static constexpr double LOD_HIDE
Return this constant from ViewGetLOD() to hide the item unconditionally.
Definition view_item.h:176
GAL * GetGAL() const
Return the GAL this view is using to draw graphical primitives.
Definition view.h:207
bool IsHiddenOnOverlay(const VIEW_ITEM *aItem) const
Definition view.cpp:1813
void SortLayers(std::vector< int > &aLayers) const
Change the order of given layer ids, so after sorting the order corresponds to layers rendering order...
Definition view.cpp:738
bool IsLayerVisible(int aLayer) const
Return information about visibility of a particular layer.
Definition view.h:427
PAINTER * GetPainter() const
Return the painter object used by the view for drawing #VIEW_ITEMS.
Definition view.h:225
bool IsPadCopperLayer(int aLayer)
Definition layer_ids.h:879
bool IsPointsLayer(int aLayer)
Definition layer_ids.h:897
#define LAYER_ID_COUNT
Must update this if you add any enums after Gerbview!
Definition layer_ids.h:624
bool IsClearanceLayer(int aLayer)
Definition layer_ids.h:891
@ LAYER_PAD_COPPER_START
Virtual layers for pad copper on a given copper layer.
Definition layer_ids.h:335
@ LAYER_PAD_PLATEDHOLES
to draw pad holes (plated)
Definition layer_ids.h:267
@ LAYER_VIA_COPPER_START
Virtual layers for via copper on a given copper layer.
Definition layer_ids.h:339
@ LAYER_CLEARANCE_START
Virtual layers for pad/via/track clearance outlines for a given copper layer.
Definition layer_ids.h:343
@ LAYER_ZONE_START
Virtual layers for stacking zones and tracks on a given copper layer.
Definition layer_ids.h:331
@ LAYER_POINT_START
Virtual layers for points per board layer.
Definition layer_ids.h:351
@ LAYER_SELECT_OVERLAY
Selected items overlay.
Definition layer_ids.h:276
@ LAYER_PAD_HOLEWALLS
Definition layer_ids.h:293
bool IsViaCopperLayer(int aLayer)
Definition layer_ids.h:885
bool IsZoneFillLayer(int aLayer)
Definition layer_ids.h:873
The Cairo implementation of the graphics abstraction layer.
Definition eda_group.h:29