(empty message)
[qanava.git] / src / can / canCanvas.h
blobbf6e7af716404013e2c6a638daf9083ef9dde473
1 /*
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.
23 // \file canCanvas.h
24 // \author Benoit Autheman (benoit@faktiss.net)
25 // \date 2004 October 13
26 //-----------------------------------------------------------------------------
29 #ifndef canCanvas_h
30 #define canCanvas_h
33 // QT headers
34 #include <qobject.h>
35 #include <qcanvas.h>
36 #include <qimage.h>
39 // Qanava headers
40 #include "canAdvStyle.h"
41 #include "canGrid.h"
44 // STL headers
45 #include <vector>
46 #include <set>
49 //-----------------------------------------------------------------------------
50 namespace qan { // ::qan
51 //! Namespace for all classes related to concrete canvas graphical elements.
52 namespace can { // ::qan::can
54 class Item;
55 class GraphView;
57 //! Manage image loading in a canvas (avoid image duplication, etc.)
58 /*!
59 \nosubgrouping
61 class ImageManager
63 public:
65 ImageManager( ) { }
67 public:
69 QImage* getImage( const std::string& fileName );
71 protected:
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.
80 /*!
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:
90 \code
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( );
115 \endcode
117 \sa la::Grid, can::Grid, can::AdvStyle::Manager and can::Positionner
118 \nosubgrouping
120 class Canvas : public QCanvas
122 Q_OBJECT
124 /*! \name Canvas Constructor/Destructor *///---------------------------
125 //@{
126 public:
128 //! Canvas constructor with size initialization.
129 Canvas( int w, int h, QColor backColor = QColor( 255, 255, 255 ) );
130 //@}
131 //---------------------------------------------------------------------
135 /*! \name Item Management *///----------------------------------------
136 //@{
137 public:
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.
146 Items& getItems( );
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 );
154 //! .
155 void setGraphView( GraphView* view ) { _view = view; }
157 //! .
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; }
169 public slots:
171 //! Called when the canvas is resized (connected to QCanvas resized() slot).
172 virtual void canvasResized( );
174 private:
176 //! List of relation items present on the canvas.
177 Items _items;
179 //! STL set to store freezed items.
180 std::set< void* > _freezed;
182 GraphView* _view;
184 //! Style manager.
185 AdvStyle::Manager _styleManager;
187 //! Manage QT image needed by canvas graphical elements.
188 ImageManager _imageManager;
190 //! Canvas grid.
191 can::Grid _grid;
192 //@}
193 //---------------------------------------------------------------------
195 } // ::qan::can
196 } // ::qan
197 //-----------------------------------------------------------------------------
200 #endif // canCanvas_h