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 : boundingRect
167 //================================================================
168 QRectF HYDROGUI_PrsImage::boundingRect() const
170 return myPixmapItem->boundingRect();
173 //================================================================
174 // Function : compute
176 //================================================================
177 void HYDROGUI_PrsImage::compute()
181 myPixmapItem = new QGraphicsPixmapItem( this );
182 addToGroup( myPixmapItem );
186 myCaptionItem = new QGraphicsSimpleTextItem( this );
188 QFont aFont = myCaptionItem->font();
189 aFont.setPointSize( 14 );
190 myCaptionItem->setFont( aFont );
192 addToGroup( myCaptionItem );
194 if( !myPrsImageFrame )
196 myPrsImageFrame = new HYDROGUI_PrsImageFrame( this );
197 addToGroup( myPrsImageFrame );
200 myPixmapItem->setPixmap( QPixmap::fromImage( myImage ) );
202 myCaptionItem->setPos( 0, -30 );
203 myCaptionItem->setVisible( false );
205 myPrsImageFrame->compute();
208 //================================================================
211 //================================================================
212 void HYDROGUI_PrsImage::addTo( GraphicsView_ViewPort* theViewPort )
214 HYDROGUI_Prs::addTo( theViewPort );
215 theViewPort->addItem( myPrsImageFrame );
218 //================================================================
219 // Function : removeFrom
221 //================================================================
222 void HYDROGUI_PrsImage::removeFrom( GraphicsView_ViewPort* theViewPort )
224 HYDROGUI_Prs::removeFrom( theViewPort );
225 theViewPort->removeItem( myPrsImageFrame );
228 //================================================================
229 // Function : checkHighlight
231 //================================================================
232 bool HYDROGUI_PrsImage::checkHighlight( double theX, double theY, QCursor& theCursor ) const
234 QRect aRect = myPixmapItem->boundingRect().toRect();
235 QPolygon aPolygon = sceneTransform().mapToPolygon( aRect );
236 if( aPolygon.containsPoint( QPoint( theX, theY ), Qt::OddEvenFill ) )
238 if( myIsTransformationPointPreview )
240 if( myTransformationPointType != None )
241 theCursor = *getTransformationPointCursor();
244 theCursor = *getHighlightCursor();
250 //================================================================
253 //================================================================
254 bool HYDROGUI_PrsImage::select( double theX, double theY, const QRectF& theRect )
256 if( myIsTransformationPointPreview )
258 if( myTransformationPointType == None || !theRect.isEmpty() )
261 QPoint aPos = pos().toPoint();
263 TransformationPoint& aTransformationPoint = myTransformationPointMap[ myTransformationPointType ];
264 aTransformationPoint.Point = QPoint( (int)theX, (int)theY ) - aPos;
265 computeTransformationPoints();
269 bool anIsSelected = HYDROGUI_Prs::select( theX, theY, theRect );
270 myPrsImageFrame->updateVisibility();
274 //================================================================
275 // Function : unselect
277 //================================================================
278 void HYDROGUI_PrsImage::unselect()
280 HYDROGUI_Prs::unselect();
281 if( !myIsTransformationPointPreview )
282 myPrsImageFrame->updateVisibility();
285 //================================================================
286 // Function : setSelected
288 //================================================================
289 void HYDROGUI_PrsImage::setSelected( bool theState )
291 HYDROGUI_Prs::setSelected( theState );
292 if( !myIsTransformationPointPreview )
293 myPrsImageFrame->updateVisibility();
296 //================================================================
297 // Function : computeTransformationPoints
299 //================================================================
300 void HYDROGUI_PrsImage::computeTransformationPoints( const bool theObligatoryInit )
303 for( int aPointType = PointA; aPointType <= PointC; aPointType++ )
305 if( myTransformationPointMap.isEmpty() || theObligatoryInit )
306 initTrsfPoints( aPointType );
307 // Show/hide the point if necessary
308 updateTrsfPoint( aPointType );
314 QGraphicsItemGroup* HYDROGUI_PrsImage::createPointItem( const QString& theCaption,
315 const QColor& theColor )
317 QGraphicsEllipseItem* aPointItem = new QGraphicsEllipseItem( this );
318 aPointItem->setPen( QPen( theColor ) );
319 aPointItem->setBrush( QBrush( theColor ) );
322 QRectF aRect( -QPointF( aRadius, aRadius ), QSizeF( aRadius * 2 + 1, aRadius * 2 + 1 ) );
323 aPointItem->setRect( aRect );
324 aPointItem->setPos( QPointF( 0, 0 ) );
326 QGraphicsSimpleTextItem* aCaptionItem = aCaptionItem = new QGraphicsSimpleTextItem( theCaption, this );
327 aCaptionItem->setPen( QPen( theColor ) );
328 aCaptionItem->setBrush( QBrush( theColor ) );
329 QFont aFont = aCaptionItem->font();
330 aFont.setPointSize( qApp->font().pointSize() );
331 aCaptionItem->setFont( aFont );
332 aCaptionItem->setPos( QPointF( -aRadius * 2, aRadius * 2 ) );
334 QGraphicsItemGroup* aGroupItem = new QGraphicsItemGroup( this );
335 aGroupItem->addToGroup( aPointItem );
336 aGroupItem->addToGroup( aCaptionItem );
337 aGroupItem->setVisible( false );
338 aGroupItem->setFlag( QGraphicsItem::ItemIgnoresTransformations );
343 //================================================================
344 // Function : initTrsfPoints
346 //================================================================
347 void HYDROGUI_PrsImage::initTrsfPoints( const int thePointType )
351 QColor aColor = Qt::black;
353 int aWidth = myImage.width();
354 int aHeight = myImage.height();
355 switch( thePointType )
358 aPoint = QPoint( 0, 0 );
360 aColor = Qt::darkRed;
363 aPoint = QPoint( aWidth, 0 );
365 aColor = Qt::darkGreen;
368 aPoint = QPoint( 0, aHeight );
370 aColor = Qt::darkBlue;
374 TransformationPoint aTransformationPoint;
375 if ( myTransformationPointMap.contains( thePointType ) )
376 aTransformationPoint = myTransformationPointMap[thePointType];
378 aTransformationPoint.Point = aPoint;
379 aTransformationPoint.Caption = aCaption;
380 if ( !aTransformationPoint.GroupItem )
381 aTransformationPoint.GroupItem = createPointItem( aCaption, aColor );
382 aTransformationPoint.GroupItem->setPos( aPoint );
384 myTransformationPointMap[ thePointType ] = aTransformationPoint;
387 //================================================================
388 // Function : updateTrsfPoint
390 //================================================================
391 void HYDROGUI_PrsImage::updateTrsfPoint( const int thePointType )
393 // If image is transformed only by two points then the point C is invisible
394 bool anIsPointVisible = myIsTransformationPointPreview && (
395 ( !myIsByTwoPoints ) || ( myIsByTwoPoints && ( thePointType != PointC ) ) );
397 TransformationPoint& aTransformationPoint = myTransformationPointMap[ thePointType ];
399 const QPointF& aPoint = aTransformationPoint.Point;
400 aTransformationPoint.GroupItem->setPos( aPoint );
401 aTransformationPoint.GroupItem->setVisible( anIsPointVisible );
404 //================================================================
405 // Function : getIsByTwoPoints
407 //================================================================
408 bool HYDROGUI_PrsImage::getIsByTwoPoints() const
410 return myIsByTwoPoints;
413 //================================================================
414 // Function : setIsByTwoPoints
416 //================================================================
417 void HYDROGUI_PrsImage::setIsByTwoPoints( const bool theIsByTwoPoints )
419 myIsByTwoPoints = theIsByTwoPoints;
420 if ( myTransformationPointMap.contains( PointC ) )
422 // Show/hide the point C if necessary
423 updateTrsfPoint( PointC );