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