Salome HOME
Image composing.
[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   myPrsImageFrame->updateVisibility();
217 }
218
219 //================================================================
220 // Function : setSelected
221 // Purpose  : 
222 //================================================================
223 void HYDROGUI_PrsImage::setSelected( bool theState )
224 {
225   HYDROGUI_Prs::setSelected( theState );
226   myPrsImageFrame->updateVisibility();
227 }
228
229 //================================================================
230 // Function : computeTransformationPoints
231 // Purpose  : 
232 //================================================================
233 void HYDROGUI_PrsImage::computeTransformationPoints()
234 {
235   if( myTransformationPointMap.isEmpty() )
236   {
237     int aWidth = myImage.width();
238     int aHeight = myImage.height();
239
240     for( int aPointType = PointA; aPointType <= PointC; aPointType++ )
241     {
242       TransformationPoint aTransformationPoint;
243
244       QPoint aPoint;
245       QString aCaption;
246       QColor aColor = Qt::black;
247       switch( aPointType )
248       {
249         case PointA:
250           aPoint = QPoint( 0, 0 );
251           aCaption = "A";
252           aColor = Qt::darkRed;
253           break;
254         case PointB:
255           aPoint = QPoint( aWidth, 0 );
256           aCaption = "B";
257           aColor = Qt::darkGreen;
258           break;
259         case PointC:
260           aPoint = QPoint( 0, aHeight );
261           aCaption = "C";
262           aColor = Qt::darkBlue;
263           break;
264       }
265
266       aTransformationPoint.Point = aPoint;
267       aTransformationPoint.Caption = aCaption;
268
269       aTransformationPoint.PointItem = new QGraphicsEllipseItem( this );
270       aTransformationPoint.PointItem->setVisible( false );
271       aTransformationPoint.PointItem->setPen( QPen( aColor ) );
272       aTransformationPoint.PointItem->setBrush( QBrush( aColor ) );
273
274       aTransformationPoint.CaptionItem = new QGraphicsSimpleTextItem( aCaption, this );
275       aTransformationPoint.CaptionItem->setVisible( false );
276       aTransformationPoint.CaptionItem->setPen( QPen( aColor ) );
277       aTransformationPoint.CaptionItem->setBrush( QBrush( aColor ) );
278
279       myTransformationPointMap[ aPointType ] = aTransformationPoint;
280     }
281   }
282
283   bool anIsVisible = myIsTransformationPointPreview;
284   for( int aPointType = PointA; aPointType <= PointC; aPointType++ )
285   {
286     TransformationPoint& aTransformationPoint = myTransformationPointMap[ aPointType ];
287
288     double aRadius = 5;
289     const QPointF& aPoint = aTransformationPoint.Point;
290     QRectF aRect( aPoint - QPointF( aRadius, aRadius ), QSizeF( aRadius * 2 + 1, aRadius * 2 + 1 ) );
291     aTransformationPoint.PointItem->setRect( aRect );
292     aTransformationPoint.PointItem->setVisible( anIsVisible );
293
294     QPointF aCaptionShift( -aRadius * 2, aRadius * 2 );
295     aTransformationPoint.CaptionItem->setPos( aPoint + aCaptionShift );
296     aTransformationPoint.CaptionItem->setVisible( anIsVisible );
297   }
298 }