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 myTransformationPointCursor = new QCursor( Qt::CrossCursor );
45 //=======================================================================
46 // name : HYDROGUI_PrsImage
47 // Purpose : Destructor
48 //=======================================================================
49 HYDROGUI_PrsImage::~HYDROGUI_PrsImage()
51 if( myTransformationPointCursor )
53 delete myTransformationPointCursor;
54 myTransformationPointCursor = 0;
58 //================================================================
59 // Function : setImage
61 //================================================================
62 void HYDROGUI_PrsImage::setImage( const QImage& theImage )
67 //================================================================
68 // Function : getImage
70 //================================================================
71 QImage HYDROGUI_PrsImage::getImage() const
76 //================================================================
77 // Function : setIsTransformationPointPreview
79 //================================================================
80 void HYDROGUI_PrsImage::setIsTransformationPointPreview( const bool theState )
82 myIsTransformationPointPreview = theState;
85 //================================================================
86 // Function : getIsTransformationPointPreview
88 //================================================================
89 bool HYDROGUI_PrsImage::getIsTransformationPointPreview() const
91 return myIsTransformationPointPreview;
94 //================================================================
95 // Function : setTransformationPointMode
97 //================================================================
98 void HYDROGUI_PrsImage::setTransformationPointMode( const int theMode )
100 myTransformationPointMode = theMode;
101 if( theMode != None )
102 computeTransformationPoints();
105 //================================================================
106 // Function : setTransformationPointMap
108 //================================================================
109 void HYDROGUI_PrsImage::setTransformationPointMap( const TransformationPointMap& theMap )
111 myTransformationPointMap = theMap;
112 if( !theMap.isEmpty() )
113 computeTransformationPoints();
116 //================================================================
117 // Function : boundingRect
119 //================================================================
120 QRectF HYDROGUI_PrsImage::boundingRect() const
122 return myPixmapItem->boundingRect();
125 //================================================================
126 // Function : compute
128 //================================================================
129 void HYDROGUI_PrsImage::compute()
133 myPixmapItem = new QGraphicsPixmapItem( this );
134 addToGroup( myPixmapItem );
136 if( !myPrsImageFrame )
138 myPrsImageFrame = new HYDROGUI_PrsImageFrame( this );
139 addToGroup( myPrsImageFrame );
141 myPixmapItem->setPixmap( QPixmap::fromImage( myImage ) );
142 myPrsImageFrame->compute();
145 //================================================================
148 //================================================================
149 void HYDROGUI_PrsImage::addTo( GraphicsView_ViewPort* theViewPort )
151 HYDROGUI_Prs::addTo( theViewPort );
152 theViewPort->addItem( myPrsImageFrame );
155 //================================================================
156 // Function : removeFrom
158 //================================================================
159 void HYDROGUI_PrsImage::removeFrom( GraphicsView_ViewPort* theViewPort )
161 HYDROGUI_Prs::removeFrom( theViewPort );
162 theViewPort->removeItem( myPrsImageFrame );
165 //================================================================
166 // Function : checkHighlight
168 //================================================================
169 bool HYDROGUI_PrsImage::checkHighlight( double theX, double theY, QCursor& theCursor ) const
171 QRect aRect = myPixmapItem->boundingRect().toRect();
172 QPolygon aPolygon = sceneTransform().mapToPolygon( aRect );
173 if( aPolygon.containsPoint( QPoint( theX, theY ), Qt::OddEvenFill ) )
175 if( myIsTransformationPointPreview )
177 if( myTransformationPointMode != None )
178 theCursor = *getTransformationPointCursor();
181 theCursor = *getHighlightCursor();
187 //================================================================
190 //================================================================
191 bool HYDROGUI_PrsImage::select( double theX, double theY, const QRectF& theRect )
193 if( myIsTransformationPointPreview )
195 if( myTransformationPointMode == None || !theRect.isEmpty() )
198 TransformationPoint& aTransformationPoint = myTransformationPointMap[ myTransformationPointMode ];
199 aTransformationPoint.Point = QPoint( (int)theX, (int)theY );
200 computeTransformationPoints();
204 bool anIsSelected = HYDROGUI_Prs::select( theX, theY, theRect );
205 myPrsImageFrame->updateVisibility();
209 //================================================================
210 // Function : unselect
212 //================================================================
213 void HYDROGUI_PrsImage::unselect()
215 HYDROGUI_Prs::unselect();
216 if( !myIsTransformationPointPreview )
217 myPrsImageFrame->updateVisibility();
220 //================================================================
221 // Function : setSelected
223 //================================================================
224 void HYDROGUI_PrsImage::setSelected( bool theState )
226 HYDROGUI_Prs::setSelected( theState );
227 if( !myIsTransformationPointPreview )
228 myPrsImageFrame->updateVisibility();
231 //================================================================
232 // Function : computeTransformationPoints
234 //================================================================
235 void HYDROGUI_PrsImage::computeTransformationPoints()
237 if( myTransformationPointMap.isEmpty() )
239 int aWidth = myImage.width();
240 int aHeight = myImage.height();
242 for( int aPointType = PointA; aPointType <= PointC; aPointType++ )
244 TransformationPoint aTransformationPoint;
248 QColor aColor = Qt::black;
252 aPoint = QPoint( 0, 0 );
254 aColor = Qt::darkRed;
257 aPoint = QPoint( aWidth, 0 );
259 aColor = Qt::darkGreen;
262 aPoint = QPoint( 0, aHeight );
264 aColor = Qt::darkBlue;
268 aTransformationPoint.Point = aPoint;
269 aTransformationPoint.Caption = aCaption;
271 aTransformationPoint.PointItem = new QGraphicsEllipseItem( this );
272 aTransformationPoint.PointItem->setVisible( false );
273 aTransformationPoint.PointItem->setPen( QPen( aColor ) );
274 aTransformationPoint.PointItem->setBrush( QBrush( aColor ) );
276 aTransformationPoint.CaptionItem = new QGraphicsSimpleTextItem( aCaption, this );
277 aTransformationPoint.CaptionItem->setVisible( false );
278 aTransformationPoint.CaptionItem->setPen( QPen( aColor ) );
279 aTransformationPoint.CaptionItem->setBrush( QBrush( aColor ) );
281 myTransformationPointMap[ aPointType ] = aTransformationPoint;
285 bool anIsVisible = myIsTransformationPointPreview;
286 for( int aPointType = PointA; aPointType <= PointC; aPointType++ )
288 TransformationPoint& aTransformationPoint = myTransformationPointMap[ aPointType ];
291 const QPointF& aPoint = aTransformationPoint.Point;
292 QRectF aRect( aPoint - QPointF( aRadius, aRadius ), QSizeF( aRadius * 2 + 1, aRadius * 2 + 1 ) );
293 aTransformationPoint.PointItem->setRect( aRect );
294 aTransformationPoint.PointItem->setVisible( anIsVisible );
296 QPointF aCaptionShift( -aRadius * 2, aRadius * 2 );
297 aTransformationPoint.CaptionItem->setPos( aPoint + aCaptionShift );
298 aTransformationPoint.CaptionItem->setVisible( anIsVisible );