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  m_layer( LAYER_SELECT_OVERLAY )
45 {
46 }
47 
48 
50 {
51  // VIEW_ITEM destructor removes the object from its parent view
52 }
53 
54 
55 void VIEW_GROUP::Add( VIEW_ITEM* aItem )
56 {
57  m_groupItems.push_back( aItem );
58 }
59 
60 
62 {
63  for( auto iter = m_groupItems.begin(); iter != m_groupItems.end(); ++iter )
64  {
65  if( aItem == *iter )
66  {
67  m_groupItems.erase( iter );
68  break;
69  }
70  }
71 }
72 
73 
75 {
76  m_groupItems.clear();
77 }
78 
79 
80 unsigned int VIEW_GROUP::GetSize() const
81 {
82  return m_groupItems.size();
83 }
84 
85 
86 VIEW_ITEM *VIEW_GROUP::GetItem( unsigned int idx ) const
87 {
88  return m_groupItems[idx];
89 }
90 
91 
93 {
94  BOX2I bb;
95 
96  if( !m_groupItems.size() )
97  {
98  bb.SetMaximum();
99  }
100  else
101  {
102  bb = m_groupItems[0]->ViewBBox();
103 
104  for( auto item : m_groupItems )
105  bb.Merge( item->ViewBBox() );
106  }
107 
108  return bb;
109 }
110 
111 
112 void VIEW_GROUP::ViewDraw( int aLayer, VIEW* aView ) const
113 {
114  KIGFX::GAL* gal = aView->GetGAL();
115  PAINTER* painter = aView->GetPainter();
116  bool isSelection = m_layer == LAYER_SELECT_OVERLAY;
117  const auto drawList = updateDrawList();
118 
119  std::unordered_map<int, std::vector<VIEW_ITEM*>> layer_item_map;
120 
121  // Build a list of layers used by the items in the group
122  for( auto item : drawList )
123  {
124  int item_layers[VIEW::VIEW_MAX_LAYERS], item_layers_count;
125  item->ViewGetLayers( item_layers, item_layers_count );
126 
127  for( int i = 0; i < item_layers_count; i++ )
128  {
129  if( layer_item_map.count( item_layers[i] ) == 0 )
130  {
131  layer_item_map.emplace( std::make_pair( item_layers[i],
132  std::vector<VIEW_ITEM*>() ) );
133  }
134 
135  layer_item_map[ item_layers[i] ].push_back( item );
136  }
137  }
138 
139  int layers[VIEW::VIEW_MAX_LAYERS] = { 0 };
140  int layers_count = 0;
141 
142  for( const auto& entry : layer_item_map )
143  {
144  layers[ layers_count++ ] = entry.first;
145  }
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_FR:
165  case LAYER_PAD_BK:
166  draw = true;
167  break;
168  default:
169  break;
170  }
171  }
172 
173  if( draw )
174  {
175  gal->AdvanceDepth();
176 
177  for( auto item : layer_item_map[ layers[i] ] )
178  {
179  if( !painter->Draw( item, layers[i] ) )
180  item->ViewDraw( layers[i], aView ); // Alternative drawing method
181  }
182  }
183  }
184 
185  gal->PopDepth();
186 }
187 
188 
189 void VIEW_GROUP::ViewGetLayers( int aLayers[], int& aCount ) const
190 {
191  // Everything is displayed on a single layer
192  aLayers[0] = m_layer;
193  aCount = 1;
194 }
195 
196 
198 {
199  for( unsigned int i = 0 ; i < GetSize(); i++ )
200  delete GetItem( i );
201 
202  Clear();
203 }
204 
205 
207 {
208  return m_groupItems;
209 }
210 
211 
212 /*void VIEW_GROUP::ItemsSetVisibility( bool aVisible )
213 {
214  for(unsigned int i = 0 ; i < GetSize(); i++)
215  GetItem(i)->ViewSetVisible( aVisible );
216 }
217 
218 
219 void VIEW_GROUP::ItemsViewUpdate( VIEW_ITEM::VIEW_UPDATE_FLAGS aFlags )
220 {
221  for(unsigned int i = 0 ; i < GetSize(); i++)
222  GetItem(i)->ViewUpdate( aFlags );
223 }*/
Class CAIRO_GAL is the cairo implementation of the graphics abstraction layer.
Definition: class_module.h:58
virtual VIEW_ITEM * GetItem(unsigned int aIdx) const
Definition: view_group.cpp:86
multilayer pads, usually with holes
bool IsLayerVisible(int aLayer) const
Function IsLayerVisible() Returns information about visibility of a particular layer.
Definition: view.h:427
virtual const ITEMS updateDrawList() const
Definition: view_group.cpp:206
virtual void Clear()
Function Clear() Removes all the stored items from the group.
Definition: view_group.cpp:74
Class VIEW_ITEM - is an abstract base class for deriving all objects that can be added to a VIEW...
Definition: view_item.h:84
virtual void ViewDraw(int aLayer, VIEW *aView) const override
Function ViewDraw() Draws all the stored items in the group on the given layer.
Definition: view_group.cpp:112
smd pads, back layer
static constexpr int VIEW_MAX_LAYERS
maximum number of layers that may be shown
Definition: view.h:699
virtual const BOX2I ViewBBox() const override
Function ViewBBox() Returns the bounding box for all stored items covering all its layers...
Definition: view_group.cpp:92
void SortLayers(int aLayers[], int &aCount) const
Function SortLayers() Changes the order of given layer ids, so after sorting the order corresponds to...
Definition: view.cpp:666
Class PAINTER contains all the knowledge about how to draw graphical object onto any particular outpu...
Definition: painter.h:308
void FreeItems()
Function FreeItems() Frees all the items that were added to the group.
Definition: view_group.cpp:197
void PopDepth()
Restores previously stored drawing depth for the depth stack.
GAL * GetGAL() const
Function GetGAL() Returns the GAL this view is using to draw graphical primitives.
Definition: view.h:180
void SetMaximum()
Definition: box2.h:71
int m_layer
Layer on which the group is drawn.
Definition: view_group.h:131
virtual void Remove(VIEW_ITEM *aItem)
Function Remove() Removes an item from the group.
Definition: view_group.cpp:61
VIEW_GROUP extends VIEW_ITEM by possibility of grouping items into a single object.
PAINTER * GetPainter() const
Function GetPainter() Returns the painter object used by the view for drawing VIEW_ITEMS.
Definition: view.h:199
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:384
virtual void Add(VIEW_ITEM *aItem)
Function Add() Adds an item to the group.
Definition: view_group.cpp:55
VIEW_GROUP(VIEW *aView=NULL)
Definition: view_group.cpp:43
virtual unsigned int GetSize() const
Function GetSize() Returns the number of stored items.
Definition: view_group.cpp:80
virtual ~VIEW_GROUP()
Definition: view_group.cpp:49
virtual bool Draw(const VIEW_ITEM *aItem, int aLayer)=0
Function Draw Takes an instance of VIEW_ITEM and passes it to a function that know how to draw the it...
std::vector< VIEW_ITEM * > ITEMS
Definition: view_group.h:45
void PushDepth()
Stores current drawing depth on the depth stack.
smd pads, front layer
Board layer functions and definitions.
ITEMS m_groupItems
Container for storing VIEW_ITEMs.
Definition: view_group.h:135
virtual void ViewGetLayers(int aLayers[], int &aCount) const override
Function ViewGetLayers() Returns all the layers used by the stored items.
Definition: view_group.cpp:189
size_t i
Definition: json11.cpp:597
currently selected items overlay
Class VIEW.
Definition: view.h:61
void AdvanceDepth()
Changes the current depth to deeper, so it is possible to draw objects right beneath other...
Class GAL is the abstract interface for drawing on a 2D-surface.