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, you may find one here:
20 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
21 * or you may search the http://www.gnu.org website for the version 2 license,
22 * or you may write to the Free Software Foundation, Inc.,
23 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
24 *
25 */
26
34#include <set>
35#include <core/kicad_algo.h>
36#include <algorithm>
37#include <view/view_group.h>
38#include <view/view.h>
39#include <view/view_item.h>
40#include <gal/painter.h>
42#include <layer_ids.h>
43
44using namespace KIGFX;
45
47 VIEW_ITEM(),
48 m_layer( LAYER_SELECT_OVERLAY )
49{
50}
51
52
54{
55 // VIEW_ITEM destructor removes the object from its parent view
56}
57
58
59wxString VIEW_GROUP::GetClass() const
60{
61 return wxT( "VIEW_GROUP" );
62}
63
64
66{
67 m_groupItems.push_back( aItem );
68}
69
70
72{
73 std::erase( m_groupItems, aItem );
74}
75
76
78{
79 m_groupItems.clear();
80}
81
82
83unsigned int VIEW_GROUP::GetSize() const
84{
85 return m_groupItems.size();
86}
87
88
89VIEW_ITEM *VIEW_GROUP::GetItem( unsigned int idx ) const
90{
91 return m_groupItems[idx];
92}
93
94
96{
97 BOX2I bb;
98
99 if( !m_groupItems.size() )
100 {
101 bb.SetMaximum();
102 }
103 else
104 {
105 bb = m_groupItems[0]->ViewBBox();
106
107 for( VIEW_ITEM* item : m_groupItems )
108 bb.Merge( item->ViewBBox() );
109 }
110
111 return bb;
112}
113
114
115void VIEW_GROUP::ViewDraw( int aLayer, VIEW* aView ) const
116{
117 KIGFX::GAL* gal = aView->GetGAL();
118 PAINTER* painter = aView->GetPainter();
119 bool isSelection = m_layer == LAYER_SELECT_OVERLAY;
120
121 const std::vector<VIEW_ITEM*> drawList = updateDrawList();
122
123 std::map<int, std::vector<VIEW_ITEM*>> layer_item_map;
124
125 // Build a list of layers used by the items in the group
126 for( VIEW_ITEM* item : drawList )
127 {
128 if( aView->IsHiddenOnOverlay( item ) )
129 continue;
130
131 std::vector<int> layers = item->ViewGetLayers();
132
133 for( auto layer : layers )
134 {
135 wxCHECK2_MSG( layer <= LAYER_ID_COUNT, continue, wxT( "Invalid item layer" ) );
136 layer_item_map[ layer ].push_back( item );
137 }
138 }
139
140 if( layer_item_map.empty() )
141 return;
142
143 std::vector<int> layers;
144 layers.reserve( layer_item_map.size() );
145
146 for( const std::pair<const int, std::vector<VIEW_ITEM*>>& entry : layer_item_map )
147 layers.push_back( entry.first );
148
149 aView->SortLayers( layers );
150
151 // Now draw the layers in sorted order
152
154
155 for( int layer : layers )
156 {
157 bool draw = aView->IsLayerVisible( layer );
158
159 if( IsZoneFillLayer( layer ) )
160 {
161 // The visibility of solid areas must follow the visiblility of the zone layer
162 int zone_main_layer = layer - LAYER_ZONE_START;
163 draw = aView->IsLayerVisible( zone_main_layer );
164 }
165 else if( IsPadCopperLayer( layer ) )
166 {
167 draw = aView->IsLayerVisible( layer - LAYER_PAD_COPPER_START );
168 }
169 else if( IsViaCopperLayer( layer ) )
170 {
171 draw = aView->IsLayerVisible( layer - LAYER_VIA_COPPER_START );
172 }
173 else if( IsClearanceLayer( layer ) )
174 {
175 draw = aView->IsLayerVisible( layer - LAYER_CLEARANCE_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}*/
constexpr void SetMaximum()
Definition: box2.h:80
constexpr BOX2< Vec > & Merge(const BOX2< Vec > &aRect)
Modify the position and size of the rectangle in order to contain aRect.
Definition: box2.h:658
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:59
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.
Definition: view_group.cpp:77
virtual unsigned int GetSize() const
Return the number of stored items.
Definition: view_group.cpp:83
virtual void ViewDraw(int aLayer, VIEW *aView) const override
Draw all the stored items in the group on the given layer.
Definition: view_group.cpp:115
virtual VIEW_ITEM * GetItem(unsigned int aIdx) const
Definition: view_group.cpp:89
void FreeItems()
Free all the items that were added to the group.
Definition: view_group.cpp:218
std::vector< int > ViewGetLayers() const override
Return the all the layers within the VIEW the object is painted on.
Definition: view_group.cpp:211
std::vector< VIEW_ITEM * > m_groupItems
Definition: view_group.h:111
wxString GetClass() const override
Return the class name.
Definition: view_group.cpp:59
virtual void Remove(VIEW_ITEM *aItem)
Remove an item from the group.
Definition: view_group.cpp:71
VIEW_GROUP(VIEW *aView=nullptr)
Definition: view_group.cpp:46
virtual ~VIEW_GROUP()
Definition: view_group.cpp:53
virtual void Add(VIEW_ITEM *aItem)
Add an item to the group.
Definition: view_group.cpp:65
virtual const std::vector< VIEW_ITEM * > updateDrawList() const
Definition: view_group.cpp:227
virtual const BOX2I ViewBBox() const override
Return the bounding box for all stored items covering all its layers.
Definition: view_group.cpp:95
An abstract base class for deriving all objects that can be added to a VIEW.
Definition: view_item.h:86
static constexpr double LOD_HIDE
Return this constant from ViewGetLOD() to hide the item unconditionally.
Definition: view_item.h:180
Hold a (potentially large) number of VIEW_ITEMs and renders them on a graphics device provided by the...
Definition: view.h:66
GAL * GetGAL() const
Return the #GAL this view is using to draw graphical primitives.
Definition: view.h:202
bool IsHiddenOnOverlay(const VIEW_ITEM *aItem) const
Definition: view.cpp:1663
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:670
bool IsLayerVisible(int aLayer) const
Return information about visibility of a particular layer.
Definition: view.h:422
PAINTER * GetPainter() const
Return the painter object used by the view for drawing #VIEW_ITEMS.
Definition: view.h:220
bool IsPadCopperLayer(int aLayer)
Definition: layer_ids.h:868
#define LAYER_ID_COUNT
Must update this if you add any enums after Gerbview!
Definition: layer_ids.h:614
bool IsClearanceLayer(int aLayer)
Definition: layer_ids.h:880
@ LAYER_PAD_COPPER_START
Virtual layers for pad copper on a given copper layer.
Definition: layer_ids.h:332
@ LAYER_PAD_PLATEDHOLES
to draw pad holes (plated)
Definition: layer_ids.h:270
@ LAYER_VIA_COPPER_START
Virtual layers for via copper on a given copper layer.
Definition: layer_ids.h:336
@ LAYER_CLEARANCE_START
Virtual layers for pad/via/track clearance outlines for a given copper layer.
Definition: layer_ids.h:340
@ LAYER_ZONE_START
Virtual layers for stacking zones and tracks on a given copper layer.
Definition: layer_ids.h:328
@ LAYER_SELECT_OVERLAY
Selected items overlay.
Definition: layer_ids.h:279
@ LAYER_PAD_HOLEWALLS
Definition: layer_ids.h:296
bool IsViaCopperLayer(int aLayer)
Definition: layer_ids.h:874
bool IsZoneFillLayer(int aLayer)
Definition: layer_ids.h:862
The Cairo implementation of the graphics abstraction layer.
Definition: eda_group.h:33