0.3.1
[qanava.git] / src / qanStyle.h
blobfef1a74f12409fc855e7c7063d766c3dbf9fea57
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 Qarte software.
23 // \file qanStyle.h
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
33 // QT headers
34 #include <QIcon>
35 #include <QVariant>
36 #include <QList>
37 #include <QMap>
38 #include <QAbstractListModel>
41 //-----------------------------------------------------------------------------
42 namespace qan // ::qan
44 //! Specify graphic and other attributes for a specific primitive (usually a Canvas Item).
45 /*!
46 \nosubgrouping
48 class Style : public QAbstractListModel
50 Q_OBJECT
52 public:
54 //! Manage styles for a set of objects (usually graphics items).
55 /*!
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
58 directly.
60 \sa GraphScene
61 \nosubgrouping
63 class Manager
65 /*! \name Manager Constructor/Destructor *///------------------
66 //@{
67 public:
69 Manager( );
71 virtual ~Manager( );
72 //@}
73 //-------------------------------------------------------------
77 /*! \name Style Management *///--------------------------------
78 //@{
79 public:
81 //! Clear this manager from all mapping and delete registered styles.
82 void clear( );
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; }
105 protected:
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;
113 //! .
114 ObjectStyleMap _objectStyleMap;
116 //! .
117 TypeStyleMap _typeStyleMap;
119 Style* _empty;
120 //@}
121 //-------------------------------------------------------------
125 /*! \name Image Caching Management *///------------------------
126 //@{
127 public:
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).
133 void clearImages( );
135 protected:
137 typedef QMap< QString, QImage > NameImageMap;
139 //! Map image filename to concrete QT images.
140 NameImageMap _nameImageMap;
141 //@}
142 //-------------------------------------------------------------
146 /*! \name Style Constructor/Destructor *///------------------------
147 //@{
148 public:
150 //! Style constructor.
151 Style( ) : _name( "" ) { }
153 //! Style constructor with name initialisation.
154 Style( QString name ) : _name( name ) { }
156 private:
158 //! Style empty private copy constructor.
159 Style( const Style& style );
160 //@}
161 //-----------------------------------------------------------------
165 /*! \name Style Name Management *///-------------------------------
166 //@{
167 public:
169 //! Get the style name.
170 QString getName( ) const { return _name; }
172 protected:
174 //! Style name.
175 QString _name;
176 //@}
177 //-----------------------------------------------------------------
181 /*! \name Attribute Management *///--------------------------------
182 //@{
183 public:
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 >( );
198 return 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;
243 //! .
244 QString getImageName( QString name ) const;
246 protected:
248 typedef QMap< QString, QVariant > NameValueMap;
250 bool isImageName( QString name ) { return _imageNames.contains( name ); }
252 QList< QString > _imageNames;
254 //! .
255 NameValueMap _nameValueMap;
256 //@}
257 //-----------------------------------------------------------------
260 /*! \name Qt Model Interface *///----------------------------------
261 //@{
262 public:
264 //! Define the article attribute offset (row) in table.
265 enum AttributeOffset
267 TITLE = 0,
268 AUTHOR = 1,
269 SOURCE = 2,
270 SUB = 3,
271 SUP = 4
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;
285 //@}
286 //-----------------------------------------------------------------
289 /*! \name Model Signals Management *///----------------------------
290 //@{
291 signals:
293 void modified( );
294 //@}
295 //-----------------------------------------------------------------
297 } // ::qan
298 //-----------------------------------------------------------------------------
301 #endif // qan_can_Style_h