2 Qanava - Graph drawing library for QT
3 Copyright (C) 2005 Benoit AUTHEMAN
5 This program is free software; you can redistribute it and/or
6 modify it under the terms of the GNU General Public License
7 as published by the Free Software Foundation; either version 2
8 of the License, or (at your option) any later version.
10 This program 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
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; 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.
24 // \author Benoit Autheman (benoit@faktiss.net)
25 // \date 2004 October 13
26 //-----------------------------------------------------------------------------
40 #include "canAdvStyle.h"
49 //-----------------------------------------------------------------------------
50 namespace qan
{ // ::qan
51 //! Namespace for all classes related to concrete canvas graphical elements.
52 namespace can
{ // ::qan::can
57 //! Manage image loading in a canvas (avoid image duplication, etc.)
69 QImage
* getImage( const std::string
& fileName
);
73 typedef std::map
< const std::string
, QImage
* > NameImageMap
;
75 //! Map image filename to concrete QT images.
76 NameImageMap _nameImageMap
;
79 //! Display graphical items associed to structured objects as well as their relations.
81 Canvas is feed by a Positionner object that manage the association between
82 Node elements and canvas graphical Item objects.<br>
84 Items' appearence can be changed by attaching styles (AdvStyle objects) to a specific
85 item trought the Style::Manager returned by the getStyleManager() method.<br>
87 <img src="./images/qanavatestshot.png" alt="Qanava test">
89 Exemple use of a Canvas to display a simple graph of two nodes:
91 // Create a canvas and its view
92 can::Canvas* canvas = new can::Canvas( 300, 150 );
93 can::Editor* editor = new can::Editor( *canvas, _frame, "" );
94 connect( canvas, SIGNAL(resized()), editor, SLOT(resize()) );
96 // Create a simple graph
97 la::Graph* graph = new la::Graph( );
98 la::Node& na = *new la::Node( "Node A" );
99 graph->registerNode( na );
100 la::Node& nb = *new la::Node( "Node B" );
101 graph->registerNode( nb );
102 graph->createEdge( na, nb );
104 // Create graphic items on the canvas to display the graph
105 can::AdvStyle::Manager styleManager; // Don't specify styles
106 can::GraphPositionner* positionner = new can::GraphPositionner( *_canvas, *graph );
107 positionner->feed( styleManager );
109 // Create the grid and feed the canvas with the graph nodes associed items
110 can::Grid* grid = new can::Grid( *canvas);
111 la::Layout* layout = new la::Random( *grid );
112 layout->layout( *graph, canvas->width( ), canvas->height( ), ltm::utl::ProgressVoid( ) );
113 positionner->show( );
114 positionner->update( );
117 \sa la::Grid, can::Grid, can::AdvStyle::Manager and can::Positionner
120 class Canvas
: public QCanvas
124 /*! \name Canvas Constructor/Destructor *///---------------------------
128 //! Canvas constructor with size initialization.
129 Canvas( int w
, int h
, QColor backColor
= QColor( 255, 255, 255 ) );
131 //---------------------------------------------------------------------
135 /*! \name Item Management *///----------------------------------------
139 //! STL container for relations elements.
140 typedef std::vector
< Item
* > Items
;
142 //! Add a relation element to this canvas.
143 void addItem( Item
& item
);
145 //! Get this canvas items.
148 //! Test if an item is freezed (ie. can't be moved).
149 bool isFreed( void* item
);
151 //! Freeze a specific canvas item (the item can't be moved after having been freed).
152 void setFreezed( void* item
);
155 void setGraphView( GraphView
* view
) { _view
= view
; }
158 GraphView
* getGraphView( ) { return _view
; }
160 //! Get the canvas style manager (a style is eventually associed to each canvas item).
161 AdvStyle::Manager
& getStyleManager( ) { return _styleManager
; }
163 //! Get the QT image manager.
164 ImageManager
& getImageManager( ) { return _imageManager
; }
166 //! Get the canvas grid.
167 can::Grid
& getGrid( ) { return _grid
; }
171 //! Called when the canvas is resized (connected to QCanvas resized() slot).
172 virtual void canvasResized( );
176 //! List of relation items present on the canvas.
179 //! STL set to store freezed items.
180 std::set
< void* > _freezed
;
185 AdvStyle::Manager _styleManager
;
187 //! Manage QT image needed by canvas graphical elements.
188 ImageManager _imageManager
;
193 //---------------------------------------------------------------------
197 //-----------------------------------------------------------------------------
200 #endif // canCanvas_h