From 5e2837d81210128e2373564b2b3269fccd6712fd Mon Sep 17 00:00:00 2001 From: ouv Date: Tue, 2 Jul 2013 11:46:29 +0000 Subject: [PATCH] Raw development for ImageViewer. --- src/GraphicsView/GraphicsView_PrsImage.cxx | 4 +- .../GraphicsView_PrsImageFrame.cxx | 76 +++++++++++++++---- src/GraphicsView/GraphicsView_PrsImageFrame.h | 17 ++++- 3 files changed, 80 insertions(+), 17 deletions(-) diff --git a/src/GraphicsView/GraphicsView_PrsImage.cxx b/src/GraphicsView/GraphicsView_PrsImage.cxx index 8a61d0959..af423b0cf 100644 --- a/src/GraphicsView/GraphicsView_PrsImage.cxx +++ b/src/GraphicsView/GraphicsView_PrsImage.cxx @@ -106,7 +106,7 @@ QImage GraphicsView_PrsImage::getImage() const } //================================================================ -// Function : getTransformation +// Function : getTransform // Purpose : //================================================================ QTransform GraphicsView_PrsImage::getTransform() const @@ -552,6 +552,8 @@ void GraphicsView_PrsImage::finishRotate() setRotationAroundCenter( this, myRotationAngle ); setRotationAroundCenter( myImageFrame, myRotationAngle ); + myImageFrame->setRotationAngle( myRotationAngle ); // for anchors + enablePreview( false ); } diff --git a/src/GraphicsView/GraphicsView_PrsImageFrame.cxx b/src/GraphicsView/GraphicsView_PrsImageFrame.cxx index 649147683..e361d821c 100644 --- a/src/GraphicsView/GraphicsView_PrsImageFrame.cxx +++ b/src/GraphicsView/GraphicsView_PrsImageFrame.cxx @@ -33,7 +33,7 @@ #include #define FRAME_Z_VALUE 1000 -#define ANCHOR_RADIUS 10 +#define ANCHOR_RADIUS 3 #define PI 3.14159265359 //======================================================================= @@ -121,10 +121,9 @@ void GraphicsView_PrsImageFrame::compute() { if( myAnchorMap.isEmpty() ) { - for( int aType = Top; aType <= TopMost; aType++ ) + for( int aType = TopMost; aType <= BottomRight; aType++ ) { UnscaledGraphicsEllipseItem* anAnchorItem = new UnscaledGraphicsEllipseItem( this ); - //anAnchorItem->setFlag( QGraphicsItem::ItemIgnoresTransformations, true ); Qt::GlobalColor aColor = Qt::white; if( aType == TopMost ) @@ -305,7 +304,7 @@ QPointF GraphicsView_PrsImageFrame::centerPoint() QPointF aPoint2 = myAnchorMap[ Bottom ]->getBasePoint(); QPointF aPoint3 = myAnchorMap[ Left ]->getBasePoint(); QPointF aPoint4 = myAnchorMap[ Right ]->getBasePoint(); - return ( aPoint1 + aPoint2 + aPoint3 + aPoint4 ) /4.; + return ( aPoint1 + aPoint2 + aPoint3 + aPoint4 ) / 4.; } //================================================================ @@ -329,6 +328,7 @@ void GraphicsView_PrsImageFrame::computeAnchorItems() QRectF aRect = myPrsImage->boundingRect(); QMap anAnchorPointMap; + anAnchorPointMap[ TopMost ] = ( aRect.topLeft() + aRect.topRight() ) / 2; anAnchorPointMap[ Top ] = ( aRect.topLeft() + aRect.topRight() ) / 2; anAnchorPointMap[ Bottom ] = ( aRect.bottomLeft() + aRect.bottomRight() ) / 2; anAnchorPointMap[ Left ] = ( aRect.topLeft() + aRect.bottomLeft() ) / 2; @@ -338,10 +338,6 @@ void GraphicsView_PrsImageFrame::computeAnchorItems() anAnchorPointMap[ BottomLeft ] = aRect.bottomLeft(); anAnchorPointMap[ BottomRight ] = aRect.bottomRight(); - // tmp - anAnchorPointMap[ TopMost ] = ( aRect.topLeft() + aRect.topRight() ) / 2 - - QPointF( 0, 4 * ANCHOR_RADIUS ); - qreal ar = ANCHOR_RADIUS; QMapIterator anIter( anAnchorPointMap ); while( anIter.hasNext() ) @@ -352,6 +348,9 @@ void GraphicsView_PrsImageFrame::computeAnchorItems() QRectF anAnchorRect( anAnchorPoint - QPointF( ar, ar ), QSizeF( ar * 2, ar * 2 ) ); myAnchorMap[ anAnchorType ]->setRect( anAnchorRect ); myAnchorMap[ anAnchorType ]->setBasePoint( anAnchorPoint ); + + if( anAnchorType == TopMost ) + myAnchorMap[ anAnchorType ]->setOffset( QPointF( 0, -6 * ANCHOR_RADIUS ) ); } } @@ -377,6 +376,18 @@ void GraphicsView_PrsImageFrame::updateAnchorItems() setVisible( anIsSelected ); } +//================================================================ +// Function : setRotationAngle +// Purpose : +//================================================================ +void GraphicsView_PrsImageFrame::setRotationAngle( const double theRotationAngle ) +{ + AnchorMapIterator anIter( myAnchorMap ); + while( anIter.hasNext() ) + if( UnscaledGraphicsEllipseItem* anAnchorItem = anIter.next().value() ) + anAnchorItem->setRotationAngle( theRotationAngle ); +} + //================================================================ // Function : getResizeCursor // Purpose : @@ -434,7 +445,9 @@ QCursor* GraphicsView_PrsImageFrame::getResizeCursor( const int theAnchor ) cons // Purpose : Constructor //======================================================================= GraphicsView_PrsImageFrame::UnscaledGraphicsEllipseItem::UnscaledGraphicsEllipseItem( QGraphicsItem* theParent ) -: QGraphicsEllipseItem( theParent ) +: QGraphicsEllipseItem( theParent ), + myRotationAngle( 0.0 ), + myScale( 1.0 ) { } @@ -446,6 +459,30 @@ GraphicsView_PrsImageFrame::UnscaledGraphicsEllipseItem::~UnscaledGraphicsEllips { } +//================================================================ +// Function : boundingRect +// Purpose : +//================================================================ +QRectF GraphicsView_PrsImageFrame::UnscaledGraphicsEllipseItem::boundingRect() const +{ + QRectF aRect = QGraphicsEllipseItem::boundingRect(); + + if( fabs( myScale ) < 1e-10 ) + return aRect; + + QPointF aCenter = aRect.center(); + double aWidth = aRect.width() / myScale; + double aHeight = aRect.height() / myScale; + + double anOffsetX = myOffset.x() / myScale; + double anOffsetY = myOffset.y() / myScale; + + aRect = QRectF( aCenter.x() - aWidth / 2 + anOffsetX, + aCenter.y() - aHeight / 2 + anOffsetY, + aWidth, aHeight ); + return aRect; +} + //================================================================ // Function : GenerateTranslationOnlyTransform // Purpose : @@ -471,9 +508,22 @@ void GraphicsView_PrsImageFrame::UnscaledGraphicsEllipseItem::paint( const QStyleOptionGraphicsItem* theOption, QWidget* theWidget ) { - //thePainter->save(); - //thePainter->setTransform( GenerateTranslationOnlyTransform( thePainter->transform(), - // myBasePoint ) ); + QTransform aTransform = thePainter->transform(); + aTransform.rotate( -myRotationAngle ); // exclude rotation from matrix + myScale = aTransform.m11(); // same as m22(), viewer specific + + // draw a connection line (mainly, for top-most anchor) + thePainter->drawLine( myBasePoint, boundingRect().center() ); + + thePainter->save(); + thePainter->setTransform( GenerateTranslationOnlyTransform( thePainter->transform(), + myBasePoint ) ); + + double anAngle = myRotationAngle * PI / 180.; + double aDX = myOffset.x() * cos( anAngle ) - myOffset.y() * sin( anAngle ); + double aDY = myOffset.x() * sin( anAngle ) + myOffset.y() * cos( anAngle ); + thePainter->translate( aDX, aDY ); + QGraphicsEllipseItem::paint( thePainter, theOption, theWidget ); - //thePainter->restore(); + thePainter->restore(); } diff --git a/src/GraphicsView/GraphicsView_PrsImageFrame.h b/src/GraphicsView/GraphicsView_PrsImageFrame.h index 24b5f7a3b..736b6c46b 100644 --- a/src/GraphicsView/GraphicsView_PrsImageFrame.h +++ b/src/GraphicsView/GraphicsView_PrsImageFrame.h @@ -40,9 +40,8 @@ class GRAPHICSVIEW_API GraphicsView_PrsImageFrame : public GraphicsView_Object public: class UnscaledGraphicsEllipseItem; - enum AnchorType { Undefined = 0, Top, Bottom, Left, Right, - TopLeft, TopRight, BottomLeft, BottomRight, - TopMost }; + enum AnchorType { Undefined = 0, TopMost, Top, Bottom, Left, Right, + TopLeft, TopRight, BottomLeft, BottomRight }; typedef QMap AnchorMap; typedef QMapIterator AnchorMapIterator; @@ -81,6 +80,8 @@ public: void computeAnchorItems(); void updateAnchorItems(); + void setRotationAngle( const double theRotationAngle ); + protected: QCursor* getVerCursor() const { return myVerCursor; } QCursor* getHorCursor() const { return myHorCursor; } @@ -121,11 +122,21 @@ public: void setBasePoint( const QPointF& thePoint ) { myBasePoint = thePoint; } const QPointF& getBasePoint() const { return myBasePoint; } + void setOffset( const QPointF& theOffset ) { myOffset = theOffset; } + const QPointF& getOffset() const { return myOffset; } + + void setRotationAngle( const double theAngle ) { myRotationAngle = theAngle; } + public: + virtual QRectF boundingRect() const; virtual void paint( QPainter*, const QStyleOptionGraphicsItem*, QWidget* ); private: QPointF myBasePoint; + QPointF myOffset; + + double myRotationAngle; + double myScale; }; #endif -- 2.39.2