Salome HOME
Python console has been added for HYDRO module (Bug #22).
[modules/hydro.git] / src / HYDROGUI / HYDROGUI_Tool.cxx
index 5bfab0b2bc63e2f1d55c7b10c58740ebbb83888d..29d053f046bb210563677941224d417d9c8f979d 100644 (file)
 #include "HYDROGUI_Prs.h"
 
 #include <HYDROData_Document.h>
-#include <HYDROData_Image.h>
 #include <HYDROData_Iterator.h>
 
+#include <GraphicsView_Viewer.h>
+
 #include <LightApp_Application.h>
+#include <LightApp_DataOwner.h>
+#include <LightApp_SelectionMgr.h>
 
 #include <QtxWorkstack.h>
 
 #include <SUIT_ViewManager.h>
 #include <SUIT_ViewWindow.h>
 
+#include <QTextCodec>
+#include <QDockWidget>
+
+// Definition of this id allows to use 'latin1' (Qt alias for 'ISO-8859-1')
+// encoding instead of default 'System'
+#define USE_LATIN1_ENCODING
+
+QString HYDROGUI_Tool::ToQString( const TCollection_AsciiString& src )
+{
+#ifdef USE_LATIN1_ENCODING
+  QTextCodec* codec = QTextCodec::codecForName( "latin1" ); // alias for ISO-8859-1
+#else
+  QTextCodec* codec = QTextCodec::codecForLocale();
+#endif
+  QString res;
+  if ( !src.IsEmpty() )
+    res = codec ? codec->toUnicode( (char*)src.ToCString(), src.Length() ) :
+      QString( (char*)src.ToCString() );
+  return res;
+}
+
+QString HYDROGUI_Tool::ToQString( const TCollection_ExtendedString& src )
+{
+  return QString( (QChar*)src.ToExtString(), src.Length() );
+}
+
+QString HYDROGUI_Tool::ToQString( const Handle(TCollection_HAsciiString)& src )
+{
+  if( src.IsNull() )
+    return QString();
+  else
+    return ToQString( src->String() );
+}
+
+QString HYDROGUI_Tool::ToQString( const Handle(TCollection_HExtendedString)& src )
+{
+  if( src.IsNull() )
+    return QString();
+  return ToQString( src->String() );
+}
+
+TCollection_AsciiString HYDROGUI_Tool::ToAsciiString( const QString& src )
+{
+  TCollection_AsciiString res;
+  if( !src.isNull() )
+  {
+#ifdef USE_LATIN1_ENCODING
+    QTextCodec* codec = QTextCodec::codecForName( "latin1" ); // alias for ISO-8859-1
+#else
+    QTextCodec* codec = QTextCodec::codecForLocale();
+#endif
+    if( codec )
+    {
+      QByteArray str = codec->fromUnicode( src );
+      res = TCollection_AsciiString( (Standard_CString)str.constData() );
+    }
+    else
+      res = TCollection_AsciiString( src.toLatin1().data() );
+  }
+  return res;
+}
+
+TCollection_ExtendedString HYDROGUI_Tool::ToExtString( const QString& src )
+{
+  if( src.isEmpty() )
+    return TCollection_ExtendedString();
+
+  Standard_Integer len = src.length();
+  Standard_ExtString extStr = new Standard_ExtCharacter[ ( len + 1 ) * 2 ];
+  memcpy( (void*)extStr, src.unicode(), len * 2 );
+  ((short*)extStr)[ len ] = 0;
+
+  TCollection_ExtendedString trg( extStr );
+  delete [] extStr;
+  return trg;
+}
+
+Handle(TCollection_HAsciiString) HYDROGUI_Tool::ToHAsciiString( const QString& src )
+{
+  return new TCollection_HAsciiString( ToAsciiString( src ) );
+}
+
+Handle(TCollection_HExtendedString) HYDROGUI_Tool::ToHExtString( const QString& src )
+{
+  return new TCollection_HExtendedString( ToExtString( src ) );
+}
+
+void HYDROGUI_Tool::LambertToDouble( const int theDegrees,
+                                     const int theMinutes,
+                                     const double theSeconds,
+                                     double& theCoord )
+{
+  // ouv: check the case of negative degrees
+  theCoord = theDegrees * 3600 + theMinutes * 60 + theSeconds;
+}
+
+void HYDROGUI_Tool::DoubleToLambert( const double theCoord,
+                                     int& theDegrees,
+                                     int& theMinutes,
+                                     double& theSeconds )
+{
+  // ouv: check the case of negative degrees
+  theDegrees = int( theCoord / 3600 );
+
+  double aRemainder = theCoord - theDegrees * 3600;
+  theMinutes = int( aRemainder / 60 );
+
+  theSeconds = aRemainder - theMinutes * 60;
+}
+
+bool HYDROGUI_Tool::IsEqual( const Handle(HYDROData_Object)& theObj1,
+                             const Handle(HYDROData_Object)& theObj2 )
+{
+  if( !theObj1.IsNull() && !theObj2.IsNull() )
+    return theObj1->Label() == theObj2->Label(); //ouv: check that the names can be used here
+  return false;
+}
+
 void HYDROGUI_Tool::SetActiveViewManager( HYDROGUI_Module* theModule,
                                           SUIT_ViewManager* theViewManager )
 {
@@ -50,36 +171,241 @@ void HYDROGUI_Tool::SetActiveViewManager( HYDROGUI_Module* theModule,
           aWorkstack->setActiveWindow( aViewWindow );
 }
 
-void HYDROGUI_Tool::GetPrsSubObjects( const HYDROGUI_DataModel* theGUIModel,
-                                      const int theViewerId, // currently unused
+void HYDROGUI_Tool::GetPrsSubObjects( HYDROGUI_Module* theModule,
                                       HYDROData_SequenceOfObjects& theSeq )
 {
-  if( !theGUIModel )
-    return;
-
-  const int aStudyId = theGUIModel->module()->application()->activeStudy()->id();
-
-  Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( aStudyId );
+  Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( theModule->getStudyId() );
   if( aDocument.IsNull() )
     return;
 
   HYDROData_Iterator anIterator( aDocument, KIND_IMAGE );
   for( ; anIterator.More(); anIterator.Next() )
   {
-    Handle(HYDROData_Image) anImageObj =
-      Handle(HYDROData_Image)::DownCast( anIterator.Current() );
-    if( !anImageObj.IsNull() )
-      theSeq.Append( anImageObj );
+    Handle(HYDROData_Object) anObject = anIterator.Current();
+    if( !anObject.IsNull() )
+      theSeq.Append( anObject );
+  }
+
+  anIterator = HYDROData_Iterator( aDocument, KIND_POLYLINE );
+  for( ; anIterator.More(); anIterator.Next() )
+  {
+    Handle(HYDROData_Object) anObject = anIterator.Current();
+    if( !anObject.IsNull() )
+      theSeq.Append( anObject );
   }
 }
 
 HYDROGUI_Prs* HYDROGUI_Tool::GetPresentation( const Handle(HYDROData_Object)& theObj,
                                               const GraphicsView_ObjectList& theObjects )
 {
-  GraphicsView_ObjectListIterator anIter( theObjects );
-  while( anIter.hasNext() )
-    if( HYDROGUI_Prs* aPrs = dynamic_cast<HYDROGUI_Prs*>( anIter.next() ) )
-      if( aPrs->getObject()->Label() == theObj->Label() )
-        return aPrs;
+  if( !theObj.IsNull() )
+  {
+    GraphicsView_ObjectListIterator anIter( theObjects );
+    while( anIter.hasNext() )
+    {
+      if( HYDROGUI_Prs* aPrs = dynamic_cast<HYDROGUI_Prs*>( anIter.next() ) )
+      {
+        Handle(HYDROData_Object) anObj = aPrs->getObject();
+        if( IsEqual( anObj, theObj ) )
+          return aPrs;
+      }
+    }
+  }
+  return NULL;
+}
+
+GraphicsView_ObjectList HYDROGUI_Tool::GetPrsList( GraphicsView_ViewPort* theViewPort )
+{
+  GraphicsView_ObjectList aList;
+  if( theViewPort )
+  {
+    GraphicsView_ObjectListIterator anIter( theViewPort->getObjects() );
+    while( anIter.hasNext() )
+      if( HYDROGUI_Prs* aPrs = dynamic_cast<HYDROGUI_Prs*>( anIter.next() ) )
+        aList.append( aPrs );
+  }
+  return aList;
+}
+
+HYDROData_SequenceOfObjects HYDROGUI_Tool::GetSelectedObjects( HYDROGUI_Module* theModule )
+{
+  HYDROData_SequenceOfObjects aSeq;
+
+  HYDROGUI_DataModel* aModel = theModule->getDataModel();
+
+  SUIT_SelectionMgr* aSelectionMgr = theModule->getApp()->selectionMgr();
+  SUIT_DataOwnerPtrList anOwners;
+  aSelectionMgr->selected( anOwners );
+
+  QStringList aCollectedNameList; // to avoid duplication
+  foreach( SUIT_DataOwner* aSUITOwner, anOwners )
+  {
+    if( LightApp_DataOwner* anOwner = dynamic_cast<LightApp_DataOwner*>( aSUITOwner ) )
+    {
+      Handle(HYDROData_Object) anObject = aModel->objectByEntry( anOwner->entry() );
+      if( !anObject.IsNull() )
+      {
+        QString aName = anObject->GetName();
+        if( !aCollectedNameList.contains( aName ) )
+        {
+          aSeq.Append( anObject );
+          aCollectedNameList.append( aName );
+        }
+      }
+    }
+  }
+  return aSeq;
+}
+
+Handle(HYDROData_Object) HYDROGUI_Tool::GetSelectedObject( HYDROGUI_Module* theModule )
+{
+  HYDROData_SequenceOfObjects aSeq = GetSelectedObjects( theModule );
+  if( !aSeq.IsEmpty() )
+    return aSeq.First();
   return NULL;
 }
+
+Handle(HYDROData_Object) HYDROGUI_Tool::FindObjectByName( HYDROGUI_Module* theModule,
+                                                          const QString& theName,
+                                                          const ObjectKind theObjectKind )
+{
+  Handle(HYDROData_Object) anObject;
+
+  Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( theModule->getStudyId() );
+  if( aDocument.IsNull() )
+    return anObject;
+
+  HYDROData_Iterator anIter( aDocument, theObjectKind );
+  for( ; anIter.More(); anIter.Next() )
+  {
+    Handle(HYDROData_Object) anObjectRef = anIter.Current();
+    if( !anObjectRef.IsNull() && anObjectRef->GetName() == theName )
+    {
+      anObject = anObjectRef;
+      break;
+    }
+  }
+  return anObject;
+}
+
+QString HYDROGUI_Tool::GenerateObjectName( HYDROGUI_Module* theModule,
+                                           const QString& thePrefix )
+{
+  QString aName;
+  int anId = 1;
+  while( anId < 100 )
+  {
+    aName = QString( "%1_%2" ).arg( thePrefix ).arg( QString::number( anId++ ) );
+
+    // check that there are no other objects with the same name in the document
+    Handle(HYDROData_Object) anObject = FindObjectByName( theModule, aName, KIND_UNKNOWN );
+    if( anObject.IsNull() )
+      break;
+  }
+  return aName;
+}
+
+size_t HYDROGUI_Tool::GetActiveGraphicsViewId( HYDROGUI_Module* theModule )
+{
+  size_t aViewId = 0;
+  SUIT_ViewManager* aViewMgr = theModule->getApp()->activeViewManager();
+  if( !aViewMgr || aViewMgr->getType() != GraphicsView_Viewer::Type() )
+    return aViewId;
+
+  if( SUIT_ViewModel* aViewer = aViewMgr->getViewModel() )
+    aViewId = (size_t)aViewer;
+  return aViewId;
+}
+
+QList<size_t> HYDROGUI_Tool::GetGraphicsViewIdList( HYDROGUI_Module* theModule )
+{
+  QList<size_t> aList;
+  ViewManagerList aViewMgrs;
+  theModule->getApp()->viewManagers( GraphicsView_Viewer::Type(), aViewMgrs );
+  QListIterator<SUIT_ViewManager*> anIter( aViewMgrs );
+  while( anIter.hasNext() )
+  {
+    if( SUIT_ViewManager* aViewMgr = anIter.next() )
+    {
+      if( SUIT_ViewModel* aViewer = aViewMgr->getViewModel() )
+        aList.append( (size_t)aViewer );
+    }
+  }
+  return aList;
+}
+
+void HYDROGUI_Tool::GetObjectReferences( const Handle(HYDROData_Image)& theImage,
+                                         HYDROData_SequenceOfObjects& theRefObjects,
+                                         QStringList& theRefNames )
+{
+  if( theImage.IsNull() )
+    return;
+
+  for( int anIndex = 0, aNbRef = theImage->NbReferences(); anIndex < aNbRef; anIndex++ )
+  {
+    Handle(HYDROData_Object) aRefObj = theImage->Reference( anIndex );
+    if( !aRefObj.IsNull() && !aRefObj->IsRemoved() )
+    {
+      QString aName = aRefObj->GetName();
+      if( !theRefNames.contains( aName ) )
+      {
+        theRefObjects.Append( aRefObj );
+        theRefNames.append( aRefObj->GetName() );
+        if( aRefObj->GetKind() == KIND_IMAGE )
+        {
+          Handle(HYDROData_Image) aRefImage = Handle(HYDROData_Image)::DownCast( aRefObj );
+          if( !aRefImage.IsNull() )
+            GetObjectReferences( aRefImage, theRefObjects, theRefNames );
+        }
+      }
+    }
+  }
+}
+
+void HYDROGUI_Tool::GetObjectBackReferences( HYDROGUI_Module* theModule,
+                                             const Handle(HYDROData_Object)& theObj,
+                                             HYDROData_SequenceOfObjects& theBackRefObjects,
+                                             QStringList& theBackRefNames )
+{
+  if( theObj.IsNull() )
+    return;
+
+  Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( theModule->getStudyId() );
+  if( aDocument.IsNull() )
+    return;
+
+  QString aName = theObj->GetName();
+
+  HYDROData_Iterator anIterator( aDocument, KIND_IMAGE );
+  for( ; anIterator.More(); anIterator.Next() )
+  {
+    Handle(HYDROData_Image) anImage = Handle(HYDROData_Image)::DownCast( anIterator.Current() );
+    if( !anImage.IsNull() )
+    {
+      HYDROData_SequenceOfObjects aRefObjects;
+      QStringList aRefNames;
+      GetObjectReferences( anImage, aRefObjects, aRefNames );
+      if( aRefNames.contains( aName ) )
+      {
+        theBackRefObjects.Append( anImage );
+        theBackRefNames.append( anImage->GetName() );
+      }
+    }
+  }
+}
+
+
+QDockWidget* HYDROGUI_Tool::WindowDock( QWidget* wid )
+{
+  if ( !wid )
+    return 0;
+
+  QDockWidget* dock = 0;
+  QWidget* w = wid->parentWidget();
+  while ( w && !dock )
+  {
+    dock = ::qobject_cast<QDockWidget*>( w );
+    w = w->parentWidget();
+  }
+  return dock;
+}