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 QTransform aTransform = aParent->getViewTransform();
168 double aScale = aTransform.m11(); // same as m22(), viewer specific
169 if( fabs( aScale ) < EPSILON )
172 QPointF aCenter = aRect.center();
173 double aWidth = aRect.width() / aScale;
174 double aHeight = aRect.height() / aScale;
176 aRect = QRectF( aCenter.x() - aWidth / 2, aCenter.y() - aHeight / 2, aWidth, aHeight );
180 //================================================================
181 // Function : GenerateTranslationOnlyTransform
183 //================================================================
184 static QTransform GenerateTranslationOnlyTransform( const QTransform &theOriginalTransform,
185 const QPointF &theTargetPoint )
187 qreal dx = theOriginalTransform.m11() * theTargetPoint.x() - theTargetPoint.x() +
188 theOriginalTransform.m21() * theTargetPoint.y() +
189 theOriginalTransform.m31();
190 qreal dy = theOriginalTransform.m22() * theTargetPoint.y() - theTargetPoint.y() +
191 theOriginalTransform.m12() * theTargetPoint.x() +
192 theOriginalTransform.m32();
193 return QTransform::fromTranslate( dx, dy );
196 //================================================================
199 //================================================================
200 void HYDROGUI_PrsImageFrame::UnscaledGraphicsEllipseItem::paint(
201 QPainter* thePainter,
202 const QStyleOptionGraphicsItem* theOption,
206 thePainter->setTransform( GenerateTranslationOnlyTransform( thePainter->transform(),
209 QGraphicsEllipseItem::paint( thePainter, theOption, theWidget );
210 thePainter->restore();