0.3.1
[qanava.git] / src / qanGraphView.cpp
blobffe997ad0612b553f526e0480b57a09b1cff0a07
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 canGraphView.cpp
24 // \author Benoit Autheman (benoit@libqanava.org)
25 // \date 2006 July 29
26 //-----------------------------------------------------------------------------
29 // Qanava headers
30 #include "./qanGraphView.h"
31 #include "./qanGridItem.h"
34 // QT headers
35 #include <QPen>
36 #include <QMouseEvent>
39 //-----------------------------------------------------------------------------
40 namespace qan { // ::qan
43 /* GraphView Constructors/Destructors *///----------------------------------
44 GraphView::GraphView( QWidget* parent, QColor backColor, QSize size ) :
45 QGraphicsView( parent ),
46 _graph( 0 ),
47 _grid( 0 ),
48 _controllerManager( this ),
49 _zoom( 1.0 ),
50 _zoomMaxFactor( 2 ),
51 _layout( new Random( ) )
53 configureView( backColor, size );
55 getControllerManager( ).registerController( new PanController( *this ) );
56 getControllerManager( ).registerController( new ZoomWindowController( *this ) );
57 getControllerManager( ).registerController( new ZoomController( *this ) );
60 GraphView::GraphView( Graph& graph, QWidget* parent, QColor backColor, QSize size ) :
61 QGraphicsView( &graph.getM( ), parent ),
62 _graph( &graph ),
63 _grid( 0 ),
64 _controllerManager( this ),
65 _zoom( 1.0 ),
66 _zoomMaxFactor( 2 )
68 configureView( backColor, size );
70 getControllerManager( ).registerController( new PanController( *this ) );
71 getControllerManager( ).registerController( new ZoomWindowController( *this ) );
72 getControllerManager( ).registerController( new ZoomController( *this ) );
74 setGraph( graph );
77 void GraphView::configureView( QColor backColor, QSize size )
79 setHorizontalScrollBarPolicy( Qt::ScrollBarAlwaysOn );
80 setVerticalScrollBarPolicy( Qt::ScrollBarAlwaysOn );
81 setAlignment( Qt::AlignLeft | Qt::AlignTop );
82 setSizePolicy( QSizePolicy::Expanding, QSizePolicy::Expanding );
84 //connect( _graphicsView, SIGNAL(itemDoubleClicked(QGraphicsItem*)), this, SLOT(graphicItemDoubleClicked(QGraphicsItem*)) );
86 QPalette palette;
87 palette.setColor( backgroundRole( ), backColor );
88 viewport( )->setPalette( palette );
91 void GraphView::setGraph( Graph& graph )
93 _graph = &graph;
94 GraphScene& graphScene = graph.getM( );
95 graphScene.setItemIndexMethod( QGraphicsScene::NoIndex );
96 graphScene.registerGraphicNodeFactory( new NodeItem::DefaultFactory( ) );
97 graphScene.init( graph.getRootNodes( ) );
98 setScene( &graphScene );
100 //-----------------------------------------------------------------------------
104 /* Grid Management *///--------------------------------------------------------
105 void GraphView::drawBackground( QPainter* painter, const QRectF& rect )
107 if ( _grid != 0 )
108 _grid->drawBackground( *painter, rect );
110 //-----------------------------------------------------------------------------
114 /* View Controller Management *///---------------------------------------------
115 void GraphView::keyPressEvent( QKeyEvent* e )
117 QGraphicsView::keyPressEvent( e );
119 if ( _controllerManager.keyPressEvent( e ) )
120 viewport( )->update( );
123 void GraphView::mousePressEvent( QMouseEvent* e )
125 QGraphicsView::mousePressEvent( e );
127 if ( _controllerManager.mousePressEvent( e ) )
128 viewport( )->update( );
131 void GraphView::mouseReleaseEvent( QMouseEvent* e )
133 QGraphicsView::mouseReleaseEvent( e );
135 if ( _controllerManager.mouseReleaseEvent( e ) )
136 viewport( )->update( );
139 void GraphView::mouseMoveEvent( QMouseEvent* e )
141 QGraphicsView::mouseMoveEvent( e );
143 if ( _controllerManager.mouseMoveEvent( e ) )
144 viewport( )->update( );
147 void GraphView::mouseDoubleClickEvent( QMouseEvent* e )
149 QGraphicsView::mouseDoubleClickEvent( e );
151 if ( _controllerManager.mouseDoubleClickEvent( e ) )
152 viewport( )->update( );
154 QGraphicsItem* item = itemAt( e->pos( ) );
155 if ( item != 0 )
156 emit itemDoubleClicked( item->parentItem( ) != 0 ? item->parentItem( ) : item );
159 void GraphView::wheelEvent( QWheelEvent* e )
161 QGraphicsView::wheelEvent( e );
163 if ( _controllerManager.wheelEvent( e ) )
164 viewport( )->update( );
167 QAction* GraphView::getAction( QString name )
169 Controller* controller = _controllerManager.getController( name );
170 if ( controller != 0 )
171 return controller->getAction( );
173 Controller::Manager::iterator controllerIter = _controllerManager.begin( );
174 for ( ; controllerIter != _controllerManager.end( ); controllerIter++ )
176 QAction* action = ( *controllerIter )->getAction( name ); // Create an action by name
177 if ( action != 0 )
178 return action;
180 return 0;
182 //-----------------------------------------------------------------------------
186 /* Zoom Management *///-------------------------------------------------------
187 void GraphView::setZoom( double zoom )
189 qreal factor = matrix( ).scale( zoom, zoom ).mapRect( QRectF( 0., 0., 1., 1.) ).width( );
190 if ( factor < 0.07 || factor > 100 )
191 return;
193 if ( zoom > 0.1 )
195 _zoom = zoom;
196 QMatrix m;
197 m.scale( zoom, zoom );
198 setMatrix( m );
203 With a maximum zoom factor of 2 (this default value can be changed with setZoomMaxFactor() method), a
204 normal zoom (1) is obtained by setting the zoom to 50% (half of the maximum magnification
205 factor).
207 \param value Zoom value in percent between (almost) 0 and the max zoom magnification (must be 0_1 to 100).
209 void GraphView::setZoomPercent( int value )
211 double zoomPercent = value;
212 if ( zoomPercent == 0 )
213 zoomPercent = 1;
214 if ( zoomPercent > 100 )
215 zoomPercent = 100;
217 // Set zoom factor
218 double zoomMaxFactor = ( _zoomMaxFactor < 0.1 ? 0.1 : _zoomMaxFactor );
219 setZoom( zoomPercent / 100.f * zoomMaxFactor );
221 //-----------------------------------------------------------------------------
225 /* Style Management *///-------------------------------------------------------
226 /*! This is a shortcut method, style's should usually be managed trough GraphScene's
227 methods.
229 void GraphView::applyStyle( Node* node, Style* style )
231 if ( getGraphScene( ) != 0 )
232 getGraphScene( )->applyStyle( node, style );
234 //-----------------------------------------------------------------------------
238 /* Layout Management *///------------------------------------------------------
240 \note The ownership of the given layout object is transferred to this view.
241 \param layout New layout to use with this view (must not be 0).
243 void GraphView::setGraphLayout( Layout* layout, QProgressDialog* progress )
245 if ( layout == 0 )
246 return;
248 if ( _layout != 0 )
250 //delete _layout;
251 _layout = 0;
253 _layout = layout;
256 void GraphView::layoutGraph( QProgressDialog* progress, Layout* layout, int step, Node* except )
258 if ( _graph == 0 )
259 return;
260 if ( _layout == 0 && layout == 0 )
261 return;
263 QRectF r = sceneRect( );
264 r.setWidth( std::max( r.width( ), ( double )width( ) ) );
265 r.setHeight( std::max( r.height( ), ( double )height( ) ) );
267 assert( getGraphScene( ) != 0 );
268 if ( layout != 0 )
269 layout->layout( *_graph, getGraphScene( ), r, progress, step );
270 else if ( _layout != 0 && getGrid( ) != 0 )
271 _layout->layout( *_graph, getGraphScene( ), r, progress, step );
273 getGraphScene( )->updatePositions( );
275 update( );
277 //-----------------------------------------------------------------------------
280 } // ::qan
281 //-----------------------------------------------------------------------------