1 // Copyright (C) 2013-2016 CEA/DEN, EDF R&D, OPEN CASCADE
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 // Lesser General Public License for more details.
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 #include "GraphicsView_ViewPort.h"
22 #include "GraphicsView_Object.h"
23 #include "GraphicsView_Scene.h"
24 #include "GraphicsView_ViewTransformer.h"
26 #include "SUIT_ResourceMgr.h"
27 #include "SUIT_Session.h"
30 #include <QGraphicsSceneMouseEvent>
31 #include <QGridLayout>
35 #include <QRubberBand>
42 #define FOREGROUND_Z_VALUE -2
43 #define GRID_Z_VALUE -1
44 #define SKETCH_Z_VALUE 3000
46 int GraphicsView_ViewPort::nCounter = 0;
47 QCursor* GraphicsView_ViewPort::defCursor = 0;
48 QCursor* GraphicsView_ViewPort::handCursor = 0;
49 QCursor* GraphicsView_ViewPort::panCursor = 0;
50 QCursor* GraphicsView_ViewPort::panglCursor = 0;
51 QCursor* GraphicsView_ViewPort::zoomCursor = 0;
52 QCursor* GraphicsView_ViewPort::sketchCursor = 0;
54 //=======================================================================
55 // Name : GraphicsView_ViewPort::ViewLabel
56 // Purpose : Wrapper for label, which can ignore move events sent from
57 // QGraphicsView::scrollContentsBy() method, which,
58 // in its turn, called from GraphicsView_ViewPort::pan()
59 //=======================================================================
60 class GraphicsView_ViewPort::ViewLabel : public QLabel
63 ViewLabel( QWidget* theParent )
64 : QLabel( theParent ),
65 myAcceptMoveEvents( false )
70 void setAcceptMoveEvents( bool theFlag )
72 myAcceptMoveEvents = theFlag;
76 virtual void moveEvent( QMoveEvent* theEvent )
78 if( myAcceptMoveEvents )
79 QLabel::moveEvent( theEvent );
80 else // return the label to the initial position
82 myAcceptMoveEvents = true;
83 move( theEvent->oldPos() );
84 myAcceptMoveEvents = false;
89 bool myAcceptMoveEvents;
92 //================================================================
93 // Function : createCursors
95 //================================================================
96 void GraphicsView_ViewPort::createCursors ()
98 defCursor = new QCursor( Qt::ArrowCursor );
99 handCursor = new QCursor( Qt::PointingHandCursor );
100 panCursor = new QCursor( Qt::SizeAllCursor );
101 panglCursor = new QCursor( Qt::CrossCursor );
103 SUIT_ResourceMgr* rmgr = SUIT_Session::session()->resourceMgr();
104 zoomCursor = new QCursor( rmgr->loadPixmap( "GraphicsView", tr( "ICON_GV_CURSOR_ZOOM" ) ) );
106 sketchCursor = new QCursor( Qt::CrossCursor );
109 //================================================================
110 // Function : destroyCursors
112 //================================================================
113 void GraphicsView_ViewPort::destroyCursors()
115 delete defCursor; defCursor = 0;
116 delete handCursor; handCursor = 0;
117 delete panCursor; panCursor = 0;
118 delete panglCursor; panglCursor = 0;
119 delete zoomCursor; zoomCursor = 0;
120 delete sketchCursor; sketchCursor = 0;
123 //=======================================================================
124 // Name : GraphicsView_ViewPort
125 // Purpose : Constructor
126 //=======================================================================
127 GraphicsView_ViewPort::GraphicsView_ViewPort( QWidget* theParent )
128 : QGraphicsView( theParent ),
129 myInteractionFlags( 0 ),
131 myViewLabelPosition( VLP_None ),
132 myViewLabelLayout( 0 ),
133 myIsMousePositionEnabled( false ),
134 myForegroundItem( 0 ),
136 myIsTransforming( false ),
137 myHighlightedObject( 0 ),
140 myIsHighlighting( false ),
141 mySelectionIterator( 0 ),
143 myAreSelectionPointsInitialized( false ),
144 mySketchingItem( 0 ),
145 myIsPrepareToSketch( false ),
146 myIsSketching( false ),
147 myIsSketchingByPath( false ),
148 myIsDragging( false ),
149 myIsDragPositionInitialized( false ),
150 myIsPulling( false ),
151 myPullingObject( 0 ),
152 myStoredCursor( Qt::ArrowCursor )
155 myScene = new GraphicsView_Scene( this );
162 setInteractionFlags( EditFlags );
163 //setInteractionFlag( TraceBoundingRect );
164 //setInteractionFlag( DraggingByMiddleButton );
165 //setInteractionFlag( ImmediateContextMenu );
166 //setInteractionFlag( ImmediateSelection );
167 //setInteractionFlag( Sketching );
170 setBackgroundBrush( QBrush( Qt::white ) );
173 myIsForegroundEnabled = false;
174 myForegroundSize = QSizeF( 100, 30 );
175 myForegroundMargin = 0.0;
176 myForegroundColor = Qt::white;
177 myForegroundFrameColor = Qt::black;
178 myForegroundFrameLineWidth = 1.0;
181 myIsGridEnabled = false;
182 myGridCellSize = 100;
183 myGridLineStyle = Qt::DotLine;
184 myGridLineColor = Qt::darkGray;
186 // default index method (BspTreeIndex) leads to
187 // crashes in QGraphicsView::paintEvent() method
188 myScene->setItemIndexMethod( QGraphicsScene::NoIndex );
190 // render hints (default - TextAntialiasing only)
191 setRenderHints( QPainter::Antialiasing |
192 QPainter::TextAntialiasing |
193 QPainter::SmoothPixmapTransform |
194 QPainter::HighQualityAntialiasing );
196 connect( myScene, SIGNAL( gsKeyEvent( QKeyEvent* ) ),
197 this, SLOT( onKeyEvent( QKeyEvent* ) ) );
198 connect( myScene, SIGNAL( gsMouseEvent( QGraphicsSceneMouseEvent* ) ),
199 this, SLOT( onMouseEvent( QGraphicsSceneMouseEvent* ) ) );
200 connect( myScene, SIGNAL( gsWheelEvent( QGraphicsSceneWheelEvent* ) ),
201 this, SLOT( onWheelEvent( QGraphicsSceneWheelEvent* ) ) );
202 connect( myScene, SIGNAL( gsContextMenuEvent( QGraphicsSceneContextMenuEvent* ) ),
203 this, SLOT( onContextMenuEvent( QGraphicsSceneContextMenuEvent* ) ) );
205 connect( myScene, SIGNAL( gsBoundingRectChanged() ),
206 this, SLOT( onBoundingRectChanged() ) );
211 //=======================================================================
212 // Name : GraphicsView_ViewPort
213 // Purpose : Destructor
214 //=======================================================================
215 GraphicsView_ViewPort::~GraphicsView_ViewPort()
226 //================================================================
227 // Function : initialize
229 //================================================================
230 void GraphicsView_ViewPort::initialize()
232 if ( nCounter++ == 0 )
235 setMouseTracking( true );
236 setFocusPolicy( Qt::StrongFocus );
239 //================================================================
240 // Function : cleanup
242 //================================================================
243 void GraphicsView_ViewPort::cleanup()
245 if ( --nCounter == 0 )
249 //================================================================
250 // Function : addItem
252 //================================================================
253 void GraphicsView_ViewPort::addItem( QGraphicsItem* theItem )
255 if( GraphicsView_Object* anObject = dynamic_cast<GraphicsView_Object*>( theItem ) )
257 int aPriority = anObject->getPriority();
258 GraphicsView_ObjectList::iterator anIter, anIterEnd = myObjects.end();
259 for( anIter = myObjects.begin(); anIter != anIterEnd; anIter++ )
261 if( GraphicsView_Object* anObjectRef = *anIter )
263 if( anObjectRef->getPriority() > aPriority )
267 myObjects.insert( anIter, anObject );
268 anObject->setViewTransform( transform() );
269 anObject->addTo( this );
272 myScene->addItem( theItem );
273 onBoundingRectChanged();
276 //================================================================
277 // Function : isItemAdded
279 //================================================================
280 bool GraphicsView_ViewPort::isItemAdded( QGraphicsItem* theItem )
282 if( GraphicsView_Object* anObject = dynamic_cast<GraphicsView_Object*>( theItem ) )
284 for( GraphicsView_ObjectList::iterator anIter = myObjects.begin(); anIter != myObjects.end(); anIter++ )
285 if( theItem == *anIter )
289 for( int i = 0; i < myScene->items().size(); i++ )
290 if( theItem == myScene->items().at(i) )
296 //================================================================
297 // Function : removeItem
299 //================================================================
300 void GraphicsView_ViewPort::removeItem( QGraphicsItem* theItem )
302 if( GraphicsView_Object* anObject = dynamic_cast<GraphicsView_Object*>( theItem ) )
304 if( myHighlightedObject == anObject )
305 myHighlightedObject = 0;
306 mySelectedObjects.removeAll( anObject );
307 myObjects.removeAll( anObject );
308 anObject->removeFrom( this );
311 myScene->removeItem( theItem );
312 onBoundingRectChanged();
315 //================================================================
316 // Function : getObjects
318 //================================================================
319 GraphicsView_ObjectList GraphicsView_ViewPort::getObjects( SortType theSortType ) const
321 if( theSortType == SelectedFirst )
323 // to append selected objects after their non-selected siblings with similar priority
324 int aCurrentPriority = -1;
325 GraphicsView_ObjectList aSelectedObjects;
326 GraphicsView_ObjectList aTopmostObjects;
328 GraphicsView_ObjectList aList;
329 GraphicsView_ObjectListIterator anIter( myObjects );
330 while( anIter.hasNext() )
332 if( GraphicsView_Object* anObject = anIter.next() )
334 if( anObject->isOnTop() )
336 aTopmostObjects.append( anObject );
340 int aPriority = anObject->getPriority();
341 if( aPriority > aCurrentPriority )
343 if( !aSelectedObjects.isEmpty() )
345 aList.append( aSelectedObjects );
346 aSelectedObjects.clear();
348 aCurrentPriority = aPriority;
351 if( anObject->isSelected() )
352 aSelectedObjects.append( anObject );
354 aList.append( anObject );
358 // for selected objects with highest priority,
359 // which were not pushed to the result list yet
360 if( !aSelectedObjects.isEmpty() )
362 aList.append( aSelectedObjects );
363 aSelectedObjects.clear();
366 aList.append( aTopmostObjects );
371 if( theSortType == SortByZLevel ) // double loop, needs to be optimized
373 GraphicsView_ObjectList aList;
375 GraphicsView_ObjectListIterator anIter( myObjects );
376 while( anIter.hasNext() )
378 if( GraphicsView_Object* anObject = anIter.next() )
380 double aZValue = anObject->zValue();
381 GraphicsView_ObjectList::iterator anIter1, anIter1End = aList.end();
382 for( anIter1 = aList.begin(); anIter1 != anIter1End; anIter1++ )
383 if( GraphicsView_Object* anObjectRef = *anIter1 )
384 if( anObjectRef->zValue() > aZValue )
386 aList.insert( anIter1, anObject );
392 return myObjects; // theSortType == NoSorting
395 //================================================================
396 // Function : objectsBoundingRect
398 //================================================================
399 QRectF GraphicsView_ViewPort::objectsBoundingRect( bool theOnlyVisible ) const
402 QListIterator<QGraphicsItem*> anIter( items() );
403 while( anIter.hasNext() )
405 if( GraphicsView_Object* anObject = dynamic_cast<GraphicsView_Object*>( anIter.next() ) )
407 if( theOnlyVisible && !anObject->isVisible() )
410 QRectF anObjectRect = anObject->getRect();
411 if( !anObjectRect.isNull() )
414 aRect = anObject->getRect();
416 aRect |= anObject->getRect();
423 //================================================================
424 // Function : dumpView
426 //================================================================
427 QImage GraphicsView_ViewPort::dumpView( bool theWholeScene,
430 if( !theWholeScene ) // just grab the view contents
432 QPixmap aPixmap = QPixmap::grabWindow( viewport()->winId() );
433 return aPixmap.toImage();
436 // get a bounding rect of all presented objects
437 // (itemsBoundingRect() method is unsuitable)
438 QRectF aRect = objectsBoundingRect();
442 QRectF aTargetRect( 0, 0, aRect.width(), aRect.height() );
443 if( theSize.isValid() )
445 double aRatioX = theSize.width() / aTargetRect.width();
446 double aRatioY = theSize.height() / aTargetRect.height();
447 double aRatio = qMin( aRatioX, aRatioY );
448 aTargetRect.setWidth( aTargetRect.width() * aRatio );
449 aTargetRect.setHeight( aTargetRect.height() * aRatio );
452 // render the scene to an image
453 QImage anImage( aTargetRect.toRect().size(), QImage::Format_RGB32 );
454 QPainter aPainter( &anImage );
455 aPainter.setRenderHints( renderHints() );
457 myScene->render( &aPainter, aTargetRect, aRect );
462 bool GraphicsView_ViewPort::dumpViewToPSFormat(const QString& fileName)
464 QPrinter printer(QPrinter::HighResolution);
465 printer.setOutputFileName(fileName);
467 if (!painter.begin(&printer))
469 myScene->render(&painter);
474 //================================================================
475 // Function : setSceneGap
477 //================================================================
478 void GraphicsView_ViewPort::setSceneGap( double theSceneGap )
480 mySceneGap = theSceneGap;
481 onBoundingRectChanged();
484 //================================================================
485 // Function : setFitAllGap
487 //================================================================
488 void GraphicsView_ViewPort::setFitAllGap( double theFitAllGap )
490 myFitAllGap = theFitAllGap;
493 //================================================================
494 // Function : interactionFlags
496 //================================================================
497 int GraphicsView_ViewPort::interactionFlags() const
499 return myInteractionFlags;
502 //================================================================
503 // Function : hasInteractionFlag
505 //================================================================
506 bool GraphicsView_ViewPort::hasInteractionFlag( InteractionFlag theFlag )
508 return ( interactionFlags() & theFlag ) == theFlag;
511 //================================================================
512 // Function : setInteractionFlag
514 //================================================================
515 void GraphicsView_ViewPort::setInteractionFlag( InteractionFlag theFlag,
519 setInteractionFlags( myInteractionFlags | theFlag );
521 setInteractionFlags( myInteractionFlags & ~theFlag );
524 //================================================================
525 // Function : setInteractionFlags
527 //================================================================
528 void GraphicsView_ViewPort::setInteractionFlags( InteractionFlags theFlags )
530 myInteractionFlags = theFlags;
533 //================================================================
534 // Function : setViewLabelPosition
536 //================================================================
537 void GraphicsView_ViewPort::setViewLabelPosition( ViewLabelPosition thePosition,
540 if( theIsForced && !myViewLabel )
541 myViewLabel = new ViewLabel( viewport() );
546 if( thePosition == VLP_None )
548 myViewLabel->setVisible( false );
552 if( myViewLabelLayout )
553 delete myViewLabelLayout;
555 myViewLabelLayout = new QGridLayout( viewport() );
556 myViewLabelLayout->setMargin( 10 );
557 myViewLabelLayout->setSpacing( 0 );
559 int aRow = 0, aColumn = 0;
560 switch( thePosition )
562 case VLP_TopLeft: aRow = 0; aColumn = 0; break;
563 case VLP_TopRight: aRow = 0; aColumn = 1; break;
564 case VLP_BottomLeft: aRow = 1; aColumn = 0; break;
565 case VLP_BottomRight: aRow = 1; aColumn = 1; break;
569 myViewLabelLayout->addWidget( myViewLabel, aRow, aColumn );
570 myViewLabelLayout->setRowStretch( 1 - aRow, 1 );
571 myViewLabelLayout->setColumnStretch( 1 - aColumn, 1 );
573 myViewLabel->setVisible( true );
576 //================================================================
577 // Function : setViewLabelText
579 //================================================================
580 void GraphicsView_ViewPort::setViewLabelText( const QString& theText )
583 myViewLabel->setText( theText );
586 //================================================================
587 // Function : setMousePositionEnabled
589 //================================================================
590 void GraphicsView_ViewPort::setMousePositionEnabled( bool theState )
592 myIsMousePositionEnabled = theState;
596 setViewLabelPosition( VLP_BottomLeft, true );
598 int aMouseX = 0, aMouseY = 0;
599 setViewLabelText( QString( "(%1, %2)" ).arg( aMouseX ).arg( aMouseY ) );
602 setViewLabelPosition( VLP_None );
605 //================================================================
606 // Function : backgroundColor
608 //================================================================
609 QColor GraphicsView_ViewPort::backgroundColor() const
611 return backgroundBrush().color();
614 //================================================================
615 // Function : setBackgroundColor
617 //================================================================
618 void GraphicsView_ViewPort::setBackgroundColor( const QColor& theColor )
620 setBackgroundBrush( QBrush( theColor ) );
623 //================================================================
624 // Function : setForegroundEnabled
626 //================================================================
627 void GraphicsView_ViewPort::setForegroundEnabled( bool theState )
629 myIsForegroundEnabled = theState;
632 //================================================================
633 // Function : setForegroundSize
635 //================================================================
636 void GraphicsView_ViewPort::setForegroundSize( const QSizeF& theSize )
638 myForegroundSize = theSize;
641 //================================================================
642 // Function : setForegroundMargin
644 //================================================================
645 void GraphicsView_ViewPort::setForegroundMargin( double theMargin )
647 myForegroundMargin = theMargin;
650 //================================================================
651 // Function : setForegroundColor
653 //================================================================
654 void GraphicsView_ViewPort::setForegroundColor( const QColor& theColor )
656 myForegroundColor = theColor;
659 //================================================================
660 // Function : setForegroundFrameColor
662 //================================================================
663 void GraphicsView_ViewPort::setForegroundFrameColor( const QColor& theColor )
665 myForegroundFrameColor = theColor;
668 //================================================================
669 // Function : setForegroundFrameLineWidth
671 //================================================================
672 void GraphicsView_ViewPort::setForegroundFrameLineWidth( double theLineWidth )
674 myForegroundFrameLineWidth = theLineWidth;
677 //================================================================
678 // Function : updateForeground
680 //================================================================
681 void GraphicsView_ViewPort::updateForeground()
683 if( myIsForegroundEnabled )
685 if( !myForegroundItem )
686 myForegroundItem = myScene->addRect( QRectF(), QPen(), QBrush( Qt::white ) );
687 myForegroundItem->setZValue( FOREGROUND_Z_VALUE );
689 QPointF aPoint = QPointF();
690 QRectF aRect( aPoint, myForegroundSize );
691 aRect.adjust( -myForegroundMargin, -myForegroundMargin,
692 myForegroundMargin, myForegroundMargin );
693 myForegroundItem->setRect( aRect );
695 QBrush aBrush = myForegroundItem->brush();
696 aBrush.setColor( myForegroundColor );
697 myForegroundItem->setBrush( aBrush );
699 QPen aPen = myForegroundItem->pen();
700 aPen.setColor( myForegroundFrameColor );
701 aPen.setWidthF( myForegroundFrameLineWidth );
702 myForegroundItem->setPen( aPen );
704 myForegroundItem->setVisible( true );
706 myScene->setSceneRect( aRect.adjusted( -mySceneGap, -mySceneGap, mySceneGap, mySceneGap ) );
710 if( myForegroundItem )
711 myForegroundItem->setVisible( false );
714 updateGrid(); // foreground size could be changed
717 //================================================================
718 // Function : setGridEnabled
720 //================================================================
721 void GraphicsView_ViewPort::setGridEnabled( bool theState )
723 myIsGridEnabled = theState;
726 //================================================================
727 // Function : setGridCellSize
729 //================================================================
730 void GraphicsView_ViewPort::setGridCellSize( int theCellSize )
732 myGridCellSize = theCellSize;
735 //================================================================
736 // Function : setGridLineStyle
738 //================================================================
739 void GraphicsView_ViewPort::setGridLineStyle( int theLineStyle )
741 myGridLineStyle = theLineStyle;
744 //================================================================
745 // Function : setGridLineColor
747 //================================================================
748 void GraphicsView_ViewPort::setGridLineColor( const QColor& theLineColor )
750 myGridLineColor = theLineColor;
753 //================================================================
754 // Function : updateGrid
756 //================================================================
757 void GraphicsView_ViewPort::updateGrid()
759 if( myIsGridEnabled )
762 myGridItem = myScene->addPath( QPainterPath() );
763 myGridItem->setZValue( GRID_Z_VALUE );
765 double aWidth = myForegroundSize.width();
766 double aHeight = myForegroundSize.height();
768 int aGridNbX = int( aWidth / myGridCellSize ) + 1;
769 int aGridNbY = int( aHeight / myGridCellSize ) + 1;
773 for( anIndex = 0; anIndex < aGridNbX; anIndex++ )
775 double x = myGridCellSize * (double)anIndex;
776 aPath.moveTo( x, 0 );
777 aPath.lineTo( x, aHeight );
779 for( anIndex = 0; anIndex < aGridNbY; anIndex++ )
781 double y = myGridCellSize * (double)anIndex;
782 aPath.moveTo( 0, y );
783 aPath.lineTo( aWidth, y );
785 myGridItem->setPath( aPath );
787 QPen aPen = myGridItem->pen();
788 aPen.setStyle( (Qt::PenStyle)myGridLineStyle );
789 aPen.setColor( myGridLineColor );
790 myGridItem->setPen( aPen );
792 myGridItem->setVisible( true );
797 myGridItem->setVisible( false );
801 //================================================================
804 //================================================================
805 void GraphicsView_ViewPort::reset()
810 //================================================================
813 //================================================================
814 void GraphicsView_ViewPort::pan( double theDX, double theDY )
816 myIsTransforming = true;
819 myViewLabel->setAcceptMoveEvents( false );
821 if( QScrollBar* aHBar = horizontalScrollBar() )
822 aHBar->setValue( aHBar->value() - theDX );
823 if( QScrollBar* aVBar = verticalScrollBar() )
824 aVBar->setValue( aVBar->value() + theDY );
827 myViewLabel->setAcceptMoveEvents( true );
829 myIsTransforming = false;
834 //================================================================
835 // Function : setCenter
837 //================================================================
838 void GraphicsView_ViewPort::setCenter( double theX, double theY )
840 myIsTransforming = true;
842 setTransform( myCurrentTransform );
843 centerOn( theX, theY );
845 myIsTransforming = false;
850 //================================================================
853 //================================================================
854 void GraphicsView_ViewPort::zoom( double theX1, double theY1, double theX2, double theY2 )
856 myIsTransforming = true;
858 double aDX = theX2 - theX1;
859 double aDY = theY2 - theY1;
860 double aZoom = sqrt( aDX * aDX + aDY * aDY ) / 100 + 1;
861 aZoom = ( aDX > 0 ) ? aZoom : 1 / aZoom;
863 QTransform aTransform = transform();
864 aTransform.scale( aZoom, aZoom );
865 double aM11 = aTransform.m11();
866 double aM22 = aTransform.m22();
867 // increasing of diagonal coefficients (>300) leads to a crash sometimes
868 // at the values of 100 some primitives are drawn incorrectly
869 if( qMax( aM11, aM22 ) < 100 )
870 setTransform( aTransform );
872 myIsTransforming = false;
877 //================================================================
878 // Function : fitRect
880 //================================================================
881 void GraphicsView_ViewPort::fitRect( const QRectF& theRect )
883 myIsTransforming = true;
885 fitInView( theRect, Qt::KeepAspectRatio );
887 myIsTransforming = false;
892 //================================================================
893 // Function : fitSelect
895 //================================================================
896 void GraphicsView_ViewPort::fitSelect()
898 myIsTransforming = true;
901 for( initSelected(); moreSelected(); nextSelected() )
903 if( GraphicsView_Object* aMovingObject = selectedObject() )
905 QRectF aRect = aMovingObject->getRect();
906 if( aGlobalRect.isNull() )
909 aGlobalRect |= aRect;
913 if( !aGlobalRect.isNull() )
915 double aGap = qMax( aGlobalRect.width(), aGlobalRect.height() ) / 5;
916 aGlobalRect.adjust( -aGap, -aGap, aGap, aGap );
917 fitInView( aGlobalRect, Qt::KeepAspectRatio );
920 myIsTransforming = false;
925 //================================================================
928 //================================================================
929 void GraphicsView_ViewPort::fitAll( bool theKeepScale )
931 myIsTransforming = true;
934 myCurrentTransform = transform();
936 double aGap = myFitAllGap;
937 QRectF aRect = objectsBoundingRect( true );
938 fitInView( aRect.adjusted( -aGap, -aGap, aGap, aGap ), Qt::KeepAspectRatio );
940 myIsTransforming = false;
945 //================================================================
946 // Function : fitWidth
948 //================================================================
949 void GraphicsView_ViewPort::fitWidth()
951 myIsTransforming = true;
953 double aGap = myFitAllGap;
954 QRectF aRect = objectsBoundingRect( true );
956 double aTop = aRect.top();
957 double aLeft = aRect.left();
960 aRect.setY( aRect.center().y() );
961 aRect.setHeight( aMargin );
963 fitInView( aRect.adjusted( -aGap, -aGap, aGap, aGap ), Qt::KeepAspectRatio );
964 ensureVisible( aLeft, aTop, aMargin, aMargin, 0, aGap );
966 myIsTransforming = false;
971 //================================================================
972 // Function : applyTransform
974 //================================================================
975 void GraphicsView_ViewPort::applyTransform()
977 GraphicsView_ObjectListIterator anIter( getObjects() );
978 while( anIter.hasNext() )
979 if( GraphicsView_Object* anObject = anIter.next() )
980 anObject->setViewTransform( transform() );
983 //================================================================
984 // Function : currentBlock
986 //================================================================
987 GraphicsView_ViewPort::BlockStatus GraphicsView_ViewPort::currentBlock()
989 if( isDragging() && !myDragPosition.isNull() )
990 return BlockStatus( BS_Selection );
992 if( myAreSelectionPointsInitialized && ( myFirstSelectionPoint != myLastSelectionPoint ) )
993 return BlockStatus( BS_Selection );
996 return BlockStatus( BS_Selection );
1001 //================================================================
1002 // Function : highlight
1004 //================================================================
1005 void GraphicsView_ViewPort::highlight( double theX, double theY )
1007 myIsHighlighting = true;
1008 myHighlightX = theX;
1009 myHighlightY = theY;
1011 bool anIsHighlighted = false;
1012 bool anIsOnObject = false;
1014 GraphicsView_Object* aPreviousHighlightedObject = myHighlightedObject;
1015 GraphicsView_Object* aHighlightedObject = 0;
1019 GraphicsView_ObjectList aList = getObjects( SortByZLevel );
1020 GraphicsView_ObjectListIterator anIter( aList );
1021 anIter.toBack(); // objects with higher priority have to be checked earlier
1022 while( anIter.hasPrevious() )
1024 if( GraphicsView_Object* anObject = anIter.previous() )
1026 if( anObject->isVisible() && anObject->isSelectable() )
1028 if( anObject->checkHighlight( theX, theY, aCursor ) )
1030 anIsOnObject = true;
1031 anIsHighlighted = anObject->highlight( theX, theY );
1034 if( anIsHighlighted )
1036 aHighlightedObject = anObject;
1043 setCursor( aCursor );
1048 while( anIter.hasNext() )
1049 if( GraphicsView_Object* anObject = anIter.next() )
1050 anObject->unhighlight();
1052 myHighlightedObject = 0;
1055 else if( !myHighlightedObject && anIsHighlighted )
1057 myHighlightedObject = aHighlightedObject;
1059 else if( myHighlightedObject && !anIsHighlighted )
1061 myHighlightedObject->unhighlight();
1062 myHighlightedObject = 0;
1064 else if( myHighlightedObject && anIsHighlighted )
1066 myHighlightedObject->highlight( theX, theY );
1067 if( myHighlightedObject != aHighlightedObject )
1069 myHighlightedObject->unhighlight();
1070 myHighlightedObject = aHighlightedObject;
1075 //================================================================
1076 // Function : clearHighlighted
1078 //================================================================
1079 void GraphicsView_ViewPort::clearHighlighted()
1081 if( myHighlightedObject )
1083 myHighlightedObject->unhighlight();
1084 myHighlightedObject = 0;
1088 //================================================================
1089 // Function : select
1091 //================================================================
1092 int GraphicsView_ViewPort::select( const QRectF& theRect, bool theIsAppend )
1094 if( !myIsHighlighting )
1095 return GVSS_NoChanged;
1097 GV_SelectionStatus aStatus = GVSS_Invalid;
1098 if( theRect.isNull() ) // point selection
1100 if( myHighlightedObject )
1102 if( mySelectedObjects.count() == 1 &&
1103 mySelectedObjects.first() == myHighlightedObject )
1104 aStatus = GVSS_LocalChanged;
1108 GraphicsView_ObjectListIterator anIter( mySelectedObjects );
1109 while( anIter.hasNext() )
1110 if( GraphicsView_Object* anObject = anIter.next() )
1111 if( myHighlightedObject != anObject )
1112 anObject->unselect();
1114 if( !mySelectedObjects.isEmpty() && aStatus == GVSS_Invalid )
1115 aStatus = GVSS_GlobalChanged;
1116 mySelectedObjects.clear();
1118 else if( myHighlightedObject->isSelected() && aStatus != GVSS_LocalChanged )
1120 mySelectedObjects.removeAll( myHighlightedObject );
1121 myHighlightedObject->unselect();
1123 if( !mySelectedObjects.isEmpty() && aStatus == GVSS_Invalid )
1124 aStatus = GVSS_GlobalChanged;
1129 if( myHighlightedObject->select( myHighlightX, myHighlightY, QRectF() ) &&
1130 mySelectedObjects.indexOf( myHighlightedObject ) == -1 )
1132 mySelectedObjects.append( myHighlightedObject );
1133 if( aStatus == GVSS_Invalid )
1134 aStatus = GVSS_GlobalChanged;
1136 else if( aStatus == GVSS_LocalChanged )
1137 aStatus = GVSS_GlobalChanged;
1142 if( !myHighlightedObject )
1146 GraphicsView_ObjectListIterator anIter( mySelectedObjects );
1147 while( anIter.hasNext() )
1148 if( GraphicsView_Object* anObject = anIter.next() )
1149 if( myHighlightedObject != anObject )
1150 anObject->unselect();
1152 if( !mySelectedObjects.isEmpty() )
1153 aStatus = GVSS_GlobalChanged;
1154 mySelectedObjects.clear();
1159 return GVSS_NoChanged;
1161 else // rectangle selection
1163 aStatus = GVSS_NoChanged;
1165 bool updateAll = false;
1168 if( !mySelectedObjects.isEmpty() )
1169 aStatus = GVSS_GlobalChanged;
1171 GraphicsView_ObjectListIterator anIter( mySelectedObjects );
1172 while( anIter.hasNext() )
1173 if( GraphicsView_Object* anObject = anIter.next() )
1174 if( myHighlightedObject != anObject )
1175 anObject->unselect();
1176 mySelectedObjects.clear();
1179 QListIterator<QGraphicsItem*> anIter( items() );
1180 while( anIter.hasNext() )
1182 if( GraphicsView_Object* anObject = dynamic_cast<GraphicsView_Object*>( anIter.next() ) )
1184 if( anObject->isVisible() && anObject->isSelectable() )
1186 bool anIsSelected = false;
1187 QRectF aRect = anObject->getRect();
1188 if( theRect.contains( aRect ) && myIsHighlighting )
1189 anIsSelected = anObject->select( myHighlightX, myHighlightY, theRect );
1191 if( anIsSelected && mySelectedObjects.indexOf( anObject ) == -1 )
1193 mySelectedObjects.append( anObject );
1194 aStatus = GVSS_GlobalChanged;
1203 //================================================================
1204 // Function : clearSelected
1206 //================================================================
1207 void GraphicsView_ViewPort::clearSelected()
1209 GraphicsView_ObjectListIterator anIter( mySelectedObjects );
1210 while( anIter.hasNext() )
1211 if( GraphicsView_Object* anObject = anIter.next() )
1212 anObject->unselect();
1213 mySelectedObjects.clear();
1216 //================================================================
1217 // Function : setSelected
1219 //================================================================
1220 void GraphicsView_ViewPort::setSelected( GraphicsView_Object* theObject )
1224 theObject->setSelected( true );
1225 mySelectedObjects.append( theObject );
1229 //================================================================
1230 // Function : nbSelected
1232 //================================================================
1233 int GraphicsView_ViewPort::nbSelected() const
1235 return mySelectedObjects.count();
1238 //================================================================
1239 // Function : initSelected
1241 //================================================================
1242 void GraphicsView_ViewPort::initSelected()
1244 mySelectionIterator = 0;
1247 //================================================================
1248 // Function : moreSelected
1250 //================================================================
1251 bool GraphicsView_ViewPort::moreSelected()
1253 return mySelectionIterator < nbSelected();
1256 //================================================================
1257 // Function : nextSelected
1259 //================================================================
1260 bool GraphicsView_ViewPort::nextSelected()
1262 if( mySelectionIterator >= 0 && mySelectionIterator < nbSelected() )
1264 mySelectionIterator++;
1270 //================================================================
1271 // Function : selectedObject
1273 //================================================================
1274 GraphicsView_Object* GraphicsView_ViewPort::selectedObject()
1276 if( mySelectionIterator >= 0 && mySelectionIterator < nbSelected() )
1277 return mySelectedObjects[ mySelectionIterator ];
1281 //================================================================
1282 // Function : startSelectByRect
1284 //================================================================
1285 void GraphicsView_ViewPort::startSelectByRect( int x, int y )
1287 if( !myAreSelectionPointsInitialized )
1289 myFirstSelectionPoint = QPoint( x, y );
1290 myLastSelectionPoint = QPoint( x, y );
1291 myAreSelectionPointsInitialized = true;
1296 myRectBand = new QRubberBand( QRubberBand::Rectangle, this );
1298 palette.setColor( myRectBand->foregroundRole(), Qt::white );
1299 myRectBand->setPalette( palette );
1304 //================================================================
1305 // Function : drawSelectByRect
1307 //================================================================
1308 void GraphicsView_ViewPort::drawSelectByRect( int x, int y )
1310 if( myAreSelectionPointsInitialized )
1314 myLastSelectionPoint.setX( x );
1315 myLastSelectionPoint.setY( y );
1317 QRect aRect = selectionRect();
1318 myRectBand->setGeometry( aRect );
1319 myRectBand->setVisible( aRect.isValid() );
1323 //================================================================
1324 // Function : isSelectByRect
1326 //================================================================
1327 bool GraphicsView_ViewPort::isSelectByRect() const
1329 return myAreSelectionPointsInitialized;
1332 //================================================================
1333 // Function : finishSelectByRect
1335 //================================================================
1336 void GraphicsView_ViewPort::finishSelectByRect()
1338 if( myAreSelectionPointsInitialized )
1347 myAreSelectionPointsInitialized = false;
1351 //================================================================
1352 // Function : selectionRect
1354 //================================================================
1355 QRect GraphicsView_ViewPort::selectionRect()
1358 if( myAreSelectionPointsInitialized )
1360 aRect.setLeft( qMin( myFirstSelectionPoint.x(), myLastSelectionPoint.x() ) );
1361 aRect.setTop( qMin( myFirstSelectionPoint.y(), myLastSelectionPoint.y() ) );
1362 aRect.setRight( qMax( myFirstSelectionPoint.x(), myLastSelectionPoint.x() ) );
1363 aRect.setBottom( qMax( myFirstSelectionPoint.y(), myLastSelectionPoint.y() ) );
1368 //================================================================
1369 // Function : prepareToSketch
1371 //================================================================
1372 void GraphicsView_ViewPort::prepareToSketch( bool theStatus )
1374 myIsPrepareToSketch = theStatus;
1376 setCursor( *getSketchCursor() );
1379 //================================================================
1380 // Function : isPrepareToSketch
1382 //================================================================
1383 bool GraphicsView_ViewPort::isPrepareToSketch()
1385 return myIsPrepareToSketch;
1388 //================================================================
1389 // Function : startSketching
1391 //================================================================
1392 void GraphicsView_ViewPort::startSketching( const QPointF& thePoint,
1395 prepareToSketch( false );
1397 if( !mySketchingItem )
1399 mySketchingItem = new QGraphicsPathItem();
1400 mySketchingItem->setZValue( SKETCH_Z_VALUE );
1402 QPen aPen = mySketchingItem->pen();
1403 aPen.setStyle( Qt::DotLine );
1404 mySketchingItem->setPen( aPen );
1406 addItem( mySketchingItem );
1409 mySketchingPoint = thePoint;
1412 aPath.moveTo( mySketchingPoint );
1413 mySketchingItem->setPath( aPath );
1414 mySketchingItem->setVisible( true );
1416 myIsSketching = true;
1417 myIsSketchingByPath = theIsPath;
1420 //================================================================
1421 // Function : drawSketching
1423 //================================================================
1424 void GraphicsView_ViewPort::drawSketching( const QPointF& thePoint )
1426 bool anIsPath = false;
1427 if( mySketchingItem && isSketching( &anIsPath ) )
1429 QPainterPath aPath = mySketchingItem->path();
1430 if( anIsPath ) // arbitrary path
1431 aPath.lineTo( thePoint );
1434 // make a valid rectangle
1435 double x1 = mySketchingPoint.x(), y1 = mySketchingPoint.y();
1436 double x2 = thePoint.x(), y2 = thePoint.y();
1437 QPointF aPoint1( qMin( x1, x2 ), qMin( y1, y2 ) );
1438 QPointF aPoint2( qMax( x1, x2 ), qMax( y1, y2 ) );
1439 QRectF aRect( aPoint1, aPoint2 );
1441 aPath = QPainterPath();
1442 aPath.addRect( aRect );
1444 mySketchingItem->setPath( aPath );
1448 //================================================================
1449 // Function : finishSketching
1451 //================================================================
1452 void GraphicsView_ViewPort::finishSketching( bool theStatus )
1454 prepareToSketch( false );
1456 mySketchingItem->setVisible( false );
1457 myIsSketching = false;
1459 setCursor( *getDefaultCursor() );
1463 QPainterPath aPath = mySketchingItem->path();
1464 emit vpSketchingFinished( aPath );
1468 //================================================================
1469 // Function : isSketching
1471 //================================================================
1472 bool GraphicsView_ViewPort::isSketching( bool* theIsPath ) const
1475 *theIsPath = myIsSketchingByPath;
1476 return myIsSketching;
1479 //================================================================
1480 // Function : dragObjects
1482 //================================================================
1483 void GraphicsView_ViewPort::dragObjects( QGraphicsSceneMouseEvent* e )
1485 GraphicsView_Object* anObject = getHighlightedObject();
1487 if( myDragPosition.isNull() )
1489 myDragPosition = e->scenePos();
1493 GraphicsView_ObjectList anObjectsToMove;
1494 if( anObject && anObject->isMovable() && ( e->buttons() & Qt::LeftButton ) )
1496 if( anObject->isSelected() )
1498 for( initSelected(); moreSelected(); nextSelected() )
1499 if( GraphicsView_Object* aMovingObject = selectedObject() )
1500 if( aMovingObject->isMovable() )
1501 anObjectsToMove.append( aMovingObject );
1504 anObjectsToMove.append( anObject );
1506 else if( hasInteractionFlag( DraggingByMiddleButton ) &&
1507 nbSelected() && ( e->buttons() & Qt::MidButton ) )
1509 for( initSelected(); moreSelected(); nextSelected() )
1510 if( GraphicsView_Object* aMovingObject = selectedObject() )
1511 if( aMovingObject->isMovable() )
1512 anObjectsToMove.append( aMovingObject );
1515 if( anObjectsToMove.isEmpty() )
1518 double aDX = e->scenePos().x() - myDragPosition.x();
1519 double aDY = e->scenePos().y() - myDragPosition.y();
1521 bool anIsMovingByXAllowed = true, anIsMovingByYAllowed = true;
1522 GraphicsView_ObjectListIterator anIter( anObjectsToMove );
1523 while( anIter.hasNext() )
1524 if( GraphicsView_Object* aMovingObject = anIter.next() )
1526 if( !aMovingObject->isMovingByXAllowed( aDX ) )
1527 anIsMovingByXAllowed = false;
1528 if( !aMovingObject->isMovingByYAllowed( aDY ) )
1529 anIsMovingByYAllowed = false;
1532 if( !anIsMovingByXAllowed && !anIsMovingByYAllowed )
1533 return; // myDragPosition shouldn't be changed
1535 if( !anIsMovingByXAllowed )
1538 if( !anIsMovingByYAllowed )
1541 anIter = anObjectsToMove;
1542 while( anIter.hasNext() )
1543 if( GraphicsView_Object* aMovingObject = anIter.next() )
1544 aMovingObject->move( aDX, aDY );
1546 if( anIsMovingByXAllowed )
1547 myDragPosition.setX( e->scenePos().x() );
1549 if( anIsMovingByYAllowed )
1550 myDragPosition.setY( e->scenePos().y() );
1553 //================================================================
1554 // Function : startPulling
1556 //================================================================
1557 bool GraphicsView_ViewPort::startPulling( const QPointF& thePoint )
1559 QListIterator<QGraphicsItem*> anIter( items() );
1560 while( anIter.hasNext() )
1562 if( GraphicsView_Object* anObject = dynamic_cast<GraphicsView_Object*>( anIter.next() ) )
1564 QRectF aRect = anObject->getPullingRect();
1565 if( aRect.contains( thePoint ) && anObject->startPulling( thePoint ) )
1568 myPullingObject = anObject;
1569 //setCursor( *getHandCursor() ); // testing ImageViewer
1577 //================================================================
1578 // Function : drawPulling
1580 //================================================================
1581 void GraphicsView_ViewPort::drawPulling( const QPointF& thePoint )
1583 GraphicsView_Object* aLockedObject = 0;
1585 QListIterator<QGraphicsItem*> anIter( items() );
1586 while( anIter.hasNext() )
1588 if( GraphicsView_Object* anObject = dynamic_cast<GraphicsView_Object*>( anIter.next() ) )
1590 if( !anObject->isVisible() )
1593 QRectF aRect = anObject->getPullingRect();
1594 if( aRect.contains( thePoint ) && anObject->portContains( thePoint ) )
1596 aLockedObject = anObject;
1602 myPullingObject->pull( thePoint, aLockedObject, getSelectedObjects() );
1605 //================================================================
1606 // Function : finishPulling
1608 //================================================================
1609 void GraphicsView_ViewPort::finishPulling( bool theStatus )
1611 myIsPulling = false;
1612 myPullingObject->finishPulling( theStatus, getSelectedObjects() );
1613 setCursor( *getDefaultCursor() );
1616 //================================================================
1617 // Function : cancelCurrentOperation
1619 //================================================================
1620 bool GraphicsView_ViewPort::cancelCurrentOperation()
1622 myIsHighlighting = false;
1626 for( initSelected(); moreSelected(); nextSelected() )
1627 if( GraphicsView_Object* aMovingObject = selectedObject() )
1628 aMovingObject->finishMove( false );
1630 if( GraphicsView_Object* aMovingObject = getHighlightedObject() )
1631 aMovingObject->finishMove( false );
1633 myIsDragging = false;
1634 myDragPosition = QPointF();
1635 //setCursor( myStoredCursor );
1636 setCursor( *getDefaultCursor() );
1643 finishPulling( false );
1647 if( isSketching() || isPrepareToSketch() )
1649 finishSketching( false );
1656 //================================================================
1657 // Function : onBoundingRectChanged
1659 //================================================================
1660 void GraphicsView_ViewPort::onBoundingRectChanged()
1662 if( hasInteractionFlag( TraceBoundingRect ) )
1664 QRectF aRect = objectsBoundingRect( true );
1665 myScene->setSceneRect( aRect.adjusted( -mySceneGap, -mySceneGap, mySceneGap, mySceneGap ) );
1669 //================================================================
1670 // Function : onKeyEvent
1672 //================================================================
1673 void GraphicsView_ViewPort::onKeyEvent( QKeyEvent* e )
1675 emit vpKeyEvent( e );
1678 //================================================================
1679 // Function : onMouseEvent
1681 //================================================================
1682 void GraphicsView_ViewPort::onMouseEvent( QGraphicsSceneMouseEvent* e )
1684 emit vpMouseEvent( e );
1686 bool anIsHandled = false;
1689 case QEvent::GraphicsSceneMousePress:
1691 if( hasInteractionFlag( EditFlags ) && nbSelected() )
1692 for( initSelected(); moreSelected() && !anIsHandled; nextSelected() )
1693 if( GraphicsView_Object* anObject = selectedObject() )
1694 anIsHandled = anObject->handleMousePress( e );
1696 if( !anIsHandled && hasInteractionFlag( Dragging ) )
1698 bool anAccel = e->modifiers() & GraphicsView_ViewTransformer::accelKey();
1699 if( ( getHighlightedObject() &&
1700 getHighlightedObject()->isMovable() &&
1701 !( anAccel || e->button() != Qt::LeftButton ) ) ||
1702 ( hasInteractionFlag( DraggingByMiddleButton ) &&
1703 nbSelected() && !anAccel && e->button() == Qt::MidButton ) )
1705 myIsDragging = true;
1706 myStoredCursor = cursor();
1707 setCursor( Qt::ClosedHandCursor );
1712 case QEvent::GraphicsSceneMouseMove:
1714 if( hasInteractionFlag( EditFlags ) && nbSelected() )
1715 for( initSelected(); moreSelected() && !anIsHandled; nextSelected() )
1716 if( GraphicsView_Object* anObject = selectedObject() )
1717 anIsHandled = anObject->handleMouseMove( e );
1719 if( !anIsHandled && !isPulling() && myIsDragging )
1723 case QEvent::GraphicsSceneMouseRelease:
1725 if( hasInteractionFlag( EditFlags ) && nbSelected() )
1726 for( initSelected(); moreSelected() && !anIsHandled; nextSelected() )
1727 if( GraphicsView_Object* anObject = selectedObject() )
1728 anIsHandled = anObject->handleMouseRelease( e );
1730 if( !anIsHandled && !isPulling() && myIsDragging )
1732 emit vpObjectBeforeMoving();
1734 bool anIsMoved = false;
1735 for( initSelected(); moreSelected(); nextSelected() )
1736 if( GraphicsView_Object* aMovingObject = selectedObject() )
1737 anIsMoved = aMovingObject->finishMove( true ) || anIsMoved;
1739 if( GraphicsView_Object* aMovingObject = getHighlightedObject() )
1740 anIsMoved = aMovingObject->finishMove( true ) || anIsMoved;
1742 myIsDragging = false;
1743 myDragPosition = QPointF();
1744 setCursor( myStoredCursor );
1746 emit vpObjectAfterMoving( anIsMoved );
1750 case QEvent::GraphicsSceneMouseDoubleClick:
1751 break; // do nothing, just emit the signal
1756 if( myIsMousePositionEnabled )
1758 int aMouseX = (int)e->scenePos().x();
1759 int aMouseY = (int)e->scenePos().y();
1760 setViewLabelText( QString( "(%1, %2)" ).arg( aMouseX ).arg( aMouseY ) );
1764 //================================================================
1765 // Function : onWheelEvent
1767 //================================================================
1768 void GraphicsView_ViewPort::onWheelEvent( QGraphicsSceneWheelEvent* e )
1770 emit vpWheelEvent( e );
1773 //================================================================
1774 // Function : onContextMenuEvent
1776 //================================================================
1777 void GraphicsView_ViewPort::onContextMenuEvent( QGraphicsSceneContextMenuEvent* e )
1779 emit vpContextMenuEvent( e );
1782 //================================================================
1783 // Function : scrollContentsBy
1785 //================================================================
1786 void GraphicsView_ViewPort::scrollContentsBy( int theDX, int theDY )
1789 myViewLabel->setAcceptMoveEvents( false );
1791 QGraphicsView::scrollContentsBy( theDX, theDY );
1794 myViewLabel->setAcceptMoveEvents( true );