From 575f1a6b443ad007af8b0c27b2c29ad98e5db86d Mon Sep 17 00:00:00 2001 From: ouv Date: Tue, 9 Jul 2013 07:46:22 +0000 Subject: [PATCH] Sketching. --- src/GraphicsView/GraphicsView_ViewFrame.cxx | 3 + src/GraphicsView/GraphicsView_ViewFrame.h | 2 + src/GraphicsView/GraphicsView_ViewPort.cxx | 144 ++++++++++++++++++-- src/GraphicsView/GraphicsView_ViewPort.h | 21 +++ src/GraphicsView/GraphicsView_Viewer.cxx | 100 ++++++++++++-- src/GraphicsView/GraphicsView_Viewer.h | 8 +- 6 files changed, 257 insertions(+), 21 deletions(-) diff --git a/src/GraphicsView/GraphicsView_ViewFrame.cxx b/src/GraphicsView/GraphicsView_ViewFrame.cxx index 9838a4059..89f9e0b31 100644 --- a/src/GraphicsView/GraphicsView_ViewFrame.cxx +++ b/src/GraphicsView/GraphicsView_ViewFrame.cxx @@ -75,6 +75,9 @@ GraphicsView_ViewFrame::GraphicsView_ViewFrame( SUIT_Desktop* d, GraphicsView_Vi this, SLOT( wheelEvent( QGraphicsSceneWheelEvent* ) ) ); connect( myViewPort, SIGNAL( vpContextMenuEvent( QGraphicsSceneContextMenuEvent* ) ), this, SLOT( contextMenuEvent( QGraphicsSceneContextMenuEvent* ) ) ); + + connect( myViewPort, SIGNAL( vpSketchingFinished( QPainterPath ) ), + this, SIGNAL( sketchingFinished( QPainterPath ) ) ); } //======================================================================= diff --git a/src/GraphicsView/GraphicsView_ViewFrame.h b/src/GraphicsView/GraphicsView_ViewFrame.h index 0bff15a74..8e3ffe60a 100644 --- a/src/GraphicsView/GraphicsView_ViewFrame.h +++ b/src/GraphicsView/GraphicsView_ViewFrame.h @@ -85,6 +85,8 @@ signals: void mouseDoubleClicked( QGraphicsSceneMouseEvent* ); void wheeling( QGraphicsSceneWheelEvent* ); + void sketchingFinished( QPainterPath ); + private: void createActions(); int createToolBar(); diff --git a/src/GraphicsView/GraphicsView_ViewPort.cxx b/src/GraphicsView/GraphicsView_ViewPort.cxx index 026932eeb..9b2aa85ac 100644 --- a/src/GraphicsView/GraphicsView_ViewPort.cxx +++ b/src/GraphicsView/GraphicsView_ViewPort.cxx @@ -40,12 +40,17 @@ #include +#define FOREGROUND_Z_VALUE -2 +#define GRID_Z_VALUE -1 +#define SKETCH_Z_VALUE 3000 + int GraphicsView_ViewPort::nCounter = 0; QCursor* GraphicsView_ViewPort::defCursor = 0; QCursor* GraphicsView_ViewPort::handCursor = 0; QCursor* GraphicsView_ViewPort::panCursor = 0; QCursor* GraphicsView_ViewPort::panglCursor = 0; QCursor* GraphicsView_ViewPort::zoomCursor = 0; +QCursor* GraphicsView_ViewPort::sketchCursor = 0; //======================================================================= // Name : GraphicsView_ViewPort::NameLabel @@ -99,6 +104,8 @@ void GraphicsView_ViewPort::createCursors () SUIT_ResourceMgr* rmgr = SUIT_Session::session()->resourceMgr(); zoomCursor = new QCursor( rmgr->loadPixmap( "GraphicsView", tr( "ICON_GV_CURSOR_ZOOM" ) ) ); + + sketchCursor = new QCursor( Qt::CrossCursor ); } //================================================================ @@ -107,11 +114,12 @@ void GraphicsView_ViewPort::createCursors () //================================================================ void GraphicsView_ViewPort::destroyCursors() { - delete defCursor; defCursor = 0; - delete handCursor; handCursor = 0; - delete panCursor; panCursor = 0; - delete panglCursor; panglCursor = 0; - delete zoomCursor; zoomCursor = 0; + delete defCursor; defCursor = 0; + delete handCursor; handCursor = 0; + delete panCursor; panCursor = 0; + delete panglCursor; panglCursor = 0; + delete zoomCursor; zoomCursor = 0; + delete sketchCursor; sketchCursor = 0; } //======================================================================= @@ -134,6 +142,10 @@ GraphicsView_ViewPort::GraphicsView_ViewPort( QWidget* theParent ) mySelectionIterator( 0 ), myRectBand( 0 ), myAreSelectionPointsInitialized( false ), + mySketchingItem( 0 ), + myIsPrepareToSketch( false ), + myIsSketching( false ), + myIsSketchingByPath( false ), myIsDragging( false ), myIsDragPositionInitialized( false ), myIsPulling( false ), @@ -152,7 +164,8 @@ GraphicsView_ViewPort::GraphicsView_ViewPort( QWidget* theParent ) //setInteractionFlag( TraceBoundingRect ); //setInteractionFlag( DraggingByMiddleButton ); //setInteractionFlag( ImmediateContextMenu ); - setInteractionFlag( ImmediateSelection ); + setInteractionFlag( ImmediateSelection ); // testing ImageViewer + setInteractionFlag( Sketching ); // testing ImageViewer // background setBackgroundBrush( QBrush( Qt::white ) ); @@ -621,7 +634,7 @@ void GraphicsView_ViewPort::updateForeground() { if( !myForegroundItem ) myForegroundItem = myScene->addRect( QRectF(), QPen(), QBrush( Qt::white ) ); - myForegroundItem->setZValue( -2 ); + myForegroundItem->setZValue( FOREGROUND_Z_VALUE ); QPointF aPoint = QPointF(); QRectF aRect( aPoint, myForegroundSize ); @@ -697,7 +710,7 @@ void GraphicsView_ViewPort::updateGrid() { if( !myGridItem ) myGridItem = myScene->addPath( QPainterPath() ); - myGridItem->setZValue( -1 ); + myGridItem->setZValue( GRID_Z_VALUE ); double aWidth = myForegroundSize.width(); double aHeight = myForegroundSize.height(); @@ -1302,6 +1315,117 @@ QRect GraphicsView_ViewPort::selectionRect() return aRect; } +//================================================================ +// Function : prepareToSketch +// Purpose : +//================================================================ +void GraphicsView_ViewPort::prepareToSketch( bool theStatus ) +{ + myIsPrepareToSketch = theStatus; + if( theStatus ) + setCursor( *getSketchCursor() ); +} + +//================================================================ +// Function : isPrepareToSketch +// Purpose : +//================================================================ +bool GraphicsView_ViewPort::isPrepareToSketch() +{ + return myIsPrepareToSketch; +} + +//================================================================ +// Function : startSketching +// Purpose : +//================================================================ +void GraphicsView_ViewPort::startSketching( const QPointF& thePoint, + bool theIsPath ) +{ + prepareToSketch( false ); + + if( !mySketchingItem ) + { + mySketchingItem = new QGraphicsPathItem(); + mySketchingItem->setZValue( SKETCH_Z_VALUE ); + + QPen aPen = mySketchingItem->pen(); + aPen.setStyle( Qt::DotLine ); + mySketchingItem->setPen( aPen ); + + addItem( mySketchingItem ); + } + + mySketchingPoint = thePoint; + + QPainterPath aPath; + aPath.moveTo( mySketchingPoint ); + mySketchingItem->setPath( aPath ); + mySketchingItem->setVisible( true ); + + myIsSketching = true; + myIsSketchingByPath = theIsPath; +} + +//================================================================ +// Function : drawSketching +// Purpose : +//================================================================ +void GraphicsView_ViewPort::drawSketching( const QPointF& thePoint ) +{ + bool anIsPath = false; + if( mySketchingItem && isSketching( &anIsPath ) ) + { + QPainterPath aPath = mySketchingItem->path(); + if( anIsPath ) // arbitrary path + aPath.lineTo( thePoint ); + else // rectangle + { + // make a valid rectangle + double x1 = mySketchingPoint.x(), y1 = mySketchingPoint.y(); + double x2 = thePoint.x(), y2 = thePoint.y(); + QPointF aPoint1( qMin( x1, x2 ), qMin( y1, y2 ) ); + QPointF aPoint2( qMax( x1, x2 ), qMax( y1, y2 ) ); + QRectF aRect( aPoint1, aPoint2 ); + + aPath = QPainterPath(); + aPath.addRect( aRect ); + } + mySketchingItem->setPath( aPath ); + } +} + +//================================================================ +// Function : finishSketching +// Purpose : +//================================================================ +void GraphicsView_ViewPort::finishSketching( bool theStatus ) +{ + prepareToSketch( false ); // just in case + + mySketchingItem->setVisible( false ); + myIsSketching = false; + + setCursor( *getDefaultCursor() ); + + if( theStatus ) + { + QPainterPath aPath = mySketchingItem->path(); + emit vpSketchingFinished( aPath ); + } +} + +//================================================================ +// Function : isSketching +// Purpose : +//================================================================ +bool GraphicsView_ViewPort::isSketching( bool* theIsPath ) const +{ + if( theIsPath ) + *theIsPath = myIsSketchingByPath; + return myIsSketching; +} + //================================================================ // Function : dragObjects // Purpose : @@ -1534,7 +1658,7 @@ void GraphicsView_ViewPort::onMouseEvent( QGraphicsSceneMouseEvent* e ) if( hasInteractionFlag( EditFlags ) && nbSelected() ) for( initSelected(); moreSelected() && !anIsHandled; nextSelected() ) if( GraphicsView_Object* anObject = selectedObject() ) - anIsHandled = anObject->handleMousePress( e ); + anIsHandled = anObject->handleMouseMove( e ); if( !anIsHandled && !isPulling() && myIsDragging ) dragObjects( e ); @@ -1545,7 +1669,7 @@ void GraphicsView_ViewPort::onMouseEvent( QGraphicsSceneMouseEvent* e ) if( hasInteractionFlag( EditFlags ) && nbSelected() ) for( initSelected(); moreSelected() && !anIsHandled; nextSelected() ) if( GraphicsView_Object* anObject = selectedObject() ) - anIsHandled = anObject->handleMousePress( e ); + anIsHandled = anObject->handleMouseRelease( e ); if( !anIsHandled && !isPulling() && myIsDragging ) { diff --git a/src/GraphicsView/GraphicsView_ViewPort.h b/src/GraphicsView/GraphicsView_ViewPort.h index a37ea0157..897111b90 100644 --- a/src/GraphicsView/GraphicsView_ViewPort.h +++ b/src/GraphicsView/GraphicsView_ViewPort.h @@ -58,6 +58,7 @@ public: DraggingByMiddleButton = 0x0010, ImmediateContextMenu = 0x0020, ImmediateSelection = 0x0040, + Sketching = 0x0080 }; Q_DECLARE_FLAGS( InteractionFlags, InteractionFlag ) @@ -185,6 +186,15 @@ public: bool isSelectByRect() const; QRect selectionRect(); + // sketching + void prepareToSketch( bool theStatus ); + bool isPrepareToSketch(); + void startSketching( const QPointF& thePoint, + bool theIsPath ); + void drawSketching( const QPointF& thePoint ); + void finishSketching( bool theStatus ); + bool isSketching( bool* theIsPath = 0 ) const; + // dragging bool isDragging() { return myIsDragging; } @@ -205,6 +215,7 @@ public: static QCursor* getPanCursor() { return panCursor; } static QCursor* getPanglCursor() { return panglCursor; } static QCursor* getZoomCursor() { return zoomCursor; } + static QCursor* getSketchCursor() { return sketchCursor; } public slots: void onBoundingRectChanged(); @@ -224,6 +235,8 @@ signals: void vpWheelEvent( QGraphicsSceneWheelEvent* ); void vpContextMenuEvent( QGraphicsSceneContextMenuEvent* ); + void vpSketchingFinished( QPainterPath ); + void vpObjectBeforeMoving(); void vpObjectAfterMoving( bool ); @@ -240,6 +253,7 @@ private: static QCursor* panCursor; static QCursor* panglCursor; static QCursor* zoomCursor; + static QCursor* sketchCursor; private: // scene @@ -292,6 +306,13 @@ private: QPoint myLastSelectionPoint; bool myAreSelectionPointsInitialized; + // sketching + QGraphicsPathItem* mySketchingItem; + QPointF mySketchingPoint; + bool myIsPrepareToSketch; + bool myIsSketching; + bool myIsSketchingByPath; + // dragging int myIsDragging; QPointF myDragPosition; diff --git a/src/GraphicsView/GraphicsView_Viewer.cxx b/src/GraphicsView/GraphicsView_Viewer.cxx index 62caf9136..09eba4bb4 100644 --- a/src/GraphicsView/GraphicsView_Viewer.cxx +++ b/src/GraphicsView/GraphicsView_Viewer.cxx @@ -92,6 +92,9 @@ SUIT_ViewWindow* GraphicsView_Viewer::createView( SUIT_Desktop* theDesktop ) connect( aViewFrame, SIGNAL( wheeling( QGraphicsSceneWheelEvent* ) ), this, SLOT( onWheelEvent( QGraphicsSceneWheelEvent* ) ) ); + connect( aViewFrame, SIGNAL( sketchingFinished( QPainterPath ) ), + this, SLOT( onSketchingFinished( QPainterPath ) ) ); + return aViewFrame; } @@ -113,7 +116,8 @@ void GraphicsView_Viewer::contextMenuPopup( QMenu* thePopup ) thePopup->addAction( tr( "ADD_IMAGE" ), this, SLOT( onAddImage() ) ); thePopup->addSeparator(); - thePopup->addAction( tr( "TEST_IMAGE_COMPOSITION" ), this, SLOT( onTestImageComposition() ) ); + thePopup->addAction( tr( "TEST_FUSE_OPERATOR" ), this, SLOT( onTestFuseOperator() ) ); + thePopup->addAction( tr( "TEST_CROP_OPERATOR" ), this, SLOT( onTestCropOperatorPrepare() ) ); } else { @@ -376,6 +380,13 @@ void GraphicsView_Viewer::handleMousePress( QGraphicsSceneMouseEvent* e ) { bool append = bool ( e->modifiers() & GraphicsView_Selector::getAppendKey() ); if( e->button() == Qt::LeftButton && + aViewPort->hasInteractionFlag( GraphicsView_ViewPort::Sketching ) && + aViewPort->isPrepareToSketch() ) + { + // Use 'append' flag for sketching by arbitrary path + aViewPort->startSketching( e->scenePos(), append ); + } + else if( e->button() == Qt::LeftButton && aViewPort->hasInteractionFlag( GraphicsView_ViewPort::Pulling ) && !aViewPort->isSelectByRect() && !aViewPort->isDragging() && @@ -392,8 +403,7 @@ void GraphicsView_Viewer::handleMousePress( QGraphicsSceneMouseEvent* e ) QPoint p = aViewPort->mapFromScene( e->scenePos() ); aViewPort->startSelectByRect( p.x(), p.y() ); } - else if( !append && - e->button() != Qt::MidButton && + else if( e->button() != Qt::MidButton && !append && aViewPort->hasInteractionFlag( GraphicsView_ViewPort::ImmediateSelection ) && aViewPort->nbSelected() < 2 ) { @@ -406,7 +416,6 @@ void GraphicsView_Viewer::handleMousePress( QGraphicsSceneMouseEvent* e ) { // If the 'immediate context menu' mode is enabled, // try to perform selection before invoking context menu - //bool append = bool ( e->modifiers() & GraphicsView_Selector::getAppendKey() ); getSelector()->select( QRectF(), append ); } } @@ -419,21 +428,28 @@ void GraphicsView_Viewer::handleMousePress( QGraphicsSceneMouseEvent* e ) //================================================================ void GraphicsView_Viewer::handleMouseMove( QGraphicsSceneMouseEvent* e ) { + GraphicsView_ViewPort* aViewPort = getActiveViewPort(); + // highlight for selection - bool dragged = ( e->buttons() & ( Qt::LeftButton | Qt::MidButton | Qt::RightButton ) ); - if ( !dragged ) + bool anIsDragged = ( e->buttons() & ( Qt::LeftButton | Qt::MidButton | Qt::RightButton ) ); + bool anIsPrepareToSketch = aViewPort && aViewPort->isPrepareToSketch(); + if ( !anIsDragged && !anIsPrepareToSketch ) { if ( getSelector() ) getSelector()->detect( e->scenePos().x(), e->scenePos().y() ); } // try to activate other operations - if( GraphicsView_ViewPort* aViewPort = getActiveViewPort() ) + if( aViewPort ) { if( aViewPort->isPulling() ) { aViewPort->drawPulling( e->scenePos() ); } + else if( aViewPort->isSketching() ) + { + aViewPort->drawSketching( e->scenePos() ); + } else if( e->button() == Qt::LeftButton && aViewPort->hasInteractionFlag( GraphicsView_ViewPort::Pulling ) && !aViewPort->isSelectByRect() && @@ -477,6 +493,10 @@ void GraphicsView_Viewer::handleMouseRelease( QGraphicsSceneMouseEvent* e ) { aViewPort->finishPulling( true ); } + else if( aViewPort->isSketching() ) + { + aViewPort->finishSketching( true ); + } else if( !aViewPort->getHighlightedObject() ) { QRect aSelRect = aViewPort->selectionRect(); @@ -518,6 +538,16 @@ void GraphicsView_Viewer::handleWheel( QGraphicsSceneWheelEvent* e ) } } +//================================================================ +// Function : onSketchingFinished +// Purpose : +//================================================================ +void GraphicsView_Viewer::onSketchingFinished( QPainterPath thePath ) +{ + // testing ImageViewer + onTestCropOperatorPerform( thePath ); +} + //================================================================ // Function : onSelectionDone // Purpose : @@ -774,15 +804,17 @@ void GraphicsView_Viewer::onPrsProperties() } //================================================================ -// Function : onTestImageComposition +// Function : onTestFuseOperator // Purpose : //================================================================ -void GraphicsView_Viewer::onTestImageComposition() +void GraphicsView_Viewer::onTestFuseOperator() { if( GraphicsView_ViewPort* aViewPort = getActiveViewPort() ) { GraphicsView_ObjectList aList = aViewPort->getObjects(); - int aCount = aList.count(); + if( aList.count() < 3 ) + return; + GraphicsView_PrsImage* anObj1 = dynamic_cast( aList[0] ); GraphicsView_PrsImage* anObj2 = dynamic_cast( aList[2] ); @@ -814,3 +846,51 @@ void GraphicsView_Viewer::onTestImageComposition() aViewPort->removeItem( anObj2 ); } } + +//================================================================ +// Function : onTestCropOperatorPrepare +// Purpose : +//================================================================ +void GraphicsView_Viewer::onTestCropOperatorPrepare() +{ + if( GraphicsView_ViewPort* aViewPort = getActiveViewPort() ) + aViewPort->prepareToSketch( true ); +} + +//================================================================ +// Function : onTestCropOperatorPerform +// Purpose : +//================================================================ +void GraphicsView_Viewer::onTestCropOperatorPerform( QPainterPath thePath ) +{ + if( GraphicsView_ViewPort* aViewPort = getActiveViewPort() ) + { + GraphicsView_ObjectList aList = aViewPort->getObjects(); + if( aList.count() < 1 ) + return; + + GraphicsView_PrsImage* anObj = dynamic_cast( aList[0] ); + + ImageComposer_Image anImage; + anImage = anObj->getImage(); + anImage.setTransform( anObj->getTransform() ); + + ImageComposer_Image aResult = anImage & thePath; + GraphicsView_PrsImage* aResPrs = new GraphicsView_PrsImage(); + aResPrs->setImage( aResult ); + + double aPosX, aPosY, aScaleX, aScaleY, aRotationAngle; + anObj->getPosition( aPosX, aPosY ); + anObj->getScaling( aScaleX, aScaleY ); + anObj->getRotationAngle( aRotationAngle ); + + aResPrs->setPosition( aResult.transform().dx(), aResult.transform().dy() ); + aResPrs->setScaling( aScaleX, aScaleY ); + aResPrs->setRotationAngle( aRotationAngle ); + + aResPrs->compute(); + + aViewPort->addItem( aResPrs ); + aViewPort->removeItem( anObj ); + } +} diff --git a/src/GraphicsView/GraphicsView_Viewer.h b/src/GraphicsView/GraphicsView_Viewer.h index b5c20d4d0..c3d272583 100644 --- a/src/GraphicsView/GraphicsView_Viewer.h +++ b/src/GraphicsView/GraphicsView_Viewer.h @@ -29,6 +29,8 @@ #include +#include + class QGraphicsSceneMouseEvent; class QGraphicsSceneWheelEvent; class QKeyEvent; @@ -91,13 +93,14 @@ protected slots: virtual void onMouseEvent( QGraphicsSceneMouseEvent* ); virtual void onWheelEvent( QGraphicsSceneWheelEvent* ); + virtual void onSketchingFinished( QPainterPath ); + virtual void onSelectionDone( GV_SelectionChangeStatus ); virtual void onSelectionCancel(); virtual void onChangeBgColor(); // testing ImageViewer - void onTestImageComposition(); void onAddImage(); void onRemoveImages(); void onBringToFront(); @@ -105,6 +108,9 @@ protected slots: void onBringForward(); void onSendBackward(); void onPrsProperties(); + void onTestFuseOperator(); + void onTestCropOperatorPrepare(); + void onTestCropOperatorPerform( QPainterPath thePath ); private: void handleKeyPress( QKeyEvent* ); -- 2.39.2