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 //-----------------------------------------------------------------------------
30 #include "./qanStyle.h"
40 namespace qan
{ // ::qan
43 /* Style Manager Constructor/Destructor *///-----------------------------------
44 Style::Manager::Manager( ) :
45 _empty( new Style( "empty" ) )
50 Style::Manager::~Manager( )
56 //-----------------------------------------------------------------------------
60 /* Style Management *///-------------------------------------------------------
61 void Style::Manager::clear( )
63 QSet
< Style
* > styles
= QSet
< Style
* >::fromList( _objectStyleMap
.values( ) );
64 foreach ( Style
* style
, styles
)
66 _objectStyleMap
.clear( );
67 _typeStyleMap
.clear( );
71 void Style::Manager::setStyle( void* object
, Style
& style
)
73 if ( !_objectStyleMap
.contains( object
) )
74 _objectStyleMap
.insert( object
, &style
);
77 Style
* Style::Manager::getStyle( void* object
)
79 return _objectStyleMap
.value( object
, 0 );
82 const Style
* Style::Manager::getStyle( const void* object
) const
84 return _objectStyleMap
.value( const_cast< void* >( object
), 0 );
87 void Style::Manager::setStyle( int type
, Style
& style
)
89 // Set the style if the type don't already have a style
90 if ( !_typeStyleMap
.contains( type
) )
91 _typeStyleMap
.insert( type
, &style
);
94 Style
* Style::Manager::getStyle( int type
)
96 return _typeStyleMap
.value( type
, 0 );
99 const Style
* Style::Manager::getStyle( int type
) const
101 return _typeStyleMap
.value( type
, 0 );
103 //-----------------------------------------------------------------------------
106 /* Image Caching Management *///-----------------------------------------------
108 \return A cached image loaded from the given filename. If fileName is invalid, the returned image can be queried for isNull().
110 QImage
Style::Manager::getImage( QString fileName
)
112 NameImageMap::iterator imageIter
= _nameImageMap
.find( fileName
);
113 if ( imageIter
!= _nameImageMap
.end( ) )
114 return imageIter
.value( );
117 QImage
image( fileName
);
118 if ( image
.isNull( ) )
122 _nameImageMap
.insert( fileName
, image
);
130 void Style::Manager::clearImages( )
132 _nameImageMap
.clear( );
134 //-----------------------------------------------------------------------------
138 /* Attribute Management *///---------------------------------------------------
139 void Style::add( QString name
, QVariant variant
)
141 _nameValueMap
.insert( name
, variant
);
142 reset( ); // Force coherent Interview update whitout using (begin/end)Insert()
146 void Style::remove( QString name
)
148 _nameValueMap
.remove( name
);
149 _imageNames
.removeAll( name
);
150 reset( ); // Force coherent Interview update whitout using (begin/end)Insert()
155 \return true if the attribute has been renamed, false otherwise (for example, the name was already used by another attribute).
157 bool Style::rename( QString name
, QString newName
)
159 if ( has( newName
) )
162 QVariant value
= get( name
);
164 add( newName
, value
);
165 reset( ); // Force coherent Interview update
170 \return true if a QVariant is registered under the given name, false otherwise.
172 bool Style::has( QString name
) const
174 NameValueMap::const_iterator nameIter
= _nameValueMap
.find( name
);
175 return ( nameIter
!= _nameValueMap
.end( ) );
179 \return A QVariant registered under a given name, or an invalid QVariant if the name is unknown.
181 QVariant
Style::get( QString name
)
183 return _nameValueMap
.value( name
, QVariant( ) );
187 \return A const QVariant registered under a given name, or an invalid QVariant if the name is unknown.
189 const QVariant
Style::get( QString name
) const
192 NameValueMap::const_iterator nameIter = _nameValueMap.find( name );
193 if ( nameIter != _nameValueMap.end( ) )
194 result = nameIter->second;
196 return _nameValueMap
.value( name
, QVariant( ) );
199 void Style::addColor( QString name
, int r
, int g
, int b
)
202 value
= QColor( r
, g
, b
); // QVariant does not support QColor in its ctor (non gui only types)
207 \return The color registered with name, an invalid Qcolor otherwise.
209 QColor
Style::getColor( QString name
) const
212 const QVariant
& value
= get( name
);
213 if ( value
.isValid( ) )
214 color
= value
.value
< QColor
>( );
218 void Style::addIcon( QString name
, QIcon
& icon
)
221 value
= icon
; // QVariant does not support gui types in its ctor
225 QIcon
Style::getIcon( QString name
) const
228 const QVariant
& value
= get( name
);
229 if ( value
.isValid( ) )
230 icon
= value
.value
< QIcon
>( );
234 void Style::addImage( QString name
, QImage image
)
237 value
= image
; // QVariant does not support gui types in its ctor
241 void Style::addImageName( QString name
, QString fileName
)
243 add( name
, QVariant( fileName
) );
244 _imageNames
.push_back( name
);
246 bool Style::hasImageName( QString name
) const
248 return ( _imageNames
.contains( name
) );
251 QString
Style::getImageName( QString name
) const
253 if ( hasImageName( name
) ) // Check if name is an image name type
255 QVariant imageName
= get( name
);
256 if ( imageName
.type( ) == QVariant::String
)
257 return imageName
.toString( );
261 //-----------------------------------------------------------------------------
265 /* Qt Model Interface *///-----------------------------------------------------
266 QVariant
Style::data( const QModelIndex
& index
, int role
) const
268 if ( !index
.isValid( ) )
269 return QVariant( "index invalid" );
273 if ( ( role
== Qt::FontRole
) && ( index
.column( ) == 0 ) )
276 font
.setWeight( QFont::Bold
);
280 if ( role
== Qt::DisplayRole
)
282 if ( index
.column( ) == 0 )
284 if ( index
.row( ) == 0 )
285 d
= QString( "name" );
287 d
= _nameValueMap
.keys( ).at( index
.row( ) - 1 );
289 else if ( index
.column( ) == 1 )
291 if ( index
.row( ) == 0 )
294 d
= _nameValueMap
.values( ).at( index
.row( ) - 1 );
301 bool Style::setData( const QModelIndex
& index
, const QVariant
& value
, int role
)
303 if ( index
.row( ) == 1 )
304 return QAbstractListModel::setData( index
, value
, role
);
306 if ( role
== Qt::EditRole
&& index
.column( ) == 1 ) // Edit values
308 QString k
= _nameValueMap
.keys( ).at( index
.row( ) - 1 );
309 QVariant v
= _nameValueMap
.values( ).at( index
.row( ) - 1 );
310 if ( v
.type( ) == value
.type( ) )
313 _nameValueMap
.insert( k
, v
);
314 emit
dataChanged( index
, index
);
319 if ( role
== Qt::EditRole
&& index
.column( ) == 0 ) // Edit names
321 if ( value
.type( ) == QVariant::String
)
323 QString k
= _nameValueMap
.keys( ).at( index
.row( ) - 1 );
324 if ( rename( k
, value
.toString( ) ) ) // Rename old name with the editor content and eventually validate the input
326 emit
dataChanged( index
, index
);
332 return QAbstractListModel::setData( index
, value
, role
);
335 Qt::ItemFlags
Style::flags( const QModelIndex
&index
) const
337 if ( !index
.isValid( ) )
338 return Qt::ItemIsEnabled
;
340 if ( index
.row( ) == 0 )
341 return Qt::ItemIsEnabled
| Qt::ItemIsSelectable
; // First row (style name) is not editable
343 return Qt::ItemIsEnabled
| Qt::ItemIsEditable
; // Other rows are editable in both the name and value columns
345 return Qt::ItemIsEnabled
;
348 QVariant
Style::headerData( int section
, Qt::Orientation orientation
, int role
) const
350 if ( orientation
== Qt::Horizontal
&& role
== Qt::DisplayRole
)
353 return QString( "Property" );
354 else if ( section
== 1 )
355 return QString( "Value" );
360 int Style::rowCount( const QModelIndex
& parent
) const
365 int Style::columnCount( const QModelIndex
& parent
) const
369 //-----------------------------------------------------------------------------