0.3.1
[qanava.git] / src / qanStyle.cpp
bloba25cce6505717bf2f16f098da78604c11fec117b
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.cpp
24 // \author Benoit Autheman (benoit@libqanava.org)
25 // \date 2005 January 03
26 //-----------------------------------------------------------------------------
29 // Qanava headers
30 #include "./qanStyle.h"
33 // QT headers
34 #include <QColor>
35 #include <QVariant>
36 #include <QFont>
37 #include <QSet>
40 namespace qan { // ::qan
43 /* Style Manager Constructor/Destructor *///-----------------------------------
44 Style::Manager::Manager( ) :
45 _empty( new Style( "empty" ) )
50 Style::Manager::~Manager( )
52 if ( _empty != 0 )
53 delete _empty;
54 clear( );
56 //-----------------------------------------------------------------------------
60 /* Style Management *///-------------------------------------------------------
61 void Style::Manager::clear( )
63 QSet< Style* > styles = QSet< Style* >::fromList( _objectStyleMap.values( ) );
64 foreach ( Style* style, styles )
65 delete style;
66 _objectStyleMap.clear( );
67 _typeStyleMap.clear( );
68 clearImages( );
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( );
115 else
117 QImage image( fileName );
118 if ( image.isNull( ) )
119 return image;
120 else
122 _nameImageMap.insert( fileName, image );
123 return image;
127 return QImage();
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()
143 emit modified( );
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()
151 emit modified( );
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 ) )
160 return false;
162 QVariant value = get( name );
163 remove( name );
164 add( newName, value );
165 reset( ); // Force coherent Interview update
166 return true;
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
191 /*QVariant result;
192 NameValueMap::const_iterator nameIter = _nameValueMap.find( name );
193 if ( nameIter != _nameValueMap.end( ) )
194 result = nameIter->second;
195 return result;*/
196 return _nameValueMap.value( name, QVariant( ) );
199 void Style::addColor( QString name, int r, int g, int b )
201 QVariant value;
202 value = QColor( r, g, b ); // QVariant does not support QColor in its ctor (non gui only types)
203 add( name, value );
207 \return The color registered with name, an invalid Qcolor otherwise.
209 QColor Style::getColor( QString name ) const
211 QColor color;
212 const QVariant& value = get( name );
213 if ( value.isValid( ) )
214 color = value.value< QColor >( );
215 return color;
218 void Style::addIcon( QString name, QIcon& icon )
220 QVariant value;
221 value = icon; // QVariant does not support gui types in its ctor
222 add( name, value );
225 QIcon Style::getIcon( QString name ) const
227 QIcon icon;
228 const QVariant& value = get( name );
229 if ( value.isValid( ) )
230 icon = value.value< QIcon >( );
231 return icon;
234 void Style::addImage( QString name, QImage image )
236 QVariant value;
237 value = image; // QVariant does not support gui types in its ctor
238 add( name, value );
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( );
259 return QString( );
261 //-----------------------------------------------------------------------------
265 /* Qt Model Interface *///-----------------------------------------------------
266 QVariant Style::data( const QModelIndex& index, int role ) const
268 if ( !index.isValid( ) )
269 return QVariant( "index invalid" );
271 QVariant d;
273 if ( ( role == Qt::FontRole ) && ( index.column( ) == 0 ) )
275 QFont font;
276 font.setWeight( QFont::Bold );
277 d = font;
280 if ( role == Qt::DisplayRole )
282 if ( index.column( ) == 0 )
284 if ( index.row( ) == 0 )
285 d = QString( "name" );
286 else
287 d = _nameValueMap.keys( ).at( index.row( ) - 1 );
289 else if ( index.column( ) == 1 )
291 if ( index.row( ) == 0 )
292 d = getName( );
293 else
294 d = _nameValueMap.values( ).at( index.row( ) - 1 );
298 return d;
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( ) )
312 v = value;
313 _nameValueMap.insert( k, v );
314 emit dataChanged( index, index );
315 emit modified( );
316 return true;
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 );
327 emit modified( );
328 return true;
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
342 else
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 )
352 if ( section == 0 )
353 return QString( "Property" );
354 else if ( section == 1 )
355 return QString( "Value" );
357 return QVariant( );
360 int Style::rowCount( const QModelIndex& parent ) const
362 return 1 + size( );
365 int Style::columnCount( const QModelIndex& parent ) const
367 return 2;
369 //-----------------------------------------------------------------------------
372 } // ::qan