Salome HOME
Get rid of Qt classes in the data model.
[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 #include <QApplication>
31
32 //=======================================================================
33 // name    : HYDROGUI_PrsImage
34 // Purpose : Constructor
35 //=======================================================================
36 HYDROGUI_PrsImage::HYDROGUI_PrsImage( const Handle(HYDROData_Entity)& theObject )
37 : HYDROGUI_Prs( theObject ),
38   myPixmapItem( 0 ),
39   myCaptionItem( 0 ),
40   myPrsImageFrame( 0 ),
41   myIsTransformationPointPreview( false ),
42   myIsByTwoPoints( true ),
43   myTransformationPointType( None )
44 {
45   myTransformationPointCursor = new QCursor( Qt::CrossCursor );
46 }
47
48 //=======================================================================
49 // name    : HYDROGUI_PrsImage
50 // Purpose : Destructor
51 //=======================================================================
52 HYDROGUI_PrsImage::~HYDROGUI_PrsImage()
53 {
54   if( myTransformationPointCursor )
55   {
56     delete myTransformationPointCursor;
57     myTransformationPointCursor = 0;
58   }
59 }
60
61 //================================================================
62 // Function : setImage
63 // Purpose  : 
64 //================================================================
65 void HYDROGUI_PrsImage::setImage( const QImage& theImage, const bool theCompute )
66 {
67   myImage = theImage;
68   if ( theCompute ) {
69     compute();
70     computeTransformationPoints( true );
71   }
72 }
73
74 //================================================================
75 // Function : getImage
76 // Purpose  : 
77 //================================================================
78 QImage HYDROGUI_PrsImage::getImage() const
79 {
80   return myImage;
81 }
82
83 //================================================================
84 // Function : setCaption
85 // Purpose  : 
86 //================================================================
87 void HYDROGUI_PrsImage::setCaption( const QString& theCaption )
88 {
89   if( myCaptionItem )
90   {
91     myCaptionItem->setText( theCaption );
92     myCaptionItem->setVisible( !theCaption.isEmpty() );
93   }
94 }
95
96 //================================================================
97 // Function : getCaption
98 // Purpose  : 
99 //================================================================
100 QString HYDROGUI_PrsImage::getCaption() const
101 {
102   if( myCaptionItem )
103     return myCaptionItem->text();
104   return QString();
105 }
106
107 //================================================================
108 // Function : setIsTransformationPointPreview
109 // Purpose  : 
110 //================================================================
111 void HYDROGUI_PrsImage::setIsTransformationPointPreview( const bool theState )
112 {
113   myIsTransformationPointPreview = theState;
114 }
115
116 //================================================================
117 // Function : getIsTransformationPointPreview
118 // Purpose  : 
119 //================================================================
120 bool HYDROGUI_PrsImage::getIsTransformationPointPreview() const
121 {
122   return myIsTransformationPointPreview;
123 }
124
125 //================================================================
126 // Function : setTransformationPointType
127 // Purpose  : 
128 //================================================================
129 void HYDROGUI_PrsImage::setTransformationPointType( const int thePointType )
130 {
131   myTransformationPointType = thePointType;
132   if( thePointType != None )
133     computeTransformationPoints();
134 }
135
136 //================================================================
137 // Function : setTransformationPointMap
138 // Purpose  : 
139 //================================================================
140 void HYDROGUI_PrsImage::setTransformationPointMap( const TransformationPointMap& theMap )
141 {
142   myTransformationPointMap = theMap;
143   if( !theMap.isEmpty() )
144     computeTransformationPoints();
145 }
146
147 //================================================================
148 // Function : updateTransformationPoint
149 // Purpose  : 
150 //================================================================
151 void HYDROGUI_PrsImage::updateTransformationPoint( const int thePointType,
152                                                    const bool theIsY,
153                                                    const int theValue )
154 {
155   if( myTransformationPointMap.find( thePointType ) != myTransformationPointMap.end() )
156   {
157     TransformationPoint& aTransformationPoint = myTransformationPointMap[ thePointType ];
158     QPoint& aPoint = aTransformationPoint.Point;
159     theIsY ? aPoint.setY( theValue ) : aPoint.setX( theValue );
160     computeTransformationPoints();
161   }
162 }
163
164 //================================================================
165 // Function : boundingRect
166 // Purpose  : 
167 //================================================================
168 QRectF HYDROGUI_PrsImage::boundingRect() const
169 {
170   return myPixmapItem->boundingRect();
171 }
172
173 //================================================================
174 // Function : compute
175 // Purpose  : 
176 //================================================================
177 void HYDROGUI_PrsImage::compute()
178 {
179   if( !myPixmapItem )
180   {
181     myPixmapItem = new QGraphicsPixmapItem( this );
182     addToGroup( myPixmapItem );
183   }
184   if( !myCaptionItem )
185   {
186     myCaptionItem = new QGraphicsSimpleTextItem( this );
187
188     QFont aFont = myCaptionItem->font();
189     aFont.setPointSize( 14 );
190     myCaptionItem->setFont( aFont );
191
192     addToGroup( myCaptionItem );
193   }
194   if( !myPrsImageFrame )
195   {
196     myPrsImageFrame = new HYDROGUI_PrsImageFrame( this );
197     addToGroup( myPrsImageFrame );
198   }
199
200   myPixmapItem->setPixmap( QPixmap::fromImage( myImage ) );
201
202   myCaptionItem->setPos( 0, -30 );
203   myCaptionItem->setVisible( false );
204
205   myPrsImageFrame->compute();
206 }
207
208 //================================================================
209 // Function : addTo
210 // Purpose  : 
211 //================================================================
212 void HYDROGUI_PrsImage::addTo( GraphicsView_ViewPort* theViewPort )
213 {
214   HYDROGUI_Prs::addTo( theViewPort );
215   theViewPort->addItem( myPrsImageFrame );
216 }
217
218 //================================================================
219 // Function : removeFrom
220 // Purpose  : 
221 //================================================================
222 void HYDROGUI_PrsImage::removeFrom( GraphicsView_ViewPort* theViewPort )
223 {
224   HYDROGUI_Prs::removeFrom( theViewPort );
225   theViewPort->removeItem( myPrsImageFrame );
226 }
227
228 //================================================================
229 // Function : checkHighlight
230 // Purpose  : 
231 //================================================================
232 bool HYDROGUI_PrsImage::checkHighlight( double theX, double theY, QCursor& theCursor ) const
233 {
234   QRect aRect = myPixmapItem->boundingRect().toRect();
235   QPolygon aPolygon = sceneTransform().mapToPolygon( aRect );
236   if( aPolygon.containsPoint( QPoint( theX, theY ), Qt::OddEvenFill ) )
237   {
238     if( myIsTransformationPointPreview )
239     {
240       if( myTransformationPointType != None )
241         theCursor = *getTransformationPointCursor();
242     }
243     else
244       theCursor = *getHighlightCursor();
245     return true;
246   }
247   return false;
248 }
249
250 //================================================================
251 // Function : select
252 // Purpose  : 
253 //================================================================
254 bool HYDROGUI_PrsImage::select( double theX, double theY, const QRectF& theRect )
255 {
256   if( myIsTransformationPointPreview )
257   {
258     if( myTransformationPointType == None || !theRect.isEmpty() )
259       return false;
260
261     QPoint aPos = pos().toPoint();
262
263     TransformationPoint& aTransformationPoint = myTransformationPointMap[ myTransformationPointType ];
264     aTransformationPoint.Point = QPoint( (int)theX, (int)theY ) - aPos;
265     computeTransformationPoints();
266     return true;
267   }
268
269   bool anIsSelected = HYDROGUI_Prs::select( theX, theY, theRect );
270   myPrsImageFrame->updateVisibility();
271   return anIsSelected;
272 }
273
274 //================================================================
275 // Function : unselect
276 // Purpose  : 
277 //================================================================
278 void HYDROGUI_PrsImage::unselect()
279 {
280   HYDROGUI_Prs::unselect();
281   if( !myIsTransformationPointPreview )
282     myPrsImageFrame->updateVisibility();
283 }
284
285 //================================================================
286 // Function : setSelected
287 // Purpose  : 
288 //================================================================
289 void HYDROGUI_PrsImage::setSelected( bool theState )
290 {
291   HYDROGUI_Prs::setSelected( theState );
292   if( !myIsTransformationPointPreview )
293     myPrsImageFrame->updateVisibility();
294 }
295
296 //================================================================
297 // Function : computeTransformationPoints
298 // Purpose  : 
299 //================================================================
300 void HYDROGUI_PrsImage::computeTransformationPoints( const bool theObligatoryInit )
301 {
302   {
303     for( int aPointType = PointA; aPointType <= PointC; aPointType++ )
304     {
305       if( myTransformationPointMap.isEmpty() || theObligatoryInit )
306         initTrsfPoints( aPointType );
307       // Show/hide the point if necessary
308       updateTrsfPoint( aPointType );
309
310     }
311   }
312 }
313
314 QGraphicsItemGroup* HYDROGUI_PrsImage::createPointItem( const QString& theCaption,
315                                                         const QColor& theColor )
316 {
317   QGraphicsEllipseItem* aPointItem = new QGraphicsEllipseItem( this );
318   aPointItem->setPen( QPen( theColor ) );
319   aPointItem->setBrush( QBrush( theColor ) );
320
321   double aRadius = 3;
322   QRectF aRect( -QPointF( aRadius, aRadius ), QSizeF( aRadius * 2 + 1, aRadius * 2 + 1 ) );
323   aPointItem->setRect( aRect );
324   aPointItem->setPos( QPointF( 0, 0 ) );
325
326   QGraphicsSimpleTextItem* aCaptionItem = aCaptionItem = new QGraphicsSimpleTextItem( theCaption, this );
327   aCaptionItem->setPen( QPen( theColor ) );
328   aCaptionItem->setBrush( QBrush( theColor ) );
329   QFont aFont = aCaptionItem->font();
330   aFont.setPointSize( qApp->font().pointSize() );
331   aCaptionItem->setFont( aFont );
332   aCaptionItem->setPos( QPointF( -aRadius * 2, aRadius * 2 ) );
333
334   QGraphicsItemGroup* aGroupItem = new QGraphicsItemGroup( this );
335   aGroupItem->addToGroup( aPointItem );
336   aGroupItem->addToGroup( aCaptionItem );
337   aGroupItem->setVisible( false );
338   aGroupItem->setFlag( QGraphicsItem::ItemIgnoresTransformations );
339
340   return aGroupItem;
341 }
342
343 //================================================================
344 // Function : initTrsfPoints
345 // Purpose  : 
346 //================================================================
347 void HYDROGUI_PrsImage::initTrsfPoints( const int thePointType )
348 {
349   QPoint aPoint;
350   QString aCaption;
351   QColor aColor = Qt::black;
352
353   int aWidth = myImage.width();
354   int aHeight = myImage.height();
355   switch( thePointType )
356   {
357     case PointA:
358       aPoint = QPoint( 0, 0 );
359       aCaption = "A";
360       aColor = Qt::darkRed;
361       break;
362     case PointB:
363       aPoint = QPoint( aWidth, 0 );
364       aCaption = "B";
365       aColor = Qt::darkGreen;
366       break;
367     case PointC:
368       aPoint = QPoint( 0, aHeight );
369       aCaption = "C";
370       aColor = Qt::darkBlue;
371       break;
372   }
373
374   TransformationPoint aTransformationPoint;
375   if ( myTransformationPointMap.contains( thePointType ) )
376     aTransformationPoint = myTransformationPointMap[thePointType];
377
378   aTransformationPoint.Point = aPoint;
379   aTransformationPoint.Caption = aCaption;
380   if ( !aTransformationPoint.GroupItem )
381     aTransformationPoint.GroupItem = createPointItem( aCaption, aColor );
382   aTransformationPoint.GroupItem->setPos( aPoint );
383
384   myTransformationPointMap[ thePointType ] = aTransformationPoint;
385 }
386
387 //================================================================
388 // Function : updateTrsfPoint
389 // Purpose  : 
390 //================================================================
391 void HYDROGUI_PrsImage::updateTrsfPoint( const int thePointType )
392 {
393   // If image is transformed only by two points then the point C is invisible
394   bool anIsPointVisible = myIsTransformationPointPreview && ( 
395     ( !myIsByTwoPoints ) || ( myIsByTwoPoints && ( thePointType != PointC ) ) );
396
397   TransformationPoint& aTransformationPoint = myTransformationPointMap[ thePointType ];
398
399   const QPointF& aPoint = aTransformationPoint.Point;
400   aTransformationPoint.GroupItem->setPos( aPoint );
401   aTransformationPoint.GroupItem->setVisible( anIsPointVisible );
402 }
403
404 //================================================================
405 // Function : getIsByTwoPoints
406 // Purpose  : 
407 //================================================================
408 bool HYDROGUI_PrsImage::getIsByTwoPoints() const
409 {
410   return myIsByTwoPoints;
411 }
412
413 //================================================================
414 // Function : setIsByTwoPoints
415 // Purpose  : 
416 //================================================================
417 void HYDROGUI_PrsImage::setIsByTwoPoints( const bool theIsByTwoPoints )
418 {
419   myIsByTwoPoints = theIsByTwoPoints;
420   if ( myTransformationPointMap.contains( PointC ) )
421   {
422     // Show/hide the point C if necessary
423     updateTrsfPoint( PointC );
424   }
425 }