Salome HOME
9e7be1425bceb3b3ad0d459d8e889020f2e24f0a
[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     for( int aPointType = PointA; aPointType <= PointC; aPointType++ )
302     {
303       TransformationPoint aTransformationPoint;
304
305       QPoint aPoint;
306       QString aCaption;
307       QColor aColor = Qt::black;
308       switch( aPointType )
309       {
310         case PointA:
311           aPoint = QPoint( 0, 0 );
312           aCaption = "A";
313           aColor = Qt::darkRed;
314           break;
315         case PointB:
316           aPoint = QPoint( aWidth, 0 );
317           aCaption = "B";
318           aColor = Qt::darkGreen;
319           break;
320         case PointC:
321           aPoint = QPoint( 0, aHeight );
322           aCaption = "C";
323           aColor = Qt::darkBlue;
324           break;
325       }
326
327       aTransformationPoint.Point = aPoint;
328       aTransformationPoint.Caption = aCaption;
329
330       aTransformationPoint.PointItem = new QGraphicsEllipseItem( this );
331       aTransformationPoint.PointItem->setVisible( false );
332       aTransformationPoint.PointItem->setPen( QPen( aColor ) );
333       aTransformationPoint.PointItem->setBrush( QBrush( aColor ) );
334
335       aTransformationPoint.CaptionItem = new QGraphicsSimpleTextItem( aCaption, this );
336       aTransformationPoint.CaptionItem->setVisible( false );
337       aTransformationPoint.CaptionItem->setPen( QPen( aColor ) );
338       aTransformationPoint.CaptionItem->setBrush( QBrush( aColor ) );
339
340       myTransformationPointMap[ aPointType ] = aTransformationPoint;
341     }
342   }
343
344   bool anIsVisible = myIsTransformationPointPreview;
345   for( int aPointType = PointA; aPointType <= PointC; aPointType++ )
346   {
347     TransformationPoint& aTransformationPoint = myTransformationPointMap[ aPointType ];
348
349     double aRadius = 5;
350     const QPointF& aPoint = aTransformationPoint.Point;
351     QRectF aRect( aPoint - QPointF( aRadius, aRadius ), QSizeF( aRadius * 2 + 1, aRadius * 2 + 1 ) );
352     aTransformationPoint.PointItem->setRect( aRect );
353     aTransformationPoint.PointItem->setVisible( anIsVisible );
354
355     QPointF aCaptionShift( -aRadius * 2, aRadius * 2 );
356     aTransformationPoint.CaptionItem->setPos( aPoint + aCaptionShift );
357     aTransformationPoint.CaptionItem->setVisible( anIsVisible );
358   }
359 }