KiCad PCB EDA Suite
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 (C) 2021 KiCad Developers, see AUTHORS.txt for contributors.
6  * @author Maciej Suminski <maciej.suminski@cern.ch>
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 <painter.h>
40 #include <layer_ids.h>
41 
42 using namespace KIGFX;
43 
45  VIEW_ITEM(),
46  m_layer( LAYER_SELECT_OVERLAY )
47 {
48 }
49 
50 
52 {
53  // VIEW_ITEM destructor removes the object from its parent view
54 }
55 
56 
57 void VIEW_GROUP::Add( VIEW_ITEM* aItem )
58 {
59  m_groupItems.push_back( aItem );
60 }
61 
62 
64 {
66 }
67 
68 
70 {
71  m_groupItems.clear();
72 }
73 
74 
75 unsigned int VIEW_GROUP::GetSize() const
76 {
77  return m_groupItems.size();
78 }
79 
80 
81 VIEW_ITEM *VIEW_GROUP::GetItem( unsigned int idx ) const
82 {
83  return m_groupItems[idx];
84 }
85 
86 
88 {
89  BOX2I bb;
90 
91  if( !m_groupItems.size() )
92  {
93  bb.SetMaximum();
94  }
95  else
96  {
97  bb = m_groupItems[0]->ViewBBox();
98 
99  for( VIEW_ITEM* item : m_groupItems )
100  bb.Merge( item->ViewBBox() );
101  }
102 
103  return bb;
104 }
105 
106 
107 void VIEW_GROUP::ViewDraw( int aLayer, VIEW* aView ) const
108 {
109  KIGFX::GAL* gal = aView->GetGAL();
110  PAINTER* painter = aView->GetPainter();
111  bool isSelection = m_layer == LAYER_SELECT_OVERLAY;
112 
113  const std::vector<VIEW_ITEM*> drawList = updateDrawList();
114 
115  std::unordered_map<int, std::vector<VIEW_ITEM*>> layer_item_map;
116 
117  // Build a list of layers used by the items in the group
118  for( VIEW_ITEM* item : drawList )
119  {
120  int item_layers[VIEW::VIEW_MAX_LAYERS], item_layers_count;
121  item->ViewGetLayers( item_layers, item_layers_count );
122 
123  for( int i = 0; i < item_layers_count; i++ )
124  {
125  if( layer_item_map.count( item_layers[i] ) == 0 )
126  {
127  layer_item_map.emplace( std::make_pair( item_layers[i],
128  std::vector<VIEW_ITEM*>() ) );
129  }
130 
131  layer_item_map[ item_layers[i] ].push_back( item );
132  }
133  }
134 
135  int layers[VIEW::VIEW_MAX_LAYERS] = { 0 };
136  int layers_count = 0;
137 
138  for( const std::pair<const int, std::vector<VIEW_ITEM*>>& entry : layer_item_map )
139  layers[ layers_count++ ] = entry.first;
140 
141  aView->SortLayers( layers, layers_count );
142 
143  // Now draw the layers in sorted order
144 
145  gal->PushDepth();
146 
147  for( int i = 0; i < layers_count; i++ )
148  {
149  int layer = layers[i];
150  bool draw = aView->IsLayerVisible( layer );
151 
152  if( isSelection )
153  {
154  switch( layer )
155  {
156  case LAYER_PADS_TH:
158  case LAYER_PAD_HOLEWALLS:
159  case LAYER_PAD_FR:
160  case LAYER_PAD_BK:
161  draw = true;
162  break;
163  default:
164  break;
165  }
166  }
167 
168  if( draw )
169  {
170  gal->AdvanceDepth();
171 
172  for( VIEW_ITEM* item : layer_item_map[ layers[i] ] )
173  {
174  if( !painter->Draw( item, layers[i] ) )
175  item->ViewDraw( layers[i], aView ); // Alternative drawing method
176  }
177  }
178  }
179 
180  gal->PopDepth();
181 }
182 
183 
184 void VIEW_GROUP::ViewGetLayers( int aLayers[], int& aCount ) const
185 {
186  // Everything is displayed on a single layer
187  aLayers[0] = m_layer;
188  aCount = 1;
189 }
190 
191 
193 {
194  for( unsigned int i = 0 ; i < GetSize(); i++ )
195  delete GetItem( i );
196 
197  Clear();
198 }
199 
200 
201 const std::vector<VIEW_ITEM*> VIEW_GROUP::updateDrawList() const
202 {
203  return m_groupItems;
204 }
205 
206 
207 /*void VIEW_GROUP::ItemsSetVisibility( bool aVisible )
208 {
209  for(unsigned int i = 0 ; i < GetSize(); i++)
210  GetItem(i)->ViewSetVisible( aVisible );
211 }
212 
213 
214 void VIEW_GROUP::ItemsViewUpdate( VIEW_ITEM::VIEW_UPDATE_FLAGS aFlags )
215 {
216  for(unsigned int i = 0 ; i < GetSize(); i++)
217  GetItem(i)->ViewUpdate( aFlags );
218 }*/
currently selected items overlay
Definition: layer_ids.h:215
smd pads, front layer
Definition: layer_ids.h:198
The Cairo implementation of the graphics abstraction layer.
Definition: color4d.cpp:236
smd pads, back layer
Definition: layer_ids.h:199
GAL * GetGAL() const
Return the #GAL this view is using to draw graphical primitives.
Definition: view.h:190
VIEW_GROUP(VIEW *aView=nullptr)
Definition: view_group.cpp:44
virtual void Clear()
Remove all the stored items from the group.
Definition: view_group.cpp:69
An abstract base class for deriving all objects that can be added to a VIEW.
Definition: view_item.h:76
virtual VIEW_ITEM * GetItem(unsigned int aIdx) const
Definition: view_group.cpp:81
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:107
PAINTER * GetPainter() const
Return the painter object used by the view for drawing #VIEW_ITEMS.
Definition: view.h:208
static constexpr int VIEW_MAX_LAYERS
maximum number of layers that may be shown
Definition: view.h:711
void SortLayers(int aLayers[], int &aCount) const
Change the order of given layer ids, so after sorting the order corresponds to layers rendering order...
Definition: view.cpp:647
virtual unsigned int GetSize() const
Return the number of stored items.
Definition: view_group.cpp:75
virtual const BOX2I ViewBBox() const override
Return the bounding box for all stored items covering all its layers.
Definition: view_group.cpp:87
Contains all the knowledge about how to draw graphical object onto any particular output device.
Definition: painter.h:57
void FreeItems()
Free all the items that were added to the group.
Definition: view_group.cpp:192
void PopDepth()
Restore previously stored drawing depth for the depth stack.
void SetMaximum()
Definition: box2.h:57
virtual void Remove(VIEW_ITEM *aItem)
Remove an item from the group.
Definition: view_group.cpp:63
std::vector< VIEW_ITEM * > m_groupItems
Definition: view_group.h:113
BOX2< Vec > & Merge(const BOX2< Vec > &aRect)
Modify the position and size of the rectangle in order to contain aRect.
Definition: box2.h:363
virtual void Add(VIEW_ITEM *aItem)
Add an item to the group.
Definition: view_group.cpp:57
virtual const std::vector< VIEW_ITEM * > updateDrawList() const
Definition: view_group.cpp:201
virtual ~VIEW_GROUP()
Definition: view_group.cpp:51
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.
void PushDepth()
Store current drawing depth on the depth stack.
multilayer pads, usually with holes
Definition: layer_ids.h:209
virtual void ViewGetLayers(int aLayers[], int &aCount) const override
Return all the layers used by the stored items.
Definition: view_group.cpp:184
to draw pad holes (plated)
Definition: layer_ids.h:210
Hold a (potentially large) number of VIEW_ITEMs and renders them on a graphics device provided by the...
Definition: view.h:68
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:164
void AdvanceDepth()
Change the current depth to deeper, so it is possible to draw objects right beneath other.
Abstract interface for drawing on a 2D-surface.
bool IsLayerVisible(int aLayer) const
Return information about visibility of a particular layer.
Definition: view.h:405