Salome HOME
Minor fix.
[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   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 : setTransformationPointMode
96 // Purpose  : 
97 //================================================================
98 void HYDROGUI_PrsImage::setTransformationPointMode( const int theMode )
99 {
100   myTransformationPointMode = theMode;
101   if( theMode != 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 : boundingRect
118 // Purpose  : 
119 //================================================================
120 QRectF HYDROGUI_PrsImage::boundingRect() const
121 {
122   return myPixmapItem->boundingRect();
123 }
124
125 //================================================================
126 // Function : compute
127 // Purpose  : 
128 //================================================================
129 void HYDROGUI_PrsImage::compute()
130 {
131   if( !myPixmapItem )
132   {
133     myPixmapItem = new QGraphicsPixmapItem( this );
134     addToGroup( myPixmapItem );
135   }
136   if( !myPrsImageFrame )
137   {
138     myPrsImageFrame = new HYDROGUI_PrsImageFrame( this );
139     addToGroup( myPrsImageFrame );
140   }
141   myPixmapItem->setPixmap( QPixmap::fromImage( myImage ) );
142   myPrsImageFrame->compute();
143 }
144
145 //================================================================
146 // Function : addTo
147 // Purpose  : 
148 //================================================================
149 void HYDROGUI_PrsImage::addTo( GraphicsView_ViewPort* theViewPort )
150 {
151   HYDROGUI_Prs::addTo( theViewPort );
152   theViewPort->addItem( myPrsImageFrame );
153 }
154
155 //================================================================
156 // Function : removeFrom
157 // Purpose  : 
158 //================================================================
159 void HYDROGUI_PrsImage::removeFrom( GraphicsView_ViewPort* theViewPort )
160 {
161   HYDROGUI_Prs::removeFrom( theViewPort );
162   theViewPort->removeItem( myPrsImageFrame );
163 }
164
165 //================================================================
166 // Function : checkHighlight
167 // Purpose  : 
168 //================================================================
169 bool HYDROGUI_PrsImage::checkHighlight( double theX, double theY, QCursor& theCursor ) const
170 {
171   QRect aRect = myPixmapItem->boundingRect().toRect();
172   QPolygon aPolygon = sceneTransform().mapToPolygon( aRect );
173   if( aPolygon.containsPoint( QPoint( theX, theY ), Qt::OddEvenFill ) )
174   {
175     if( myIsTransformationPointPreview )
176     {
177       if( myTransformationPointMode != None )
178         theCursor = *getTransformationPointCursor();
179     }
180     else
181       theCursor = *getHighlightCursor();
182     return true;
183   }
184   return false;
185 }
186
187 //================================================================
188 // Function : select
189 // Purpose  : 
190 //================================================================
191 bool HYDROGUI_PrsImage::select( double theX, double theY, const QRectF& theRect )
192 {
193   if( myIsTransformationPointPreview )
194   {
195     if( myTransformationPointMode == None || !theRect.isEmpty() )
196       return false;
197
198     TransformationPoint& aTransformationPoint = myTransformationPointMap[ myTransformationPointMode ];
199     aTransformationPoint.Point = QPoint( (int)theX, (int)theY );
200     computeTransformationPoints();
201     return true;
202   }
203
204   bool anIsSelected = HYDROGUI_Prs::select( theX, theY, theRect );
205   myPrsImageFrame->updateVisibility();
206   return anIsSelected;
207 }
208
209 //================================================================
210 // Function : unselect
211 // Purpose  : 
212 //================================================================
213 void HYDROGUI_PrsImage::unselect()
214 {
215   HYDROGUI_Prs::unselect();
216   if( !myIsTransformationPointPreview )
217     myPrsImageFrame->updateVisibility();
218 }
219
220 //================================================================
221 // Function : setSelected
222 // Purpose  : 
223 //================================================================
224 void HYDROGUI_PrsImage::setSelected( bool theState )
225 {
226   HYDROGUI_Prs::setSelected( theState );
227   if( !myIsTransformationPointPreview )
228     myPrsImageFrame->updateVisibility();
229 }
230
231 //================================================================
232 // Function : computeTransformationPoints
233 // Purpose  : 
234 //================================================================
235 void HYDROGUI_PrsImage::computeTransformationPoints()
236 {
237   if( myTransformationPointMap.isEmpty() )
238   {
239     int aWidth = myImage.width();
240     int aHeight = myImage.height();
241
242     for( int aPointType = PointA; aPointType <= PointC; aPointType++ )
243     {
244       TransformationPoint aTransformationPoint;
245
246       QPoint aPoint;
247       QString aCaption;
248       QColor aColor = Qt::black;
249       switch( aPointType )
250       {
251         case PointA:
252           aPoint = QPoint( 0, 0 );
253           aCaption = "A";
254           aColor = Qt::darkRed;
255           break;
256         case PointB:
257           aPoint = QPoint( aWidth, 0 );
258           aCaption = "B";
259           aColor = Qt::darkGreen;
260           break;
261         case PointC:
262           aPoint = QPoint( 0, aHeight );
263           aCaption = "C";
264           aColor = Qt::darkBlue;
265           break;
266       }
267
268       aTransformationPoint.Point = aPoint;
269       aTransformationPoint.Caption = aCaption;
270
271       aTransformationPoint.PointItem = new QGraphicsEllipseItem( this );
272       aTransformationPoint.PointItem->setVisible( false );
273       aTransformationPoint.PointItem->setPen( QPen( aColor ) );
274       aTransformationPoint.PointItem->setBrush( QBrush( aColor ) );
275
276       aTransformationPoint.CaptionItem = new QGraphicsSimpleTextItem( aCaption, this );
277       aTransformationPoint.CaptionItem->setVisible( false );
278       aTransformationPoint.CaptionItem->setPen( QPen( aColor ) );
279       aTransformationPoint.CaptionItem->setBrush( QBrush( aColor ) );
280
281       myTransformationPointMap[ aPointType ] = aTransformationPoint;
282     }
283   }
284
285   bool anIsVisible = myIsTransformationPointPreview;
286   for( int aPointType = PointA; aPointType <= PointC; aPointType++ )
287   {
288     TransformationPoint& aTransformationPoint = myTransformationPointMap[ aPointType ];
289
290     double aRadius = 5;
291     const QPointF& aPoint = aTransformationPoint.Point;
292     QRectF aRect( aPoint - QPointF( aRadius, aRadius ), QSizeF( aRadius * 2 + 1, aRadius * 2 + 1 ) );
293     aTransformationPoint.PointItem->setRect( aRect );
294     aTransformationPoint.PointItem->setVisible( anIsVisible );
295
296     QPointF aCaptionShift( -aRadius * 2, aRadius * 2 );
297     aTransformationPoint.CaptionItem->setPos( aPoint + aCaptionShift );
298     aTransformationPoint.CaptionItem->setVisible( anIsVisible );
299   }
300 }