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