2 Qanava - Graph drawing library for QT
3 Copyright (C) 2006 Benoit AUTHEMAN
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with this library; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 //-----------------------------------------------------------------------------
21 // This file is a part of the Qanava software.
24 // \author Benoit Autheman (benoit@libqanava.org)
25 // \date 2004 February 15
26 //-----------------------------------------------------------------------------
34 #include "./qanConfig.h"
50 //-----------------------------------------------------------------------------
51 namespace qan
{ // ::qan
56 //! Allow an inheritor classe to manage a set of attributes.
58 initAttributes() method must be call in the direct inheritor ctor if only the
59 default constructor has been used.
63 /*! \name Node Attributes Management *///--------------------------
67 AttrFunc( ) { initAttributes( 0 ); }
69 AttrFunc( unsigned int attributeCount
) { initAttributes( attributeCount
); }
71 template < typename T
>
72 void addAttribute( T t
)
74 _attributes
.push_back( new T( t
) );
77 template < typename T
>
78 T
* getAttribute( int role
)
80 assert( role
< _attributes
.size( ) );
81 return ( _attributes
[ role
] != 0 ? static_cast< T
* >( _attributes
[ role
] ) : 0 );
84 template < typename T
>
85 const T
* getAttribute( int role
) const
87 assert( role
< _attributes
.size( ) );
88 return ( _attributes
[ role
] != 0 ? static_cast< const T
* >( _attributes
[ role
] ) : 0 );
91 template < typename T
>
92 void setAttribute( int role
, const T
& t
)
94 assert( role
< _attributes
.size( ) );
95 if ( _attributes
[ role
] == 0 )
96 _attributes
[ role
] = new T( t
);
98 *static_cast< T
* >( _attributes
[ role
] ) = t
;
101 void initAttributes( unsigned int attributeCount
)
103 if ( _attributes
.size( ) < ( int )attributeCount
) // Add attributes to fit the requested attribute count
105 for ( unsigned int attribute
= _attributes
.size( ); attribute
< attributeCount
; attribute
++ )
106 _attributes
.push_back( 0 );
112 QList
< void* > _attributes
;
114 //-----------------------------------------------------------------
119 //! Model a weighted directed edge between two nodes.
123 class Edge
: public AttrFunc
133 /*! \name Generator Constructor/Destructor *///--------------------
137 //! Node constructor with source and destination initialization.
138 Edge( Node
& src
, Node
& dst
, float weight
= 1.f
) : AttrFunc( 2 ),
142 setAttribute( WEIGHT
, weight
);
143 setLabel( QString( "" ) );
146 //! Node constructor with source and destination initialization.
147 Edge( Node
* src
, Node
* dst
, float weight
= 1.f
) : AttrFunc( 2 ),
151 setAttribute( WEIGHT
, weight
);
152 setLabel( QString( "" ) );
155 //! Typedef for a QT list of Edge.
156 typedef QList
< Edge
* > List
;
158 //! Typedef for a QT set of Edge.
159 typedef QSet
< Edge
* > Set
;
161 //-----------------------------------------------------------------
165 /*! \name Source/Destination Management *///-----------------------
170 bool hasSrc( ) const { return ( _src
!= 0 ); }
172 //! Get edge destination.
173 bool hasDst( ) const { return ( _dst
!= 0 ); }
176 Node
& getSrc( ) { return *_src
; }
178 //! Get edge destination.
179 Node
& getDst( ) { return *_dst
; }
181 //! Get const reference on the edge source.
182 const Node
& getSrc( ) const { return *_src
; }
184 //! Get const reference on the edge destination.
185 const Node
& getDst( ) const { return *_dst
; }
187 //! Get edge's weight.
188 float getWeight( ) const { return *getAttribute
< float >( Edge::WEIGHT
); }
190 //! Set edge's weight.
191 void setWeight( float weight
) { setAttribute
< float >( Edge::WEIGHT
, weight
); }
193 //! Set edge source and destination (use this method carefully it is normally reserved for serialization implementation).
194 void set( Node
* src
, Node
* dst
);
196 //! Get this edge label.
197 const QString
& getLabel( ) const { return *getAttribute
< QString
>( Edge::LABEL
); }
199 //! Set this edge label.
200 void setLabel( const QString
& label
) { setAttribute
< QString
>( Edge::LABEL
, label
); }
207 //! Edge destination.
210 //-----------------------------------------------------------------
213 //-----------------------------------------------------------------------------