Salome HOME
Fix for the bug #42: point C is not activated, but point C is shown in preview in...
[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_Entity)& theObject )
36 : HYDROGUI_Prs( theObject ),
37   myPixmapItem( 0 ),
38   myCaptionItem( 0 ),
39   myPrsImageFrame( 0 ),
40   myIsTransformationPointPreview( false ),
41   myTransformationPointType( None )
42 {
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( myTransformationPointCursor )
53   {
54     delete myTransformationPointCursor;
55     myTransformationPointCursor = 0;
56   }
57 }
58
59 //================================================================
60 // Function : setImage
61 // Purpose  : 
62 //================================================================
63 void HYDROGUI_PrsImage::setImage( const QImage& theImage )
64 {
65   myImage = theImage;
66 }
67
68 //================================================================
69 // Function : getImage
70 // Purpose  : 
71 //================================================================
72 QImage HYDROGUI_PrsImage::getImage() const
73 {
74   return myImage;
75 }
76
77 //================================================================
78 // Function : setCaption
79 // Purpose  : 
80 //================================================================
81 void HYDROGUI_PrsImage::setCaption( const QString& theCaption )
82 {
83   if( myCaptionItem )
84   {
85     myCaptionItem->setText( theCaption );
86     myCaptionItem->setVisible( !theCaption.isEmpty() );
87   }
88 }
89
90 //================================================================
91 // Function : getCaption
92 // Purpose  : 
93 //================================================================
94 QString HYDROGUI_PrsImage::getCaption() const
95 {
96   if( myCaptionItem )
97     return myCaptionItem->text();
98   return QString();
99 }
100
101 //================================================================
102 // Function : setIsTransformationPointPreview
103 // Purpose  : 
104 //================================================================
105 void HYDROGUI_PrsImage::setIsTransformationPointPreview( const bool theState )
106 {
107   myIsTransformationPointPreview = theState;
108 }
109
110 //================================================================
111 // Function : getIsTransformationPointPreview
112 // Purpose  : 
113 //================================================================
114 bool HYDROGUI_PrsImage::getIsTransformationPointPreview() const
115 {
116   return myIsTransformationPointPreview;
117 }
118
119 //================================================================
120 // Function : setTransformationPointType
121 // Purpose  : 
122 //================================================================
123 void HYDROGUI_PrsImage::setTransformationPointType( const int thePointType )
124 {
125   myTransformationPointType = thePointType;
126   if( thePointType != None )
127     computeTransformationPoints();
128 }
129
130 //================================================================
131 // Function : setTransformationPointMap
132 // Purpose  : 
133 //================================================================
134 void HYDROGUI_PrsImage::setTransformationPointMap( const TransformationPointMap& theMap )
135 {
136   myTransformationPointMap = theMap;
137   if( !theMap.isEmpty() )
138     computeTransformationPoints();
139 }
140
141 //================================================================
142 // Function : updateTransformationPoint
143 // Purpose  : 
144 //================================================================
145 void HYDROGUI_PrsImage::updateTransformationPoint( const int thePointType,
146                                                    const bool theIsY,
147                                                    const int theValue )
148 {
149   if( myTransformationPointMap.find( thePointType ) != myTransformationPointMap.end() )
150   {
151     TransformationPoint& aTransformationPoint = myTransformationPointMap[ thePointType ];
152     QPoint& aPoint = aTransformationPoint.Point;
153     theIsY ? aPoint.setY( theValue ) : aPoint.setX( theValue );
154     computeTransformationPoints();
155   }
156 }
157
158 //================================================================
159 // Function : boundingRect
160 // Purpose  : 
161 //================================================================
162 QRectF HYDROGUI_PrsImage::boundingRect() const
163 {
164   return myPixmapItem->boundingRect();
165 }
166
167 //================================================================
168 // Function : compute
169 // Purpose  : 
170 //================================================================
171 void HYDROGUI_PrsImage::compute()
172 {
173   if( !myPixmapItem )
174   {
175     myPixmapItem = new QGraphicsPixmapItem( this );
176     addToGroup( myPixmapItem );
177   }
178   if( !myCaptionItem )
179   {
180     myCaptionItem = new QGraphicsSimpleTextItem( this );
181
182     QFont aFont = myCaptionItem->font();
183     aFont.setPointSize( 14 );
184     myCaptionItem->setFont( aFont );
185
186     addToGroup( myCaptionItem );
187   }
188   if( !myPrsImageFrame )
189   {
190     myPrsImageFrame = new HYDROGUI_PrsImageFrame( this );
191     addToGroup( myPrsImageFrame );
192   }
193
194   myPixmapItem->setPixmap( QPixmap::fromImage( myImage ) );
195
196   myCaptionItem->setPos( 0, -30 );
197   myCaptionItem->setVisible( false );
198
199   myPrsImageFrame->compute();
200 }
201
202 //================================================================
203 // Function : addTo
204 // Purpose  : 
205 //================================================================
206 void HYDROGUI_PrsImage::addTo( GraphicsView_ViewPort* theViewPort )
207 {
208   HYDROGUI_Prs::addTo( theViewPort );
209   theViewPort->addItem( myPrsImageFrame );
210 }
211
212 //================================================================
213 // Function : removeFrom
214 // Purpose  : 
215 //================================================================
216 void HYDROGUI_PrsImage::removeFrom( GraphicsView_ViewPort* theViewPort )
217 {
218   HYDROGUI_Prs::removeFrom( theViewPort );
219   theViewPort->removeItem( myPrsImageFrame );
220 }
221
222 //================================================================
223 // Function : checkHighlight
224 // Purpose  : 
225 //================================================================
226 bool HYDROGUI_PrsImage::checkHighlight( double theX, double theY, QCursor& theCursor ) const
227 {
228   QRect aRect = myPixmapItem->boundingRect().toRect();
229   QPolygon aPolygon = sceneTransform().mapToPolygon( aRect );
230   if( aPolygon.containsPoint( QPoint( theX, theY ), Qt::OddEvenFill ) )
231   {
232     if( myIsTransformationPointPreview )
233     {
234       if( myTransformationPointType != None )
235         theCursor = *getTransformationPointCursor();
236     }
237     else
238       theCursor = *getHighlightCursor();
239     return true;
240   }
241   return false;
242 }
243
244 //================================================================
245 // Function : select
246 // Purpose  : 
247 //================================================================
248 bool HYDROGUI_PrsImage::select( double theX, double theY, const QRectF& theRect )
249 {
250   if( myIsTransformationPointPreview )
251   {
252     if( myTransformationPointType == None || !theRect.isEmpty() )
253       return false;
254
255     QPoint aPos = pos().toPoint();
256
257     TransformationPoint& aTransformationPoint = myTransformationPointMap[ myTransformationPointType ];
258     aTransformationPoint.Point = QPoint( (int)theX, (int)theY ) - aPos;
259     computeTransformationPoints();
260     return true;
261   }
262
263   bool anIsSelected = HYDROGUI_Prs::select( theX, theY, theRect );
264   myPrsImageFrame->updateVisibility();
265   return anIsSelected;
266 }
267
268 //================================================================
269 // Function : unselect
270 // Purpose  : 
271 //================================================================
272 void HYDROGUI_PrsImage::unselect()
273 {
274   HYDROGUI_Prs::unselect();
275   if( !myIsTransformationPointPreview )
276     myPrsImageFrame->updateVisibility();
277 }
278
279 //================================================================
280 // Function : setSelected
281 // Purpose  : 
282 //================================================================
283 void HYDROGUI_PrsImage::setSelected( bool theState )
284 {
285   HYDROGUI_Prs::setSelected( theState );
286   if( !myIsTransformationPointPreview )
287     myPrsImageFrame->updateVisibility();
288 }
289
290 //================================================================
291 // Function : computeTransformationPoints
292 // Purpose  : 
293 //================================================================
294 void HYDROGUI_PrsImage::computeTransformationPoints()
295 {
296   if( myTransformationPointMap.isEmpty() )
297   {
298     int aWidth = myImage.width();
299     int aHeight = myImage.height();
300
301     // Create presentations for transformation points A, B and C
302     for( int aPointType = PointA; aPointType <= PointC; aPointType++ )
303     {
304       TransformationPoint aTransformationPoint;
305
306       QPoint aPoint;
307       QString aCaption;
308       QColor aColor = Qt::black;
309       switch( aPointType )
310       {
311         case PointA:
312           aPoint = QPoint( 0, 0 );
313           aCaption = "A";
314           aColor = Qt::darkRed;
315           break;
316         case PointB:
317           aPoint = QPoint( aWidth, 0 );
318           aCaption = "B";
319           aColor = Qt::darkGreen;
320           break;
321         case PointC:
322           aPoint = QPoint( 0, aHeight );
323           aCaption = "C";
324           aColor = Qt::darkBlue;
325           break;
326       }
327
328       aTransformationPoint.Point = aPoint;
329       aTransformationPoint.Caption = aCaption;
330
331       aTransformationPoint.PointItem = new QGraphicsEllipseItem( this );
332       aTransformationPoint.PointItem->setVisible( false );
333       aTransformationPoint.PointItem->setPen( QPen( aColor ) );
334       aTransformationPoint.PointItem->setBrush( QBrush( aColor ) );
335
336       aTransformationPoint.CaptionItem = new QGraphicsSimpleTextItem( aCaption, this );
337       aTransformationPoint.CaptionItem->setVisible( false );
338       aTransformationPoint.CaptionItem->setPen( QPen( aColor ) );
339       aTransformationPoint.CaptionItem->setBrush( QBrush( aColor ) );
340
341       myTransformationPointMap[ aPointType ] = aTransformationPoint;
342     }
343   }
344
345   for( int aPointType = PointA; aPointType <= PointC; aPointType++ )
346   {
347     // Show/hide the point if necessary
348     updateTrsfPoint( aPointType );
349   }
350 }
351
352 //================================================================
353 // Function : updateTrsfPoint
354 // Purpose  : 
355 //================================================================
356 void HYDROGUI_PrsImage::updateTrsfPoint( const int thePointType )
357 {
358   // If image is transformed only by two points then the point C is invisible
359   bool anIsPointVisible = myIsTransformationPointPreview && ( 
360     ( !myIsByTwoPoints ) || ( myIsByTwoPoints && ( thePointType != PointC ) ) );
361   TransformationPoint& aTransformationPoint = myTransformationPointMap[ thePointType ];
362
363   double aRadius = 5;
364   const QPointF& aPoint = aTransformationPoint.Point;
365   QRectF aRect( aPoint - QPointF( aRadius, aRadius ), QSizeF( aRadius * 2 + 1, aRadius * 2 + 1 ) );
366   aTransformationPoint.PointItem->setRect( aRect );
367   aTransformationPoint.PointItem->setVisible( anIsPointVisible );
368
369   QPointF aCaptionShift( -aRadius * 2, aRadius * 2 );
370   aTransformationPoint.CaptionItem->setPos( aPoint + aCaptionShift );
371   aTransformationPoint.CaptionItem->setVisible( anIsPointVisible );
372 }
373
374 //================================================================
375 // Function : getIsByTwoPoints
376 // Purpose  : 
377 //================================================================
378 bool HYDROGUI_PrsImage::getIsByTwoPoints() const
379 {
380   return myIsByTwoPoints;
381 }
382
383 //================================================================
384 // Function : setIsByTwoPoints
385 // Purpose  : 
386 //================================================================
387 void HYDROGUI_PrsImage::setIsByTwoPoints( const bool theIsByTwoPoints )
388 {
389   myIsByTwoPoints = theIsByTwoPoints;
390   if ( myTransformationPointMap.contains( PointC ) )
391   {
392     // Show/hide the point C if necessary
393     updateTrsfPoint( PointC );
394   }
395 }