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 Qarte software.
24 // \author Benoit Autheman (benoit@libqanava.org)
25 // \date 2005 January 03
26 //-----------------------------------------------------------------------------
29 #ifndef qan_can_Style_h
30 #define qan_can_Style_h
38 #include <QAbstractListModel>
41 //-----------------------------------------------------------------------------
42 namespace qan
// ::qan
44 //! Specify graphic and other attributes for a specific primitive (usually a Canvas Item).
48 class Style
: public QAbstractListModel
54 //! Manage styles for a set of objects (usually graphics items).
56 If you are applying style to nodes, it is usually safer to use the dedicated methods
57 from GraphScene such as applyStyle() rather than accessing this style manager
65 /*! \name Manager Constructor/Destructor *///------------------
73 //-------------------------------------------------------------
77 /*! \name Style Management *///--------------------------------
81 //! Clear this manager from all mapping and delete registered styles.
84 //! Set the style for a specific object.
85 void setStyle( void* object
, Style
& style
);
87 //! Set the style for a specific node type.
88 void setStyle( int type
, Style
& style
);
90 //! Get the style for a specific object.
91 Style
* getStyle( void* object
);
93 //! Get the style for a specific object.
94 const Style
* getStyle( const void* object
) const;
96 //! Get the style for a specific node type.
97 Style
* getStyle( int type
);
99 //! Get the style for a specific node type.
100 const Style
* getStyle( int type
) const;
102 //! Get a void style (empty style is owned by this manager, but not registered in).
103 Style
* getEmptyStyle( ) const { return _empty
; }
107 //! Map style to their target object.
108 typedef QMap
< void*, Style
* > ObjectStyleMap
;
110 //! Map style to their target object.
111 typedef QMap
< int, Style
* > TypeStyleMap
;
114 ObjectStyleMap _objectStyleMap
;
117 TypeStyleMap _typeStyleMap
;
121 //-------------------------------------------------------------
125 /*! \name Image Caching Management *///------------------------
129 //! Get an image from its file name or its cached version the second call of the same filename.
130 QImage
getImage( QString fileName
);
132 //! Clear this image manager mappings and data (images).
137 typedef QMap
< QString
, QImage
> NameImageMap
;
139 //! Map image filename to concrete QT images.
140 NameImageMap _nameImageMap
;
142 //-------------------------------------------------------------
146 /*! \name Style Constructor/Destructor *///------------------------
150 //! Style constructor.
151 Style( ) : _name( "" ) { }
153 //! Style constructor with name initialisation.
154 Style( QString name
) : _name( name
) { }
158 //! Style empty private copy constructor.
159 Style( const Style
& style
);
161 //-----------------------------------------------------------------
165 /*! \name Style Name Management *///-------------------------------
169 //! Get the style name.
170 QString
getName( ) const { return _name
; }
177 //-----------------------------------------------------------------
181 /*! \name Attribute Management *///--------------------------------
185 //! Add an attribute and register its name.
186 template < typename T
>
187 void addT( QString name
, T value
)
189 add( name
, QVariant( value
) );
192 template < typename T
>
193 T
getT( QString name
) const
195 QVariant value
= get( name
);
196 if ( value
.isValid( ) )
197 return value
.value
< T
>( );
201 //! Return the number of attributes/value couples registered in this style.
202 unsigned int size( ) const { return _nameValueMap
.size( ); }
204 //! Add an attribute and register its name.
205 void add( QString name
, QVariant value
);
207 //! Remove a style attribute by name.
208 void remove( QString name
);
210 //! Change an attribute name (changing to an already existing name, has no effect).
211 bool rename( QString name
, QString newName
);
213 //! Test if an attribute with a specific name has been set.
214 bool has( QString name
) const;
216 //! Get an attribute by name (invalide QVariant is returned if the name does not exists).
217 QVariant
get( QString name
);
219 //! Get an attribute by name (invalide QVariant is returned if the name does not exists).
220 const QVariant
get( QString name
) const;
222 //! Specialized version of add() for QColor objects.
223 void addColor( QString name
, int r
, int g
, int b
);
225 //! Get a previously registered QColor by name.
226 QColor
getColor( QString name
) const;
228 //! Specialized version of add() for QIcon objects.
229 void addIcon( QString name
, QIcon
& icon
);
231 //! Get a previously registered QIcon by name.
232 QIcon
getIcon( QString name
) const;
234 //! Specialized version of add() for QImage objects.
235 void addImage( QString name
, QImage image
);
237 //! Add an image attribute, image loading is delayed until display, and images shared accross styles.
238 void addImageName( QString name
, QString fileName
);
240 //! Test if an image name with a specific name has been set.
241 bool hasImageName( QString name
) const;
244 QString
getImageName( QString name
) const;
248 typedef QMap
< QString
, QVariant
> NameValueMap
;
250 bool isImageName( QString name
) { return _imageNames
.contains( name
); }
252 QList
< QString
> _imageNames
;
255 NameValueMap _nameValueMap
;
257 //-----------------------------------------------------------------
260 /*! \name Qt Model Interface *///----------------------------------
264 //! Define the article attribute offset (row) in table.
274 virtual QVariant
data( const QModelIndex
&index
, int role
) const;
276 virtual bool setData( const QModelIndex
& index
, const QVariant
& value
, int role
= Qt::EditRole
);
278 virtual Qt::ItemFlags
flags( const QModelIndex
&index
) const;
280 virtual QVariant
headerData( int section
, Qt::Orientation orientation
, int role
= Qt::DisplayRole
) const;
282 virtual int rowCount( const QModelIndex
& parent
= QModelIndex( ) ) const;
284 virtual int columnCount( const QModelIndex
& parent
= QModelIndex( ) ) const;
286 //-----------------------------------------------------------------
289 /*! \name Model Signals Management *///----------------------------
295 //-----------------------------------------------------------------
298 //-----------------------------------------------------------------------------
301 #endif // qan_can_Style_h