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 canGraphicsView.cpp
24 // \author Benoit Autheman (benoit@libqanava.org)
26 //-----------------------------------------------------------------------------
30 #include "./qanGraphicsView.h"
31 #include "./qanGridItem.h"
36 #include <QMouseEvent>
39 //-----------------------------------------------------------------------------
40 namespace qan
{ // ::qan
43 /* GraphicsView Constructors/Destructors *///----------------------------------
44 GraphicsView::GraphicsView( QWidget
* parent
) :
45 QGraphicsView( parent
),
47 _controllerManager( this ),
51 setHorizontalScrollBarPolicy( Qt::ScrollBarAlwaysOn
);
52 setVerticalScrollBarPolicy( Qt::ScrollBarAlwaysOn
);
53 setAlignment( Qt::AlignLeft
| Qt::AlignTop
);
55 getControllerManager( ).registerController( new PanController( *this ) );
56 getControllerManager( ).registerController( new ZoomWindowController( *this ) );
57 getControllerManager( ).registerController( new ZoomController( *this ) );
59 setSizePolicy( QSizePolicy::Expanding
, QSizePolicy::Expanding
);
62 GraphicsView::GraphicsView( QGraphicsScene
* scene
, QWidget
* parent
) :
63 QGraphicsView( scene
, parent
),
65 _controllerManager( this ),
69 setHorizontalScrollBarPolicy( Qt::ScrollBarAlwaysOn
);
70 setVerticalScrollBarPolicy( Qt::ScrollBarAlwaysOn
);
71 setAlignment( Qt::AlignLeft
| Qt::AlignTop
);
73 getControllerManager( ).registerController( new PanController( *this ) );
74 getControllerManager( ).registerController( new ZoomWindowController( *this ) );
75 getControllerManager( ).registerController( new ZoomController( *this ) );
77 setSizePolicy( QSizePolicy::Expanding
, QSizePolicy::Expanding
);
79 scene
->setItemIndexMethod( QGraphicsScene::NoIndex
);
81 //-----------------------------------------------------------------------------
85 /* Grid Management *///--------------------------------------------------------
86 void GraphicsView::drawBackground( QPainter
* painter
, const QRectF
& rect
)
89 _grid
->drawBackground( *painter
, rect
);
91 //-----------------------------------------------------------------------------
95 /* View Controller Management *///---------------------------------------------
96 void GraphicsView::keyPressEvent( QKeyEvent
* e
)
98 QGraphicsView::keyPressEvent( e
);
100 if ( _controllerManager
.keyPressEvent( e
) )
101 viewport( )->update( );
104 void GraphicsView::mousePressEvent( QMouseEvent
* e
)
106 QGraphicsView::mousePressEvent( e
);
108 if ( _controllerManager
.mousePressEvent( e
) )
109 viewport( )->update( );
112 void GraphicsView::mouseReleaseEvent( QMouseEvent
* e
)
114 QGraphicsView::mouseReleaseEvent( e
);
116 if ( _controllerManager
.mouseReleaseEvent( e
) )
117 viewport( )->update( );
120 void GraphicsView::mouseMoveEvent( QMouseEvent
* e
)
122 QGraphicsView::mouseMoveEvent( e
);
124 if ( _controllerManager
.mouseMoveEvent( e
) )
125 viewport( )->update( );
128 void GraphicsView::mouseDoubleClickEvent( QMouseEvent
* e
)
130 QGraphicsView::mouseDoubleClickEvent( e
);
132 if ( _controllerManager
.mouseDoubleClickEvent( e
) )
133 viewport( )->update( );
135 QGraphicsItem
* item
= itemAt( e
->pos( ) );
137 emit
itemDoubleClicked( item
->parentItem( ) != 0 ? item
->parentItem( ) : item
);
140 void GraphicsView::wheelEvent( QWheelEvent
* e
)
142 QGraphicsView::wheelEvent( e
);
144 if ( _controllerManager
.wheelEvent( e
) )
145 viewport( )->update( );
148 QAction
* GraphicsView::getAction( QString name
)
150 Controller
* controller
= _controllerManager
.getController( name
);
151 if ( controller
!= 0 )
152 return controller
->getAction( );
154 Controller::Manager::iterator controllerIter
= _controllerManager
.begin( );
155 for ( ; controllerIter
!= _controllerManager
.end( ); controllerIter
++ )
157 QAction
* action
= ( *controllerIter
)->getAction( name
); // Create an action by name
163 //-----------------------------------------------------------------------------
167 /* Zoom Management *///-------------------------------------------------------
168 void GraphicsView::setZoom( double zoom
)
170 qreal factor
= matrix( ).scale( zoom
, zoom
).mapRect( QRectF( 0., 0., 1., 1.) ).width( );
171 if ( factor
< 0.07 || factor
> 100 )
178 m
.scale( zoom
, zoom
);
184 With a maximum zoom factor of 2 (this default value can be changed with setZoomMaxFactor() method), a
185 normal zoom (1) is obtained by setting the zoom to 50% (half of the maximum magnification
188 \param value Zoom value in percent between (almost) 0 and the max zoom magnification (must be 0_1 to 100).
190 void GraphicsView::setZoomPercent( int value
)
192 double zoomPercent
= value
;
193 if ( zoomPercent
== 0 )
195 if ( zoomPercent
> 100 )
199 double zoomMaxFactor
= ( _zoomMaxFactor
< 0.1 ? 0.1 : _zoomMaxFactor
);
200 setZoom( zoomPercent
/ 100.f
* zoomMaxFactor
);
202 //-----------------------------------------------------------------------------
206 //-----------------------------------------------------------------------------