Salome HOME
refs #568: use ordered list view with selection synchronized with object browser...
[modules/hydro.git] / src / HYDROGUI / HYDROGUI_ShapeImage.cxx
index 8e54e9839605f1c58f07ee249e555a657b9927ed..3d5199656b4476fdd4db7a80643ff37f836e7209 100644 (file)
@@ -1,8 +1,4 @@
-// Copyright (C) 2007-2015  CEA/DEN, EDF R&D, OPEN CASCADE
-//
-// Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
-// CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
-//
+// Copyright (C) 2014-2015  EDF-R&D
 // This library is free software; you can redistribute it and/or
 // modify it under the terms of the GNU Lesser General Public
 // License as published by the Free Software Foundation; either
 //
 
 #include <HYDROGUI_ShapeImage.h>
-#include <HYDROGUI_DataObject.h>
+
 #include <HYDROGUI_Tool.h>
-#include <HYDROData_Document.h>
+#include <HYDROGUI_ImagePrs.h>
+
 #include <HYDROData_Image.h>
+#include <HYDROData_Document.h>
 
-#include <AIS_TexturedShape.hxx>
-#include <BRepBuilderAPI_MakeEdge.hxx>
-#include <BRepBuilderAPI_MakeWire.hxx>
 #include <gp_Pnt.hxx>
