1 // Copyright (C) 2014-2015 EDF-R&D
2 // This library is free software; you can redistribute it and/or
3 // modify it under the terms of the GNU Lesser General Public
4 // License as published by the Free Software Foundation; either
5 // version 2.1 of the License, or (at your option) any later version.
7 // This library is distributed in the hope that it will be useful,
8 // but WITHOUT ANY WARRANTY; without even the implied warranty of
9 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
10 // Lesser General Public License for more details.
12 // You should have received a copy of the GNU Lesser General Public
13 // License along with this library; if not, write to the Free Software
14 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
19 #include "HYDROGUI_PrsImageFrame.h"
21 #include "HYDROGUI_PrsImage.h"
26 #include "HYDRO_trace.hxx"
28 #define FRAME_Z_VALUE 1000
29 #define ANCHOR_RADIUS 3
32 //=======================================================================
33 // name : HYDROGUI_PrsImageFrame
34 // Purpose : Constructor
35 //=======================================================================
36 HYDROGUI_PrsImageFrame::HYDROGUI_PrsImageFrame( HYDROGUI_PrsImage* thePrsImage )
37 : GraphicsView_Object( thePrsImage ),
38 myPrsImage( thePrsImage )
42 //=======================================================================
43 // name : HYDROGUI_PrsImageFrame
44 // Purpose : Destructor
45 //=======================================================================
46 HYDROGUI_PrsImageFrame::~HYDROGUI_PrsImageFrame()
50 //================================================================
51 // Function : boundingRect
53 //================================================================
54 QRectF HYDROGUI_PrsImageFrame::boundingRect() const
57 AnchorMapIterator anIter( myAnchorMap );
58 while( anIter.hasNext() )
60 if( QGraphicsEllipseItem* anAnchorItem = anIter.next().value() )
62 QRectF anAnchorRect = anAnchorItem->boundingRect();
63 if( !anAnchorRect.isNull() )
68 aRect |= anAnchorRect;
75 //================================================================
78 //================================================================
79 void HYDROGUI_PrsImageFrame::compute()
82 if( myAnchorMap.isEmpty() )
84 for( int aType = TopLeft; aType <= BottomRight; aType++ )
86 UnscaledGraphicsEllipseItem* anAnchorItem = new UnscaledGraphicsEllipseItem( this );
87 anAnchorItem->setBrush( QBrush( Qt::white ) );
88 myAnchorMap.insert( aType, anAnchorItem );
89 addToGroup( anAnchorItem );
93 setZValue( FRAME_Z_VALUE );
96 // QTransform transform;
97 // transform.scale(-0.5, 1.0);
98 // this->setTransform(transform, false);
102 //================================================================
103 // Function : computeAnchorItems
105 //================================================================
106 void HYDROGUI_PrsImageFrame::computeAnchorItems()
111 QRectF aRect = myPrsImage->boundingRect();
113 QMap<int, QPointF> anAnchorPointMap;
114 anAnchorPointMap[ TopLeft ] = aRect.topLeft();
115 anAnchorPointMap[ TopRight ] = aRect.topRight();
116 anAnchorPointMap[ BottomLeft ] = aRect.bottomLeft();
117 anAnchorPointMap[ BottomRight ] = aRect.bottomRight();
119 qreal ar = ANCHOR_RADIUS;
120 QMapIterator<int, QPointF> anIter( anAnchorPointMap );
121 while( anIter.hasNext() )
123 int anAnchorType = anIter.next().key();
124 const QPointF& anAnchorPoint = anIter.value();
126 QRectF anAnchorRect( anAnchorPoint - QPointF( ar, ar ), QSizeF( ar * 2, ar * 2 ) );
127 myAnchorMap[ anAnchorType ]->setRect( anAnchorRect );
128 myAnchorMap[ anAnchorType ]->setBasePoint( anAnchorPoint );
132 //================================================================
133 // Function : updateVisibility
135 //================================================================
136 void HYDROGUI_PrsImageFrame::updateVisibility()
139 DEBTRACE("updateVisibility " << myPrsImage->isSelected());
140 setVisible( myPrsImage && myPrsImage->isSelected() );
143 //=======================================================================
144 // name : HYDROGUI_PrsImageFrame::UnscaledGraphicsEllipseItem
145 // Purpose : Constructor
146 //=======================================================================
147 HYDROGUI_PrsImageFrame::UnscaledGraphicsEllipseItem::UnscaledGraphicsEllipseItem( QGraphicsItem* theParent )
148 : QGraphicsEllipseItem( theParent )
152 //=======================================================================
153 // name : HYDROGUI_PrsImageFrame::UnscaledGraphicsEllipseItem
154 // Purpose : Destructor
155 //=======================================================================
156 HYDROGUI_PrsImageFrame::UnscaledGraphicsEllipseItem::~UnscaledGraphicsEllipseItem()
160 //================================================================
161 // Function : boundingRect
163 //================================================================
164 QRectF HYDROGUI_PrsImageFrame::UnscaledGraphicsEllipseItem::boundingRect() const
166 QRectF aRect = QGraphicsEllipseItem::boundingRect();
168 GraphicsView_Object* aParent = dynamic_cast<GraphicsView_Object*>( parentItem() );
172 // take into account a transformation of the base image item
173 double aXScale = 1.0;
174 double aYScale = 1.0;
175 if( QGraphicsItem* aGrandParent = aParent->parentItem() )
177 QTransform aTransform = aGrandParent->transform();
178 QLineF aLine( 0, 0, 1, 1 );
179 aLine = aTransform.map( aLine );
180 aXScale = aLine.dx();
181 aYScale = aLine.dy();
184 QTransform aViewTransform = aParent->getViewTransform();
185 double aScale = aViewTransform.m11(); // same as m22(), viewer specific
186 if( fabs( aScale ) < EPSILON || fabs( aXScale ) < EPSILON || fabs( aYScale ) < EPSILON)
189 QPointF aCenter = aRect.center();
190 double aWidth = aRect.width() / aScale / aXScale;
191 double aHeight = aRect.height() / aScale / aYScale;
193 aRect = QRectF( aCenter.x() - aWidth / 2, aCenter.y() - aHeight / 2, aWidth, aHeight );
197 //================================================================
198 // Function : GenerateTranslationOnlyTransform
200 //================================================================
201 static QTransform GenerateTranslationOnlyTransform( const QTransform &theOriginalTransform,
202 const QPointF &theTargetPoint )
204 qreal dx = theOriginalTransform.m11() * theTargetPoint.x() - theTargetPoint.x() +
205 theOriginalTransform.m21() * theTargetPoint.y() +
206 theOriginalTransform.m31();
207 qreal dy = theOriginalTransform.m22() * theTargetPoint.y() - theTargetPoint.y() +
208 theOriginalTransform.m12() * theTargetPoint.x() +
209 theOriginalTransform.m32();
210 return QTransform::fromTranslate( dx, dy );
213 //================================================================
216 //================================================================
217 void HYDROGUI_PrsImageFrame::UnscaledGraphicsEllipseItem::paint(
218 QPainter* thePainter,
219 const QStyleOptionGraphicsItem* theOption,
223 thePainter->setTransform( GenerateTranslationOnlyTransform( thePainter->transform(),
225 QGraphicsEllipseItem::paint( thePainter, theOption, theWidget );
226 thePainter->restore();