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
33
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
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 else if( IsPointsLayer( layer ) )
178 {
179 draw = aView->IsLayerVisible( layer - LAYER_POINT_START );
180 }
181
182 if( isSelection )
183 {
184 switch( layer )
185 {
188 draw = true;
189 break;
190
191 default:
192 break;
193 }
194 }
195
196 if( draw )
197 {
198 gal->AdvanceDepth();
199
200 for( VIEW_ITEM* item : layer_item_map[ layer ] )
201 {
202 // Ignore LOD scale for selected items, but don't ignore things explicitly
203 // hidden.
204 if( item->ViewGetLOD( layer, aView ) == LOD_HIDE )
205 continue;
206
207 if( !painter->Draw( item, layer ) )
208 item->ViewDraw( layer, aView ); // Alternative drawing method
209 }
210 }
211 }
212}
213
214
215std::vector<int> VIEW_GROUP::ViewGetLayers() const
216{
217 // Everything is displayed on a single layer
218 return { m_layer };
219}
220
221
223{
224 for( unsigned int i = 0 ; i < GetSize(); i++ )
225 delete GetItem( i );
226
227 Clear();
228}
229
230
231const std::vector<VIEW_ITEM*> VIEW_GROUP::updateDrawList() const
232{
233 return m_groupItems;
234}
235
236
237/*void VIEW_GROUP::ItemsSetVisibility( bool aVisible )
238{
239 for(unsigned int i = 0 ; i < GetSize(); i++)
240 GetItem(i)->ViewSetVisible( aVisible );
241}
242
243
244void VIEW_GROUP::ItemsViewUpdate( VIEW_ITEM::VIEW_UPDATE_FLAGS aFlags )
245{
246 for(unsigned int i = 0 ; i < GetSize(); i++)
247 GetItem(i)->ViewUpdate( aFlags );
248}*/
BOX2< VECTOR2I > BOX2I
Definition box2.h:922
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.
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:111
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:88
friend class VIEW
Definition view_item.h:205
static constexpr double LOD_HIDE
Return this constant from ViewGetLOD() to hide the item unconditionally.
Definition view_item.h:180
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:877
bool IsPointsLayer(int aLayer)
Definition layer_ids.h:895
#define LAYER_ID_COUNT
Must update this if you add any enums after Gerbview!
Definition layer_ids.h:623
bool IsClearanceLayer(int aLayer)
Definition layer_ids.h:889
@ LAYER_PAD_COPPER_START
Virtual layers for pad copper on a given copper layer.
Definition layer_ids.h:336
@ 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:340
@ LAYER_CLEARANCE_START
Virtual layers for pad/via/track clearance outlines for a given copper layer.
Definition layer_ids.h:344
@ LAYER_ZONE_START
Virtual layers for stacking zones and tracks on a given copper layer.
Definition layer_ids.h:332
@ LAYER_POINT_START
Virtual layers for points per board layer.
Definition layer_ids.h:352
@ 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:883
bool IsZoneFillLayer(int aLayer)
Definition layer_ids.h:871
The Cairo implementation of the graphics abstraction layer.
Definition eda_group.h:33