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>
30 #include <QApplication>
32 //=======================================================================
33 // name : HYDROGUI_PrsImage
34 // Purpose : Constructor
35 //=======================================================================
36 HYDROGUI_PrsImage::HYDROGUI_PrsImage( const Handle(HYDROData_Entity)& theObject )
37 : HYDROGUI_Prs( theObject ),
41 myIsTransformationPointPreview( false ),
42 myIsByTwoPoints( true ),
43 myTransformationPointType( None )
45 myTransformationPointCursor = new QCursor( Qt::CrossCursor );
48 //=======================================================================
49 // name : HYDROGUI_PrsImage
50 // Purpose : Destructor
51 //=======================================================================
52 HYDROGUI_PrsImage::~HYDROGUI_PrsImage()
54 if( myTransformationPointCursor )
56 delete myTransformationPointCursor;
57 myTransformationPointCursor = 0;
61 //================================================================
62 // Function : setImage
64 //================================================================
65 void HYDROGUI_PrsImage::setImage( const QImage& theImage )
70 //================================================================
71 // Function : getImage
73 //================================================================
74 QImage HYDROGUI_PrsImage::getImage() const
79 //================================================================
80 // Function : setCaption
82 //================================================================
83 void HYDROGUI_PrsImage::setCaption( const QString& theCaption )
87 myCaptionItem->setText( theCaption );
88 myCaptionItem->setVisible( !theCaption.isEmpty() );
92 //================================================================
93 // Function : getCaption
95 //================================================================
96 QString HYDROGUI_PrsImage::getCaption() const
99 return myCaptionItem->text();
103 //================================================================
104 // Function : setIsTransformationPointPreview
106 //================================================================
107 void HYDROGUI_PrsImage::setIsTransformationPointPreview( const bool theState )
109 myIsTransformationPointPreview = theState;
112 //================================================================
113 // Function : getIsTransformationPointPreview
115 //================================================================
116 bool HYDROGUI_PrsImage::getIsTransformationPointPreview() const
118 return myIsTransformationPointPreview;
121 //================================================================
122 // Function : setTransformationPointType
124 //================================================================
125 void HYDROGUI_PrsImage::setTransformationPointType( const int thePointType )
127 myTransformationPointType = thePointType;
128 if( thePointType != None )
129 computeTransformationPoints();
132 //================================================================
133 // Function : setTransformationPointMap
135 //================================================================
136 void HYDROGUI_PrsImage::setTransformationPointMap( const TransformationPointMap& theMap )
138 myTransformationPointMap = theMap;
139 if( !theMap.isEmpty() )
140 computeTransformationPoints();
143 //================================================================
144 // Function : updateTransformationPoint
146 //================================================================
147 void HYDROGUI_PrsImage::updateTransformationPoint( const int thePointType,
151 if( myTransformationPointMap.find( thePointType ) != myTransformationPointMap.end() )
153 TransformationPoint& aTransformationPoint = myTransformationPointMap[ thePointType ];
154 QPoint& aPoint = aTransformationPoint.Point;
155 theIsY ? aPoint.setY( theValue ) : aPoint.setX( theValue );
156 computeTransformationPoints();
160 //================================================================
161 // Function : boundingRect
163 //================================================================
164 QRectF HYDROGUI_PrsImage::boundingRect() const
166 return myPixmapItem->boundingRect();
169 //================================================================
170 // Function : compute
172 //================================================================
173 void HYDROGUI_PrsImage::compute()
177 myPixmapItem = new QGraphicsPixmapItem( this );
178 addToGroup( myPixmapItem );
182 myCaptionItem = new QGraphicsSimpleTextItem( this );
184 QFont aFont = myCaptionItem->font();
185 aFont.setPointSize( 14 );
186 myCaptionItem->setFont( aFont );
188 addToGroup( myCaptionItem );
190 if( !myPrsImageFrame )
192 myPrsImageFrame = new HYDROGUI_PrsImageFrame( this );
193 addToGroup( myPrsImageFrame );
196 myPixmapItem->setPixmap( QPixmap::fromImage( myImage ) );
198 myCaptionItem->setPos( 0, -30 );
199 myCaptionItem->setVisible( false );
201 myPrsImageFrame->compute();
204 //================================================================
207 //================================================================
208 void HYDROGUI_PrsImage::addTo( GraphicsView_ViewPort* theViewPort )
210 HYDROGUI_Prs::addTo( theViewPort );
211 theViewPort->addItem( myPrsImageFrame );
214 //================================================================
215 // Function : removeFrom
217 //================================================================
218 void HYDROGUI_PrsImage::removeFrom( GraphicsView_ViewPort* theViewPort )
220 HYDROGUI_Prs::removeFrom( theViewPort );
221 theViewPort->removeItem( myPrsImageFrame );
224 //================================================================
225 // Function : checkHighlight
227 //================================================================
228 bool HYDROGUI_PrsImage::checkHighlight( double theX, double theY, QCursor& theCursor ) const
230 QRect aRect = myPixmapItem->boundingRect().toRect();
231 QPolygon aPolygon = sceneTransform().mapToPolygon( aRect );
232 if( aPolygon.containsPoint( QPoint( theX, theY ), Qt::OddEvenFill ) )
234 if( myIsTransformationPointPreview )
236 if( myTransformationPointType != None )
237 theCursor = *getTransformationPointCursor();
240 theCursor = *getHighlightCursor();
246 //================================================================
249 //================================================================
250 bool HYDROGUI_PrsImage::select( double theX, double theY, const QRectF& theRect )
252 if( myIsTransformationPointPreview )
254 if( myTransformationPointType == None || !theRect.isEmpty() )
257 QPoint aPos = pos().toPoint();
259 TransformationPoint& aTransformationPoint = myTransformationPointMap[ myTransformationPointType ];
260 aTransformationPoint.Point = QPoint( (int)theX, (int)theY ) - aPos;
261 computeTransformationPoints();
265 bool anIsSelected = HYDROGUI_Prs::select( theX, theY, theRect );
266 myPrsImageFrame->updateVisibility();
270 //================================================================
271 // Function : unselect
273 //================================================================
274 void HYDROGUI_PrsImage::unselect()
276 HYDROGUI_Prs::unselect();
277 if( !myIsTransformationPointPreview )
278 myPrsImageFrame->updateVisibility();
281 //================================================================
282 // Function : setSelected
284 //================================================================
285 void HYDROGUI_PrsImage::setSelected( bool theState )
287 HYDROGUI_Prs::setSelected( theState );
288 if( !myIsTransformationPointPreview )
289 myPrsImageFrame->updateVisibility();
292 //================================================================
293 // Function : computeTransformationPoints
295 //================================================================
296 void HYDROGUI_PrsImage::computeTransformationPoints()
298 if( myTransformationPointMap.isEmpty() )
300 int aWidth = myImage.width();
301 int aHeight = myImage.height();
303 // Create presentations for transformation points A, B and C
304 for( int aPointType = PointA; aPointType <= PointC; aPointType++ )
306 TransformationPoint aTransformationPoint;
310 QColor aColor = Qt::black;
314 aPoint = QPoint( 0, 0 );
316 aColor = Qt::darkRed;
319 aPoint = QPoint( aWidth, 0 );
321 aColor = Qt::darkGreen;
324 aPoint = QPoint( 0, aHeight );
326 aColor = Qt::darkBlue;
330 aTransformationPoint.Point = aPoint;
331 aTransformationPoint.Caption = aCaption;
333 QGraphicsEllipseItem* aPointItem = new QGraphicsEllipseItem( this );
334 aPointItem->setPen( QPen( aColor ) );
335 aPointItem->setBrush( QBrush( aColor ) );
338 QRectF aRect( -QPointF( aRadius, aRadius ), QSizeF( aRadius * 2 + 1, aRadius * 2 + 1 ) );
339 aPointItem->setRect( aRect );
340 aPointItem->setPos( QPointF( 0, 0 ) );
342 QGraphicsSimpleTextItem* aCaptionItem = aCaptionItem = new QGraphicsSimpleTextItem( aCaption, this );
343 aCaptionItem->setPen( QPen( aColor ) );
344 aCaptionItem->setBrush( QBrush( aColor ) );
345 QFont aFont = aCaptionItem->font();
346 aFont.setPointSize( qApp->font().pointSize() );
347 aCaptionItem->setFont( aFont );
348 aCaptionItem->setPos( QPointF( -aRadius * 2, aRadius * 2 ) );
350 aTransformationPoint.GroupItem = new QGraphicsItemGroup( this );
351 aTransformationPoint.GroupItem->addToGroup( aPointItem );
352 aTransformationPoint.GroupItem->addToGroup( aCaptionItem );
353 aTransformationPoint.GroupItem->setVisible( false );
354 aTransformationPoint.GroupItem->setFlag( QGraphicsItem::ItemIgnoresTransformations );
355 aTransformationPoint.GroupItem->setPos( aPoint );
357 myTransformationPointMap[ aPointType ] = aTransformationPoint;
361 for( int aPointType = PointA; aPointType <= PointC; aPointType++ )
363 // Show/hide the point if necessary
364 updateTrsfPoint( aPointType );
368 //================================================================
369 // Function : updateTrsfPoint
371 //================================================================
372 void HYDROGUI_PrsImage::updateTrsfPoint( const int thePointType )
374 // If image is transformed only by two points then the point C is invisible
375 bool anIsPointVisible = myIsTransformationPointPreview && (
376 ( !myIsByTwoPoints ) || ( myIsByTwoPoints && ( thePointType != PointC ) ) );
378 TransformationPoint& aTransformationPoint = myTransformationPointMap[ thePointType ];
380 const QPointF& aPoint = aTransformationPoint.Point;
381 aTransformationPoint.GroupItem->setPos( aPoint );
382 aTransformationPoint.GroupItem->setVisible( anIsPointVisible );
385 //================================================================
386 // Function : getIsByTwoPoints
388 //================================================================
389 bool HYDROGUI_PrsImage::getIsByTwoPoints() const
391 return myIsByTwoPoints;
394 //================================================================
395 // Function : setIsByTwoPoints
397 //================================================================
398 void HYDROGUI_PrsImage::setIsByTwoPoints( const bool theIsByTwoPoints )
400 myIsByTwoPoints = theIsByTwoPoints;
401 if ( myTransformationPointMap.contains( PointC ) )
403 // Show/hide the point C if necessary
404 updateTrsfPoint( PointC );