From cbd888f4c6419d6a8cb562cd15ed5a30c336a78e Mon Sep 17 00:00:00 2001 From: ouv Date: Fri, 28 Jun 2013 12:37:12 +0000 Subject: [PATCH] Raw development for ImageViewer. --- src/GraphicsView/GraphicsView_Object.h | 8 +- src/GraphicsView/GraphicsView_PrsImage.cxx | 101 +++++++++++------- src/GraphicsView/GraphicsView_PrsImage.h | 13 ++- .../GraphicsView_PrsImageFrame.cxx | 69 +++++++++++- src/GraphicsView/GraphicsView_PrsImageFrame.h | 6 +- src/GraphicsView/GraphicsView_ViewPort.cxx | 4 +- src/GraphicsView/Makefile.am | 4 +- 7 files changed, 149 insertions(+), 56 deletions(-) diff --git a/src/GraphicsView/GraphicsView_Object.h b/src/GraphicsView/GraphicsView_Object.h index 3208ba3b6..40c891867 100644 --- a/src/GraphicsView/GraphicsView_Object.h +++ b/src/GraphicsView/GraphicsView_Object.h @@ -25,6 +25,8 @@ #include "GraphicsView.h" +#include "GraphicsView_Defs.h" + #include class GraphicsView_ViewPort; @@ -79,8 +81,10 @@ public: virtual QRectF getPullingRect() const { return getRect(); } virtual bool portContains( const QPointF& ) { return false; } virtual bool startPulling( const QPointF& ) { return false; } - virtual void pull( const QPointF&, GraphicsView_Object* ) {} - virtual void finishPulling() {} + virtual void pull( const QPointF&, + GraphicsView_Object*, + const GraphicsView_ObjectList& ) {} + virtual void finishPulling( const GraphicsView_ObjectList& ) {} virtual bool isPulling() { return false; } virtual bool handleMousePress( QGraphicsSceneMouseEvent* ) { return false; } diff --git a/src/GraphicsView/GraphicsView_PrsImage.cxx b/src/GraphicsView/GraphicsView_PrsImage.cxx index 75475f75e..f327118f4 100644 --- a/src/GraphicsView/GraphicsView_PrsImage.cxx +++ b/src/GraphicsView/GraphicsView_PrsImage.cxx @@ -29,11 +29,11 @@ #include #include #include - -#include +#include #include +#define EPSILON 1e-6 #define PI 3.14159265359 //======================================================================= @@ -364,31 +364,22 @@ QPointF GraphicsView_PrsImage::centerPoint() // Purpose : //================================================================ void GraphicsView_PrsImage::processResize( const int theAnchor, - const QPointF& thePoint1, - const QPointF& thePoint2 ) + const double theDX, + const double theDY ) { - if( thePoint1 == thePoint2 ) + if( fabs( theDX ) < EPSILON && fabs( theDY ) < EPSILON ) return; if( !myPreviewPixmapItem->isVisible() ) enablePreview( true ); - QPixmap aCurrentPixmap = myPixmapItem->pixmap(); - int aWidth = aCurrentPixmap.width(); - int aHeight = aCurrentPixmap.height(); - - double aDX = thePoint2.x() - thePoint1.x(); - double aDY = thePoint2.y() - thePoint1.y(); - double anAngle = -1 * myRotationAngle * PI / 180.; - gp_Pnt2d aPnt1( thePoint1.x(), thePoint1.y() ); - gp_Pnt2d aPnt2( thePoint2.x(), thePoint2.y() ); - gp_Vec2d aVec( aPnt1, aPnt2 ); - gp_Vec2d aVecHor( 10, 0 ); + QVector2D aVec( theDX, theDY ); + QVector2D aVecHor( 10, 0 ); - double aDist = aPnt1.Distance( aPnt2 ); - double anAngle1 = aVec.Angle( aVecHor ); + double aDist = sqrt( theDX * theDX + theDY * theDY ); + double anAngle1 = computeAngle( aVec, aVecHor ); double anAngle2 = anAngle1 - anAngle; double aSizeDX = aDist * cos( anAngle2 ); @@ -415,7 +406,7 @@ void GraphicsView_PrsImage::processResize( const int theAnchor, aSizeShift = QPointF( aSizeDX, 0 ); break; case GraphicsView_PrsImageFrame::TopLeft: - aPosShift = QPointF( aDX, aDY ); + aPosShift = QPointF( theDX, theDY ); aSizeShift = QPointF( -aSizeDX, -aSizeDY ); break; case GraphicsView_PrsImageFrame::TopRight: @@ -432,12 +423,16 @@ void GraphicsView_PrsImage::processResize( const int theAnchor, break; } - aWidth += (int)aSizeShift.x(); - aHeight += (int)aSizeShift.y(); - if( aWidth < 10 || aHeight < 10 ) // tmp + QPixmap aCurrentPixmap = myPixmapItem->pixmap(); + int aWidth = aCurrentPixmap.width(); + int aHeight = aCurrentPixmap.height(); + + int aNewWidth = aWidth + (int)aSizeShift.x(); + int aNewHeight = aHeight + (int)aSizeShift.y(); + if( aNewWidth < 10 || aNewHeight < 10 ) // tmp return; - QPixmap aPixmap = myPixmap.scaled( aWidth, aHeight ); + QPixmap aPixmap = myPixmap.scaled( aNewWidth, aNewHeight ); myPreviewPixmapItem->setPixmap( aPixmap ); myPreviewPixmapItem->setPos( pos() + aPosShift ); @@ -480,32 +475,36 @@ void GraphicsView_PrsImage::finishResize() } //================================================================ -// Function : processRotate +// Function : computeRotationAngle // Purpose : //================================================================ -void GraphicsView_PrsImage::processRotate( const QPointF& thePoint1, - const QPointF& thePoint2 ) +double GraphicsView_PrsImage::computeRotationAngle( const QPointF& thePoint1, + const QPointF& thePoint2 ) const { - if( thePoint1 == thePoint2 ) - return; - - if( !myPreviewPixmapItem->isVisible() ) - enablePreview( true ); - QRectF aRect = getRect(); QPointF aCenter = aRect.center(); - gp_Pnt2d aPnt0( aCenter.x(), aCenter.y() ); - gp_Pnt2d aPnt1( thePoint1.x(), thePoint1.y() ); - gp_Pnt2d aPnt2( thePoint2.x(), thePoint2.y() ); - - gp_Vec2d aVec1( aPnt0, aPnt1 ); - gp_Vec2d aVec2( aPnt0, aPnt2 ); + QVector2D aVec1( thePoint1 - aCenter ); + QVector2D aVec2( thePoint2 - aCenter ); - double anAngle = aVec1.Angle( aVec2 ); + double anAngle = computeAngle( aVec1, aVec2 ); double anAngleDeg = anAngle / PI * 180.; + return anAngleDeg; +} + +//================================================================ +// Function : processRotate +// Purpose : +//================================================================ +void GraphicsView_PrsImage::processRotate( const double theAngle ) +{ + if( fabs( theAngle ) < EPSILON ) + return; - myPreviewRotationAngle = myRotationAngle + anAngleDeg; + if( !myPreviewPixmapItem->isVisible() ) + enablePreview( true ); + + myPreviewRotationAngle = myRotationAngle + theAngle; setRotationAroundCenter( myPreviewPixmapItem, myPreviewRotationAngle ); } @@ -542,3 +541,25 @@ void GraphicsView_PrsImage::enablePreview( const bool theState ) else myPreviewPixmapItem->setVisible( false ); } + +//================================================================ +// Function : computeAngle +// Purpose : +//================================================================ +double GraphicsView_PrsImage::computeAngle( const QVector2D& theVector1, + const QVector2D& theVector2 ) +{ + if( theVector1.isNull() || theVector2.isNull() ) + return 0.0; + + double aDotProduct = QVector2D::dotProduct( theVector1, theVector2 ); + double aCos = aDotProduct / theVector1.length() / theVector2.length(); + + double aCrossProduct = theVector1.x() * theVector2.y() - theVector1.y() * theVector2.x(); + + double anAngle = acos( aCos ); + if( aCrossProduct < 0 ) + anAngle *= -1; + + return anAngle; +} diff --git a/src/GraphicsView/GraphicsView_PrsImage.h b/src/GraphicsView/GraphicsView_PrsImage.h index 735179266..a63440c4e 100644 --- a/src/GraphicsView/GraphicsView_PrsImage.h +++ b/src/GraphicsView/GraphicsView_PrsImage.h @@ -80,17 +80,22 @@ public: protected: void processResize( const int theAnchor, - const QPointF& thePoint1, - const QPointF& thePoint2 ); + const double theDX, + const double theDY ); void finishResize(); - void processRotate( const QPointF& thePoint1, - const QPointF& thePoint2 ); + double computeRotationAngle( const QPointF& thePoint1, + const QPointF& thePoint2 ) const; + + void processRotate( const double theAngle ); void finishRotate(); protected: void enablePreview( const bool theState ); + static double computeAngle( const QVector2D& theVector1, + const QVector2D& theVector2 ); + protected: QPixmap myPixmap; diff --git a/src/GraphicsView/GraphicsView_PrsImageFrame.cxx b/src/GraphicsView/GraphicsView_PrsImageFrame.cxx index b14dba132..d3ebcdd32 100644 --- a/src/GraphicsView/GraphicsView_PrsImageFrame.cxx +++ b/src/GraphicsView/GraphicsView_PrsImageFrame.cxx @@ -30,6 +30,10 @@ #include #include +#include + +#define PI 3.14159265359 + #define ANCHOR_RADIUS 10 //======================================================================= @@ -208,22 +212,64 @@ bool GraphicsView_PrsImageFrame::startPulling( const QPointF& thePoint ) // Function : pull // Purpose : //================================================================ -void GraphicsView_PrsImageFrame::pull( const QPointF& thePoint, GraphicsView_Object* ) +void GraphicsView_PrsImageFrame::pull( const QPointF& thePoint, + GraphicsView_Object* theLockedObject, // unused + const GraphicsView_ObjectList& theSyncObjects ) { if( !myPrsImage ) return; + if( thePoint == myPullingPoint ) + return; + + double aDX = thePoint.x() - myPullingPoint.x(); + double aDY = thePoint.y() - myPullingPoint.y(); + + double aDAngle = myPrsImage->computeRotationAngle( myPullingPoint, thePoint ); + if( myPullingAnchor >= Top && myPullingAnchor <= BottomRight ) - myPrsImage->processResize( myPullingAnchor, myPullingPoint, thePoint ); + myPrsImage->processResize( myPullingAnchor, aDX, aDY ); else if( myPullingAnchor == TopMost ) - myPrsImage->processRotate( myPullingPoint, thePoint ); + myPrsImage->processRotate( aDAngle ); + + QRectF aRect = myPrsImage->getRect(); + double anAngle = 0; + myPrsImage->getRotationAngle( anAngle ); + + GraphicsView_ObjectListIterator anIter( theSyncObjects ); + while( anIter.hasNext() ) + { + if( GraphicsView_PrsImage* aPrsImage = dynamic_cast( anIter.next() ) ) + { + if( aPrsImage != myPrsImage ) + { + if( myPullingAnchor >= Top && myPullingAnchor <= BottomRight ) + { + QRectF aRectRef = aPrsImage->getRect(); + double anAngleRef = 0; + aPrsImage->getRotationAngle( anAngleRef ); + + double aDXRef = aDX * aRectRef.width() / aRect.width(); + double aDYRef = aDY * aRectRef.height() / aRect.height(); + + double anAngleDiff = ( anAngleRef - anAngle ) * PI / 180.; + double aDXRefTrans = aDXRef * cos( anAngleDiff ) - aDYRef * sin( anAngleDiff ); + double aDYRefTrans = aDXRef * sin( anAngleDiff ) + aDYRef * cos( anAngleDiff ); + + aPrsImage->processResize( myPullingAnchor, aDXRefTrans, aDYRefTrans ); + } + else if( myPullingAnchor == TopMost ) + aPrsImage->processRotate( aDAngle ); + } + } + } } //================================================================ // Function : finishPulling // Purpose : //================================================================ -void GraphicsView_PrsImageFrame::finishPulling() +void GraphicsView_PrsImageFrame::finishPulling( const GraphicsView_ObjectList& theSyncObjects ) { if( !myPrsImage ) return; @@ -232,6 +278,21 @@ void GraphicsView_PrsImageFrame::finishPulling() myPrsImage->finishResize(); else if( myPullingAnchor == TopMost ) myPrsImage->finishRotate(); + + GraphicsView_ObjectListIterator anIter( theSyncObjects ); + while( anIter.hasNext() ) + { + if( GraphicsView_PrsImage* aPrsImage = dynamic_cast( anIter.next() ) ) + { + if( aPrsImage != myPrsImage ) + { + if( myPullingAnchor >= Top && myPullingAnchor <= BottomRight ) + aPrsImage->finishResize(); + else if( myPullingAnchor == TopMost ) + aPrsImage->finishRotate(); + } + } + } } //================================================================ diff --git a/src/GraphicsView/GraphicsView_PrsImageFrame.h b/src/GraphicsView/GraphicsView_PrsImageFrame.h index f71771ef0..07b443a86 100644 --- a/src/GraphicsView/GraphicsView_PrsImageFrame.h +++ b/src/GraphicsView/GraphicsView_PrsImageFrame.h @@ -65,8 +65,10 @@ public: virtual QRectF getPullingRect() const; virtual bool portContains( const QPointF& ) { return false; } // useless virtual bool startPulling( const QPointF& ); - virtual void pull( const QPointF&, GraphicsView_Object* ); - virtual void finishPulling(); + virtual void pull( const QPointF&, + GraphicsView_Object*, + const GraphicsView_ObjectList& ); + virtual void finishPulling( const GraphicsView_ObjectList& ); virtual bool isPulling() { return myIsPulling; } virtual QPointF centerPoint(); diff --git a/src/GraphicsView/GraphicsView_ViewPort.cxx b/src/GraphicsView/GraphicsView_ViewPort.cxx index e323de9b2..8892099ce 100644 --- a/src/GraphicsView/GraphicsView_ViewPort.cxx +++ b/src/GraphicsView/GraphicsView_ViewPort.cxx @@ -1361,7 +1361,7 @@ void GraphicsView_ViewPort::drawPulling( const QPointF& thePoint ) } } - myPullingObject->pull( thePoint, aLockedObject ); + myPullingObject->pull( thePoint, aLockedObject, getSelectedObjects() ); } //================================================================ @@ -1371,7 +1371,7 @@ void GraphicsView_ViewPort::drawPulling( const QPointF& thePoint ) void GraphicsView_ViewPort::finishPulling() { myIsPulling = false; - myPullingObject->finishPulling(); + myPullingObject->finishPulling( getSelectedObjects() ); setCursor( *getDefaultCursor() ); } diff --git a/src/GraphicsView/Makefile.am b/src/GraphicsView/Makefile.am index 3d9a61301..688ce1fbe 100644 --- a/src/GraphicsView/Makefile.am +++ b/src/GraphicsView/Makefile.am @@ -81,6 +81,6 @@ nodist_salomeres_DATA = \ GraphicsView_msg_en.qm \ GraphicsView_msg_fr.qm -libGraphicsView_la_CPPFLAGS = $(QT_INCLUDES) $(CAS_CPPFLAGS) -I$(srcdir)/../SUIT -I$(srcdir)/../Qtx -libGraphicsView_la_LDFLAGS = $(QT_MT_LIBS) $(CAS_KERNEL) +libGraphicsView_la_CPPFLAGS = $(QT_INCLUDES) -I$(srcdir)/../SUIT -I$(srcdir)/../Qtx +libGraphicsView_la_LDFLAGS = $(QT_MT_LIBS) libGraphicsView_la_LIBADD = ../SUIT/libsuit.la -- 2.39.2