-#include <TopoDS_Edge.hxx>
-#include <TopoDS_Wire.hxx>
-
-#include <SUIT_MessageBox.h>
-#include <QFile>
 
 HYDROGUI_ShapeImage::HYDROGUI_ShapeImage( const Handle(AIS_InteractiveContext)& theContext,
                                           const Handle_HYDROData_Image&         theImage,
@@ -45,181 +35,71 @@ HYDROGUI_ShapeImage::HYDROGUI_ShapeImage( const Handle(AIS_InteractiveContext)&
 
 HYDROGUI_ShapeImage::~HYDROGUI_ShapeImage()
 {
-  removeTextureFile();
 }
 
 void HYDROGUI_ShapeImage::update( bool theIsUpdateViewer, bool isDeactivateSelection )
 {
   setIsToUpdate( false );
 
-  // Try to retrieve information from object
-  Handle(HYDROData_Image) anImageObj = Handle(HYDROData_Image)::DownCast( getObject() );
-
-  if( !anImageObj.IsNull() )
-  {
-    removeTextureFile();
-
-    QString aTextureFileName = generateTextureFileName( anImageObj );
-
-    QImage anImage = anImageObj->Image();
-    QString aFilePath = anImageObj->GetFilePath();
-    QTransform aTrsf = anImageObj->Trsf();
-
-    int aWidth = anImage.width();
-    int aHeight = anImage.height();
-
-    QString anImageError = "";
-
-    QTransform anInversion = QTransform::fromScale( -1, -1 );
-    anImage = anImage.transformed( anInversion * aTrsf, Qt::SmoothTransformation );
-
-    if ( anImage.isNull() )
-      anImageError = QObject::tr( "IMAGE_TRANSFORMATION_CAN_NOT_BE_APPLYED" );
-    else
-    {
-      // Workaround: Scale the texture image to the nearest width multiple 4 due to the CASCADE bug 23813
-      int aTrsfWidth = anImage.width();
-      int aDelta = aTrsfWidth % 4;
-      if ( aDelta > 0 )
-      {
-        aTrsfWidth += ( 4 - aDelta );
-      }
-      anImage = anImage.scaledToWidth( aTrsfWidth );
-
-      // temporary optimization, to reduce the saved image size (and the texture quality)
-      QImage anImageToSave = anImage; //RKV:reduceTexture( anImage, 500 );
-
-      bool isSaved = anImageToSave.save( aTextureFileName );
-      if ( !isSaved )
-        anImageError = QObject::tr( "FILE_CAN_NOT_BE_CREATED" ).arg( aTextureFileName );
-      else
-        QFile::setPermissions( aTextureFileName, (QFile::Permissions)0x4FFFF );
-    }
-
-    if ( !anImageError.isEmpty() )
-    {
-      SUIT_MessageBox::warning( 0, QObject::tr( "SHAPE_IMAGE_ERROR" ),
-                                QObject::tr( "IMAGE_CAN_NOT_BE_CREATED" ) + anImageError );
-    }
+  buildShape();
 
-    QPointF aPoint1( 0, 0 );            // 1: top left
-    QPointF aPoint2( aWidth, 0 );       // 2: top right
-    QPointF aPoint3( aWidth, aHeight ); // 3: bottom right
-    QPointF aPoint4( 0, aHeight );      // 4: bottom left
-
-    aPoint1 = aTrsf.map( aPoint1 );
-    aPoint2 = aTrsf.map( aPoint2 );
-    aPoint3 = aTrsf.map( aPoint3 );
-    aPoint4 = aTrsf.map( aPoint4 );
-
-    QPolygonF aPolygon = QPolygonF() << aPoint1 << aPoint2 << aPoint3 << aPoint4;
-    QRectF aRect = aPolygon.boundingRect();
-
-    gp_Pnt aPnt1( aRect.topLeft().x(), aRect.topLeft().y(), 0 );
-    gp_Pnt aPnt2( aRect.topRight().x(), aRect.topRight().y(), 0 );
-    gp_Pnt aPnt3( aRect.bottomRight().x(), aRect.bottomRight().y(), 0 );
-    gp_Pnt aPnt4( aRect.bottomLeft().x(), aRect.bottomLeft().y(), 0 );
-
-    Handle_HYDROData_Document aDoc = HYDROData_Document::Document( anImageObj->Label() );
-    aDoc->Transform( aPnt1, true );
-    aDoc->Transform( aPnt2, true );
-    aDoc->Transform( aPnt3, true );
-    aDoc->Transform( aPnt4, true );
-
-    TopoDS_Edge anEdge1 = BRepBuilderAPI_MakeEdge( aPnt1, aPnt2 ).Edge();
-    TopoDS_Edge anEdge2 = BRepBuilderAPI_MakeEdge( aPnt2, aPnt3 ).Edge();
-    TopoDS_Edge anEdge3 = BRepBuilderAPI_MakeEdge( aPnt3, aPnt4 ).Edge();
-    TopoDS_Edge anEdge4 = BRepBuilderAPI_MakeEdge( aPnt4, aPnt1 ).Edge();
-
-    TopoDS_Wire aWire = BRepBuilderAPI_MakeWire( anEdge1, anEdge2, anEdge3, anEdge4 ).Wire();
-    aWire.Closed( true );
-
-    setTextureFileName( aTextureFileName, false, false );
-    setFace( aWire, false, false, aTextureFileName );
-    isDeactivateSelection = true;
-  }
   HYDROGUI_Shape::update( theIsUpdateViewer, isDeactivateSelection );
 }
 
-void HYDROGUI_ShapeImage::setTextureFileName( const QString& theFileName,
-                                              const bool     theToDisplay,
-                                              const bool     theIsUpdateViewer )
+Handle(AIS_InteractiveObject) HYDROGUI_ShapeImage::createShape() const
 {
-  myTextureFileName = theFileName;
-  updateShape( theToDisplay, theIsUpdateViewer );
-}
-
-QString HYDROGUI_ShapeImage::getTextureFileName() const
-{
-  return myTextureFileName;
-}
-
-QString HYDROGUI_ShapeImage::generateTextureFileName( const Handle(HYDROData_Entity)& theImageObj )
-{
-  QString aResult;
-  if( !theImageObj.IsNull() )
-  {
-    QString aTempDir = HYDROGUI_Tool::GetTempDir( true );
-
-    int aStudyId = HYDROGUI_Tool::GetActiveStudyId();
-    QString aPrefix = QString( "image_%1" ).arg( aStudyId );
-
-    QString anEntry = HYDROGUI_DataObject::dataObjectEntry( theImageObj, false );
-    anEntry.replace( ':', '_' );
+    Handle(HYDROData_Image) anImageObj = Handle(HYDROData_Image)::DownCast( getObject() );
+    if ( anImageObj.IsNull() )
+        return Handle_AIS_InteractiveObject();
 
-    QString anExtension = "bmp";
-
-    aResult = QString( "%1/%2_%3.%4" ).arg( aTempDir, aPrefix, anEntry, anExtension );
-  }
-  return aResult;
+    Handle(HYDROGUI_ImagePrs) aPrs = new HYDROGUI_ImagePrs( imagePixMap( anImageObj ), imageContour( anImageObj ) );
+    return aPrs;
 }
 
-void HYDROGUI_ShapeImage::removeTextureFile() const
+Handle(Image_PixMap) HYDROGUI_ShapeImage::imagePixMap( const Handle(HYDROData_Image)& theImageObj ) const
 {
-  QFile aFile( getTextureFileName() );
-  if( aFile.exists() )
-    aFile.remove();
-}
-
-QImage HYDROGUI_ShapeImage::reduceTexture( const QImage& theImage, const int theSizeLimit )
-{
-  double aSizeLimit = (double)theSizeLimit;
-  double aWidth = (double)theImage.width();
-  double aHeight = (double)theImage.height();
-  if( aWidth > aSizeLimit || aHeight > aSizeLimit )
-  {
-    if( aWidth > aHeight )
+    Handle(Image_PixMap) aPix;
+    if ( !theImageObj.IsNull() )
     {
-      aHeight /= ( aWidth / aSizeLimit );
-      aWidth = aSizeLimit;
+        QTransform aTrsf = theImageObj->Trsf();
+        QImage anImage = theImageObj->Image();
+//        anImage = anImage.transformed( QTransform::fromScale( -1, -1 ) * aTrsf, Qt::SmoothTransformation );
+
+        if ( !anImage.isNull() )
+        {
+            // Workaround: Scale the texture image to the nearest width multiple 4 due to the CASCADE bug 23813
+            int aTrsfWidth = anImage.width();
+            int aDelta = aTrsfWidth % 4;
+            if ( aDelta > 0 )
+                aTrsfWidth += ( 4 - aDelta );
+      
+            anImage = anImage.scaledToWidth( aTrsfWidth );
+
+            aPix = HYDROGUI_Tool::Pixmap( anImage );
+        }
     }
-    else
-    {
-      aWidth /= ( aHeight / aSizeLimit );
-      aHeight = aSizeLimit;
-    }
-  }
-  return theImage.scaled( aWidth, aHeight, Qt::KeepAspectRatio, Qt::SmoothTransformation );
+    return aPix;
 }
 
-Handle_AIS_InteractiveObject HYDROGUI_ShapeImage::createShape() const
+QPolygonF HYDROGUI_ShapeImage::imageContour( const Handle_HYDROData_Image& theImageObj ) const
 {
-  QString aTextureFileName = getTextureFileName();
-  bool anIsTexture = !aTextureFileName.isEmpty();
-  if ( anIsTexture )
-  {
-    Handle_AIS_TexturedShape aTexturedShape = new AIS_TexturedShape( getTopoShape() );
-
-    aTexturedShape->SetTextureFileName( HYDROGUI_Tool::ToAsciiString( aTextureFileName ) );
-    aTexturedShape->SetTextureMapOn();
-    // Just use the texture image as is
-    aTexturedShape->DisableTextureModulate();
-    aTexturedShape->SetTextureRepeat( false ); // don't repeat the texture image on the face
-
-    return aTexturedShape;
-  }
-  else
-    return Handle_AIS_InteractiveObject();
+    QPolygonF aContour;
+    if ( !theImageObj.IsNull() )
+    {
+        QTransform aTrsf = theImageObj->Trsf();
+        QImage anImage = theImageObj->Image();
+
+        QPolygonF aPolygon = QPolygonF() << QPointF( 0, 0 ) << QPointF( anImage.width(), 0 ) 
+                                         << QPointF( anImage.width(), anImage.height() ) << QPointF( 0, anImage.height() );
+        aPolygon = aTrsf.map( aPolygon );
+
+        Handle(HYDROData_Document) aDoc = HYDROData_Document::Document( theImageObj->Label() );
+        for ( QPolygonF::iterator it = aPolygon.begin(); it != aPolygon.end(); ++it )
+        {
+            gp_Pnt aPnt( (*it).x(), (*it).y(), 0 );
+            aDoc->Transform( aPnt, true );
+            aContour.append( QPointF( aPnt.X(), aPnt.Y() ) );
+        }
+    }
+    return aContour;
 }
-