0.3.1
[qanava.git] / src / qanEdge.h
blob1b2ca908dc93898ed20c13250c40eb2f2cefa8be
1 /*
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.
23 // \file laEdge.cpp
24 // \author Benoit Autheman (benoit@libqanava.org)
25 // \date 2004 February 15
26 //-----------------------------------------------------------------------------
29 #ifndef laEdge_h
30 #define laEdge_h
33 // Qanava headers
34 #include "./qanConfig.h"
37 // Standard headers
38 #include <string>
39 #include <list>
40 #include <set>
41 #include <cassert>
44 // QT headers
45 #include <QString>
46 #include <QList>
47 #include <QSet>
50 //-----------------------------------------------------------------------------
51 namespace qan { // ::qan
53 class Node;
56 //! Allow an inheritor classe to manage a set of attributes.
57 /*!
58 initAttributes() method must be call in the direct inheritor ctor if only the
59 default constructor has been used.
61 class AttrFunc
63 /*! \name Node Attributes Management *///--------------------------
64 //@{
65 public:
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 );
97 else
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 );
110 private:
112 QList< void* > _attributes;
113 //@}
114 //-----------------------------------------------------------------
119 //! Model a weighted directed edge between two nodes.
121 \nosubgrouping
123 class Edge : public AttrFunc
125 //! Attribute role.
126 enum Role
128 WEIGHT = 0,
129 LABEL = 1,
130 USER = 2
133 /*! \name Generator Constructor/Destructor *///--------------------
134 //@{
135 public:
137 //! Node constructor with source and destination initialization.
138 Edge( Node& src, Node& dst, float weight = 1.f ) : AttrFunc( 2 ),
139 _src( &src ),
140 _dst( &dst )
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 ),
148 _src( src ),
149 _dst( dst )
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;
160 //@}
161 //-----------------------------------------------------------------
165 /*! \name Source/Destination Management *///-----------------------
166 //@{
167 public:
169 //! Get edge source.
170 bool hasSrc( ) const { return ( _src != 0 ); }
172 //! Get edge destination.
173 bool hasDst( ) const { return ( _dst != 0 ); }
175 //! Get edge source.
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 ); }
202 private:
204 //! Edge source.
205 Node* _src;
207 //! Edge destination.
208 Node* _dst;
209 //@}
210 //-----------------------------------------------------------------
212 } // ::qan
213 //-----------------------------------------------------------------------------
216 #endif // laEdge_h