WNetworkLayoutNode.cpp 3.28 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
//---------------------------------------------------------------------------
//
// Project: OpenWalnut ( http://www.openwalnut.org )
//
// Copyright 2009 OpenWalnut Community, BSV@Uni-Leipzig and CNCF@MPI-CBS
// For more information see http://www.openwalnut.org/copying
//
// This file is part of OpenWalnut.
//
// OpenWalnut is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// OpenWalnut 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 Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with OpenWalnut. If not, see <http://www.gnu.org/licenses/>.
//
//---------------------------------------------------------------------------

25
#include <list>
26

27
#include "WNetworkLayoutGlobals.h"
28 29 30 31 32
#include "WNetworkLayoutNode.h"

WNetworkLayoutNode::WNetworkLayoutNode( WQtNetworkItem *item )
{
    m_referencedItem = item;
33 34
    if( item != NULL )
    {
35
        m_referencedItem->m_layoutNode = this;
36
        m_referencedItem->setPos( QPointF( 0, 0 ) );
37
    }
38 39
}

40
WNetworkLayoutNode::~WNetworkLayoutNode()
41
{
42 43 44 45 46 47
    if( m_referencedItem != NULL )
    {
        m_referencedItem->m_layoutNode = NULL;
        for( std::list< WNetworkLayoutNode * >::iterator iter = m_parents.begin();
                iter != m_parents.end(); ++iter )
        {
48
            ( *iter )->remove( this );
49 50
        }
    }
51 52 53 54
}

void WNetworkLayoutNode::add( WNetworkLayoutNode *node )
{
55 56 57 58 59 60 61
    m_children.push_back( node );
    node->addParent( this );
}

void WNetworkLayoutNode::addParent( WNetworkLayoutNode *node )
{
    m_parents.push_back( node );
62 63
}

64
std::list< WNetworkLayoutNode* > WNetworkLayoutNode::getChildren()
65
{
66
    return m_children;
67 68
}

Robert Frohl's avatar
Robert Frohl committed
69
QPointF WNetworkLayoutNode::getGridPos()
70 71 72 73
{
    return m_gridPos;
}

74 75 76 77 78 79 80 81 82 83
int WNetworkLayoutNode::nChildren()
{
    return m_children.size();
}

int WNetworkLayoutNode::nParents()
{
    return m_parents.size();
}

Robert Frohl's avatar
Robert Frohl committed
84
void WNetworkLayoutNode::remove( WNetworkLayoutNode *node )
85 86
{
    std::list< WNetworkLayoutNode * >::iterator iter;
87 88
    iter = std::find( m_children.begin(), m_children.end(), node );
    if( iter != m_children.end() )
89
    {
90
        m_children.erase( iter );
91
    }
92 93 94
    else
    {
        iter = std::find( m_parents.begin(), m_parents.end(), node );
95
        if( iter != m_parents.end() )
96 97 98 99
        {
            m_parents.erase( iter );
        }
    }
100 101
}

102
void WNetworkLayoutNode::setGridPos( QPoint pos )
103
{
104 105
    if( m_referencedItem != NULL )
        // if this is a dummy node do nothing
106
    {
107 108 109
        m_gridPos = pos;
        // calc positon for m_item
        QPointF newPos;
110
        newPos.setX( ( pos.x() * WNETWORKLAYOUT_GRID_DISTANCE_X ) +
111
                0.5 * ( WNETWORKLAYOUT_GRID_DISTANCE_X - m_referencedItem->m_width ) );
112
        newPos.setY( pos.y() * WNETWORKLAYOUT_GRID_DISTANCE_Y );
113 114 115 116 117 118 119
        m_referencedItem->setPos( newPos );

        // dirty hack: update arrow position
        foreach( WQtNetworkPort *port, m_referencedItem->m_inPorts )
        {
            port->updateArrows();
        }
120 121 122
    }
}