view_group.cpp 3.87 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
/*
 * 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 <boost/foreach.hpp>
#include <layers_id_colors_and_visibility.h>

42
using namespace KIGFX;
43 44

VIEW_GROUP::VIEW_GROUP( VIEW* aView ) :
45
    m_layer( ITEM_GAL_LAYER( GP_OVERLAY ) )
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
{
    m_view = aView;
}


VIEW_GROUP::~VIEW_GROUP()
{
}


void VIEW_GROUP::Add( VIEW_ITEM* aItem )
{
    m_items.insert( aItem );
}


void VIEW_GROUP::Remove( VIEW_ITEM* aItem )
{
    m_items.erase( aItem );
}


void VIEW_GROUP::Clear()
{
    m_items.clear();
}


unsigned int VIEW_GROUP::GetSize() const
{
    return m_items.size();
}


const BOX2I VIEW_GROUP::ViewBBox() const
{
82
    BOX2I maxBox;
83

84 85
    maxBox.SetMaximum();
    return maxBox;
86 87 88
}


89
void VIEW_GROUP::ViewDraw( int aLayer, GAL* aGal ) const
90 91 92 93 94 95
{
    PAINTER* painter = m_view->GetPainter();

    // Draw all items immediately (without caching)
    BOOST_FOREACH( VIEW_ITEM* item, m_items )
    {
96 97
        aGal->PushDepth();

98 99 100 101 102 103
        int layers[VIEW::VIEW_MAX_LAYERS], layers_count;
        item->ViewGetLayers( layers, layers_count );
        m_view->SortLayers( layers, layers_count );

        for( int i = 0; i < layers_count; i++ )
        {
104 105
            if( m_view->IsCached( layers[i] ) && m_view->IsLayerVisible( layers[i] ) )
            {
106
                aGal->AdvanceDepth();
107

108
                if( !painter->Draw( item, layers[i] ) )
109
                    item->ViewDraw( layers[i], aGal ); // Alternative drawing method
110
            }
111
        }
112

113 114
        aGal->PopDepth();
    }
115 116 117 118 119
}


void VIEW_GROUP::ViewGetLayers( int aLayers[], int& aCount ) const
{
120
    // Everything is displayed on a single layer
121 122 123 124 125
    aLayers[0] = m_layer;
    aCount = 1;
}


126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
void VIEW_GROUP::FreeItems()
{
    BOOST_FOREACH( VIEW_ITEM* item, m_items )
    {
        delete item;
    }
    m_items.clear();
}


void VIEW_GROUP::ItemsSetVisibility( bool aVisible )
{
    std::set<VIEW_ITEM*>::const_iterator it, it_end;

    for( it = m_items.begin(), it_end = m_items.end(); it != it_end; ++it )
        (*it)->ViewSetVisible( aVisible );
}


Maciej Suminski's avatar
Maciej Suminski committed
145
void VIEW_GROUP::ItemsViewUpdate( VIEW_ITEM::VIEW_UPDATE_FLAGS aFlags )
146 147 148 149 150 151 152 153
{
    std::set<VIEW_ITEM*>::const_iterator it, it_end;

    for( it = m_items.begin(), it_end = m_items.end(); it != it_end; ++it )
        (*it)->ViewUpdate( aFlags );
}


154 155 156 157 158 159 160 161 162
void VIEW_GROUP::updateBbox()
{
    // Save the used VIEW, as it used nulled during Remove()
    VIEW* view = m_view;

    // Reinsert the group, so the bounding box can be updated
    view->Remove( this );
    view->Add( this );
}