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  * @author Maciej Suminski <maciej.suminski@cern.ch>
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, you may find one here:
19  * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
20  * or you may search the http://www.gnu.org website for the version 2 license,
21  * or you may write to the Free Software Foundation, Inc.,
22  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
23  *
24  */
25 
33 #include <set>
34 #include <algorithm>
35 #include <view/view_group.h>
36 #include <view/view.h>
37 #include <painter.h>
40 
41 using namespace KIGFX;
42 
44  VIEW_ITEM(),
45  m_layer( LAYER_SELECT_OVERLAY )
46 {
47 }
48 
49 
51 {
52  // VIEW_ITEM destructor removes the object from its parent view
53 }
54 
55 
56 void VIEW_GROUP::Add( VIEW_ITEM* aItem )
57 {
58  m_groupItems.push_back( aItem );
59 }
60 
61 
63 {
64  for( auto iter = m_groupItems.begin(); iter != m_groupItems.end(); ++iter )
65  {
66  if( aItem == *iter )
67  {
68  m_groupItems.erase( iter );
69  break;
70  }
71  }
72 }
73 
74 
76 {
77  m_groupItems.clear();
78 }
79 
80 
81 unsigned int VIEW_GROUP::GetSize() const
82 {
83  return m_groupItems.size();
84 }
85 
86 
87 VIEW_ITEM *VIEW_GROUP::GetItem( unsigned int idx ) const
88 {
89  return m_groupItems[idx];
90 }
91 
92 
94 {
95  BOX2I bb;
96 
97  if( !m_groupItems.size() )
98  {
99  bb.SetMaximum();
100  }
101  else
102  {
103  bb = m_groupItems[0]->ViewBBox();
104 
105  for( auto item : m_groupItems )
106  bb.Merge( item->ViewBBox() );
107  }
108 
109  return bb;
110 }
111 
112 
113 void VIEW_GROUP::ViewDraw( int aLayer, VIEW* aView ) const
114 {
115  KIGFX::GAL* gal = aView->GetGAL();
116  PAINTER* painter = aView->GetPainter();
117  bool isSelection = m_layer == LAYER_SELECT_OVERLAY;
118 
119  const std::vector<VIEW_ITEM*> drawList = updateDrawList();
120 
121  std::unordered_map<int, std::vector<VIEW_ITEM*>> layer_item_map;
122 
123  // Build a list of layers used by the items in the group
124  for( VIEW_ITEM* item : drawList )
125  {
126  int item_layers[VIEW::VIEW_MAX_LAYERS], item_layers_count;
127  item->ViewGetLayers( item_layers, item_layers_count );
128 
129  for( int i = 0; i < item_layers_count; i++ )
130  {
131  if( layer_item_map.count( item_layers[i] ) == 0 )
132  {
133  layer_item_map.emplace( std::make_pair( item_layers[i],
134  std::vector<VIEW_ITEM*>() ) );
135  }
136 
137  layer_item_map[ item_layers[i] ].push_back( item );
138  }
139  }
140 
141  int layers[VIEW::VIEW_MAX_LAYERS] = { 0 };
142  int layers_count = 0;
143 
144  for( const std::pair<const int, std::vector<VIEW_ITEM*>>& entry : layer_item_map )
145  layers[ layers_count++ ] = entry.first;
146 
147  aView->SortLayers( layers, layers_count );
148 
149  // Now draw the layers in sorted order
150 
151  gal->PushDepth();
152 
153  for( int i = 0; i < layers_count; i++ )
154  {
155  int layer = layers[i];
156  bool draw = aView->IsLayerVisible( layer );
157 
158  if( isSelection )
159  {
160  switch( layer )
161  {
162  case LAYER_PADS_TH:
164  case LAYER_PAD_HOLEWALLS:
165  case LAYER_PAD_FR:
166  case LAYER_PAD_BK:
167  draw = true;
168  break;
169  default:
170  break;
171  }
172  }
173 
174  if( draw )
175  {
176  gal->AdvanceDepth();
177 
178  for( VIEW_ITEM* item : layer_item_map[ layers[i] ] )
179  {
180  if( !painter->Draw( item, layers[i] ) )
181  item->ViewDraw( layers[i], aView ); // Alternative drawing method
182  }
183  }
184  }
185 
186  gal->PopDepth();
187 }
188 
189 
190 void VIEW_GROUP::ViewGetLayers( int aLayers[], int& aCount ) const
191 {
192  // Everything is displayed on a single layer
193  aLayers[0] = m_layer;
194  aCount = 1;
195 }
196 
197 
199 {
200  for( unsigned int i = 0 ; i < GetSize(); i++ )
201  delete GetItem( i );
202 
203  Clear();
204 }
205 
206 
208 {
209  return m_groupItems;
210 }
211 
212 
213 /*void VIEW_GROUP::ItemsSetVisibility( bool aVisible )
214 {
215  for(unsigned int i = 0 ; i < GetSize(); i++)
216  GetItem(i)->ViewSetVisible( aVisible );
217 }
218 
219 
220 void VIEW_GROUP::ItemsViewUpdate( VIEW_ITEM::VIEW_UPDATE_FLAGS aFlags )
221 {
222  for(unsigned int i = 0 ; i < GetSize(); i++)
223  GetItem(i)->ViewUpdate( aFlags );
224 }*/
The Cairo implementation of the graphics abstraction layer.
Definition: color4d.cpp:175
multilayer pads, usually with holes
GAL * GetGAL() const
Return the #GAL this view is using to draw graphical primitives.
Definition: view.h:189
virtual void Clear()
Remove all the stored items from the group.
Definition: view_group.cpp:75
An abstract base class for deriving all objects that can be added to a VIEW.
Definition: view_item.h:81
virtual VIEW_ITEM * GetItem(unsigned int aIdx) const
Definition: view_group.cpp:87
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:113
PAINTER * GetPainter() const
Return the painter object used by the view for drawing #VIEW_ITEMS.
Definition: view.h:207
static constexpr int VIEW_MAX_LAYERS
maximum number of layers that may be shown
Definition: view.h:700
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:653
virtual const ITEMS updateDrawList() const
Layer on which the group is drawn.
Definition: view_group.cpp:207
virtual unsigned int GetSize() const
Return the number of stored items.
Definition: view_group.cpp:81
virtual const BOX2I ViewBBox() const override
Return the bounding box for all stored items covering all its layers.
Definition: view_group.cpp:93
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:198
void PopDepth()
Restore previously stored drawing depth for the depth stack.
void SetMaximum()
Definition: box2.h:73
int m_layer
Container for storing VIEW_ITEMs.
Definition: view_group.h:124
virtual void Remove(VIEW_ITEM *aItem)
Remove an item from the group.
Definition: view_group.cpp:62
BOX2< Vec > & Merge(const BOX2< Vec > &aRect)
Function Merge modifies the position and size of the rectangle in order to contain aRect.
Definition: box2.h:386
virtual void Add(VIEW_ITEM *aItem)
Add an item to the group.
Definition: view_group.cpp:56
VIEW_GROUP(VIEW *aView=NULL)
Definition: view_group.cpp:43
virtual ~VIEW_GROUP()
Definition: view_group.cpp:50
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.
std::vector< VIEW_ITEM * > ITEMS
Definition: view_group.h:119
void PushDepth()
Store current drawing depth on the depth stack.
smd pads, front layer
Board layer functions and definitions.
virtual void ViewGetLayers(int aLayers[], int &aCount) const override
Return all the layers used by the stored items.
Definition: view_group.cpp:190
currently selected items overlay
Hold a (potentially large) number of VIEW_ITEMs and renders them on a graphics device provided by the...
Definition: view.h:67
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:404