1 // Copyright (C) 2007-2013 CEA/DEN, EDF R&D, OPEN CASCADE
3 // Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License.
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 // Lesser General Public License for more details.
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
23 #include "HYDROGUI_PrsImageFrame.h"
25 #include "HYDROGUI_PrsImage.h"
29 #define FRAME_Z_VALUE 1000
30 #define ANCHOR_RADIUS 3
33 //=======================================================================
34 // name : HYDROGUI_PrsImageFrame
35 // Purpose : Constructor
36 //=======================================================================
37 HYDROGUI_PrsImageFrame::HYDROGUI_PrsImageFrame( HYDROGUI_PrsImage* thePrsImage )
38 : GraphicsView_Object( thePrsImage ),
39 myPrsImage( thePrsImage )
43 //=======================================================================
44 // name : HYDROGUI_PrsImageFrame
45 // Purpose : Destructor
46 //=======================================================================
47 HYDROGUI_PrsImageFrame::~HYDROGUI_PrsImageFrame()
51 //================================================================
52 // Function : boundingRect
54 //================================================================
55 QRectF HYDROGUI_PrsImageFrame::boundingRect() const
58 AnchorMapIterator anIter( myAnchorMap );
59 while( anIter.hasNext() )
61 if( QGraphicsEllipseItem* anAnchorItem = anIter.next().value() )
63 QRectF anAnchorRect = anAnchorItem->boundingRect();
64 if( !anAnchorRect.isNull() )
69 aRect |= anAnchorRect;
76 //================================================================
79 //================================================================
80 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 );
99 //================================================================
100 // Function : computeAnchorItems
102 //================================================================
103 void HYDROGUI_PrsImageFrame::computeAnchorItems()
108 QRectF aRect = myPrsImage->boundingRect();
110 QMap<int, QPointF> anAnchorPointMap;
111 anAnchorPointMap[ TopLeft ] = aRect.topLeft();
112 anAnchorPointMap[ TopRight ] = aRect.topRight();
113 anAnchorPointMap[ BottomLeft ] = aRect.bottomLeft();
114 anAnchorPointMap[ BottomRight ] = aRect.bottomRight();
116 qreal ar = ANCHOR_RADIUS;
117 QMapIterator<int, QPointF> anIter( anAnchorPointMap );
118 while( anIter.hasNext() )
120 int anAnchorType = anIter.next().key();
121 const QPointF& anAnchorPoint = anIter.value();
123 QRectF anAnchorRect( anAnchorPoint - QPointF( ar, ar ), QSizeF( ar * 2, ar * 2 ) );
124 myAnchorMap[ anAnchorType ]->setRect( anAnchorRect );
125 myAnchorMap[ anAnchorType ]->setBasePoint( anAnchorPoint );
129 //================================================================
130 // Function : updateVisibility
132 //================================================================
133 void HYDROGUI_PrsImageFrame::updateVisibility()
135 setVisible( myPrsImage && myPrsImage->isSelected() );
138 //=======================================================================
139 // name : HYDROGUI_PrsImageFrame::UnscaledGraphicsEllipseItem
140 // Purpose : Constructor
141 //=======================================================================
142 HYDROGUI_PrsImageFrame::UnscaledGraphicsEllipseItem::UnscaledGraphicsEllipseItem( QGraphicsItem* theParent )
143 : QGraphicsEllipseItem( theParent )
147 //=======================================================================
148 // name : HYDROGUI_PrsImageFrame::UnscaledGraphicsEllipseItem
149 // Purpose : Destructor
150 //=======================================================================
151 HYDROGUI_PrsImageFrame::UnscaledGraphicsEllipseItem::~UnscaledGraphicsEllipseItem()
155 //================================================================
156 // Function : boundingRect
158 //================================================================
159 QRectF HYDROGUI_PrsImageFrame::UnscaledGraphicsEllipseItem::boundingRect() const
161 QRectF aRect = QGraphicsEllipseItem::boundingRect();
163 GraphicsView_Object* aParent = dynamic_cast<GraphicsView_Object*>( parentItem() );
167 // take into account a transformation of the base image item
168 double aXScale = 1.0;
169 double aYScale = 1.0;
170 if( QGraphicsItem* aGrandParent = aParent->parentItem() )
172 QTransform aTransform = aGrandParent->transform();
173 QLineF aLine( 0, 0, 1, 1 );
174 aLine = aTransform.map( aLine );
175 aXScale = aLine.dx();
176 aYScale = aLine.dy();
179 QTransform aViewTransform = aParent->getViewTransform();
180 double aScale = aViewTransform.m11(); // same as m22(), viewer specific
181 if( fabs( aScale ) < EPSILON || fabs( aXScale ) < EPSILON || fabs( aYScale ) < EPSILON)
184 QPointF aCenter = aRect.center();
185 double aWidth = aRect.width() / aScale / aXScale;
186 double aHeight = aRect.height() / aScale / aYScale;
188 aRect = QRectF( aCenter.x() - aWidth / 2, aCenter.y() - aHeight / 2, aWidth, aHeight );
192 //================================================================
193 // Function : GenerateTranslationOnlyTransform
195 //================================================================
196 static QTransform GenerateTranslationOnlyTransform( const QTransform &theOriginalTransform,
197 const QPointF &theTargetPoint )
199 qreal dx = theOriginalTransform.m11() * theTargetPoint.x() - theTargetPoint.x() +
200 theOriginalTransform.m21() * theTargetPoint.y() +
201 theOriginalTransform.m31();
202 qreal dy = theOriginalTransform.m22() * theTargetPoint.y() - theTargetPoint.y() +
203 theOriginalTransform.m12() * theTargetPoint.x() +
204 theOriginalTransform.m32();
205 return QTransform::fromTranslate( dx, dy );
208 //================================================================
211 //================================================================
212 void HYDROGUI_PrsImageFrame::UnscaledGraphicsEllipseItem::paint(
213 QPainter* thePainter,
214 const QStyleOptionGraphicsItem* theOption,
218 thePainter->setTransform( GenerateTranslationOnlyTransform( thePainter->transform(),
220 QGraphicsEllipseItem::paint( thePainter, theOption, theWidget );
221 thePainter->restore();