(empty message)
[qanava.git] / src / can / canAdvStyle.h
blob1b5a95c3123e0bba4d6c408c354bf61dfb5a8fcc
1 /*
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.
23 // \file qisStyle.h
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
33 // UTL headers
34 #include "../utl/utlManager.h"
37 // Std headers
38 #include <string>
39 #include <map>
42 //-----------------------------------------------------------------------------
43 namespace qan // ::qan
45 namespace can // ::qan::can
47 //! Specify graphic and other attributes for a specific primitive (usually a Canvas Item).
48 /*!
49 \nosubgrouping
51 class AdvStyle
53 public:
55 //! Manage styles for a set of objects (usually Canvas Item items).
56 /*!
57 Definition of styles for la::Node objects:
58 \code
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 );
80 \endcode
82 \sa can::AdvStyle and can::Positionner
83 \nosubgrouping
85 class Manager : public qan::utl::Manager< AdvStyle >
87 public:
89 Manager( ) { }
91 /*! \name Style Management *///--------------------------------
92 //@{
93 public:
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 );
107 protected:
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;
115 //! .
116 ObjectStyleMap _objectStyleMap;
118 //! .
119 TypeStyleMap _typeStyleMap;
120 //@}
121 //-------------------------------------------------------------
125 class Color;
127 //! Models a Style attribute (either a color, a numeric value or an image name).
129 \nosubgrouping
131 class Attribute
133 public:
135 //! Define the attribute concrete type.
136 enum Type
138 NONE = 0,
140 COLOR = 1,
142 VALUE = 2,
144 IMAGE = 4
147 //! Manage a set of attributes for a specific style.
149 \nosubgrouping
151 class Manager : public qan::utl::Manager< Attribute >
153 /*! \name Manager Attribute Access *///--------------------
154 //@{
155 public:
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;
169 protected:
171 const Attribute* getAttribute( const std::string& name ) const;
172 //@}
173 //---------------------------------------------------------
176 Attribute( Type type, const std::string& name ) :
177 _type( type ),
178 _name( 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; }
190 protected:
192 //! Attribute type.
193 Type _type;
195 //! Attribute name.
196 std::string _name;
200 //! Model an RGB color attribute.
202 \nosubgrouping
204 class Color : public Attribute
206 public:
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; }
222 protected:
224 int _r;
226 int _g;
228 int _b;
231 //! Model a numeric (integer or float) attribute.
233 \nosubgrouping
235 class Value : public Attribute
237 public:
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; }
252 protected:
254 int _i;
256 double _d;
259 //! Model an image name attribute (just the image name is stored not the image's content).
261 \nosubgrouping
263 class Image : public Attribute
265 public:
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; }
274 protected:
276 std::string _fileName;
279 /*! \name Style Constructor/Destructor *///------------------------
280 //@{
281 public:
283 //! Style constructor with name initialisation.
284 AdvStyle( const std::string& name ) : _name( name ) { }
286 //! Style virtual destructor.
287 virtual ~AdvStyle( ) { }
288 //@}
289 //-----------------------------------------------------------------
293 /*! \name Name Management *///-------------------------------------
294 //@{
295 public:
297 //! Get the style name.
298 const std::string& getName( ) const { return _name; }
300 protected:
302 //! Style name.
303 std::string _name;
304 //@}
305 //-----------------------------------------------------------------
309 /*! \name Attribute Management *///--------------------------------
310 //@{
311 public:
313 //! Get the style attribute manager.
314 Attribute::Manager& getAttributeManager( ) { return _attributeManager; }
316 //! .
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;
325 //! .
326 void addImage( const std::string& name, const std::string& fileName );
328 //! .
329 bool hasImage( const std::string& name ) const;
331 //! .
332 std::string getImage( const std::string& name ) const;
334 //! .
335 void addValue( const std::string& name, double value );
337 //! .
338 bool hasValue( const std::string& name ) const;
340 //! .
341 double getValue( const std::string& name ) const;
343 protected:
345 //! Style attribute manager.
346 Attribute::Manager _attributeManager;
347 //@}
348 //-----------------------------------------------------------------
352 /*! \name Model Signals Management *///----------------------------
353 //@{
354 public:
356 #ifndef QANAVA_NOBOOST
357 //! .
358 boost::signal0< void > modified;
359 #else
360 void modified( ) { }
361 #endif
362 #ifndef QANAVA_NOBOOST
363 //! .
364 boost::signal0< void > destroyed;
365 #else
366 void destroyed( ) { }
367 #endif
368 //@}
369 //-----------------------------------------------------------------
371 } // ::qan::can
372 } // ::qan
373 //-----------------------------------------------------------------------------
376 #endif // qab_can_Style_h