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 m_groupItems.push_back( aItem );
64}
65
66
68{
69 std::erase( m_groupItems, aItem );
70}
71
72
74{
75 m_groupItems.clear();
76}
77
78
79unsigned int VIEW_GROUP::GetSize() const
80{
81 return m_groupItems.size();
82}
83
84
85VIEW_ITEM *VIEW_GROUP::GetItem( unsigned int idx ) const
86{
87 return m_groupItems[idx];
88}
89
90
92{
93 BOX2I bb;
94
95 if( !m_groupItems.size() )
96 {
97 bb.SetMaximum();
98 }
99 else
100 {
101 bb = m_groupItems[0]->ViewBBox();
102
103 for( VIEW_ITEM* item : m_groupItems )
104 bb.Merge( item->ViewBBox() );
105 }
106
107 return bb;
108}
109
110
111void VIEW_GROUP::ViewDraw( int aLayer, VIEW* aView ) const
112{
113 KIGFX::GAL* gal = aView->GetGAL();
114 PAINTER* painter = aView->GetPainter();
115 bool isSelection = m_layer == LAYER_SELECT_OVERLAY;
116
117 const std::vector<VIEW_ITEM*> drawList = updateDrawList();
118
119 std::map<int, std::vector<VIEW_ITEM*>> layer_item_map;
120
121 // Build a list of layers used by the items in the group
122 for( VIEW_ITEM* item : drawList )
123 {
124 if( aView->IsHiddenOnOverlay( item ) )
125 continue;
126
127 std::vector<int> layers = item->ViewGetLayers();
128
129 for( auto layer : layers )
130 {
131 wxCHECK2_MSG( layer <= LAYER_ID_COUNT, continue, wxT( "Invalid item layer" ) );
132 layer_item_map[ layer ].push_back( item );
133 }
134 }
135
136 if( layer_item_map.empty() )
137 return;
138
139 std::vector<int> layers;
140 layers.reserve( layer_item_map.size() );
141
142 for( const std::pair<const int, std::vector<VIEW_ITEM*>>& entry : layer_item_map )
143 layers.push_back( entry.first );
144
145 aView->SortLayers( layers );
146
147 // Now draw the layers in sorted order
148
150
151 for( int layer : layers )
152 {
153 bool draw = aView->IsLayerVisible( layer );
154
155 if( IsZoneFillLayer( layer ) )
156 {
157 // The visibility of solid areas must follow the visiblility of the zone layer
158 int zone_main_layer = layer - LAYER_ZONE_START;
159 draw = aView->IsLayerVisible( zone_main_layer );
160 }
161 else if( IsPadCopperLayer( layer ) )
162 {
163 draw = aView->IsLayerVisible( layer - LAYER_PAD_COPPER_START );
164 }
165 else if( IsViaCopperLayer( layer ) )
166 {
167 draw = aView->IsLayerVisible( layer - LAYER_VIA_COPPER_START );
168 }
169 else if( IsClearanceLayer( layer ) )
170 {
171 draw = aView->IsLayerVisible( layer - LAYER_CLEARANCE_START );
172 }
173 else if( IsPointsLayer( layer ) )
174 {
175 draw = aView->IsLayerVisible( layer - LAYER_POINT_START );
176 }
177
178 if( isSelection )
179 {
180 switch( layer )
181 {
184 draw = true;
185 break;
186
187 default:
188 break;
189 }
190 }
191
192 if( draw )
193 {
194 gal->AdvanceDepth();
195
196 for( VIEW_ITEM* item : layer_item_map[ layer ] )
197 {
198 // Ignore LOD scale for selected items, but don't ignore things explicitly
199 // hidden.
200 if( item->ViewGetLOD( layer, aView ) == LOD_HIDE )
201 continue;
202
203 if( !painter->Draw( item, layer ) )
204 item->ViewDraw( layer, aView ); // Alternative drawing method
205 }
206 }
207 }
208}
209
210
211std::vector<int> VIEW_GROUP::ViewGetLayers() const
212{
213 // Everything is displayed on a single layer
214 return { m_layer };
215}
216
217
219{
220 for( unsigned int i = 0 ; i < GetSize(); i++ )
221 delete GetItem( i );
222
223 Clear();
224}
225
226
227const std::vector<VIEW_ITEM*> VIEW_GROUP::updateDrawList() const
228{
229 return m_groupItems;
230}
231
232
233/*void VIEW_GROUP::ItemsSetVisibility( bool aVisible )
234{
235 for(unsigned int i = 0 ; i < GetSize(); i++)
236 GetItem(i)->ViewSetVisible( aVisible );
237}
238
239
240void VIEW_GROUP::ItemsViewUpdate( VIEW_ITEM::VIEW_UPDATE_FLAGS aFlags )
241{
242 for(unsigned int i = 0 ; i < GetSize(); i++)
243 GetItem(i)->ViewUpdate( aFlags );
244}*/
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