]> SALOME platform Git repositories - modules/hydro.git/blob - src/HYDROGUI/HYDROGUI_PrsImage.cxx
Salome HOME
fa75c25eb15109310cd352268622361bad9d132e
[modules/hydro.git] / src / HYDROGUI / HYDROGUI_PrsImage.cxx
1 // Copyright (C) 2007-2013  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
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.
10 //
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.
15 //
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
19 //
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22
23 #include "HYDROGUI_PrsImage.h"
24
25 #include "HYDROGUI_PrsImageFrame.h"
26
27 #include <GraphicsView_ViewPort.h>
28
29 #include <QCursor>
30
31 //=======================================================================
32 // name    : HYDROGUI_PrsImage
33 // Purpose : Constructor
34 //=======================================================================
35 HYDROGUI_PrsImage::HYDROGUI_PrsImage( const Handle(HYDROData_Object)& theObject )
36 : HYDROGUI_Prs( theObject ),
37   myPixmapItem( 0 ),
38   myPrsImageFrame( 0 ),
39   myIsTransformationPointPreview( false ),
40   myTransformationPointMode( None )
41 {
42   myHighlightCursor = new QCursor( Qt::PointingHandCursor );
43   myTransformationPointCursor = new QCursor( Qt::CrossCursor );
44 }
45
46 //=======================================================================
47 // name    : HYDROGUI_PrsImage
48 // Purpose : Destructor
49 //=======================================================================
50 HYDROGUI_PrsImage::~HYDROGUI_PrsImage()
51 {
52   if( myHighlightCursor )
53   {
54     delete myHighlightCursor;
55     myHighlightCursor = 0;
56   }
57
58   if( myTransformationPointCursor )
59   {
60     delete myTransformationPointCursor;
61     myTransformationPointCursor = 0;
62   }
63 }
64
65 //================================================================
66 // Function : setImage
67 // Purpose  : 
68 //================================================================
69 void HYDROGUI_PrsImage::setImage( const QImage& theImage )
70 {
71   myImage = theImage;
72 }
73
74 //================================================================
75 // Function : getImage
76 // Purpose  : 
77 //================================================================
78 QImage HYDROGUI_PrsImage::getImage() const
79 {
80   return myImage;
81 }
82
83 //================================================================
84 // Function : setIsTransformationPointPreview
85 // Purpose  : 
86 //================================================================
87 void HYDROGUI_PrsImage::setIsTransformationPointPreview( const bool theState )
88 {
89   myIsTransformationPointPreview = theState;
90 }
91
92 //================================================================
93 // Function : getIsTransformationPointPreview
94 // Purpose  : 
95 //================================================================
96 bool HYDROGUI_PrsImage::getIsTransformationPointPreview() const
97 {
98   return myIsTransformationPointPreview;
99 }
100
101 //================================================================
102 // Function : setTransformationPointMode
103 // Purpose  : 
104 //================================================================
105 void HYDROGUI_PrsImage::setTransformationPointMode( const int theMode )
106 {
107   myTransformationPointMode = theMode;
108   if( theMode != None )
109     computeTransformationPoints();
110 }
111
112 //================================================================
113 // Function : boundingRect
114 // Purpose  : 
115 //================================================================
116 QRectF HYDROGUI_PrsImage::boundingRect() const
117 {
118   return myPixmapItem->boundingRect();
119 }
120
121 //================================================================
122 // Function : compute
123 // Purpose  : 
124 //================================================================
125 void HYDROGUI_PrsImage::compute()
126 {
127   if( !myPixmapItem )
128   {
129     myPixmapItem = new QGraphicsPixmapItem( this );
130     addToGroup( myPixmapItem );
131   }
132   if( !myPrsImageFrame )
133   {
134     myPrsImageFrame = new HYDROGUI_PrsImageFrame( this );
135     addToGroup( myPrsImageFrame );
136   }
137   myPixmapItem->setPixmap( QPixmap::fromImage( myImage ) );
138   myPrsImageFrame->compute();
139 }
140
141 //================================================================
142 // Function : addTo
143 // Purpose  : 
144 //================================================================
145 void HYDROGUI_PrsImage::addTo( GraphicsView_ViewPort* theViewPort )
146 {
147   GraphicsView_Object::addTo( theViewPort );
148   theViewPort->addItem( myPrsImageFrame );
149
150   double aZValue = 0;
151   GraphicsView_ObjectListIterator anIter( theViewPort->getObjects() );
152   while( anIter.hasNext() )
153   {
154     if( HYDROGUI_PrsImage* aPrs = dynamic_cast<HYDROGUI_PrsImage*>( anIter.next() ) )
155     {
156       double aZValueRef = aPrs->zValue();
157       aZValue = qMax( aZValue, aZValueRef );
158     }
159   }
160   setZValue( aZValue + 1 );
161 }
162
163 //================================================================
164 // Function : removeFrom
165 // Purpose  : 
166 //================================================================
167 void HYDROGUI_PrsImage::removeFrom( GraphicsView_ViewPort* theViewPort )
168 {
169   GraphicsView_Object::removeFrom( theViewPort );
170   theViewPort->removeItem( myPrsImageFrame );
171 }
172
173 //================================================================
174 // Function : checkHighlight
175 // Purpose  : 
176 //================================================================
177 bool HYDROGUI_PrsImage::checkHighlight( double theX, double theY, QCursor& theCursor ) const
178 {
179   QRect aRect = myPixmapItem->boundingRect().toRect();
180   QPolygon aPolygon = sceneTransform().mapToPolygon( aRect );
181   if( aPolygon.containsPoint( QPoint( theX, theY ), Qt::OddEvenFill ) )
182   {
183     if( myIsTransformationPointPreview )
184     {
185       if( myTransformationPointMode != None )
186         theCursor = *getTransformationPointCursor();
187     }
188     else
189       theCursor = *getHighlightCursor();
190     return true;
191   }
192   return false;
193 }
194
195 //================================================================
196 // Function : select
197 // Purpose  : 
198 //================================================================
199 bool HYDROGUI_PrsImage::select( double theX, double theY, const QRectF& theRect )
200 {
201   if( myIsTransformationPointPreview )
202   {
203     if( myTransformationPointMode == None || !theRect.isEmpty() )
204       return false;
205
206     TransformationPoint& aTransformationPoint = myTransformationPointMap[ myTransformationPointMode ];
207     aTransformationPoint.Point = QPoint( (int)theX, (int)theY );
208     computeTransformationPoints();
209     return true;
210   }
211
212   bool anIsSelected = GraphicsView_Object::select( theX, theY, theRect );
213   myPrsImageFrame->updateVisibility();
214   return anIsSelected;
215 }
216
217 //================================================================
218 // Function : unselect
219 // Purpose  : 
220 //================================================================
221 void HYDROGUI_PrsImage::unselect()
222 {
223   GraphicsView_Object::unselect();
224   myPrsImageFrame->updateVisibility();
225 }
226
227 //================================================================
228 // Function : setSelected
229 // Purpose  : 
230 //================================================================
231 void HYDROGUI_PrsImage::setSelected( bool theState )
232 {
233   GraphicsView_Object::setSelected( theState );
234   myPrsImageFrame->updateVisibility();
235 }
236
237 //================================================================
238 // Function : computeTransformationPoints
239 // Purpose  : 
240 //================================================================
241 void HYDROGUI_PrsImage::computeTransformationPoints()
242 {
243   if( myTransformationPointMap.isEmpty() )
244   {
245     int aWidth = myImage.width();
246     int aHeight = myImage.height();
247
248     for( int aPointType = PointA; aPointType <= PointC; aPointType++ )
249     {
250       TransformationPoint aTransformationPoint;
251
252       QPoint aPoint;
253       QString aCaption;
254       QColor aColor = Qt::black;
255       switch( aPointType )
256       {
257         case PointA:
258           aPoint = QPoint( 0, 0 );
259           aCaption = "A";
260           aColor = Qt::darkRed;
261           break;
262         case PointB:
263           aPoint = QPoint( aWidth, 0 );
264           aCaption = "B";
265           aColor = Qt::darkGreen;
266           break;
267         case PointC:
268           aPoint = QPoint( 0, aHeight );
269           aCaption = "C";
270           aColor = Qt::darkBlue;
271           break;
272       }
273
274       aTransformationPoint.Point = aPoint;
275       aTransformationPoint.Caption = aCaption;
276
277       aTransformationPoint.PointItem = new QGraphicsEllipseItem( this );
278       aTransformationPoint.PointItem->setVisible( false );
279       aTransformationPoint.PointItem->setPen( QPen( aColor ) );
280       aTransformationPoint.PointItem->setBrush( QBrush( aColor ) );
281
282       aTransformationPoint.CaptionItem = new QGraphicsSimpleTextItem( aCaption, this );
283       aTransformationPoint.CaptionItem->setVisible( false );
284       aTransformationPoint.CaptionItem->setPen( QPen( aColor ) );
285       aTransformationPoint.CaptionItem->setBrush( QBrush( aColor ) );
286
287       myTransformationPointMap[ aPointType ] = aTransformationPoint;
288     }
289   }
290
291   bool anIsVisible = myIsTransformationPointPreview;
292   for( int aPointType = PointA; aPointType <= PointC; aPointType++ )
293   {
294     TransformationPoint& aTransformationPoint = myTransformationPointMap[ aPointType ];
295
296     double aRadius = 5;
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 );
301
302     QPointF aCaptionShift( -aRadius * 2, aRadius * 2 );
303     aTransformationPoint.CaptionItem->setPos( aPoint + aCaptionShift );
304     aTransformationPoint.CaptionItem->setVisible( anIsVisible );
305   }
306 }