kicad/common/view/view_group.cpp

224 lines
5.3 KiB
C++
Raw Normal View History

/*
* This program source code file is part of KiCad, a free EDA CAD application.
*
* Copyright (C) 2013 CERN
* @author Maciej Suminski <maciej.suminski@cern.ch>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, you may find one here:
* http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
* or you may search the http://www.gnu.org website for the version 2 license,
* or you may write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
*
*/
/**
* @file view_group.cpp
* @brief VIEW_GROUP extends VIEW_ITEM by possibility of grouping items into a single object.
* VIEW_GROUP does not take over ownership of the held items. The main purpose of this class is
* to group items and draw them on a single layer (in particular the overlay).
*/
#include <set>
#include <algorithm>
#include <view/view_group.h>
#include <view/view.h>
#include <painter.h>
#include <gal/graphics_abstraction_layer.h>
#include <layers_id_colors_and_visibility.h>
using namespace KIGFX;
VIEW_GROUP::VIEW_GROUP( VIEW* aView ) :
2020-12-29 17:08:23 +00:00
VIEW_ITEM(),
m_layer( LAYER_SELECT_OVERLAY )
{
}
VIEW_GROUP::~VIEW_GROUP()
{
// VIEW_ITEM destructor removes the object from its parent view
}
void VIEW_GROUP::Add( VIEW_ITEM* aItem )
{
m_groupItems.push_back( aItem );
}
void VIEW_GROUP::Remove( VIEW_ITEM* aItem )
{
2017-04-20 09:44:58 +00:00
for( auto iter = m_groupItems.begin(); iter != m_groupItems.end(); ++iter )
{
2016-12-09 11:04:32 +00:00
if( aItem == *iter )
{
2017-04-20 09:44:58 +00:00
m_groupItems.erase( iter );
break;
}
}
}
void VIEW_GROUP::Clear()
{
m_groupItems.clear();
}
unsigned int VIEW_GROUP::GetSize() const
{
return m_groupItems.size();
}
2017-04-20 09:44:58 +00:00
VIEW_ITEM *VIEW_GROUP::GetItem( unsigned int idx ) const
{
return m_groupItems[idx];
}
2017-04-20 09:44:58 +00:00
const BOX2I VIEW_GROUP::ViewBBox() const
{
BOX2I bb;
if( !m_groupItems.size() )
{
bb.SetMaximum();
}
else
{
bb = m_groupItems[0]->ViewBBox();
for( auto item : m_groupItems )
bb.Merge( item->ViewBBox() );
}
return bb;
}
void VIEW_GROUP::ViewDraw( int aLayer, VIEW* aView ) const
{
KIGFX::GAL* gal = aView->GetGAL();
PAINTER* painter = aView->GetPainter();
bool isSelection = m_layer == LAYER_SELECT_OVERLAY;
const std::vector<VIEW_ITEM*> drawList = updateDrawList();
std::unordered_map<int, std::vector<VIEW_ITEM*>> layer_item_map;
// Build a list of layers used by the items in the group
for( VIEW_ITEM* item : drawList )
{
int item_layers[VIEW::VIEW_MAX_LAYERS], item_layers_count;
item->ViewGetLayers( item_layers, item_layers_count );
for( int i = 0; i < item_layers_count; i++ )
{
if( layer_item_map.count( item_layers[i] ) == 0 )
2013-09-06 09:31:16 +00:00
{
layer_item_map.emplace( std::make_pair( item_layers[i],
std::vector<VIEW_ITEM*>() ) );
}
layer_item_map[ item_layers[i] ].push_back( item );
}
}
int layers[VIEW::VIEW_MAX_LAYERS] = { 0 };
int layers_count = 0;
2020-12-29 10:42:42 +00:00
for( const std::pair<const int, std::vector<VIEW_ITEM*>>& entry : layer_item_map )
layers[ layers_count++ ] = entry.first;
aView->SortLayers( layers, layers_count );
// Now draw the layers in sorted order
gal->PushDepth();
for( int i = 0; i < layers_count; i++ )
{
int layer = layers[i];
bool draw = aView->IsLayerVisible( layer );
if( isSelection )
{
switch( layer )
{
case LAYER_PADS_TH:
case LAYER_PADS_PLATEDHOLES:
case LAYER_PAD_FR:
case LAYER_PAD_BK:
draw = true;
break;
default:
break;
}
}
if( draw )
{
gal->AdvanceDepth();
2020-12-29 10:42:42 +00:00
for( VIEW_ITEM* item : layer_item_map[ layers[i] ] )
{
2013-09-06 09:31:16 +00:00
if( !painter->Draw( item, layers[i] ) )
item->ViewDraw( layers[i], aView ); // Alternative drawing method
2013-09-06 09:31:16 +00:00
}
}
}
gal->PopDepth();
}
void VIEW_GROUP::ViewGetLayers( int aLayers[], int& aCount ) const
{
2013-09-06 09:31:16 +00:00
// Everything is displayed on a single layer
aLayers[0] = m_layer;
aCount = 1;
}
void VIEW_GROUP::FreeItems()
{
for( unsigned int i = 0 ; i < GetSize(); i++ )
delete GetItem( i );
Clear();
}
2016-12-09 11:04:32 +00:00
const VIEW_GROUP::ITEMS VIEW_GROUP::updateDrawList() const
{
return m_groupItems;
}
2016-12-09 11:04:32 +00:00
/*void VIEW_GROUP::ItemsSetVisibility( bool aVisible )
{
2016-12-09 11:04:32 +00:00
for(unsigned int i = 0 ; i < GetSize(); i++)
GetItem(i)->ViewSetVisible( aVisible );
}
2013-10-14 18:40:36 +00:00
void VIEW_GROUP::ItemsViewUpdate( VIEW_ITEM::VIEW_UPDATE_FLAGS aFlags )
{
2016-12-09 11:04:32 +00:00
for(unsigned int i = 0 ; i < GetSize(); i++)
GetItem(i)->ViewUpdate( aFlags );
}*/