0.3.1
[qanava.git] / src / qanNodeItem.h
blob79ce2e3a6f4b5a39baea1f079e28c76305dd8d9a
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 Qanava software.
23 // \file qanNodeItem.h
24 // \author Benoit Autheman (benoit@libqanava.org)
25 // \date 2004 October 13
26 //-----------------------------------------------------------------------------
29 #ifndef canNodeItem_h
30 #define canNodeItem_h
33 // Qanava headers
34 #include "./qanNode.h"
35 #include "./qanStyle.h"
36 #include "./qanEdgeItem.h"
39 // QT headers
40 #include <QGraphicsItem>
41 #include <QGraphicsRectItem>
42 #include <QGraphicsTextItem>
45 //-----------------------------------------------------------------------------
46 namespace qan { // ::qan
48 class AbstractNodeItem : public GraphItem
50 Q_OBJECT
52 public:
54 AbstractNodeItem( Node& node, Style::Manager& styleManager, Style* style ) :
55 GraphItem( styleManager, style ),
56 _node( node )
61 virtual ~AbstractNodeItem( )
65 //! Usually, return this casted to QGraphicsItem.
66 virtual QGraphicsItem* getGraphicsItem( ) = 0;
68 public:
70 Node& getNode( ) { return _node; }
72 private:
74 Node& _node;
76 public:
78 void addEdge( EdgeItem* edge )
80 _edges << edge;
81 connect( this, SIGNAL( destroyed( ) ), edge, SLOT( nodeDestroyed( ) ) );
84 void removeEdge( EdgeItem* edge ) { _edges.removeAll( edge ); }
86 void updateEdges( )
88 foreach ( EdgeItem* edge, _edges )
89 edge->updateItem( );
92 protected:
94 QList< EdgeItem* > _edges;
96 public:
98 class AbstractFactory
100 public:
102 AbstractFactory( int nodeType, bool defaultFactory = false ) :
103 _nodeType( nodeType ),
104 _defaultFactory( defaultFactory ) { };
106 virtual ~AbstractFactory( ) { }
108 virtual AbstractNodeItem* create( Node& node, Style::Manager& styleManager,
109 QGraphicsItem* parent, QGraphicsScene* scene, Style* style,
110 QPoint origin, const QString& label) = 0;
112 int getNodeType( ) const { return _nodeType; }
114 bool isDefaultFactory( ) const { return _defaultFactory; }
116 typedef QList< AbstractFactory* > List;
118 private:
120 int _nodeType;
122 bool _defaultFactory;
125 template < typename T >
126 class Factory : public AbstractFactory
128 public:
130 Factory( int nodeType, bool defaultFactory = false ) :
131 AbstractFactory( nodeType, defaultFactory ) { }
133 virtual AbstractNodeItem* create( Node& node, Style::Manager& styleManager, QGraphicsItem* parent, QGraphicsScene* scene, Style* style,
134 QPoint origin, const QString& label )
136 return new T( node, styleManager, *style, parent, scene, origin, label );
143 //! Model a rectangular node item on a QT graphics view.
145 The following style options are supported:
146 <ul>
147 <li> <b>'backcolor':</b> Background color, when there is no background image defined.
148 <li> <b>'bordercolor':</b> Color of the item border.
149 <li> <b>'backimage':</b> Background image (scaled to fit the item size).
150 <li> <b>'maximumwidth':</b> Maximum width of the item, content is cropped to fit this with limit.
151 <li> <b>'maximumheight':</b> Maximum height of the item, content is cropped to fit this height limit.
152 <li> <b>'fontsize':</b> Base size for the font used to display the item label.
153 <li> <b>'icon':</b> Display an icon centered in the left of the item.
154 <li> <b>'hasshadow':</b> Set this value to false to supress the node shadow.
155 </ul>
156 \nosubgrouping
158 class NodeItem : public AbstractNodeItem, public QGraphicsRectItem
160 Q_OBJECT
162 public:
164 NodeItem( Node& node, Style::Manager& styleManager, Style& style,
165 QGraphicsItem* parent, QGraphicsScene* scene,
166 QPointF origin, const QString& label );
168 virtual ~NodeItem( );
170 void paint( QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget = 0 );
172 struct DefaultFactory : public AbstractNodeItem::Factory< NodeItem >
174 DefaultFactory( ) : AbstractNodeItem::Factory< NodeItem >( -1, true ) { }
177 public slots:
179 virtual void updateItem( );
181 virtual QGraphicsItem* getGraphicsItem( ) { return static_cast< QGraphicsItem* >( this ); }
183 private:
185 QPointF _dimension;
187 QString _label;
189 QPixmap _backPixmap;
191 QColor _shadowColor;
193 double _shadowOffset;
195 QGraphicsTextItem* _labelItem;
197 QPixmap _icon;
199 protected:
201 QVariant itemChange( QGraphicsItem::GraphicsItemChange change, const QVariant& value );
203 } // ::qan
204 //-----------------------------------------------------------------------------
207 #endif // qanNodeItem_h