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_GP_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 maxBox;
95 
96  maxBox.SetMaximum();
97  return maxBox;
98 }
99 
100 
101 void VIEW_GROUP::ViewDraw( int aLayer, VIEW* aView ) const
102 {
103  auto gal = aView->GetGAL();
104  PAINTER* painter = aView->GetPainter();
105 
106  const auto drawList = updateDrawList();
107 
108  std::unordered_map<int, std::vector<VIEW_ITEM*>> layer_item_map;
109 
110  // Build a list of layers used by the items in the group
111  for( auto item : drawList )
112  {
113  int item_layers[VIEW::VIEW_MAX_LAYERS], item_layers_count;
114  item->ViewGetLayers( item_layers, item_layers_count );
115 
116  for( int i = 0; i < item_layers_count; i++ )
117  {
118  if( layer_item_map.count( item_layers[i] ) == 0 )
119  {
120  layer_item_map.emplace( std::make_pair( item_layers[i],
121  std::vector<VIEW_ITEM*>() ) );
122  }
123 
124  layer_item_map[ item_layers[i] ].push_back( item );
125  }
126  }
127 
128  int layers[VIEW::VIEW_MAX_LAYERS], layers_count = 0;
129 
130  for( const auto& entry : layer_item_map )
131  {
132  layers[ layers_count++ ] = entry.first;
133  }
134 
135  aView->SortLayers( layers, layers_count );
136 
137  // Now draw the layers in sorted order
138 
139  gal->PushDepth();
140 
141  for( int i = 0; i < layers_count; i++ )
142  {
143  if( aView->IsLayerVisible( layers[i] ) )
144  {
145  gal->AdvanceDepth();
146 
147  for( auto item : layer_item_map[ layers[i] ] )
148  {
149  if( !painter->Draw( item, layers[i] ) )
150  item->ViewDraw( layers[i], aView ); // Alternative drawing method
151  }
152  }
153  }
154 
155  gal->PopDepth();
156 }
157 
158 
159 void VIEW_GROUP::ViewGetLayers( int aLayers[], int& aCount ) const
160 {
161  // Everything is displayed on a single layer
162  aLayers[0] = m_layer;
163  aCount = 1;
164 }
165 
166 
168 {
169  for( unsigned int i = 0 ; i < GetSize(); i++ )
170  delete GetItem( i );
171 
172  Clear();
173 }
174 
175 
177 {
178  return m_groupItems;
179 }
180 
181 
182 /*void VIEW_GROUP::ItemsSetVisibility( bool aVisible )
183 {
184  for(unsigned int i = 0 ; i < GetSize(); i++)
185  GetItem(i)->ViewSetVisible( aVisible );
186 }
187 
188 
189 void VIEW_GROUP::ItemsViewUpdate( VIEW_ITEM::VIEW_UPDATE_FLAGS aFlags )
190 {
191  for(unsigned int i = 0 ; i < GetSize(); i++)
192  GetItem(i)->ViewUpdate( aFlags );
193 }*/
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
bool IsLayerVisible(int aLayer) const
Function IsLayerVisible() Returns information about visibility of a particular layer.
Definition: view.h:404
virtual const ITEMS updateDrawList() const
Definition: view_group.cpp:176
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:101
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:626
Class PAINTER contains all the knowledge about how to draw graphical object onto any particular outpu...
Definition: painter.h:278
void FreeItems()
Function FreeItems() Frees all the items that were added to the group.
Definition: view_group.cpp:167
GAL * GetGAL() const
Function GetGAL() Returns the GAL this view is using to draw graphical primitives.
Definition: view.h:177
void SetMaximum()
Definition: box2.h:61
static const int VIEW_MAX_LAYERS
maximum number of layers that may be shown
Definition: view.h:675
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.
general purpose overlay
PAINTER * GetPainter() const
Function GetPainter() Returns the painter object used by the view for drawing VIEW_ITEMS.
Definition: view.h:196
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
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:159
size_t i
Definition: json11.cpp:597
Class VIEW.
Definition: view.h:58