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>
28 //=======================================================================
29 // name : HYDROGUI_PrsImage
30 // Purpose : Constructor
31 //=======================================================================
32 HYDROGUI_PrsImage::HYDROGUI_PrsImage( const Handle(HYDROData_Entity)& theObject )
33 : HYDROGUI_Prs( theObject ),
37 myIsTransformationPointPreview( false ),
38 myIsByTwoPoints( true ),
39 myTransformationPointType( None )
41 myTransformationPointCursor = new QCursor( Qt::CrossCursor );
44 //=======================================================================
45 // name : HYDROGUI_PrsImage
46 // Purpose : Destructor
47 //=======================================================================
48 HYDROGUI_PrsImage::~HYDROGUI_PrsImage()
50 if( myTransformationPointCursor )
52 delete myTransformationPointCursor;
53 myTransformationPointCursor = 0;
57 //================================================================
58 // Function : setImage
60 //================================================================
61 void HYDROGUI_PrsImage::setImage( const QImage& theImage, const bool theCompute )
66 computeTransformationPoints( true );
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 : setTransformationPointCursorShape
163 //================================================================
164 void HYDROGUI_PrsImage::setTransformationPointCursorShape(Qt::CursorShape theCursorShape)
166 if ( myTransformationPointCursor )
167 myTransformationPointCursor->setShape(theCursorShape);
170 //================================================================
171 // Function : boundingRect
173 //================================================================
174 QRectF HYDROGUI_PrsImage::boundingRect() const
176 return myPixmapItem->boundingRect();
179 //================================================================
180 // Function : compute
182 //================================================================
183 void HYDROGUI_PrsImage::compute()
187 myPixmapItem = new QGraphicsPixmapItem( this );
188 addToGroup( myPixmapItem );
192 myCaptionItem = new QGraphicsSimpleTextItem( this );
194 QFont aFont = myCaptionItem->font();
195 aFont.setPointSize( 14 );
196 myCaptionItem->setFont( aFont );
198 addToGroup( myCaptionItem );
200 if( !myPrsImageFrame )
202 myPrsImageFrame = new HYDROGUI_PrsImageFrame( this );
203 addToGroup( myPrsImageFrame );
206 myPixmapItem->setPixmap( QPixmap::fromImage( myImage ) );
208 myCaptionItem->setPos( 0, -30 );
209 myCaptionItem->setVisible( false );
211 myPrsImageFrame->compute();
214 //================================================================
217 //================================================================
218 void HYDROGUI_PrsImage::addTo( GraphicsView_ViewPort* theViewPort )
220 HYDROGUI_Prs::addTo( theViewPort );
221 theViewPort->addItem( myPrsImageFrame );
224 //================================================================
225 // Function : removeFrom
227 //================================================================
228 void HYDROGUI_PrsImage::removeFrom( GraphicsView_ViewPort* theViewPort )
230 HYDROGUI_Prs::removeFrom( theViewPort );
231 theViewPort->removeItem( myPrsImageFrame );
234 //================================================================
235 // Function : checkHighlight
237 //================================================================
238 bool HYDROGUI_PrsImage::checkHighlight( double theX, double theY, QCursor& theCursor ) const
240 QRect aRect = myPixmapItem->boundingRect().toRect();
241 QPolygon aPolygon = sceneTransform().mapToPolygon( aRect );
242 if( aPolygon.containsPoint( QPoint( theX, theY ), Qt::OddEvenFill ) )
244 if( myIsTransformationPointPreview )
246 if( myTransformationPointType != None )
247 theCursor = *getTransformationPointCursor();
250 theCursor = *getHighlightCursor();
256 //================================================================
259 //================================================================
260 bool HYDROGUI_PrsImage::select( double theX, double theY, const QRectF& theRect )
262 if( myIsTransformationPointPreview )
264 if( myTransformationPointType == None || !theRect.isEmpty() )
267 QPoint aPos = pos().toPoint();
269 TransformationPoint& aTransformationPoint = myTransformationPointMap[ myTransformationPointType ];
270 aTransformationPoint.Point = QPoint( (int)theX, (int)theY ) - aPos;
271 computeTransformationPoints();
275 bool anIsSelected = HYDROGUI_Prs::select( theX, theY, theRect );
276 myPrsImageFrame->updateVisibility();
280 //================================================================
281 // Function : unselect
283 //================================================================
284 void HYDROGUI_PrsImage::unselect()
286 HYDROGUI_Prs::unselect();
287 if( !myIsTransformationPointPreview )
288 myPrsImageFrame->updateVisibility();
291 //================================================================
292 // Function : setSelected
294 //================================================================
295 void HYDROGUI_PrsImage::setSelected( bool theState )
297 HYDROGUI_Prs::setSelected( theState );
298 if( !myIsTransformationPointPreview )
299 myPrsImageFrame->updateVisibility();
302 //================================================================
303 // Function : computeTransformationPoints
305 //================================================================
306 void HYDROGUI_PrsImage::computeTransformationPoints( const bool theObligatoryInit )
309 for( int aPointType = PointA; aPointType <= PointC; aPointType++ )
311 if( myTransformationPointMap.isEmpty() || theObligatoryInit )
312 initTrsfPoints( aPointType );
313 // Show/hide the point if necessary
314 updateTrsfPoint( aPointType );
320 QGraphicsItemGroup* HYDROGUI_PrsImage::createPointItem( const QString& theCaption,
321 const QColor& theColor )
323 QGraphicsEllipseItem* aPointItem = new QGraphicsEllipseItem( this );
324 aPointItem->setPen( QPen( theColor ) );
325 aPointItem->setBrush( QBrush( theColor ) );
328 QRectF aRect( -QPointF( aRadius, aRadius ), QSizeF( aRadius * 2 + 1, aRadius * 2 + 1 ) );
329 aPointItem->setRect( aRect );
330 aPointItem->setPos( QPointF( 0, 0 ) );
332 QGraphicsSimpleTextItem* aCaptionItem = aCaptionItem = new QGraphicsSimpleTextItem( theCaption, this );
333 aCaptionItem->setPen( QPen( theColor ) );
334 aCaptionItem->setBrush( QBrush( theColor ) );
335 QFont aFont = aCaptionItem->font();
336 aFont.setPointSize( qApp->font().pointSize() );
337 aCaptionItem->setFont( aFont );
338 aCaptionItem->setPos( QPointF( -aRadius * 2, aRadius * 2 ) );
340 QGraphicsItemGroup* aGroupItem = new QGraphicsItemGroup( this );
341 aGroupItem->addToGroup( aPointItem );
342 aGroupItem->addToGroup( aCaptionItem );
343 aGroupItem->setVisible( false );
344 aGroupItem->setFlag( QGraphicsItem::ItemIgnoresTransformations );
349 //================================================================
350 // Function : initTrsfPoints
352 //================================================================
353 void HYDROGUI_PrsImage::initTrsfPoints( const int thePointType )
357 QColor aColor = Qt::black;
359 int aWidth = myImage.width();
360 int aHeight = myImage.height();
361 switch( thePointType )
364 aPoint = QPoint( 0, 0 );
366 aColor = Qt::darkRed;
369 aPoint = QPoint( aWidth, 0 );
371 aColor = Qt::darkGreen;
374 aPoint = QPoint( 0, aHeight );
376 aColor = Qt::darkBlue;
380 TransformationPoint aTransformationPoint;
381 if ( myTransformationPointMap.contains( thePointType ) )
382 aTransformationPoint = myTransformationPointMap[thePointType];
384 aTransformationPoint.Point = aPoint;
385 aTransformationPoint.Caption = aCaption;
386 if ( !aTransformationPoint.GroupItem )
387 aTransformationPoint.GroupItem = createPointItem( aCaption, aColor );
388 aTransformationPoint.GroupItem->setPos( aPoint );
390 myTransformationPointMap[ thePointType ] = aTransformationPoint;
393 //================================================================
394 // Function : updateTrsfPoint
396 //================================================================
397 void HYDROGUI_PrsImage::updateTrsfPoint( const int thePointType )
399 // If image is transformed only by two points then the point C is invisible
400 bool anIsPointVisible = myIsTransformationPointPreview && (
401 ( !myIsByTwoPoints ) || ( myIsByTwoPoints && ( thePointType != PointC ) ) );
403 TransformationPoint& aTransformationPoint = myTransformationPointMap[ thePointType ];
405 const QPointF& aPoint = aTransformationPoint.Point;
406 aTransformationPoint.GroupItem->setPos( aPoint );
407 aTransformationPoint.GroupItem->setVisible( anIsPointVisible );
410 //================================================================
411 // Function : getIsByTwoPoints
413 //================================================================
414 bool HYDROGUI_PrsImage::getIsByTwoPoints() const
416 return myIsByTwoPoints;
419 //================================================================
420 // Function : setIsByTwoPoints
422 //================================================================
423 void HYDROGUI_PrsImage::setIsByTwoPoints( const bool theIsByTwoPoints )
425 myIsByTwoPoints = theIsByTwoPoints;
426 if ( myTransformationPointMap.contains( PointC ) )
428 // Show/hide the point C if necessary
429 updateTrsfPoint( PointC );