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_PrsImage.h"
25 #include "HYDROGUI_PrsImageFrame.h"
27 #include <GraphicsView_ViewPort.h>
31 //=======================================================================
32 // name : HYDROGUI_PrsImage
33 // Purpose : Constructor
34 //=======================================================================
35 HYDROGUI_PrsImage::HYDROGUI_PrsImage( const Handle(HYDROData_Object)& theObject )
36 : HYDROGUI_Prs( theObject ),
39 myIsTransformationPointPreview( false ),
40 myTransformationPointType( None )
42 myTransformationPointCursor = new QCursor( Qt::CrossCursor );
45 //=======================================================================
46 // name : HYDROGUI_PrsImage
47 // Purpose : Destructor
48 //=======================================================================
49 HYDROGUI_PrsImage::~HYDROGUI_PrsImage()
51 if( myTransformationPointCursor )
53 delete myTransformationPointCursor;
54 myTransformationPointCursor = 0;
58 //================================================================
59 // Function : setImage
61 //================================================================
62 void HYDROGUI_PrsImage::setImage( const QImage& theImage )
67 //================================================================
68 // Function : getImage
70 //================================================================
71 QImage HYDROGUI_PrsImage::getImage() const
76 //================================================================
77 // Function : setIsTransformationPointPreview
79 //================================================================
80 void HYDROGUI_PrsImage::setIsTransformationPointPreview( const bool theState )
82 myIsTransformationPointPreview = theState;
85 //================================================================
86 // Function : getIsTransformationPointPreview
88 //================================================================
89 bool HYDROGUI_PrsImage::getIsTransformationPointPreview() const
91 return myIsTransformationPointPreview;
94 //================================================================
95 // Function : setTransformationPointType
97 //================================================================
98 void HYDROGUI_PrsImage::setTransformationPointType( const int thePointType )
100 myTransformationPointType = thePointType;
101 if( thePointType != None )
102 computeTransformationPoints();
105 //================================================================
106 // Function : setTransformationPointMap
108 //================================================================
109 void HYDROGUI_PrsImage::setTransformationPointMap( const TransformationPointMap& theMap )
111 myTransformationPointMap = theMap;
112 if( !theMap.isEmpty() )
113 computeTransformationPoints();
116 //================================================================
117 // Function : updateTransformationPoint
119 //================================================================
120 void HYDROGUI_PrsImage::updateTransformationPoint( const int thePointType,
124 if( myTransformationPointMap.find( thePointType ) != myTransformationPointMap.end() )
126 TransformationPoint& aTransformationPoint = myTransformationPointMap[ thePointType ];
127 QPoint& aPoint = aTransformationPoint.Point;
128 theIsY ? aPoint.setY( theValue ) : aPoint.setX( theValue );
129 computeTransformationPoints();
133 //================================================================
134 // Function : boundingRect
136 //================================================================
137 QRectF HYDROGUI_PrsImage::boundingRect() const
139 return myPixmapItem->boundingRect();
142 //================================================================
143 // Function : compute
145 //================================================================
146 void HYDROGUI_PrsImage::compute()
150 myPixmapItem = new QGraphicsPixmapItem( this );
151 addToGroup( myPixmapItem );
153 if( !myPrsImageFrame )
155 myPrsImageFrame = new HYDROGUI_PrsImageFrame( this );
156 addToGroup( myPrsImageFrame );
158 myPixmapItem->setPixmap( QPixmap::fromImage( myImage ) );
159 myPrsImageFrame->compute();
162 //================================================================
165 //================================================================
166 void HYDROGUI_PrsImage::addTo( GraphicsView_ViewPort* theViewPort )
168 HYDROGUI_Prs::addTo( theViewPort );
169 theViewPort->addItem( myPrsImageFrame );
172 //================================================================
173 // Function : removeFrom
175 //================================================================
176 void HYDROGUI_PrsImage::removeFrom( GraphicsView_ViewPort* theViewPort )
178 HYDROGUI_Prs::removeFrom( theViewPort );
179 theViewPort->removeItem( myPrsImageFrame );
182 //================================================================
183 // Function : checkHighlight
185 //================================================================
186 bool HYDROGUI_PrsImage::checkHighlight( double theX, double theY, QCursor& theCursor ) const
188 QRect aRect = myPixmapItem->boundingRect().toRect();
189 QPolygon aPolygon = sceneTransform().mapToPolygon( aRect );
190 if( aPolygon.containsPoint( QPoint( theX, theY ), Qt::OddEvenFill ) )
192 if( myIsTransformationPointPreview )
194 if( myTransformationPointType != None )
195 theCursor = *getTransformationPointCursor();
198 theCursor = *getHighlightCursor();
204 //================================================================
207 //================================================================
208 bool HYDROGUI_PrsImage::select( double theX, double theY, const QRectF& theRect )
210 if( myIsTransformationPointPreview )
212 if( myTransformationPointType == None || !theRect.isEmpty() )
215 QPoint aPos = pos().toPoint();
217 TransformationPoint& aTransformationPoint = myTransformationPointMap[ myTransformationPointType ];
218 aTransformationPoint.Point = QPoint( (int)theX, (int)theY ) - aPos;
219 computeTransformationPoints();
223 bool anIsSelected = HYDROGUI_Prs::select( theX, theY, theRect );
224 myPrsImageFrame->updateVisibility();
228 //================================================================
229 // Function : unselect
231 //================================================================
232 void HYDROGUI_PrsImage::unselect()
234 HYDROGUI_Prs::unselect();
235 if( !myIsTransformationPointPreview )
236 myPrsImageFrame->updateVisibility();
239 //================================================================
240 // Function : setSelected
242 //================================================================
243 void HYDROGUI_PrsImage::setSelected( bool theState )
245 HYDROGUI_Prs::setSelected( theState );
246 if( !myIsTransformationPointPreview )
247 myPrsImageFrame->updateVisibility();
250 //================================================================
251 // Function : computeTransformationPoints
253 //================================================================
254 void HYDROGUI_PrsImage::computeTransformationPoints()
256 if( myTransformationPointMap.isEmpty() )
258 int aWidth = myImage.width();
259 int aHeight = myImage.height();
261 for( int aPointType = PointA; aPointType <= PointC; aPointType++ )
263 TransformationPoint aTransformationPoint;
267 QColor aColor = Qt::black;
271 aPoint = QPoint( 0, 0 );
273 aColor = Qt::darkRed;
276 aPoint = QPoint( aWidth, 0 );
278 aColor = Qt::darkGreen;
281 aPoint = QPoint( 0, aHeight );
283 aColor = Qt::darkBlue;
287 aTransformationPoint.Point = aPoint;
288 aTransformationPoint.Caption = aCaption;
290 aTransformationPoint.PointItem = new QGraphicsEllipseItem( this );
291 aTransformationPoint.PointItem->setVisible( false );
292 aTransformationPoint.PointItem->setPen( QPen( aColor ) );
293 aTransformationPoint.PointItem->setBrush( QBrush( aColor ) );
295 aTransformationPoint.CaptionItem = new QGraphicsSimpleTextItem( aCaption, this );
296 aTransformationPoint.CaptionItem->setVisible( false );
297 aTransformationPoint.CaptionItem->setPen( QPen( aColor ) );
298 aTransformationPoint.CaptionItem->setBrush( QBrush( aColor ) );
300 myTransformationPointMap[ aPointType ] = aTransformationPoint;
304 bool anIsVisible = myIsTransformationPointPreview;
305 for( int aPointType = PointA; aPointType <= PointC; aPointType++ )
307 TransformationPoint& aTransformationPoint = myTransformationPointMap[ aPointType ];
310 const QPointF& aPoint = aTransformationPoint.Point;
311 QRectF aRect( aPoint - QPointF( aRadius, aRadius ), QSizeF( aRadius * 2 + 1, aRadius * 2 + 1 ) );
312 aTransformationPoint.PointItem->setRect( aRect );
313 aTransformationPoint.PointItem->setVisible( anIsVisible );
315 QPointF aCaptionShift( -aRadius * 2, aRadius * 2 );
316 aTransformationPoint.CaptionItem->setPos( aPoint + aCaptionShift );
317 aTransformationPoint.CaptionItem->setVisible( anIsVisible );