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 : boundingRect
115 //================================================================
116 QRectF HYDROGUI_PrsImage::boundingRect() const
118 return myPixmapItem->boundingRect();
121 //================================================================
122 // Function : compute
124 //================================================================
125 void HYDROGUI_PrsImage::compute()
129 myPixmapItem = new QGraphicsPixmapItem( this );
130 addToGroup( myPixmapItem );
132 if( !myPrsImageFrame )
134 myPrsImageFrame = new HYDROGUI_PrsImageFrame( this );
135 addToGroup( myPrsImageFrame );
137 myPixmapItem->setPixmap( QPixmap::fromImage( myImage ) );
138 myPrsImageFrame->compute();
141 //================================================================
144 //================================================================
145 void HYDROGUI_PrsImage::addTo( GraphicsView_ViewPort* theViewPort )
147 GraphicsView_Object::addTo( theViewPort );
148 theViewPort->addItem( myPrsImageFrame );
151 GraphicsView_ObjectListIterator anIter( theViewPort->getObjects() );
152 while( anIter.hasNext() )
154 if( HYDROGUI_PrsImage* aPrs = dynamic_cast<HYDROGUI_PrsImage*>( anIter.next() ) )
156 double aZValueRef = aPrs->zValue();
157 aZValue = qMax( aZValue, aZValueRef );
160 setZValue( aZValue + 1 );
163 //================================================================
164 // Function : removeFrom
166 //================================================================
167 void HYDROGUI_PrsImage::removeFrom( GraphicsView_ViewPort* theViewPort )
169 GraphicsView_Object::removeFrom( theViewPort );
170 theViewPort->removeItem( myPrsImageFrame );
173 //================================================================
174 // Function : checkHighlight
176 //================================================================
177 bool HYDROGUI_PrsImage::checkHighlight( double theX, double theY, QCursor& theCursor ) const
179 QRect aRect = myPixmapItem->boundingRect().toRect();
180 QPolygon aPolygon = sceneTransform().mapToPolygon( aRect );
181 if( aPolygon.containsPoint( QPoint( theX, theY ), Qt::OddEvenFill ) )
183 if( myIsTransformationPointPreview )
185 if( myTransformationPointMode != None )
186 theCursor = *getTransformationPointCursor();
189 theCursor = *getHighlightCursor();
195 //================================================================
198 //================================================================
199 bool HYDROGUI_PrsImage::select( double theX, double theY, const QRectF& theRect )
201 if( myIsTransformationPointPreview )
203 if( myTransformationPointMode == None || !theRect.isEmpty() )
206 TransformationPoint& aTransformationPoint = myTransformationPointMap[ myTransformationPointMode ];
207 aTransformationPoint.Point = QPoint( (int)theX, (int)theY );
208 computeTransformationPoints();
212 bool anIsSelected = GraphicsView_Object::select( theX, theY, theRect );
213 myPrsImageFrame->updateVisibility();
217 //================================================================
218 // Function : unselect
220 //================================================================
221 void HYDROGUI_PrsImage::unselect()
223 GraphicsView_Object::unselect();
224 myPrsImageFrame->updateVisibility();
227 //================================================================
228 // Function : setSelected
230 //================================================================
231 void HYDROGUI_PrsImage::setSelected( bool theState )
233 GraphicsView_Object::setSelected( theState );
234 myPrsImageFrame->updateVisibility();
237 //================================================================
238 // Function : computeTransformationPoints
240 //================================================================
241 void HYDROGUI_PrsImage::computeTransformationPoints()
243 if( myTransformationPointMap.isEmpty() )
245 int aWidth = myImage.width();
246 int aHeight = myImage.height();
248 for( int aPointType = PointA; aPointType <= PointC; aPointType++ )
250 TransformationPoint aTransformationPoint;
254 QColor aColor = Qt::black;
258 aPoint = QPoint( 0, 0 );
260 aColor = Qt::darkRed;
263 aPoint = QPoint( aWidth, 0 );
265 aColor = Qt::darkGreen;
268 aPoint = QPoint( 0, aHeight );
270 aColor = Qt::darkBlue;
274 aTransformationPoint.Point = aPoint;
275 aTransformationPoint.Caption = aCaption;
277 aTransformationPoint.PointItem = new QGraphicsEllipseItem( this );
278 aTransformationPoint.PointItem->setVisible( false );
279 aTransformationPoint.PointItem->setPen( QPen( aColor ) );
280 aTransformationPoint.PointItem->setBrush( QBrush( aColor ) );
282 aTransformationPoint.CaptionItem = new QGraphicsSimpleTextItem( aCaption, this );
283 aTransformationPoint.CaptionItem->setVisible( false );
284 aTransformationPoint.CaptionItem->setPen( QPen( aColor ) );
285 aTransformationPoint.CaptionItem->setBrush( QBrush( aColor ) );
287 myTransformationPointMap[ aPointType ] = aTransformationPoint;
291 bool anIsVisible = myIsTransformationPointPreview;
292 for( int aPointType = PointA; aPointType <= PointC; aPointType++ )
294 TransformationPoint& aTransformationPoint = myTransformationPointMap[ aPointType ];
297 const QPointF& aPoint = aTransformationPoint.Point;
298 QRectF aRect( aPoint - QPointF( aRadius, aRadius ), QSizeF( aRadius * 2 + 1, aRadius * 2 + 1 ) );
299 aTransformationPoint.PointItem->setRect( aRect );
300 aTransformationPoint.PointItem->setVisible( anIsVisible );
302 QPointF aCaptionShift( -aRadius * 2, aRadius * 2 );
303 aTransformationPoint.CaptionItem->setPos( aPoint + aCaptionShift );
304 aTransformationPoint.CaptionItem->setVisible( anIsVisible );