Salome HOME
Remove functionality by popup menu and the keyboard button "Delete".
[modules/hydro.git] / src / HYDROGUI / HYDROGUI_Shape.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_Shape.h"
24
25 #include "HYDROGUI_DataObject.h"
26 #include "HYDROGUI_Tool.h"
27
28 #include <AIS_Drawer.hxx>
29 #include <AIS_TexturedShape.hxx>
30
31 #include <BRepBuilderAPI_MakeEdge.hxx>
32 #include <BRepBuilderAPI_MakeWire.hxx>
33 #include <BRepBuilderAPI_MakeFace.hxx>
34
35 #include <gp_Pnt.hxx>
36
37 #include <Graphic3d_AspectFillArea3d.hxx>
38 #include <Graphic3d_MaterialAspect.hxx>
39
40 #include <HYDROData_Document.h>
41 #include <HYDROData_Image.h>
42 #include <HYDROData_ImmersibleZone.h>
43 #include <HYDROData_Polyline.h>
44 #include <HYDROData_Region.h>
45 #include <HYDROData_Zone.h>
46 #include <HYDROData_Obstacle.h>
47
48 #include <TopoDS.hxx>
49 #include <TopoDS_Wire.hxx>
50 #include <TopoDS_Face.hxx>
51
52 #include <Precision.hxx>
53
54 #include <Prs3d_ShadingAspect.hxx>
55 #include <Prs3d_LineAspect.hxx>
56 #include <Prs3d_IsoAspect.hxx>
57
58 #include <QColor>
59 #include <QFile>
60
61 HYDROGUI_Shape::HYDROGUI_Shape( const Handle(AIS_InteractiveContext)& theContext,
62                                 const Handle(HYDROData_Entity)&       theObject )
63 : myContext( theContext ),
64   myObject( theObject ),
65   myIsHighlight( false ),
66   myFillingColor( Qt::transparent ),
67   myBorderColor( Qt::black ),
68   myHighlightColor( Qt::white ),
69   myIsToUpdate( false ),
70   myIsVisible( true ),
71   myDisplayMode( AIS_WireFrame )
72 {
73 }
74
75 HYDROGUI_Shape::~HYDROGUI_Shape()
76 {
77   erase();
78
79   if ( !myShape.IsNull() )
80     myShape.Nullify();
81
82   removeTextureFile();
83 }
84
85 void HYDROGUI_Shape::display( const bool theIsUpdateViewer )
86 {
87   if ( myContext.IsNull() || myShape.IsNull() || !isVisible() )
88     return;
89
90   myContext->Display( myShape, theIsUpdateViewer );
91 }
92
93 void HYDROGUI_Shape::erase( const bool theIsUpdateViewer )
94 {
95   if ( myContext.IsNull() || myShape.IsNull() )
96     return;
97
98   myContext->Erase( myShape, theIsUpdateViewer );
99 }
100
101 void HYDROGUI_Shape::update( const bool theIsUpdateViewer )
102 {
103   setIsToUpdate( false );
104
105   if ( myContext.IsNull() )
106     return;
107
108   // Try to retrieve information from object
109   if ( !myObject.IsNull() )
110   {
111     Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( myObject->Label() );
112   
113     if ( myObject->IsKind( STANDARD_TYPE(HYDROData_ImmersibleZone) ) )
114     {
115       Handle(HYDROData_ImmersibleZone) aZoneObj =
116         Handle(HYDROData_ImmersibleZone)::DownCast( myObject );
117
118       QColor aFillingColor = aZoneObj->GetFillingColor();
119       QColor aBorderColor = aZoneObj->GetBorderColor();
120       TopoDS_Face aZoneFace = TopoDS::Face( aZoneObj->GetTopShape() );
121
122       setFillingColor( aFillingColor, false, false );
123       setBorderColor( aBorderColor, false, false );
124       setFace( aZoneFace, false, false );
125     }
126     else if ( myObject->IsKind( STANDARD_TYPE(HYDROData_Polyline) ) )
127     {
128       Handle(HYDROData_Polyline) aPolyline =
129         Handle(HYDROData_Polyline)::DownCast( myObject );
130
131       TopoDS_Wire aPolylineWire = TopoDS::Wire( aPolyline->GetTopShape() );
132
133       setWire( aPolylineWire, false, false );
134     }
135     else if ( myObject->IsKind( STANDARD_TYPE(HYDROData_Zone) ) )
136     {
137       Handle(HYDROData_Zone) aZone =
138         Handle(HYDROData_Zone)::DownCast( myObject );
139
140       TopoDS_Face aZoneFace = TopoDS::Face( aZone->GetShape() );
141
142       // Generate the filling color for zone
143       QStringList aGeomObjectsNames;
144
145       HYDROData_SequenceOfObjects aRefObjects = aZone->GetGeometryObjects();
146       HYDROData_SequenceOfObjects::Iterator anIter( aRefObjects );
147       for ( ; anIter.More(); anIter.Next() )
148       {
149         Handle(HYDROData_Object) aRefbject = 
150           Handle(HYDROData_Object)::DownCast( anIter.Value() );
151         if ( aRefbject.IsNull() )
152           continue;
153
154         QString aRefObjectName = aRefbject->GetName();
155         if ( aRefObjectName.isEmpty() )
156           continue;
157
158         aGeomObjectsNames.append( aRefObjectName );
159       }
160        
161       setFace( aZoneFace, false, false );
162       if (aZone->IsMergingNeed() && aZone->GetMergeType() == HYDROData_Zone::Merge_UNKNOWN )
163       {
164         // Red color for a zone with bathymetry conflict
165         setFillingColor( Qt::red );
166       }
167       else
168       {
169         setFillingColor( HYDROGUI_Tool::GenerateFillingColor( aDocument, aGeomObjectsNames ) );
170       }
171     }
172     else if ( myObject->IsKind( STANDARD_TYPE(HYDROData_Image) ) )
173     {
174       Handle(HYDROData_Image) anImageObj =
175         Handle(HYDROData_Image)::DownCast( myObject );
176
177       removeTextureFile();
178
179       QString aTextureFileName = generateTextureFileName( anImageObj );
180
181       QImage anImage = anImageObj->Image();
182       QString aFilePath = anImageObj->GetFilePath();
183       QTransform aTrsf = anImageObj->Trsf();
184
185       int aWidth = anImage.width();
186       int aHeight = anImage.height();
187
188       QTransform anInversion = QTransform::fromScale( -1, -1 );
189       anImage = anImage.transformed( anInversion * aTrsf, Qt::SmoothTransformation );
190
191       // temporary optimization, to reduce the saved image size (and the texture quality)
192       QImage anImageToSave = reduceTexture( anImage, 500 );
193       anImageToSave.save( aTextureFileName );
194
195       QPointF aPoint1( 0, 0 );
196       QPointF aPoint2( aWidth, 0 );
197       QPointF aPoint3( aWidth, aHeight );
198       QPointF aPoint4( 0, aHeight );
199
200       aPoint1 = aTrsf.map( aPoint1 );
201       aPoint2 = aTrsf.map( aPoint2 );
202       aPoint3 = aTrsf.map( aPoint3 );
203       aPoint4 = aTrsf.map( aPoint4 );
204
205       QPolygonF aPolygon = QPolygonF() << aPoint1 << aPoint2 << aPoint3 << aPoint4;
206       QRectF aRect = aPolygon.boundingRect();
207
208       gp_Pnt aPnt1( aRect.topLeft().x(), aRect.topLeft().y(), 0 );
209       gp_Pnt aPnt2( aRect.topRight().x(), aRect.topRight().y(), 0 );
210       gp_Pnt aPnt3( aRect.bottomRight().x(), aRect.bottomRight().y(), 0 );
211       gp_Pnt aPnt4( aRect.bottomLeft().x(), aRect.bottomLeft().y(), 0 );
212
213       TopoDS_Edge anEdge1 = BRepBuilderAPI_MakeEdge( aPnt1, aPnt2 ).Edge();
214       TopoDS_Edge anEdge2 = BRepBuilderAPI_MakeEdge( aPnt2, aPnt3 ).Edge();
215       TopoDS_Edge anEdge3 = BRepBuilderAPI_MakeEdge( aPnt3, aPnt4 ).Edge();
216       TopoDS_Edge anEdge4 = BRepBuilderAPI_MakeEdge( aPnt4, aPnt1 ).Edge();
217
218       TopoDS_Wire aWire = BRepBuilderAPI_MakeWire( anEdge1, anEdge2, anEdge3, anEdge4 ).Wire();
219
220       setTextureFileName( aTextureFileName, false, false );
221       setFace( aWire, false, false );
222     }
223     else if ( myObject->IsKind( STANDARD_TYPE(HYDROData_Obstacle) ) )
224     {
225       Handle(HYDROData_Obstacle) anObstacle =
226         Handle(HYDROData_Obstacle)::DownCast( myObject );
227
228       //TODO BEGIN of the block of code to be reimplemented
229       //TODO GetTopShape() to be used in future
230       myTopoShape = anObstacle->GetShape3D();
231       myDisplayMode = AIS_Shaded;
232
233       QColor aFillingColor = anObstacle->GetFillingColor();
234       QColor aBorderColor = anObstacle->GetBorderColor();
235
236       setFillingColor( aFillingColor, false, false );
237       setBorderColor( aBorderColor, false, false );
238
239       buildShape();
240       updateShape( false, false );
241       //TODO END of the block of code to be reimplemented
242     }
243   }
244  
245   if ( myShape.IsNull() || !isVisible() )
246     return;
247
248   myContext->Display( myShape, theIsUpdateViewer );
249 }
250
251 void HYDROGUI_Shape::setVisible( const bool theState,
252                                  const bool theIsUpdateViewer )
253 {
254   if ( myIsVisible == theState )
255     return;
256
257   myIsVisible = theState;
258
259   if ( myShape.IsNull() )
260     return;
261
262   if ( myIsVisible )
263     myContext->Display( myShape, theIsUpdateViewer );
264   else
265     myContext->Erase( myShape, theIsUpdateViewer );
266 }
267
268 void HYDROGUI_Shape::highlight( bool theIsHighlight )
269 {
270   if ( myIsHighlight == theIsHighlight )
271     return;
272
273   myIsHighlight = theIsHighlight;
274
275   if ( myContext.IsNull() || myShape.IsNull() )
276     return;
277
278   colorShapeBorder( getActiveColor() );
279   myContext->Display( myShape );
280 }
281
282 bool HYDROGUI_Shape::isHighlighted() const
283 {
284   return myIsHighlight;
285 }
286
287 void HYDROGUI_Shape::setWire( const TopoDS_Wire& theWire,
288                               const bool         theToDisplay,
289                               const bool         theIsUpdateViewer )
290 {
291   myTopoShape = theWire;
292   myDisplayMode = AIS_WireFrame;
293
294   buildShape();
295   updateShape( theToDisplay, theIsUpdateViewer );
296 }
297
298 void HYDROGUI_Shape::setFace( const TopoDS_Wire& theWire,
299                               const bool         theToDisplay,
300                               const bool         theIsUpdateViewer )
301 {
302   BRepBuilderAPI_MakeFace aFaceBuilder( theWire, Standard_True );
303   aFaceBuilder.Build();
304   if( aFaceBuilder.IsDone() )
305   {
306     TopoDS_Face aFace = aFaceBuilder.Face();
307     setFace( aFace, theToDisplay, theIsUpdateViewer );
308   }
309 }
310
311 void HYDROGUI_Shape::setFace( const TopoDS_Face& theFace,
312                               const bool         theToDisplay,
313                               const bool         theIsUpdateViewer )
314 {
315   myTopoShape = theFace;
316   myDisplayMode = myTextureFileName.isEmpty() ? AIS_Shaded : AIS_Shaded+2;
317   //Note: AIS_Shaded+2 is the same as AIS_ExactHLR
318   //TODO: it would be more suitable to use TexturedShape mode from GEOM_AISShape
319
320   buildShape();
321   updateShape( theToDisplay, theIsUpdateViewer );
322 }
323
324 void HYDROGUI_Shape::setFillingColor( const QColor& theColor,
325                                       const bool    theToDisplay,
326                                       const bool    theIsUpdateViewer )
327 {
328   myFillingColor = theColor;
329   updateShape( theToDisplay, theIsUpdateViewer );
330 }
331
332 QColor HYDROGUI_Shape::getFillingColor() const
333 {
334   return myFillingColor;
335 }
336
337 void HYDROGUI_Shape::setBorderColor( const QColor& theColor,
338                                      const bool    theToDisplay,
339                                      const bool    theIsUpdateViewer )
340 {
341   myBorderColor = theColor;
342   updateShape( theToDisplay, theIsUpdateViewer );
343 }
344
345 QColor HYDROGUI_Shape::getBorderColor() const
346 {
347   return myBorderColor;
348 }
349
350 void HYDROGUI_Shape::setHighlightColor( const QColor& theColor )
351 {
352   myHighlightColor = theColor;
353 }
354
355 QColor HYDROGUI_Shape::getHighlightColor() const
356 {
357   return myHighlightColor;
358 }
359
360 void HYDROGUI_Shape::setTextureFileName( const QString& theFileName,
361                                          const bool     theToDisplay,
362                                          const bool     theIsUpdateViewer )
363 {
364   myTextureFileName = theFileName;
365   updateShape( theToDisplay, theIsUpdateViewer );
366 }
367
368 QString HYDROGUI_Shape::getTextureFileName() const
369 {
370   return myTextureFileName;
371 }
372
373 void HYDROGUI_Shape::buildShape()
374 {
375   // Erase previously created shape
376   erase();
377
378   if ( myTopoShape.IsNull() )
379     return;
380
381   QString aTextureFileName = getTextureFileName();
382   bool anIsTexture = !aTextureFileName.isEmpty();
383
384   if ( anIsTexture )
385   {
386     myShape = new AIS_TexturedShape( myTopoShape );
387   }
388   else
389   {
390     myShape = new AIS_Shape( myTopoShape );
391   }
392
393   if ( !myObject.IsNull() )
394     myShape->SetOwner( myObject );
395
396   myShape->SetTransparency( 0 );
397   myShape->SetDisplayMode( (AIS_DisplayMode)myDisplayMode );
398
399   if( anIsTexture )
400   {
401     Handle(AIS_TexturedShape) aTexturedShape = 
402       Handle(AIS_TexturedShape)::DownCast( myShape );
403
404     aTexturedShape->SetTextureFileName( HYDROGUI_Tool::ToAsciiString( aTextureFileName ) );
405     aTexturedShape->SetTextureMapOn();
406     aTexturedShape->DisableTextureModulate();
407   }
408
409     // Init default params for shape
410   const Handle(AIS_Drawer)& anAttributes = myShape->Attributes();
411   if ( !anAttributes.IsNull() )
412   {
413     if ( myDisplayMode == AIS_Shaded )
414     {
415       Handle(Prs3d_IsoAspect) anIsoAspect = anAttributes->UIsoAspect();
416       if ( !anIsoAspect.IsNull() )
417         anIsoAspect->SetNumber( 0 );
418       
419       anIsoAspect = anAttributes->VIsoAspect();
420       if ( !anIsoAspect.IsNull() )
421         anIsoAspect->SetNumber( 0 );
422
423       Handle(Prs3d_ShadingAspect) aShadingAspect = anAttributes->ShadingAspect();
424       if ( !aShadingAspect.IsNull() )
425       {
426         Graphic3d_MaterialAspect aMatAspect;
427         aMatAspect.SetAmbient( 1 );
428         aMatAspect.SetDiffuse( 0 );
429
430         aShadingAspect->Aspect()->SetFrontMaterial( aMatAspect );
431         aShadingAspect->Aspect()->SetBackMaterial( aMatAspect );
432       }
433     }
434     else if ( myDisplayMode == AIS_WireFrame )
435     {
436       anAttributes->SetWireDraw( true );
437     }
438   }
439 }
440
441 void HYDROGUI_Shape::updateShape( const bool theToDisplay,
442                                   const bool theIsUpdateViewer )
443 {
444   if ( myShape.IsNull() )
445     return;
446
447   const Handle(AIS_Drawer)& anAttributes = myShape->Attributes();
448   if ( !anAttributes.IsNull() )
449   {
450     if ( myDisplayMode == AIS_Shaded )
451     {
452       // Coloring face filling
453       Handle(Prs3d_ShadingAspect) aShadingAspect = anAttributes->ShadingAspect();
454       if ( !aShadingAspect.IsNull() )
455       {
456         Quantity_Color aFillingColor( getQuantityColorVal( myFillingColor.red() ), 
457                                       getQuantityColorVal( myFillingColor.green() ),
458                                       getQuantityColorVal( myFillingColor.blue() ),
459                                       Quantity_TOC_RGB );
460
461         aShadingAspect->SetColor( aFillingColor );
462         aShadingAspect->SetTransparency( 1 - getQuantityColorVal( myFillingColor.alpha() ) );
463       }
464     }
465     else if ( myDisplayMode == AIS_WireFrame )
466     {
467     }
468
469     // Coloring borders
470     colorShapeBorder( getActiveColor() );
471   }
472
473   if ( !theToDisplay || !isVisible() || myContext.IsNull() )
474     return;
475   
476   myContext->Display( myShape, theIsUpdateViewer );
477 }
478
479 QColor HYDROGUI_Shape::getActiveColor() const
480 {
481   return isHighlighted() ? myHighlightColor : myBorderColor;
482 }
483
484 double HYDROGUI_Shape::getQuantityColorVal( const int theColorVal )
485 {
486   return theColorVal == 0 ? 0 : ( (double)theColorVal / 255 );
487 }
488
489 void HYDROGUI_Shape::colorShapeBorder( const QColor& theColor )
490 {
491   if ( myShape.IsNull() )
492     return;
493
494   const Handle(AIS_Drawer)& anAttributes = myShape->Attributes();
495   if ( anAttributes.IsNull() )
496     return;
497
498   Quantity_Color aBorderColor( getQuantityColorVal( theColor.red() ), 
499                                getQuantityColorVal( theColor.green() ),
500                                getQuantityColorVal( theColor.blue() ),
501                                Quantity_TOC_RGB );
502   if ( myDisplayMode == AIS_Shaded )
503   {
504     if ( theColor.alpha() == 0 )
505     {
506       anAttributes->SetFaceBoundaryDraw( false );
507     }
508     else
509     {
510       anAttributes->SetFaceBoundaryDraw( true );
511
512       Handle(Prs3d_LineAspect) aBoundaryAspect = anAttributes->FaceBoundaryAspect();
513       if ( !aBoundaryAspect.IsNull() )
514         aBoundaryAspect->SetColor( aBorderColor );
515     }
516   }
517   else if ( myDisplayMode == AIS_WireFrame )
518   {
519     myShape->SetColor( aBorderColor );
520   }
521 }
522
523 QString HYDROGUI_Shape::generateTextureFileName( const Handle(HYDROData_Entity)& theImageObj )
524 {
525   QString aResult;
526   if( !theImageObj.IsNull() )
527   {
528     QString aTempDir = HYDROGUI_Tool::GetTempDir( true );
529
530     int aStudyId = HYDROGUI_Tool::GetActiveStudyId();
531     QString aPrefix = QString( "image_%1" ).arg( aStudyId );
532
533     QString anEntry = HYDROGUI_DataObject::dataObjectEntry( theImageObj, false );
534     anEntry.replace( ':', '_' );
535
536     QString anExtension = "bmp";
537
538     aResult = QString( "%1/%2_%3.%4" ).arg( aTempDir, aPrefix, anEntry, anExtension );
539   }
540   return aResult;
541 }
542
543 void HYDROGUI_Shape::removeTextureFile() const
544 {
545   QFile aFile( getTextureFileName() );
546   if( aFile.exists() )
547     aFile.remove();
548 }
549
550 QImage HYDROGUI_Shape::reduceTexture( const QImage& theImage, const int theSizeLimit )
551 {
552   double aSizeLimit = (double)theSizeLimit;
553   double aWidth = (double)theImage.width();
554   double aHeight = (double)theImage.height();
555   if( aWidth > aSizeLimit || aHeight > aSizeLimit )
556   {
557     if( aWidth > aHeight )
558     {
559       aHeight /= ( aWidth / aSizeLimit );
560       aWidth = aSizeLimit;
561     }
562     else
563     {
564       aWidth /= ( aHeight / aSizeLimit );
565       aHeight = aSizeLimit;
566     }
567   }
568   return theImage.scaled( aWidth, aHeight, Qt::KeepAspectRatio, Qt::SmoothTransformation );
569 }