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>
31 //=======================================================================
32 // name : HYDROGUI_PrsImage
33 // Purpose : Constructor
34 //=======================================================================
35 HYDROGUI_PrsImage::HYDROGUI_PrsImage( const Handle(HYDROData_Object)& theObject )
36 : HYDROGUI_Prs( theObject ),
39 myIsTransformationPointPreview( false ),
40 myTransformationPointMode( None )
42 myHighlightCursor = new QCursor( Qt::PointingHandCursor );
43 myTransformationPointCursor = new QCursor( Qt::CrossCursor );
46 //=======================================================================
47 // name : HYDROGUI_PrsImage
48 // Purpose : Destructor
49 //=======================================================================
50 HYDROGUI_PrsImage::~HYDROGUI_PrsImage()
52 if( myHighlightCursor )
54 delete myHighlightCursor;
55 myHighlightCursor = 0;
58 if( myTransformationPointCursor )
60 delete myTransformationPointCursor;
61 myTransformationPointCursor = 0;
65 //================================================================
66 // Function : setImage
68 //================================================================
69 void HYDROGUI_PrsImage::setImage( const QImage& theImage )
74 //================================================================
75 // Function : getImage
77 //================================================================
78 QImage HYDROGUI_PrsImage::getImage() const
83 //================================================================
84 // Function : setIsTransformationPointPreview
86 //================================================================
87 void HYDROGUI_PrsImage::setIsTransformationPointPreview( const bool theState )
89 myIsTransformationPointPreview = theState;
92 //================================================================
93 // Function : getIsTransformationPointPreview
95 //================================================================
96 bool HYDROGUI_PrsImage::getIsTransformationPointPreview() const
98 return myIsTransformationPointPreview;
101 //================================================================
102 // Function : setTransformationPointMode
104 //================================================================
105 void HYDROGUI_PrsImage::setTransformationPointMode( const int theMode )
107 myTransformationPointMode = theMode;
108 if( theMode != None )
109 computeTransformationPoints();
112 //================================================================
113 // Function : setTransformationPointMap
115 //================================================================
116 void HYDROGUI_PrsImage::setTransformationPointMap( const TransformationPointMap& theMap )
118 myTransformationPointMap = theMap;
119 if( !theMap.isEmpty() )
120 computeTransformationPoints();
123 //================================================================
124 // Function : boundingRect
126 //================================================================
127 QRectF HYDROGUI_PrsImage::boundingRect() const
129 return myPixmapItem->boundingRect();
132 //================================================================
133 // Function : compute
135 //================================================================
136 void HYDROGUI_PrsImage::compute()
140 myPixmapItem = new QGraphicsPixmapItem( this );
141 addToGroup( myPixmapItem );
143 if( !myPrsImageFrame )
145 myPrsImageFrame = new HYDROGUI_PrsImageFrame( this );
146 addToGroup( myPrsImageFrame );
148 myPixmapItem->setPixmap( QPixmap::fromImage( myImage ) );
149 myPrsImageFrame->compute();
152 //================================================================
155 //================================================================
156 void HYDROGUI_PrsImage::addTo( GraphicsView_ViewPort* theViewPort )
158 GraphicsView_Object::addTo( theViewPort );
159 theViewPort->addItem( myPrsImageFrame );
162 GraphicsView_ObjectListIterator anIter( theViewPort->getObjects() );
163 while( anIter.hasNext() )
165 if( HYDROGUI_PrsImage* aPrs = dynamic_cast<HYDROGUI_PrsImage*>( anIter.next() ) )
167 double aZValueRef = aPrs->zValue();
168 aZValue = qMax( aZValue, aZValueRef );
171 setZValue( aZValue + 1 );
174 //================================================================
175 // Function : removeFrom
177 //================================================================
178 void HYDROGUI_PrsImage::removeFrom( GraphicsView_ViewPort* theViewPort )
180 GraphicsView_Object::removeFrom( theViewPort );
181 theViewPort->removeItem( myPrsImageFrame );
184 //================================================================
185 // Function : checkHighlight
187 //================================================================
188 bool HYDROGUI_PrsImage::checkHighlight( double theX, double theY, QCursor& theCursor ) const
190 QRect aRect = myPixmapItem->boundingRect().toRect();
191 QPolygon aPolygon = sceneTransform().mapToPolygon( aRect );
192 if( aPolygon.containsPoint( QPoint( theX, theY ), Qt::OddEvenFill ) )
194 if( myIsTransformationPointPreview )
196 if( myTransformationPointMode != None )
197 theCursor = *getTransformationPointCursor();
200 theCursor = *getHighlightCursor();
206 //================================================================
209 //================================================================
210 bool HYDROGUI_PrsImage::select( double theX, double theY, const QRectF& theRect )
212 if( myIsTransformationPointPreview )
214 if( myTransformationPointMode == None || !theRect.isEmpty() )
217 TransformationPoint& aTransformationPoint = myTransformationPointMap[ myTransformationPointMode ];
218 aTransformationPoint.Point = QPoint( (int)theX, (int)theY );
219 computeTransformationPoints();
223 bool anIsSelected = GraphicsView_Object::select( theX, theY, theRect );
224 myPrsImageFrame->updateVisibility();
228 //================================================================
229 // Function : unselect
231 //================================================================
232 void HYDROGUI_PrsImage::unselect()
234 GraphicsView_Object::unselect();
235 myPrsImageFrame->updateVisibility();
238 //================================================================
239 // Function : setSelected
241 //================================================================
242 void HYDROGUI_PrsImage::setSelected( bool theState )
244 GraphicsView_Object::setSelected( theState );
245 myPrsImageFrame->updateVisibility();
248 //================================================================
249 // Function : computeTransformationPoints
251 //================================================================
252 void HYDROGUI_PrsImage::computeTransformationPoints()
254 if( myTransformationPointMap.isEmpty() )
256 int aWidth = myImage.width();
257 int aHeight = myImage.height();
259 for( int aPointType = PointA; aPointType <= PointC; aPointType++ )
261 TransformationPoint aTransformationPoint;
265 QColor aColor = Qt::black;
269 aPoint = QPoint( 0, 0 );
271 aColor = Qt::darkRed;
274 aPoint = QPoint( aWidth, 0 );
276 aColor = Qt::darkGreen;
279 aPoint = QPoint( 0, aHeight );
281 aColor = Qt::darkBlue;
285 aTransformationPoint.Point = aPoint;
286 aTransformationPoint.Caption = aCaption;
288 aTransformationPoint.PointItem = new QGraphicsEllipseItem( this );
289 aTransformationPoint.PointItem->setVisible( false );
290 aTransformationPoint.PointItem->setPen( QPen( aColor ) );
291 aTransformationPoint.PointItem->setBrush( QBrush( aColor ) );
293 aTransformationPoint.CaptionItem = new QGraphicsSimpleTextItem( aCaption, this );
294 aTransformationPoint.CaptionItem->setVisible( false );
295 aTransformationPoint.CaptionItem->setPen( QPen( aColor ) );
296 aTransformationPoint.CaptionItem->setBrush( QBrush( aColor ) );
298 myTransformationPointMap[ aPointType ] = aTransformationPoint;
302 bool anIsVisible = myIsTransformationPointPreview;
303 for( int aPointType = PointA; aPointType <= PointC; aPointType++ )
305 TransformationPoint& aTransformationPoint = myTransformationPointMap[ aPointType ];
308 const QPointF& aPoint = aTransformationPoint.Point;
309 QRectF aRect( aPoint - QPointF( aRadius, aRadius ), QSizeF( aRadius * 2 + 1, aRadius * 2 + 1 ) );
310 aTransformationPoint.PointItem->setRect( aRect );
311 aTransformationPoint.PointItem->setVisible( anIsVisible );
313 QPointF aCaptionShift( -aRadius * 2, aRadius * 2 );
314 aTransformationPoint.CaptionItem->setPos( aPoint + aCaptionShift );
315 aTransformationPoint.CaptionItem->setVisible( anIsVisible );