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_Entity)& theObject )
36 : HYDROGUI_Prs( theObject ),
40 myIsTransformationPointPreview( false ),
41 myTransformationPointType( None )
43 myTransformationPointCursor = new QCursor( Qt::CrossCursor );
46 //=======================================================================
47 // name : HYDROGUI_PrsImage
48 // Purpose : Destructor
49 //=======================================================================
50 HYDROGUI_PrsImage::~HYDROGUI_PrsImage()
52 if( myTransformationPointCursor )
54 delete myTransformationPointCursor;
55 myTransformationPointCursor = 0;
59 //================================================================
60 // Function : setImage
62 //================================================================
63 void HYDROGUI_PrsImage::setImage( const QImage& theImage )
68 //================================================================
69 // Function : getImage
71 //================================================================
72 QImage HYDROGUI_PrsImage::getImage() const
77 //================================================================
78 // Function : setCaption
80 //================================================================
81 void HYDROGUI_PrsImage::setCaption( const QString& theCaption )
85 myCaptionItem->setText( theCaption );
86 myCaptionItem->setVisible( !theCaption.isEmpty() );
90 //================================================================
91 // Function : getCaption
93 //================================================================
94 QString HYDROGUI_PrsImage::getCaption() const
97 return myCaptionItem->text();
101 //================================================================
102 // Function : setIsTransformationPointPreview
104 //================================================================
105 void HYDROGUI_PrsImage::setIsTransformationPointPreview( const bool theState )
107 myIsTransformationPointPreview = theState;
110 //================================================================
111 // Function : getIsTransformationPointPreview
113 //================================================================
114 bool HYDROGUI_PrsImage::getIsTransformationPointPreview() const
116 return myIsTransformationPointPreview;
119 //================================================================
120 // Function : setTransformationPointType
122 //================================================================
123 void HYDROGUI_PrsImage::setTransformationPointType( const int thePointType )
125 myTransformationPointType = thePointType;
126 if( thePointType != None )
127 computeTransformationPoints();
130 //================================================================
131 // Function : setTransformationPointMap
133 //================================================================
134 void HYDROGUI_PrsImage::setTransformationPointMap( const TransformationPointMap& theMap )
136 myTransformationPointMap = theMap;
137 if( !theMap.isEmpty() )
138 computeTransformationPoints();
141 //================================================================
142 // Function : updateTransformationPoint
144 //================================================================
145 void HYDROGUI_PrsImage::updateTransformationPoint( const int thePointType,
149 if( myTransformationPointMap.find( thePointType ) != myTransformationPointMap.end() )
151 TransformationPoint& aTransformationPoint = myTransformationPointMap[ thePointType ];
152 QPoint& aPoint = aTransformationPoint.Point;
153 theIsY ? aPoint.setY( theValue ) : aPoint.setX( theValue );
154 computeTransformationPoints();
158 //================================================================
159 // Function : boundingRect
161 //================================================================
162 QRectF HYDROGUI_PrsImage::boundingRect() const
164 return myPixmapItem->boundingRect();
167 //================================================================
168 // Function : compute
170 //================================================================
171 void HYDROGUI_PrsImage::compute()
175 myPixmapItem = new QGraphicsPixmapItem( this );
176 addToGroup( myPixmapItem );
180 myCaptionItem = new QGraphicsSimpleTextItem( this );
182 QFont aFont = myCaptionItem->font();
183 aFont.setPointSize( 14 );
184 myCaptionItem->setFont( aFont );
186 addToGroup( myCaptionItem );
188 if( !myPrsImageFrame )
190 myPrsImageFrame = new HYDROGUI_PrsImageFrame( this );
191 addToGroup( myPrsImageFrame );
194 myPixmapItem->setPixmap( QPixmap::fromImage( myImage ) );
196 myCaptionItem->setPos( 0, -30 );
197 myCaptionItem->setVisible( false );
199 myPrsImageFrame->compute();
202 //================================================================
205 //================================================================
206 void HYDROGUI_PrsImage::addTo( GraphicsView_ViewPort* theViewPort )
208 HYDROGUI_Prs::addTo( theViewPort );
209 theViewPort->addItem( myPrsImageFrame );
212 //================================================================
213 // Function : removeFrom
215 //================================================================
216 void HYDROGUI_PrsImage::removeFrom( GraphicsView_ViewPort* theViewPort )
218 HYDROGUI_Prs::removeFrom( theViewPort );
219 theViewPort->removeItem( myPrsImageFrame );
222 //================================================================
223 // Function : checkHighlight
225 //================================================================
226 bool HYDROGUI_PrsImage::checkHighlight( double theX, double theY, QCursor& theCursor ) const
228 QRect aRect = myPixmapItem->boundingRect().toRect();
229 QPolygon aPolygon = sceneTransform().mapToPolygon( aRect );
230 if( aPolygon.containsPoint( QPoint( theX, theY ), Qt::OddEvenFill ) )
232 if( myIsTransformationPointPreview )
234 if( myTransformationPointType != None )
235 theCursor = *getTransformationPointCursor();
238 theCursor = *getHighlightCursor();
244 //================================================================
247 //================================================================
248 bool HYDROGUI_PrsImage::select( double theX, double theY, const QRectF& theRect )
250 if( myIsTransformationPointPreview )
252 if( myTransformationPointType == None || !theRect.isEmpty() )
255 QPoint aPos = pos().toPoint();
257 TransformationPoint& aTransformationPoint = myTransformationPointMap[ myTransformationPointType ];
258 aTransformationPoint.Point = QPoint( (int)theX, (int)theY ) - aPos;
259 computeTransformationPoints();
263 bool anIsSelected = HYDROGUI_Prs::select( theX, theY, theRect );
264 myPrsImageFrame->updateVisibility();
268 //================================================================
269 // Function : unselect
271 //================================================================
272 void HYDROGUI_PrsImage::unselect()
274 HYDROGUI_Prs::unselect();
275 if( !myIsTransformationPointPreview )
276 myPrsImageFrame->updateVisibility();
279 //================================================================
280 // Function : setSelected
282 //================================================================
283 void HYDROGUI_PrsImage::setSelected( bool theState )
285 HYDROGUI_Prs::setSelected( theState );
286 if( !myIsTransformationPointPreview )
287 myPrsImageFrame->updateVisibility();
290 //================================================================
291 // Function : computeTransformationPoints
293 //================================================================
294 void HYDROGUI_PrsImage::computeTransformationPoints()
296 if( myTransformationPointMap.isEmpty() )
298 int aWidth = myImage.width();
299 int aHeight = myImage.height();
301 // Create presentations for transformation points A, B and C
302 for( int aPointType = PointA; aPointType <= PointC; aPointType++ )
304 TransformationPoint aTransformationPoint;
308 QColor aColor = Qt::black;
312 aPoint = QPoint( 0, 0 );
314 aColor = Qt::darkRed;
317 aPoint = QPoint( aWidth, 0 );
319 aColor = Qt::darkGreen;
322 aPoint = QPoint( 0, aHeight );
324 aColor = Qt::darkBlue;
328 aTransformationPoint.Point = aPoint;
329 aTransformationPoint.Caption = aCaption;
331 aTransformationPoint.PointItem = new QGraphicsEllipseItem( this );
332 aTransformationPoint.PointItem->setVisible( false );
333 aTransformationPoint.PointItem->setPen( QPen( aColor ) );
334 aTransformationPoint.PointItem->setBrush( QBrush( aColor ) );
336 aTransformationPoint.CaptionItem = new QGraphicsSimpleTextItem( aCaption, this );
337 aTransformationPoint.CaptionItem->setVisible( false );
338 aTransformationPoint.CaptionItem->setPen( QPen( aColor ) );
339 aTransformationPoint.CaptionItem->setBrush( QBrush( aColor ) );
341 myTransformationPointMap[ aPointType ] = aTransformationPoint;
345 for( int aPointType = PointA; aPointType <= PointC; aPointType++ )
347 // Show/hide the point if necessary
348 updateTrsfPoint( aPointType );
352 //================================================================
353 // Function : updateTrsfPoint
355 //================================================================
356 void HYDROGUI_PrsImage::updateTrsfPoint( const int thePointType )
358 // If image is transformed only by two points then the point C is invisible
359 bool anIsPointVisible = myIsTransformationPointPreview && (
360 ( !myIsByTwoPoints ) || ( myIsByTwoPoints && ( thePointType != PointC ) ) );
361 TransformationPoint& aTransformationPoint = myTransformationPointMap[ thePointType ];
364 const QPointF& aPoint = aTransformationPoint.Point;
365 QRectF aRect( aPoint - QPointF( aRadius, aRadius ), QSizeF( aRadius * 2 + 1, aRadius * 2 + 1 ) );
366 aTransformationPoint.PointItem->setRect( aRect );
367 aTransformationPoint.PointItem->setVisible( anIsPointVisible );
369 QPointF aCaptionShift( -aRadius * 2, aRadius * 2 );
370 aTransformationPoint.CaptionItem->setPos( aPoint + aCaptionShift );
371 aTransformationPoint.CaptionItem->setVisible( anIsPointVisible );
374 //================================================================
375 // Function : getIsByTwoPoints
377 //================================================================
378 bool HYDROGUI_PrsImage::getIsByTwoPoints() const
380 return myIsByTwoPoints;
383 //================================================================
384 // Function : setIsByTwoPoints
386 //================================================================
387 void HYDROGUI_PrsImage::setIsByTwoPoints( const bool theIsByTwoPoints )
389 myIsByTwoPoints = theIsByTwoPoints;
390 if ( myTransformationPointMap.contains( PointC ) )
392 // Show/hide the point C if necessary
393 updateTrsfPoint( PointC );