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 myTransformationPointType( None )
44 myTransformationPointCursor = new QCursor( Qt::CrossCursor );
47 //=======================================================================
48 // name : HYDROGUI_PrsImage
49 // Purpose : Destructor
50 //=======================================================================
51 HYDROGUI_PrsImage::~HYDROGUI_PrsImage()
53 if( myTransformationPointCursor )
55 delete myTransformationPointCursor;
56 myTransformationPointCursor = 0;
60 //================================================================
61 // Function : setImage
63 //================================================================
64 void HYDROGUI_PrsImage::setImage( const QImage& theImage )
69 //================================================================
70 // Function : getImage
72 //================================================================
73 QImage HYDROGUI_PrsImage::getImage() const
78 //================================================================
79 // Function : setCaption
81 //================================================================
82 void HYDROGUI_PrsImage::setCaption( const QString& theCaption )
86 myCaptionItem->setText( theCaption );
87 myCaptionItem->setVisible( !theCaption.isEmpty() );
91 //================================================================
92 // Function : getCaption
94 //================================================================
95 QString HYDROGUI_PrsImage::getCaption() const
98 return myCaptionItem->text();
102 //================================================================
103 // Function : setIsTransformationPointPreview
105 //================================================================
106 void HYDROGUI_PrsImage::setIsTransformationPointPreview( const bool theState )
108 myIsTransformationPointPreview = theState;
111 //================================================================
112 // Function : getIsTransformationPointPreview
114 //================================================================
115 bool HYDROGUI_PrsImage::getIsTransformationPointPreview() const
117 return myIsTransformationPointPreview;
120 //================================================================
121 // Function : setTransformationPointType
123 //================================================================
124 void HYDROGUI_PrsImage::setTransformationPointType( const int thePointType )
126 myTransformationPointType = thePointType;
127 if( thePointType != None )
128 computeTransformationPoints();
131 //================================================================
132 // Function : setTransformationPointMap
134 //================================================================
135 void HYDROGUI_PrsImage::setTransformationPointMap( const TransformationPointMap& theMap )
137 myTransformationPointMap = theMap;
138 if( !theMap.isEmpty() )
139 computeTransformationPoints();
142 //================================================================
143 // Function : updateTransformationPoint
145 //================================================================
146 void HYDROGUI_PrsImage::updateTransformationPoint( const int thePointType,
150 if( myTransformationPointMap.find( thePointType ) != myTransformationPointMap.end() )
152 TransformationPoint& aTransformationPoint = myTransformationPointMap[ thePointType ];
153 QPoint& aPoint = aTransformationPoint.Point;
154 theIsY ? aPoint.setY( theValue ) : aPoint.setX( theValue );
155 computeTransformationPoints();
159 //================================================================
160 // Function : boundingRect
162 //================================================================
163 QRectF HYDROGUI_PrsImage::boundingRect() const
165 return myPixmapItem->boundingRect();
168 //================================================================
169 // Function : compute
171 //================================================================
172 void HYDROGUI_PrsImage::compute()
176 myPixmapItem = new QGraphicsPixmapItem( this );
177 addToGroup( myPixmapItem );
181 myCaptionItem = new QGraphicsSimpleTextItem( this );
183 QFont aFont = myCaptionItem->font();
184 aFont.setPointSize( 14 );
185 myCaptionItem->setFont( aFont );
187 addToGroup( myCaptionItem );
189 if( !myPrsImageFrame )
191 myPrsImageFrame = new HYDROGUI_PrsImageFrame( this );
192 addToGroup( myPrsImageFrame );
195 myPixmapItem->setPixmap( QPixmap::fromImage( myImage ) );
197 myCaptionItem->setPos( 0, -30 );
198 myCaptionItem->setVisible( false );
200 myPrsImageFrame->compute();
203 //================================================================
206 //================================================================
207 void HYDROGUI_PrsImage::addTo( GraphicsView_ViewPort* theViewPort )
209 HYDROGUI_Prs::addTo( theViewPort );
210 theViewPort->addItem( myPrsImageFrame );
213 //================================================================
214 // Function : removeFrom
216 //================================================================
217 void HYDROGUI_PrsImage::removeFrom( GraphicsView_ViewPort* theViewPort )
219 HYDROGUI_Prs::removeFrom( theViewPort );
220 theViewPort->removeItem( myPrsImageFrame );
223 //================================================================
224 // Function : checkHighlight
226 //================================================================
227 bool HYDROGUI_PrsImage::checkHighlight( double theX, double theY, QCursor& theCursor ) const
229 QRect aRect = myPixmapItem->boundingRect().toRect();
230 QPolygon aPolygon = sceneTransform().mapToPolygon( aRect );
231 if( aPolygon.containsPoint( QPoint( theX, theY ), Qt::OddEvenFill ) )
233 if( myIsTransformationPointPreview )
235 if( myTransformationPointType != None )
236 theCursor = *getTransformationPointCursor();
239 theCursor = *getHighlightCursor();
245 //================================================================
248 //================================================================
249 bool HYDROGUI_PrsImage::select( double theX, double theY, const QRectF& theRect )
251 if( myIsTransformationPointPreview )
253 if( myTransformationPointType == None || !theRect.isEmpty() )
256 QPoint aPos = pos().toPoint();
258 TransformationPoint& aTransformationPoint = myTransformationPointMap[ myTransformationPointType ];
259 aTransformationPoint.Point = QPoint( (int)theX, (int)theY ) - aPos;
260 computeTransformationPoints();
264 bool anIsSelected = HYDROGUI_Prs::select( theX, theY, theRect );
265 myPrsImageFrame->updateVisibility();
269 //================================================================
270 // Function : unselect
272 //================================================================
273 void HYDROGUI_PrsImage::unselect()
275 HYDROGUI_Prs::unselect();
276 if( !myIsTransformationPointPreview )
277 myPrsImageFrame->updateVisibility();
280 //================================================================
281 // Function : setSelected
283 //================================================================
284 void HYDROGUI_PrsImage::setSelected( bool theState )
286 HYDROGUI_Prs::setSelected( theState );
287 if( !myIsTransformationPointPreview )
288 myPrsImageFrame->updateVisibility();
291 //================================================================
292 // Function : computeTransformationPoints
294 //================================================================
295 void HYDROGUI_PrsImage::computeTransformationPoints()
297 if( myTransformationPointMap.isEmpty() )
299 int aWidth = myImage.width();
300 int aHeight = myImage.height();
302 // Create presentations for transformation points A, B and C
303 for( int aPointType = PointA; aPointType <= PointC; aPointType++ )
305 TransformationPoint aTransformationPoint;
309 QColor aColor = Qt::black;
313 aPoint = QPoint( 0, 0 );
315 aColor = Qt::darkRed;
318 aPoint = QPoint( aWidth, 0 );
320 aColor = Qt::darkGreen;
323 aPoint = QPoint( 0, aHeight );
325 aColor = Qt::darkBlue;
329 aTransformationPoint.Point = aPoint;
330 aTransformationPoint.Caption = aCaption;
332 QGraphicsEllipseItem* aPointItem = new QGraphicsEllipseItem( this );
333 aPointItem->setPen( QPen( aColor ) );
334 aPointItem->setBrush( QBrush( aColor ) );
337 QRectF aRect( -QPointF( aRadius, aRadius ), QSizeF( aRadius * 2 + 1, aRadius * 2 + 1 ) );
338 aPointItem->setRect( aRect );
339 aPointItem->setPos( QPointF( 0, 0 ) );
341 QGraphicsSimpleTextItem* aCaptionItem = aCaptionItem = new QGraphicsSimpleTextItem( aCaption, this );
342 aCaptionItem->setPen( QPen( aColor ) );
343 aCaptionItem->setBrush( QBrush( aColor ) );
344 QFont aFont = aCaptionItem->font();
345 aFont.setPointSize( qApp->font().pointSize() );
346 aCaptionItem->setFont( aFont );
347 aCaptionItem->setPos( QPointF( -aRadius * 2, aRadius * 2 ) );
349 aTransformationPoint.GroupItem = new QGraphicsItemGroup( this );
350 aTransformationPoint.GroupItem->addToGroup( aPointItem );
351 aTransformationPoint.GroupItem->addToGroup( aCaptionItem );
352 aTransformationPoint.GroupItem->setVisible( false );
353 aTransformationPoint.GroupItem->setFlag( QGraphicsItem::ItemIgnoresTransformations );
354 aTransformationPoint.GroupItem->setPos( aPoint );
356 myTransformationPointMap[ aPointType ] = aTransformationPoint;
360 for( int aPointType = PointA; aPointType <= PointC; aPointType++ )
362 // Show/hide the point if necessary
363 updateTrsfPoint( aPointType );
367 //================================================================
368 // Function : updateTrsfPoint
370 //================================================================
371 void HYDROGUI_PrsImage::updateTrsfPoint( const int thePointType )
373 // If image is transformed only by two points then the point C is invisible
374 bool anIsPointVisible = myIsTransformationPointPreview && (
375 ( !myIsByTwoPoints ) || ( myIsByTwoPoints && ( thePointType != PointC ) ) );
377 TransformationPoint& aTransformationPoint = myTransformationPointMap[ thePointType ];
379 const QPointF& aPoint = aTransformationPoint.Point;
380 aTransformationPoint.GroupItem->setPos( aPoint );
381 aTransformationPoint.GroupItem->setVisible( anIsPointVisible );
384 //================================================================
385 // Function : getIsByTwoPoints
387 //================================================================
388 bool HYDROGUI_PrsImage::getIsByTwoPoints() const
390 return myIsByTwoPoints;
393 //================================================================
394 // Function : setIsByTwoPoints
396 //================================================================
397 void HYDROGUI_PrsImage::setIsByTwoPoints( const bool theIsByTwoPoints )
399 myIsByTwoPoints = theIsByTwoPoints;
400 if ( myTransformationPointMap.contains( PointC ) )
402 // Show/hide the point C if necessary
403 updateTrsfPoint( PointC );