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 <view/view_group.h>
37#include <view/view.h>
38#include <view/view_item.h>
39#include <gal/painter.h>
41#include <layer_ids.h>
42
43using namespace KIGFX;
44
46 VIEW_ITEM(),
47 m_layer( LAYER_SELECT_OVERLAY )
48{
49}
50
51
53{
54 // VIEW_ITEM destructor removes the object from its parent view
55}
56
57
59{
60 m_groupItems.push_back( aItem );
61}
62
63
65{
67}
68
69
71{
72 m_groupItems.clear();
73}
74
75
76unsigned int VIEW_GROUP::GetSize() const
77{
78 return m_groupItems.size();
79}
80
81
82VIEW_ITEM *VIEW_GROUP::GetItem( unsigned int idx ) const
83{
84 return m_groupItems[idx];
85}
86
87
89{
90 BOX2I bb;
91
92 if( !m_groupItems.size() )
93 {
94 bb.SetMaximum();
95 }
96 else
97 {
98 bb = m_groupItems[0]->ViewBBox();
99
100 for( VIEW_ITEM* item : m_groupItems )
101 bb.Merge( item->ViewBBox() );
102 }
103
104 return bb;
105}
106
107
108void VIEW_GROUP::ViewDraw( int aLayer, VIEW* aView ) const
109{
110 KIGFX::GAL* gal = aView->GetGAL();
111 PAINTER* painter = aView->GetPainter();
112 bool isSelection = m_layer == LAYER_SELECT_OVERLAY;
113
114 const std::vector<VIEW_ITEM*> drawList = updateDrawList();
115
116 std::map<int, std::vector<VIEW_ITEM*>> layer_item_map;
117
118 // Build a list of layers used by the items in the group
119 for( VIEW_ITEM* item : drawList )
120 {
121 if( aView->IsHiddenOnOverlay( item ) )
122 continue;
123
124 std::vector<int> layers = item->ViewGetLayers();
125
126 for( auto layer : layers )
127 {
128 wxCHECK2_MSG( layer <= LAYER_ID_COUNT, continue, wxT( "Invalid item layer" ) );
129 layer_item_map[ layer ].push_back( item );
130 }
131 }
132
133 if( layer_item_map.empty() )
134 return;
135
136 std::vector<int> layers;
137 layers.reserve( layer_item_map.size() );
138
139 for( const std::pair<const int, std::vector<VIEW_ITEM*>>& entry : layer_item_map )
140 layers.push_back( entry.first );
141
142 aView->SortLayers( layers );
143
144 // Now draw the layers in sorted order
145
147
148 for( int layer : layers )
149 {
150 bool draw = aView->IsLayerVisible( layer );
151
152 if( IsZoneFillLayer( layer ) )
153 {
154 // The visibility of solid areas must follow the visiblility of the zone layer
155 int zone_main_layer = layer - LAYER_ZONE_START;
156 draw = aView->IsLayerVisible( zone_main_layer );
157 }
158 else if( IsPadCopperLayer( layer ) )
159 {
160 draw = aView->IsLayerVisible( layer - LAYER_PAD_COPPER_START );
161 }
162 else if( IsViaCopperLayer( layer ) )
163 {
164 draw = aView->IsLayerVisible( layer - LAYER_VIA_COPPER_START );
165 }
166 else if( IsClearanceLayer( layer ) )
167 {
168 draw = aView->IsLayerVisible( layer - LAYER_CLEARANCE_START );
169 }
170
171 if( isSelection )
172 {
173 switch( layer )
174 {
177 draw = true;
178 break;
179
180 default:
181 break;
182 }
183 }
184
185 if( draw )
186 {
187 gal->AdvanceDepth();
188
189 for( VIEW_ITEM* item : layer_item_map[ layer ] )
190 {
191 // Ignore LOD scale for selected items, but don't ignore things explicitly
192 // hidden.
193 if( item->ViewGetLOD( layer, aView ) == LOD_HIDE )
194 continue;
195
196 if( !painter->Draw( item, layer ) )
197 item->ViewDraw( layer, aView ); // Alternative drawing method
198 }
199 }
200 }
201}
202
203
204std::vector<int> VIEW_GROUP::ViewGetLayers() const
205{
206 // Everything is displayed on a single layer
207 return { m_layer };
208}
209
210
212{
213 for( unsigned int i = 0 ; i < GetSize(); i++ )
214 delete GetItem( i );
215
216 Clear();
217}
218
219
220const std::vector<VIEW_ITEM*> VIEW_GROUP::updateDrawList() const
221{
222 return m_groupItems;
223}
224
225
226/*void VIEW_GROUP::ItemsSetVisibility( bool aVisible )
227{
228 for(unsigned int i = 0 ; i < GetSize(); i++)
229 GetItem(i)->ViewSetVisible( aVisible );
230}
231
232
233void VIEW_GROUP::ItemsViewUpdate( VIEW_ITEM::VIEW_UPDATE_FLAGS aFlags )
234{
235 for(unsigned int i = 0 ; i < GetSize(); i++)
236 GetItem(i)->ViewUpdate( aFlags );
237}*/
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:70
virtual unsigned int GetSize() const
Return the number of stored items.
Definition: view_group.cpp:76
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:108
virtual VIEW_ITEM * GetItem(unsigned int aIdx) const
Definition: view_group.cpp:82
void FreeItems()
Free all the items that were added to the group.
Definition: view_group.cpp:211
std::vector< int > ViewGetLayers() const override
Return the all the layers within the VIEW the object is painted on.
Definition: view_group.cpp:204
std::vector< VIEW_ITEM * > m_groupItems
Definition: view_group.h:109
virtual void Remove(VIEW_ITEM *aItem)
Remove an item from the group.
Definition: view_group.cpp:64
VIEW_GROUP(VIEW *aView=nullptr)
Definition: view_group.cpp:45
virtual ~VIEW_GROUP()
Definition: view_group.cpp:52
virtual void Add(VIEW_ITEM *aItem)
Add an item to the group.
Definition: view_group.cpp:58
virtual const std::vector< VIEW_ITEM * > updateDrawList() const
Definition: view_group.cpp:220
virtual const BOX2I ViewBBox() const override
Return the bounding box for all stored items covering all its layers.
Definition: view_group.cpp:88
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:174
Hold a (potentially large) number of VIEW_ITEMs and renders them on a graphics device provided by the...
Definition: view.h:67
GAL * GetGAL() const
Return the #GAL this view is using to draw graphical primitives.
Definition: view.h:198
bool IsHiddenOnOverlay(const VIEW_ITEM *aItem) const
Definition: view.cpp:1651
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:660
bool IsLayerVisible(int aLayer) const
Return information about visibility of a particular layer.
Definition: view.h:418
PAINTER * GetPainter() const
Return the painter object used by the view for drawing #VIEW_ITEMS.
Definition: view.h:216
bool IsPadCopperLayer(int aLayer)
Definition: layer_ids.h:783
#define LAYER_ID_COUNT
Must update this if you add any enums after Gerbview!
Definition: layer_ids.h:530
bool IsClearanceLayer(int aLayer)
Definition: layer_ids.h:795
@ LAYER_PAD_COPPER_START
Virtual layers for pad copper on a given copper layer.
Definition: layer_ids.h:294
@ LAYER_PAD_PLATEDHOLES
to draw pad holes (plated)
Definition: layer_ids.h:233
@ LAYER_VIA_COPPER_START
Virtual layers for via copper on a given copper layer.
Definition: layer_ids.h:298
@ LAYER_CLEARANCE_START
Virtual layers for pad/via/track clearance outlines for a given copper layer.
Definition: layer_ids.h:302
@ LAYER_ZONE_START
Virtual layers for stacking zones and tracks on a given copper layer.
Definition: layer_ids.h:290
@ LAYER_SELECT_OVERLAY
Selected items overlay.
Definition: layer_ids.h:242
@ LAYER_PAD_HOLEWALLS
Definition: layer_ids.h:259
bool IsViaCopperLayer(int aLayer)
Definition: layer_ids.h:789
bool IsZoneFillLayer(int aLayer)
Definition: layer_ids.h:777
The Cairo implementation of the graphics abstraction layer.
Definition: color4d.cpp:247
void delete_matching(_Container &__c, _Value __value)
Covers for the horrifically named std::remove and std::remove_if (neither of which remove anything).
Definition: kicad_algo.h:165