2 Qanava - Graph drawing library for QT
3 Copyright (C) 2005 Benoit AUTHEMAN
5 This program is free software; you can redistribute it and/or
6 modify it under the terms of the GNU General Public License
7 as published by the Free Software Foundation; either version 2
8 of the License, or (at your option) any later version.
10 This program 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
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; 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 Qarte software.
24 // \author Benoit Autheman (benoit@faktiss.net)
25 // \date 2005 January 03
26 //-----------------------------------------------------------------------------
29 #ifndef qan_can_Style_h
30 #define qan_can_Style_h
34 #include "../utl/utlManager.h"
42 //-----------------------------------------------------------------------------
43 namespace qan
// ::qan
45 namespace can
// ::qan::can
47 //! Specify graphic and other attributes for a specific primitive (usually a Canvas Item).
55 //! Manage styles for a set of objects (usually Canvas Item items).
57 Definition of styles for la::Node objects:
59 // na, nb and nc are la::Node objects registered in a la::Graph object called graph
60 can::AdvStyle::Manager styleManager;
62 can::AdvStyle* blueNode = new can::AdvStyle( "bluenode" );
63 blueNode->addImage( "backimage", "images/gradblue.png" );
64 blueNode->addColor( "bordercolor", 0, 0, 0 );
65 styleManager.setStyle( &na, *blueNode );
67 can::AdvStyle* greenNode = new can::AdvStyle( "greennode" );
68 greenNode->addImage( "backimage", "images/gradgreen.png" );
69 greenNode->addColor( "bordercolor", 50, 100, 200 );
70 styleManager.setStyle( &nb, *greenNode );
72 can::AdvStyle* iconNode = new can::AdvStyle( "iconnode" );
73 iconNode->addImage( "backimage", "images/gradblue.png" );
74 iconNode->addImage( "icon", "images/icon.png" );
75 iconNode->addColor( "bordercolor", 0, 255, 0 );
76 styleManager.setStyle( &nc, *iconNode );
78 can::GraphPositionner* positionner = new can::GraphPositionner( *canvas, *graph );
79 positionner->feed( styleManager );
82 \sa can::AdvStyle and can::Positionner
85 class Manager
: public qan::utl::Manager
< AdvStyle
>
91 /*! \name Style Management *///--------------------------------
95 //! Set the style for a specific object.
96 void setStyle( void* object
, AdvStyle
& style
);
98 //! Set the style for a specific node type.
99 void setStyle( int type
, AdvStyle
& style
);
101 //! Get the style for a specific object.
102 AdvStyle
* getStyle( void* object
);
104 //! Get the style for a specific node type.
105 AdvStyle
* getStyle( int type
);
109 //! Map style to their target object.
110 typedef std::map
< void*, AdvStyle
* > ObjectStyleMap
;
112 //! Map style to their target object.
113 typedef std::map
< int, AdvStyle
* > TypeStyleMap
;
116 ObjectStyleMap _objectStyleMap
;
119 TypeStyleMap _typeStyleMap
;
121 //-------------------------------------------------------------
127 //! Models a Style attribute (either a color, a numeric value or an image name).
135 //! Define the attribute concrete type.
147 //! Manage a set of attributes for a specific style.
151 class Manager
: public qan::utl::Manager
< Attribute
>
153 /*! \name Manager Attribute Access *///--------------------
157 bool hasColor( const std::string
& name
) const;
159 const Color
getColor( const std::string
& name
) const;
161 bool hasImage( const std::string
& name
) const;
163 const std::string
getImage( const std::string
& name
) const;
165 bool hasValue( const std::string
& name
) const;
167 double getValue( const std::string
& name
) const;
171 const Attribute
* getAttribute( const std::string
& name
) const;
173 //---------------------------------------------------------
176 Attribute( Type type
, const std::string
& name
) :
180 Attribute( const Attribute
& attribute
) : _type( attribute
._type
), _name( attribute
._name
) { }
182 //! Get the attribute type.
183 /*! \return the attribute type. */
184 Type
getType( ) const { return _type
; }
186 //! Get the attribute name.
187 /*! \return the attribute name. */
188 const std::string
& getName( ) const { return _name
; }
200 //! Model an RGB color attribute.
204 class Color
: public Attribute
208 Color( const std::string
& name
, int r
, int g
, int b
) :
209 Attribute( COLOR
, name
), _r( r
), _g( g
), _b( b
) { }
211 Color( const Color
& color
) : Attribute( color
),
212 _r( color
._r
), _g( color
._g
), _b( color
._b
) { }
214 void setRgb( int r
, int g
, int b
) { _r
= r
; _g
= g
; _b
= b
; }
216 int getR( ) const { return _r
; }
218 int getG( ) const { return _g
; }
220 int getB( ) const { return _b
; }
231 //! Model a numeric (integer or float) attribute.
235 class Value
: public Attribute
239 Value( const std::string
& name
, int i
, double d
= 0.0 ) :
240 Attribute( VALUE
, name
), _i( i
), _d( d
) { }
242 Value( const Value
& value
) : Attribute( value
), _i( value
._i
), _d( value
._d
) { }
244 int getInt( ) const { return _i
; }
246 void setInt( int i
) { _i
= i
; }
248 double getDouble( ) const { return _d
; }
250 void setDouble( double d
) { _d
= d
; }
259 //! Model an image name attribute (just the image name is stored not the image's content).
263 class Image
: public Attribute
267 Image( const std::string
& name
, const std::string
& fileName
) :
268 Attribute( IMAGE
, name
), _fileName( fileName
) { }
270 Image( const Image
& image
) : Attribute( image
), _fileName( image
._fileName
) { }
272 const std::string
& getFileName( ) const { return _fileName
; }
276 std::string _fileName
;
279 /*! \name Style Constructor/Destructor *///------------------------
283 //! Style constructor with name initialisation.
284 AdvStyle( const std::string
& name
) : _name( name
) { }
286 //! Style virtual destructor.
287 virtual ~AdvStyle( ) { }
289 //-----------------------------------------------------------------
293 /*! \name Name Management *///-------------------------------------
297 //! Get the style name.
298 const std::string
& getName( ) const { return _name
; }
305 //-----------------------------------------------------------------
309 /*! \name Attribute Management *///--------------------------------
313 //! Get the style attribute manager.
314 Attribute::Manager
& getAttributeManager( ) { return _attributeManager
; }
317 void addColor( const std::string
& name
, int r
, int g
, int b
);
319 //! Test if there is a color of a given name in this style attribute manager.
320 bool hasColor( const std::string
& name
) const;
322 //! Get a color under a QT compatible text form (#RRGGBB).
323 std::string
getColor( const std::string
& name
) const;
326 void addImage( const std::string
& name
, const std::string
& fileName
);
329 bool hasImage( const std::string
& name
) const;
332 std::string
getImage( const std::string
& name
) const;
335 void addValue( const std::string
& name
, double value
);
338 bool hasValue( const std::string
& name
) const;
341 double getValue( const std::string
& name
) const;
345 //! Style attribute manager.
346 Attribute::Manager _attributeManager
;
348 //-----------------------------------------------------------------
352 /*! \name Model Signals Management *///----------------------------
356 #ifndef QANAVA_NOBOOST
358 boost::signal0
< void > modified
;
362 #ifndef QANAVA_NOBOOST
364 boost::signal0
< void > destroyed
;
366 void destroyed( ) { }
369 //-----------------------------------------------------------------
373 //-----------------------------------------------------------------------------
376 #endif // qab_can_Style_h