Salome HOME
90e54cf68acdb979535f67e67adc7d1b66cfd9b4
[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   myTransformationPointType( None )
41 {
42   myTransformationPointCursor = new QCursor( Qt::CrossCursor );
43 }
44
45 //=======================================================================
46 // name    : HYDROGUI_PrsImage
47 // Purpose : Destructor
48 //=======================================================================
49 HYDROGUI_PrsImage::~HYDROGUI_PrsImage()
50 {
51   if( myTransformationPointCursor )
52   {
53     delete myTransformationPointCursor;
54     myTransformationPointCursor = 0;
55   }
56 }
57
58 //================================================================
59 // Function : setImage
60 // Purpose  : 
61 //================================================================
62 void HYDROGUI_PrsImage::setImage( const QImage& theImage )
63 {
64   myImage = theImage;
65 }
66
67 //================================================================
68 // Function : getImage
69 // Purpose  : 
70 //================================================================
71 QImage HYDROGUI_PrsImage::getImage() const
72 {
73   return myImage;
74 }
75
76 //================================================================
77 // Function : setIsTransformationPointPreview
78 // Purpose  : 
79 //================================================================
80 void HYDROGUI_PrsImage::setIsTransformationPointPreview( const bool theState )
81 {
82   myIsTransformationPointPreview = theState;
83 }
84
85 //================================================================
86 // Function : getIsTransformationPointPreview
87 // Purpose  : 
88 //================================================================
89 bool HYDROGUI_PrsImage::getIsTransformationPointPreview() const
90 {
91   return myIsTransformationPointPreview;
92 }
93
94 //================================================================
95 // Function : setTransformationPointType
96 // Purpose  : 
97 //================================================================
98 void HYDROGUI_PrsImage::setTransformationPointType( const int thePointType )
99 {
100   myTransformationPointType = thePointType;
101   if( thePointType != None )
102     computeTransformationPoints();
103 }
104
105 //================================================================
106 // Function : setTransformationPointMap
107 // Purpose  : 
108 //================================================================
109 void HYDROGUI_PrsImage::setTransformationPointMap( const TransformationPointMap& theMap )
110 {
111   myTransformationPointMap = theMap;
112   if( !theMap.isEmpty() )
113     computeTransformationPoints();
114 }
115
116 //================================================================
117 // Function : updateTransformationPoint
118 // Purpose  : 
119 //================================================================
120 void HYDROGUI_PrsImage::updateTransformationPoint( const int thePointType,
121                                                    const bool theIsY,
122                                                    const int theValue )
123 {
124   if( myTransformationPointMap.find( thePointType ) != myTransformationPointMap.end() )
125   {
126     TransformationPoint& aTransformationPoint = myTransformationPointMap[ thePointType ];
127     QPoint& aPoint = aTransformationPoint.Point;
128     theIsY ? aPoint.setY( theValue ) : aPoint.setX( theValue );
129     computeTransformationPoints();
130   }
131 }
132
133 //================================================================
134 // Function : boundingRect
135 // Purpose  : 
136 //================================================================
137 QRectF HYDROGUI_PrsImage::boundingRect() const
138 {
139   return myPixmapItem->boundingRect();
140 }
141
142 //================================================================
143 // Function : compute
144 // Purpose  : 
145 //================================================================
146 void HYDROGUI_PrsImage::compute()
147 {
148   if( !myPixmapItem )
149   {
150     myPixmapItem = new QGraphicsPixmapItem( this );
151     addToGroup( myPixmapItem );
152   }
153   if( !myPrsImageFrame )
154   {
155     myPrsImageFrame = new HYDROGUI_PrsImageFrame( this );
156     addToGroup( myPrsImageFrame );
157   }
158   myPixmapItem->setPixmap( QPixmap::fromImage( myImage ) );
159   myPrsImageFrame->compute();
160 }
161
162 //================================================================
163 // Function : addTo
164 // Purpose  : 
165 //================================================================
166 void HYDROGUI_PrsImage::addTo( GraphicsView_ViewPort* theViewPort )
167 {
168   HYDROGUI_Prs::addTo( theViewPort );
169   theViewPort->addItem( myPrsImageFrame );
170 }
171
172 //================================================================
173 // Function : removeFrom
174 // Purpose  : 
175 //================================================================
176 void HYDROGUI_PrsImage::removeFrom( GraphicsView_ViewPort* theViewPort )
177 {
178   HYDROGUI_Prs::removeFrom( theViewPort );
179   theViewPort->removeItem( myPrsImageFrame );
180 }
181
182 //================================================================
183 // Function : checkHighlight
184 // Purpose  : 
185 //================================================================
186 bool HYDROGUI_PrsImage::checkHighlight( double theX, double theY, QCursor& theCursor ) const
187 {
188   QRect aRect = myPixmapItem->boundingRect().toRect();
189   QPolygon aPolygon = sceneTransform().mapToPolygon( aRect );
190   if( aPolygon.containsPoint( QPoint( theX, theY ), Qt::OddEvenFill ) )
191   {
192     if( myIsTransformationPointPreview )
193     {
194       if( myTransformationPointType != None )
195         theCursor = *getTransformationPointCursor();
196     }
197     else
198       theCursor = *getHighlightCursor();
199     return true;
200   }
201   return false;
202 }
203
204 //================================================================
205 // Function : select
206 // Purpose  : 
207 //================================================================
208 bool HYDROGUI_PrsImage::select( double theX, double theY, const QRectF& theRect )
209 {
210   if( myIsTransformationPointPreview )
211   {
212     if( myTransformationPointType == None || !theRect.isEmpty() )
213       return false;
214
215     QPoint aPos = pos().toPoint();
216
217     TransformationPoint& aTransformationPoint = myTransformationPointMap[ myTransformationPointType ];
218     aTransformationPoint.Point = QPoint( (int)theX, (int)theY ) - aPos;
219     computeTransformationPoints();
220     return true;
221   }
222
223   bool anIsSelected = HYDROGUI_Prs::select( theX, theY, theRect );
224   myPrsImageFrame->updateVisibility();
225   return anIsSelected;
226 }
227
228 //================================================================
229 // Function : unselect
230 // Purpose  : 
231 //================================================================
232 void HYDROGUI_PrsImage::unselect()
233 {
234   HYDROGUI_Prs::unselect();
235   if( !myIsTransformationPointPreview )
236     myPrsImageFrame->updateVisibility();
237 }
238
239 //================================================================
240 // Function : setSelected
241 // Purpose  : 
242 //================================================================
243 void HYDROGUI_PrsImage::setSelected( bool theState )
244 {
245   HYDROGUI_Prs::setSelected( theState );
246   if( !myIsTransformationPointPreview )
247     myPrsImageFrame->updateVisibility();
248 }
249
250 //================================================================
251 // Function : computeTransformationPoints
252 // Purpose  : 
253 //================================================================
254 void HYDROGUI_PrsImage::computeTransformationPoints()
255 {
256   if( myTransformationPointMap.isEmpty() )
257   {
258     int aWidth = myImage.width();
259     int aHeight = myImage.height();
260
261     for( int aPointType = PointA; aPointType <= PointC; aPointType++ )
262     {
263       TransformationPoint aTransformationPoint;
264
265       QPoint aPoint;
266       QString aCaption;
267       QColor aColor = Qt::black;
268       switch( aPointType )
269       {
270         case PointA:
271           aPoint = QPoint( 0, 0 );
272           aCaption = "A";
273           aColor = Qt::darkRed;
274           break;
275         case PointB:
276           aPoint = QPoint( aWidth, 0 );
277           aCaption = "B";
278           aColor = Qt::darkGreen;
279           break;
280         case PointC:
281           aPoint = QPoint( 0, aHeight );
282           aCaption = "C";
283           aColor = Qt::darkBlue;
284           break;
285       }
286
287       aTransformationPoint.Point = aPoint;
288       aTransformationPoint.Caption = aCaption;
289
290       aTransformationPoint.PointItem = new QGraphicsEllipseItem( this );
291       aTransformationPoint.PointItem->setVisible( false );
292       aTransformationPoint.PointItem->setPen( QPen( aColor ) );
293       aTransformationPoint.PointItem->setBrush( QBrush( aColor ) );
294
295       aTransformationPoint.CaptionItem = new QGraphicsSimpleTextItem( aCaption, this );
296       aTransformationPoint.CaptionItem->setVisible( false );
297       aTransformationPoint.CaptionItem->setPen( QPen( aColor ) );
298       aTransformationPoint.CaptionItem->setBrush( QBrush( aColor ) );
299
300       myTransformationPointMap[ aPointType ] = aTransformationPoint;
301     }
302   }
303
304   bool anIsVisible = myIsTransformationPointPreview;
305   for( int aPointType = PointA; aPointType <= PointC; aPointType++ )
306   {
307     TransformationPoint& aTransformationPoint = myTransformationPointMap[ aPointType ];
308
309     double aRadius = 5;
310     const QPointF& aPoint = aTransformationPoint.Point;
311     QRectF aRect( aPoint - QPointF( aRadius, aRadius ), QSizeF( aRadius * 2 + 1, aRadius * 2 + 1 ) );
312     aTransformationPoint.PointItem->setRect( aRect );
313     aTransformationPoint.PointItem->setVisible( anIsVisible );
314
315     QPointF aCaptionShift( -aRadius * 2, aRadius * 2 );
316     aTransformationPoint.CaptionItem->setPos( aPoint + aCaptionShift );
317     aTransformationPoint.CaptionItem->setVisible( anIsVisible );
318   }
319 }