1 // Copyright (C) 2007-2015 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, or (at your option) any later version.
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, const bool theCompute )
70 computeTransformationPoints( true );
74 //================================================================
75 // Function : getImage
77 //================================================================
78 QImage HYDROGUI_PrsImage::getImage() const
83 //================================================================
84 // Function : setCaption
86 //================================================================
87 void HYDROGUI_PrsImage::setCaption( const QString& theCaption )
91 myCaptionItem->setText( theCaption );
92 myCaptionItem->setVisible( !theCaption.isEmpty() );
96 //================================================================
97 // Function : getCaption
99 //================================================================
100 QString HYDROGUI_PrsImage::getCaption() const
103 return myCaptionItem->text();
107 //================================================================
108 // Function : setIsTransformationPointPreview
110 //================================================================
111 void HYDROGUI_PrsImage::setIsTransformationPointPreview( const bool theState )
113 myIsTransformationPointPreview = theState;
116 //================================================================
117 // Function : getIsTransformationPointPreview
119 //================================================================
120 bool HYDROGUI_PrsImage::getIsTransformationPointPreview() const
122 return myIsTransformationPointPreview;
125 //================================================================
126 // Function : setTransformationPointType
128 //================================================================
129 void HYDROGUI_PrsImage::setTransformationPointType( const int thePointType )
131 myTransformationPointType = thePointType;
132 if( thePointType != None )
133 computeTransformationPoints();
136 //================================================================
137 // Function : setTransformationPointMap
139 //================================================================
140 void HYDROGUI_PrsImage::setTransformationPointMap( const TransformationPointMap& theMap )
142 myTransformationPointMap = theMap;
143 if( !theMap.isEmpty() )
144 computeTransformationPoints();
147 //================================================================
148 // Function : updateTransformationPoint
150 //================================================================
151 void HYDROGUI_PrsImage::updateTransformationPoint( const int thePointType,
155 if( myTransformationPointMap.find( thePointType ) != myTransformationPointMap.end() )
157 TransformationPoint& aTransformationPoint = myTransformationPointMap[ thePointType ];
158 QPoint& aPoint = aTransformationPoint.Point;
159 theIsY ? aPoint.setY( theValue ) : aPoint.setX( theValue );
160 computeTransformationPoints();
164 //================================================================
165 // Function : setTransformationPointCursorShape
167 //================================================================
168 void HYDROGUI_PrsImage::setTransformationPointCursorShape(Qt::CursorShape theCursorShape)
170 if ( myTransformationPointCursor )
171 myTransformationPointCursor->setShape(theCursorShape);
174 //================================================================
175 // Function : boundingRect
177 //================================================================
178 QRectF HYDROGUI_PrsImage::boundingRect() const
180 return myPixmapItem->boundingRect();
183 //================================================================
184 // Function : compute
186 //================================================================
187 void HYDROGUI_PrsImage::compute()
191 myPixmapItem = new QGraphicsPixmapItem( this );
192 addToGroup( myPixmapItem );
196 myCaptionItem = new QGraphicsSimpleTextItem( this );
198 QFont aFont = myCaptionItem->font();
199 aFont.setPointSize( 14 );
200 myCaptionItem->setFont( aFont );
202 addToGroup( myCaptionItem );
204 if( !myPrsImageFrame )
206 myPrsImageFrame = new HYDROGUI_PrsImageFrame( this );
207 addToGroup( myPrsImageFrame );
210 myPixmapItem->setPixmap( QPixmap::fromImage( myImage ) );
212 myCaptionItem->setPos( 0, -30 );
213 myCaptionItem->setVisible( false );
215 myPrsImageFrame->compute();
218 //================================================================
221 //================================================================
222 void HYDROGUI_PrsImage::addTo( GraphicsView_ViewPort* theViewPort )
224 HYDROGUI_Prs::addTo( theViewPort );
225 theViewPort->addItem( myPrsImageFrame );
228 //================================================================
229 // Function : removeFrom
231 //================================================================
232 void HYDROGUI_PrsImage::removeFrom( GraphicsView_ViewPort* theViewPort )
234 HYDROGUI_Prs::removeFrom( theViewPort );
235 theViewPort->removeItem( myPrsImageFrame );
238 //================================================================
239 // Function : checkHighlight
241 //================================================================
242 bool HYDROGUI_PrsImage::checkHighlight( double theX, double theY, QCursor& theCursor ) const
244 QRect aRect = myPixmapItem->boundingRect().toRect();
245 QPolygon aPolygon = sceneTransform().mapToPolygon( aRect );
246 if( aPolygon.containsPoint( QPoint( theX, theY ), Qt::OddEvenFill ) )
248 if( myIsTransformationPointPreview )
250 if( myTransformationPointType != None )
251 theCursor = *getTransformationPointCursor();
254 theCursor = *getHighlightCursor();
260 //================================================================
263 //================================================================
264 bool HYDROGUI_PrsImage::select( double theX, double theY, const QRectF& theRect )
266 if( myIsTransformationPointPreview )
268 if( myTransformationPointType == None || !theRect.isEmpty() )
271 QPoint aPos = pos().toPoint();
273 TransformationPoint& aTransformationPoint = myTransformationPointMap[ myTransformationPointType ];
274 aTransformationPoint.Point = QPoint( (int)theX, (int)theY ) - aPos;
275 computeTransformationPoints();
279 bool anIsSelected = HYDROGUI_Prs::select( theX, theY, theRect );
280 myPrsImageFrame->updateVisibility();
284 //================================================================
285 // Function : unselect
287 //================================================================
288 void HYDROGUI_PrsImage::unselect()
290 HYDROGUI_Prs::unselect();
291 if( !myIsTransformationPointPreview )
292 myPrsImageFrame->updateVisibility();
295 //================================================================
296 // Function : setSelected
298 //================================================================
299 void HYDROGUI_PrsImage::setSelected( bool theState )
301 HYDROGUI_Prs::setSelected( theState );
302 if( !myIsTransformationPointPreview )
303 myPrsImageFrame->updateVisibility();
306 //================================================================
307 // Function : computeTransformationPoints
309 //================================================================
310 void HYDROGUI_PrsImage::computeTransformationPoints( const bool theObligatoryInit )
313 for( int aPointType = PointA; aPointType <= PointC; aPointType++ )
315 if( myTransformationPointMap.isEmpty() || theObligatoryInit )
316 initTrsfPoints( aPointType );
317 // Show/hide the point if necessary
318 updateTrsfPoint( aPointType );
324 QGraphicsItemGroup* HYDROGUI_PrsImage::createPointItem( const QString& theCaption,
325 const QColor& theColor )
327 QGraphicsEllipseItem* aPointItem = new QGraphicsEllipseItem( this );
328 aPointItem->setPen( QPen( theColor ) );
329 aPointItem->setBrush( QBrush( theColor ) );
332 QRectF aRect( -QPointF( aRadius, aRadius ), QSizeF( aRadius * 2 + 1, aRadius * 2 + 1 ) );
333 aPointItem->setRect( aRect );
334 aPointItem->setPos( QPointF( 0, 0 ) );
336 QGraphicsSimpleTextItem* aCaptionItem = aCaptionItem = new QGraphicsSimpleTextItem( theCaption, this );
337 aCaptionItem->setPen( QPen( theColor ) );
338 aCaptionItem->setBrush( QBrush( theColor ) );
339 QFont aFont = aCaptionItem->font();
340 aFont.setPointSize( qApp->font().pointSize() );
341 aCaptionItem->setFont( aFont );
342 aCaptionItem->setPos( QPointF( -aRadius * 2, aRadius * 2 ) );
344 QGraphicsItemGroup* aGroupItem = new QGraphicsItemGroup( this );
345 aGroupItem->addToGroup( aPointItem );
346 aGroupItem->addToGroup( aCaptionItem );
347 aGroupItem->setVisible( false );
348 aGroupItem->setFlag( QGraphicsItem::ItemIgnoresTransformations );
353 //================================================================
354 // Function : initTrsfPoints
356 //================================================================
357 void HYDROGUI_PrsImage::initTrsfPoints( const int thePointType )
361 QColor aColor = Qt::black;
363 int aWidth = myImage.width();
364 int aHeight = myImage.height();
365 switch( thePointType )
368 aPoint = QPoint( 0, 0 );
370 aColor = Qt::darkRed;
373 aPoint = QPoint( aWidth, 0 );
375 aColor = Qt::darkGreen;
378 aPoint = QPoint( 0, aHeight );
380 aColor = Qt::darkBlue;
384 TransformationPoint aTransformationPoint;
385 if ( myTransformationPointMap.contains( thePointType ) )
386 aTransformationPoint = myTransformationPointMap[thePointType];
388 aTransformationPoint.Point = aPoint;
389 aTransformationPoint.Caption = aCaption;
390 if ( !aTransformationPoint.GroupItem )
391 aTransformationPoint.GroupItem = createPointItem( aCaption, aColor );
392 aTransformationPoint.GroupItem->setPos( aPoint );
394 myTransformationPointMap[ thePointType ] = aTransformationPoint;
397 //================================================================
398 // Function : updateTrsfPoint
400 //================================================================
401 void HYDROGUI_PrsImage::updateTrsfPoint( const int thePointType )
403 // If image is transformed only by two points then the point C is invisible
404 bool anIsPointVisible = myIsTransformationPointPreview && (
405 ( !myIsByTwoPoints ) || ( myIsByTwoPoints && ( thePointType != PointC ) ) );
407 TransformationPoint& aTransformationPoint = myTransformationPointMap[ thePointType ];
409 const QPointF& aPoint = aTransformationPoint.Point;
410 aTransformationPoint.GroupItem->setPos( aPoint );
411 aTransformationPoint.GroupItem->setVisible( anIsPointVisible );
414 //================================================================
415 // Function : getIsByTwoPoints
417 //================================================================
418 bool HYDROGUI_PrsImage::getIsByTwoPoints() const
420 return myIsByTwoPoints;
423 //================================================================
424 // Function : setIsByTwoPoints
426 //================================================================
427 void HYDROGUI_PrsImage::setIsByTwoPoints( const bool theIsByTwoPoints )
429 myIsByTwoPoints = theIsByTwoPoints;
430 if ( myTransformationPointMap.contains( PointC ) )
432 // Show/hide the point C if necessary
433 updateTrsfPoint( PointC );