1 // Copyright (C) 2014-2015 EDF-R&D
2 // This library is free software; you can redistribute it and/or
3 // modify it under the terms of the GNU Lesser General Public
4 // License as published by the Free Software Foundation; either
5 // version 2.1 of the License, or (at your option) any later version.
7 // This library is distributed in the hope that it will be useful,
8 // but WITHOUT ANY WARRANTY; without even the implied warranty of
9 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
10 // Lesser General Public License for more details.
12 // You should have received a copy of the GNU Lesser General Public
13 // License along with this library; if not, write to the Free Software
14 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
19 #include "HYDROGUI_PrsImage.h"
21 #include "HYDROGUI_PrsImageFrame.h"
23 #include <GraphicsView_ViewPort.h>
26 #include <QApplication>
29 #include "HYDRO_trace.hxx"
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 myIsByTwoPoints( true ),
42 myTransformationPointType( None )
44 DEBTRACE("HYDROGUI_PrsImage");
45 myTransformationPointCursor = new QCursor( Qt::CrossCursor );
48 //=======================================================================
49 // name : HYDROGUI_PrsImage
50 // Purpose : Destructor
51 //=======================================================================
52 HYDROGUI_PrsImage::~HYDROGUI_PrsImage()
54 DEBTRACE("~HYDROGUI_PrsImage");
55 if( myTransformationPointCursor )
57 delete myTransformationPointCursor;
58 myTransformationPointCursor = 0;
62 //================================================================
63 // Function : setImage
65 //================================================================
66 void HYDROGUI_PrsImage::setImage( const QImage& theImage, const bool theCompute )
71 computeTransformationPoints( true );
75 //================================================================
76 // Function : getImage
78 //================================================================
79 QImage HYDROGUI_PrsImage::getImage() const
84 //================================================================
85 // Function : setCaption
87 //================================================================
88 void HYDROGUI_PrsImage::setCaption( const QString& theCaption )
92 myCaptionItem->setText( theCaption );
93 myCaptionItem->setVisible( !theCaption.isEmpty() );
97 //================================================================
98 // Function : getCaption
100 //================================================================
101 QString HYDROGUI_PrsImage::getCaption() const
104 return myCaptionItem->text();
108 //================================================================
109 // Function : setIsTransformationPointPreview
111 //================================================================
112 void HYDROGUI_PrsImage::setIsTransformationPointPreview( const bool theState )
114 myIsTransformationPointPreview = theState;
117 //================================================================
118 // Function : getIsTransformationPointPreview
120 //================================================================
121 bool HYDROGUI_PrsImage::getIsTransformationPointPreview() const
123 return myIsTransformationPointPreview;
126 //================================================================
127 // Function : setTransformationPointType
129 //================================================================
130 void HYDROGUI_PrsImage::setTransformationPointType( const int thePointType )
132 myTransformationPointType = thePointType;
133 if( thePointType != None )
134 computeTransformationPoints();
137 //================================================================
138 // Function : setTransformationPointMap
140 //================================================================
141 void HYDROGUI_PrsImage::setTransformationPointMap( const TransformationPointMap& theMap )
143 myTransformationPointMap = theMap;
144 if( !theMap.isEmpty() )
145 computeTransformationPoints();
148 //================================================================
149 // Function : updateTransformationPoint
151 //================================================================
152 void HYDROGUI_PrsImage::updateTransformationPoint( const int thePointType,
156 DEBTRACE("updateTransformationPoint " << thePointType << " theIsY " << theIsY << " theValue " << theValue);
157 if( myTransformationPointMap.find( thePointType ) != myTransformationPointMap.end() )
159 TransformationPoint& aTransformationPoint = myTransformationPointMap[ thePointType ];
160 QPoint& aPoint = aTransformationPoint.Point;
161 theIsY ? aPoint.setY( theValue ) : aPoint.setX( theValue );
162 computeTransformationPoints();
166 //================================================================
167 // Function : setTransformationPointCursorShape
169 //================================================================
170 void HYDROGUI_PrsImage::setTransformationPointCursorShape(Qt::CursorShape theCursorShape)
172 if ( myTransformationPointCursor )
173 myTransformationPointCursor->setShape(theCursorShape);
176 //================================================================
177 // Function : boundingRect
179 //================================================================
180 QRectF HYDROGUI_PrsImage::boundingRect() const
182 return myPixmapItem->boundingRect();
185 //================================================================
186 // Function : compute
188 //================================================================
189 void HYDROGUI_PrsImage::compute()
194 myPixmapItem = new QGraphicsPixmapItem( this );
195 addToGroup( myPixmapItem );
199 myCaptionItem = new QGraphicsSimpleTextItem( this );
201 QFont aFont = myCaptionItem->font();
202 aFont.setPointSize( 14 );
203 myCaptionItem->setFont( aFont );
205 addToGroup( myCaptionItem );
207 if( !myPrsImageFrame )
209 myPrsImageFrame = new HYDROGUI_PrsImageFrame( this );
210 addToGroup( myPrsImageFrame );
213 myPixmapItem->setPixmap( QPixmap::fromImage( myImage ) );
215 myCaptionItem->setPos( 0, -30 );
216 myCaptionItem->setVisible( false );
218 // QTransform transform;
219 // transform.scale(1.0, -1.0);
220 // this->setTransform(transform, false);
221 // //this->setViewTransform(transform);
223 myPrsImageFrame->compute();
226 //================================================================
229 //================================================================
230 void HYDROGUI_PrsImage::addTo( GraphicsView_ViewPort* theViewPort )
232 HYDROGUI_Prs::addTo( theViewPort );
233 theViewPort->addItem( myPrsImageFrame );
236 //================================================================
237 // Function : removeFrom
239 //================================================================
240 void HYDROGUI_PrsImage::removeFrom( GraphicsView_ViewPort* theViewPort )
242 HYDROGUI_Prs::removeFrom( theViewPort );
243 theViewPort->removeItem( myPrsImageFrame );
246 //================================================================
247 // Function : checkHighlight
249 //================================================================
250 bool HYDROGUI_PrsImage::checkHighlight( double theX, double theY, QCursor& theCursor ) const
252 QRect aRect = myPixmapItem->boundingRect().toRect();
253 QPolygon aPolygon = sceneTransform().mapToPolygon( aRect );
254 if( aPolygon.containsPoint( QPoint( theX, theY ), Qt::OddEvenFill ) )
256 if( myIsTransformationPointPreview )
258 if( myTransformationPointType != None )
259 theCursor = *getTransformationPointCursor();
262 theCursor = *getHighlightCursor();
268 //================================================================
271 //================================================================
272 bool HYDROGUI_PrsImage::select( double theX, double theY, const QRectF& theRect )
274 if( myIsTransformationPointPreview )
276 if( myTransformationPointType == None || !theRect.isEmpty() )
279 QPoint aPos = pos().toPoint();
281 TransformationPoint& aTransformationPoint = myTransformationPointMap[ myTransformationPointType ];
282 aTransformationPoint.Point = QPoint( (int)theX, (int)theY ) - aPos;
283 computeTransformationPoints();
287 bool anIsSelected = HYDROGUI_Prs::select( theX, theY, theRect );
288 myPrsImageFrame->updateVisibility();
292 //================================================================
293 // Function : unselect
295 //================================================================
296 void HYDROGUI_PrsImage::unselect()
298 HYDROGUI_Prs::unselect();
299 if( !myIsTransformationPointPreview )
300 myPrsImageFrame->updateVisibility();
303 //================================================================
304 // Function : setSelected
306 //================================================================
307 void HYDROGUI_PrsImage::setSelected( bool theState )
309 HYDROGUI_Prs::setSelected( theState );
310 if( !myIsTransformationPointPreview )
311 myPrsImageFrame->updateVisibility();
314 //================================================================
315 // Function : computeTransformationPoints
317 //================================================================
318 void HYDROGUI_PrsImage::computeTransformationPoints( const bool theObligatoryInit )
321 for( int aPointType = PointA; aPointType <= PointC; aPointType++ )
323 if( myTransformationPointMap.isEmpty() || theObligatoryInit )
324 initTrsfPoints( aPointType );
325 // Show/hide the point if necessary
326 updateTrsfPoint( aPointType );
332 QGraphicsItemGroup* HYDROGUI_PrsImage::createPointItem( const QString& theCaption,
333 const QColor& theColor )
335 QGraphicsEllipseItem* aPointItem = new QGraphicsEllipseItem( this );
336 aPointItem->setPen( QPen( theColor ) );
337 aPointItem->setBrush( QBrush( theColor ) );
340 QRectF aRect( -QPointF( aRadius, aRadius ), QSizeF( aRadius * 2 + 1, aRadius * 2 + 1 ) );
341 aPointItem->setRect( aRect );
342 aPointItem->setPos( QPointF( 0, 0 ) );
344 QGraphicsSimpleTextItem* aCaptionItem = aCaptionItem = new QGraphicsSimpleTextItem( theCaption, this );
345 aCaptionItem->setPen( QPen( theColor ) );
346 aCaptionItem->setBrush( QBrush( theColor ) );
347 QFont aFont = aCaptionItem->font();
348 aFont.setPointSize( qApp->font().pointSize() );
349 aCaptionItem->setFont( aFont );
350 aCaptionItem->setPos( QPointF( -aRadius * 2, aRadius * 2 ) );
352 QGraphicsItemGroup* aGroupItem = new QGraphicsItemGroup( this );
353 aGroupItem->addToGroup( aPointItem );
354 aGroupItem->addToGroup( aCaptionItem );
355 aGroupItem->setVisible( false );
356 // QTransform transform;
357 // transform.scale(1.0, -1.0); // retourne le point et sa lettre
358 // aGroupItem->setTransform(transform, true);
359 aGroupItem->setFlag( QGraphicsItem::ItemIgnoresTransformations );
364 //================================================================
365 // Function : initTrsfPoints
367 //================================================================
368 void HYDROGUI_PrsImage::initTrsfPoints( const int thePointType )
372 QColor aColor = Qt::black;
374 int aWidth = myImage.width();
375 int aHeight = myImage.height();
376 switch( thePointType )
379 aPoint = QPoint( 0, 0 );
381 aColor = Qt::darkRed;
384 aPoint = QPoint( aWidth, 0 );
386 aColor = Qt::darkGreen;
389 aPoint = QPoint( 0, aHeight );
391 aColor = Qt::darkBlue;
395 TransformationPoint aTransformationPoint;
396 if ( myTransformationPointMap.contains( thePointType ) )
397 aTransformationPoint = myTransformationPointMap[thePointType];
399 aTransformationPoint.Point = aPoint;
400 aTransformationPoint.Caption = aCaption;
401 if ( !aTransformationPoint.GroupItem )
402 aTransformationPoint.GroupItem = createPointItem( aCaption, aColor );
403 aTransformationPoint.GroupItem->setPos( aPoint );
405 myTransformationPointMap[ thePointType ] = aTransformationPoint;
408 //================================================================
409 // Function : updateTrsfPoint
411 //================================================================
412 void HYDROGUI_PrsImage::updateTrsfPoint( const int thePointType )
414 // If image is transformed only by two points then the point C is invisible
415 bool anIsPointVisible = myIsTransformationPointPreview && (
416 ( !myIsByTwoPoints ) || ( myIsByTwoPoints && ( thePointType != PointC ) ) );
418 TransformationPoint& aTransformationPoint = myTransformationPointMap[ thePointType ];
420 const QPointF& aPoint = aTransformationPoint.Point;
421 aTransformationPoint.GroupItem->setPos( aPoint );
422 aTransformationPoint.GroupItem->setVisible( anIsPointVisible );
425 //================================================================
426 // Function : getIsByTwoPoints
428 //================================================================
429 bool HYDROGUI_PrsImage::getIsByTwoPoints() const
431 return myIsByTwoPoints;
434 //================================================================
435 // Function : setIsByTwoPoints
437 //================================================================
438 void HYDROGUI_PrsImage::setIsByTwoPoints( const bool theIsByTwoPoints )
440 myIsByTwoPoints = theIsByTwoPoints;
441 if ( myTransformationPointMap.contains( PointC ) )
443 // Show/hide the point C if necessary
444 updateTrsfPoint( PointC );