1 // Copyright (C) 2007-2013 CEA/DEN, EDF R&D, OPEN CASCADE
3 // Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
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.
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.
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
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
23 #include "HYDROGUI_Tool.h"
25 #include "HYDROGUI_DataModel.h"
26 #include "HYDROGUI_DataObject.h"
27 #include "HYDROGUI_Module.h"
28 #include "HYDROGUI_Prs.h"
30 #include <HYDROData_Document.h>
31 #include <HYDROData_Iterator.h>
32 #include <HYDROData_ImmersibleZone.h>
33 #include <HYDROData_Tool.h>
37 #include <GraphicsView_Viewer.h>
39 #include <SalomeApp_Study.h>
41 #include <LightApp_Application.h>
42 #include <LightApp_DataOwner.h>
43 #include <LightApp_SelectionMgr.h>
45 #include <OCCViewer_ViewModel.h>
46 #include <OCCViewer_ViewFrame.h>
48 #include <QtxWorkstack.h>
49 #include <QtxActionToolMgr.h>
51 #include <STD_TabDesktop.h>
53 #include <SUIT_Session.h>
54 #include <SUIT_Study.h>
55 #include <SUIT_ViewManager.h>
56 #include <SUIT_ViewWindow.h>
58 #include <SALOMEDSClient.hxx>
60 #include <SVTK_ViewModel.h>
64 #include <QDockWidget>
67 // Definition of this id allows to use 'latin1' (Qt alias for 'ISO-8859-1')
68 // encoding instead of default 'System'
69 #define USE_LATIN1_ENCODING
71 // #define DEB_GROUPS 1
73 QString HYDROGUI_Tool::ToQString( const TCollection_AsciiString& src )
75 #ifdef USE_LATIN1_ENCODING
76 QTextCodec* codec = QTextCodec::codecForName( "latin1" ); // alias for ISO-8859-1
78 QTextCodec* codec = QTextCodec::codecForLocale();
82 res = codec ? codec->toUnicode( (char*)src.ToCString(), src.Length() ) :
83 QString( (char*)src.ToCString() );
87 QString HYDROGUI_Tool::ToQString( const TCollection_ExtendedString& src )
89 return QString( (QChar*)src.ToExtString(), src.Length() );
92 QString HYDROGUI_Tool::ToQString( const Handle(TCollection_HAsciiString)& src )
97 return ToQString( src->String() );
100 QString HYDROGUI_Tool::ToQString( const Handle(TCollection_HExtendedString)& src )
104 return ToQString( src->String() );
107 TCollection_AsciiString HYDROGUI_Tool::ToAsciiString( const QString& src )
109 TCollection_AsciiString res;
112 #ifdef USE_LATIN1_ENCODING
113 QTextCodec* codec = QTextCodec::codecForName( "latin1" ); // alias for ISO-8859-1
115 QTextCodec* codec = QTextCodec::codecForLocale();
119 QByteArray str = codec->fromUnicode( src );
120 res = TCollection_AsciiString( (Standard_CString)str.constData() );
123 res = TCollection_AsciiString( src.toLatin1().data() );
128 TCollection_ExtendedString HYDROGUI_Tool::ToExtString( const QString& src )
131 return TCollection_ExtendedString();
133 Standard_Integer len = src.length();
134 Standard_ExtString extStr = new Standard_ExtCharacter[ ( len + 1 ) * 2 ];
135 memcpy( (void*)extStr, src.unicode(), len * 2 );
136 ((short*)extStr)[ len ] = 0;
138 TCollection_ExtendedString trg( extStr );
143 Handle(TCollection_HAsciiString) HYDROGUI_Tool::ToHAsciiString( const QString& src )
145 return new TCollection_HAsciiString( ToAsciiString( src ) );
148 Handle(TCollection_HExtendedString) HYDROGUI_Tool::ToHExtString( const QString& src )
150 return new TCollection_HExtendedString( ToExtString( src ) );
153 int HYDROGUI_Tool::GetActiveStudyId()
155 if( SUIT_Session* aSession = SUIT_Session::session() )
156 if( SUIT_Application* anApp = aSession->activeApplication() )
157 if( SUIT_Study* aStudy = anApp->activeStudy() )
162 QString HYDROGUI_Tool::GetTempDir( const bool theToCreate )
166 char* tmpdir = getenv ( "HYDRO_TMP_DIR" );
169 // try to create folder if it does not exist
170 QFileInfo fi( tmpdir );
171 if ( !fi.exists() && theToCreate )
173 if ( QDir().mkdir( tmpdir ) )
174 QFile::setPermissions( tmpdir, (QFile::Permissions)0x4FFFF );
175 QFileInfo fi( tmpdir );
176 if ( !fi.exists() || !fi.isWritable() )
181 tmpdir = getenv ( "TEMP" );
183 tmpdir = getenv ( "TMP" );
194 QFileInfo fi( aRes );
195 if ( !fi.exists() || !fi.isWritable() )
196 aRes = QString::null;
201 void HYDROGUI_Tool::SetActiveViewManager( HYDROGUI_Module* theModule,
202 SUIT_ViewManager* theViewManager )
205 if( SUIT_ViewWindow* aViewWindow = theViewManager->getActiveView() )
206 if( STD_TabDesktop* aTabDesktop = dynamic_cast<STD_TabDesktop*>( theModule->getApp()->desktop() ) )
207 if( QtxWorkstack* aWorkstack = aTabDesktop->workstack() )
208 aWorkstack->setActiveWindow( aViewWindow );
211 bool HYDROGUI_Tool::IsObjectHasPresentation( const Handle(HYDROData_Entity)& theObject,
212 const QString& theViewerType )
215 if ( theObject.IsNull() )
218 ObjectKind anObjectKind = theObject->GetKind();
219 if ( theViewerType.isEmpty() || theViewerType == OCCViewer_Viewer::Type() )
221 if ( anObjectKind == KIND_IMAGE ||
222 anObjectKind == KIND_POLYLINEXY ||
223 anObjectKind == KIND_POLYLINE ||
224 anObjectKind == KIND_IMMERSIBLE_ZONE ||
225 anObjectKind == KIND_REGION ||
226 anObjectKind == KIND_ZONE ||
227 anObjectKind == KIND_OBSTACLE ||
228 anObjectKind == KIND_PROFILE ||
229 anObjectKind == KIND_STREAM ||
230 anObjectKind == KIND_CHANNEL ||
231 anObjectKind == KIND_DIGUE ||
232 anObjectKind == KIND_DUMMY_3D
234 || anObjectKind == KIND_SHAPES_GROUP ||
235 anObjectKind == KIND_SPLITTED_GROUP
243 if ( theViewerType.isEmpty() || theViewerType == SVTK_Viewer::Type() )
245 if ( anObjectKind == KIND_BATHYMETRY )
249 if ( theViewerType.isEmpty() || theViewerType == GraphicsView_Viewer::Type() )
251 if ( anObjectKind == KIND_IMAGE ||
252 anObjectKind == KIND_POLYLINEXY )
259 void HYDROGUI_Tool::GetPrsSubObjects( HYDROGUI_Module* theModule,
260 HYDROData_SequenceOfObjects& theSeq )
262 Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( theModule->getStudyId() );
263 if( !aDocument.IsNull() )
265 HYDROData_Iterator anIterator( aDocument );
266 for( ; anIterator.More(); anIterator.Next() )
268 Handle(HYDROData_Entity) anObject = anIterator.Current();
269 if ( !IsObjectHasPresentation( anObject ) )
272 theSeq.Append( anObject );
277 HYDROGUI_Prs* HYDROGUI_Tool::GetPresentation( const Handle(HYDROData_Entity)& theObj,
278 const GraphicsView_ObjectList& theObjects )
280 if( !theObj.IsNull() )
282 GraphicsView_ObjectListIterator anIter( theObjects );
283 while( anIter.hasNext() )
285 if( HYDROGUI_Prs* aPrs = dynamic_cast<HYDROGUI_Prs*>( anIter.next() ) )
287 Handle(HYDROData_Entity) anObj = aPrs->getObject();
288 if( IsEqual( anObj, theObj ) )
296 GraphicsView_ObjectList HYDROGUI_Tool::GetPrsList( GraphicsView_ViewPort* theViewPort )
298 GraphicsView_ObjectList aList;
301 GraphicsView_ObjectListIterator anIter( theViewPort->getObjects() );
302 while( anIter.hasNext() )
303 if( HYDROGUI_Prs* aPrs = dynamic_cast<HYDROGUI_Prs*>( anIter.next() ) )
304 aList.append( aPrs );
309 HYDROData_SequenceOfObjects HYDROGUI_Tool::GetSelectedObjects( HYDROGUI_Module* theModule )
311 HYDROData_SequenceOfObjects aSeq;
313 HYDROGUI_DataModel* aModel = theModule->getDataModel();
315 SUIT_SelectionMgr* aSelectionMgr = theModule->getApp()->selectionMgr();
316 SUIT_DataOwnerPtrList anOwners;
317 aSelectionMgr->selected( anOwners );
319 QStringList aCollectedNameList; // to avoid duplication
320 foreach( SUIT_DataOwner* aSUITOwner, anOwners )
322 if( LightApp_DataOwner* anOwner = dynamic_cast<LightApp_DataOwner*>( aSUITOwner ) )
324 Handle(HYDROData_Entity) anObject = aModel->objectByEntry( anOwner->entry() );
325 if( !anObject.IsNull() )
327 QString aName = anObject->GetName();
328 if( !aCollectedNameList.contains( aName ) )
330 aSeq.Append( anObject );
331 aCollectedNameList.append( aName );
339 Handle(HYDROData_Entity) HYDROGUI_Tool::GetSelectedObject( HYDROGUI_Module* theModule )
341 HYDROData_SequenceOfObjects aSeq = GetSelectedObjects( theModule );
342 if( !aSeq.IsEmpty() )
347 HYDROData_SequenceOfObjects HYDROGUI_Tool::GetGeometryObjects(
348 HYDROGUI_Module* theModule )
350 Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( theModule->getStudyId() );
352 HYDROData_SequenceOfObjects aResSeq;
354 HYDROData_Iterator anIter( aDocument );
355 for ( ; anIter.More(); anIter.Next() )
357 Handle(HYDROData_Entity) anObj = anIter.Current();
358 if ( !HYDROData_Tool::IsGeometryObject( anObj ) )
361 aResSeq.Append( anObj );
367 ObjectKind HYDROGUI_Tool::GetSelectedPartition( HYDROGUI_Module* theModule )
369 HYDROGUI_DataModel* aModel = theModule->getDataModel();
371 SUIT_SelectionMgr* aSelectionMgr = theModule->getApp()->selectionMgr();
372 SUIT_DataOwnerPtrList anOwners;
373 aSelectionMgr->selected( anOwners );
375 if( anOwners.size() != 1 )
378 if( LightApp_DataOwner* anOwner = dynamic_cast<LightApp_DataOwner*>( anOwners.first().operator->() ) )
380 QString anEntry = anOwner->entry();
381 QString aPrefix = HYDROGUI_DataObject::entryPrefix();
382 if( anEntry.left( aPrefix.length() ) == aPrefix )
384 anEntry.remove( aPrefix );
385 for( ObjectKind anObjectKind = KIND_UNKNOWN + 1; anObjectKind <= KIND_LAST; anObjectKind++ )
386 if( HYDROGUI_DataModel::tr( HYDROGUI_DataModel::partitionName( anObjectKind ).toAscii() ) == anEntry )
393 QStringList HYDROGUI_Tool::GetSelectedGeomObjects( HYDROGUI_Module* theModule,
394 QList<GEOM::shape_type> theTypes )
396 QStringList anEntryList;
398 // Get active SalomeApp_Study
399 SalomeApp_Study* aStudy = NULL;
400 if ( theModule && theModule->getApp() ) {
401 aStudy = dynamic_cast<SalomeApp_Study*>( theModule->getApp()->activeStudy() );
408 SUIT_SelectionMgr* aSelectionMgr = theModule->getApp()->selectionMgr();
409 SUIT_DataOwnerPtrList anOwners;
410 aSelectionMgr->selected( anOwners );
412 // Check if the selected objects belong to GEOM and have a shape
413 foreach( SUIT_DataOwner* aSUITOwner, anOwners )
415 if( LightApp_DataOwner* anOwner = dynamic_cast<LightApp_DataOwner*>( aSUITOwner ) )
417 QString anEntry = anOwner->entry();
418 _PTR(SObject) aSObject( aStudy->studyDS()->FindObjectID(qPrintable(anEntry)) );
420 _PTR(SComponent) aSComponent = aSObject->GetFatherComponent();
421 if ( aSComponent && aSComponent->ComponentDataType() == "GEOM" ) {
422 GEOM::GEOM_Object_var aGeomObj =
423 GEOMBase::GetObjectFromIOR( aSObject->GetIOR().c_str() );
425 if ( !aGeomObj->_is_nil() && aGeomObj->IsShape() &&
426 theTypes.contains( aGeomObj->GetShapeType() ) ) {
427 anEntryList << anEntry;
437 Handle(HYDROData_Entity) HYDROGUI_Tool::FindObjectByName( HYDROGUI_Module* theModule,
438 const QString& theName,
439 const ObjectKind theObjectKind )
441 Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( theModule->getStudyId() );
442 return HYDROData_Tool::FindObjectByName( aDocument, theName, theObjectKind );
445 HYDROData_SequenceOfObjects HYDROGUI_Tool::FindObjectsByNames( HYDROGUI_Module* theModule,
446 const QStringList& theNames,
447 const ObjectKind theObjectKind )
449 Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( theModule->getStudyId() );
450 return HYDROData_Tool::FindObjectsByNames( aDocument, theNames, theObjectKind );
453 QString HYDROGUI_Tool::GenerateObjectName( HYDROGUI_Module* theModule,
454 const QString& thePrefix,
455 const QStringList& theUsedNames,
456 const bool theIsTryToUsePurePrefix)
458 Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( theModule->getStudyId() );
459 return HYDROData_Tool::GenerateObjectName( aDocument, thePrefix, theUsedNames, theIsTryToUsePurePrefix );
462 size_t HYDROGUI_Tool::GetActiveViewId( HYDROGUI_Module* theModule,
463 const QString& theViewId )
466 SUIT_ViewManager* aViewMgr = theModule->getApp()->activeViewManager();
467 if( !aViewMgr || ( !theViewId.isEmpty() && aViewMgr->getType() != theViewId ) )
470 if( SUIT_ViewModel* aViewer = aViewMgr->getViewModel() )
471 aViewId = (size_t)aViewer;
475 size_t HYDROGUI_Tool::GetActiveGraphicsViewId( HYDROGUI_Module* theModule )
477 return GetActiveViewId( theModule, GraphicsView_Viewer::Type() );
480 size_t HYDROGUI_Tool::GetActiveOCCViewId( HYDROGUI_Module* theModule )
482 return GetActiveViewId( theModule, OCCViewer_Viewer::Type() );
485 QList<size_t> getViewIdList( HYDROGUI_Module* theModule,
486 const QString& theViewId )
489 ViewManagerList aViewMgrs;
490 theModule->getApp()->viewManagers( theViewId, aViewMgrs );
491 QListIterator<SUIT_ViewManager*> anIter( aViewMgrs );
492 while( anIter.hasNext() )
494 if( SUIT_ViewManager* aViewMgr = anIter.next() )
496 if( SUIT_ViewModel* aViewer = aViewMgr->getViewModel() )
497 aList.append( (size_t)aViewer );
503 QList<size_t> HYDROGUI_Tool::GetGraphicsViewIdList( HYDROGUI_Module* theModule )
505 return getViewIdList( theModule, GraphicsView_Viewer::Type() );
508 QList<size_t> HYDROGUI_Tool::GetOCCViewIdList( HYDROGUI_Module* theModule )
510 return getViewIdList( theModule, OCCViewer_Viewer::Type() );
513 void HYDROGUI_Tool::setOCCActionShown( OCCViewer_ViewFrame* theViewFrame,
514 const int theActionId,
520 OCCViewer_ViewWindow* aView = theViewFrame->getView( OCCViewer_ViewFrame::MAIN_VIEW );
522 aView->toolMgr()->setShown( theActionId, isShown );
523 if ( theActionId == OCCViewer_ViewWindow::MaximizedId )
524 theViewFrame->onMaximizedView( aView, true );
528 void HYDROGUI_Tool::setOCCActionShown( HYDROGUI_Module* theModule,
529 const int theActionId,
533 ViewManagerList aViewMgrs;
534 theModule->getApp()->viewManagers( OCCViewer_Viewer::Type(), aViewMgrs );
535 QListIterator<SUIT_ViewManager*> anIter( aViewMgrs );
536 while( anIter.hasNext() )
538 if( SUIT_ViewManager* aViewMgr = anIter.next() )
540 OCCViewer_ViewFrame* aViewFrame = dynamic_cast<OCCViewer_ViewFrame*>
541 ( aViewMgr->getActiveView() );
543 setOCCActionShown( aViewFrame, theActionId, isShown );
548 void HYDROGUI_Tool::GetObjectReferences( const Handle(HYDROData_Entity)& theObj,
549 HYDROData_SequenceOfObjects& theRefObjects,
550 QStringList& theRefNames )
552 if( theObj.IsNull() )
555 HYDROData_SequenceOfObjects anAllRefObjects = theObj->GetAllReferenceObjects();
556 theRefObjects.Append( anAllRefObjects );
558 for( int i = 1, n = anAllRefObjects.Length(); i <= n; ++i )
560 Handle(HYDROData_Entity) aRefObj = theRefObjects.Value( i );
561 if( aRefObj.IsNull() || aRefObj->IsRemoved() )
564 QString aRefObjectName = aRefObj->GetName();
565 if( theRefNames.contains( aRefObjectName ) )
568 theRefObjects.Append( aRefObj );
569 theRefNames.append( aRefObjectName );
571 GetObjectReferences( aRefObj, theRefObjects, theRefNames );
575 HYDROData_SequenceOfObjects HYDROGUI_Tool::GetObjectBackReferences(
576 HYDROGUI_Module* theModule,
577 const Handle(HYDROData_Entity)& theObj )
579 if( theObj.IsNull() )
580 return HYDROData_SequenceOfObjects();
582 QString anObjName = theObj->GetName();
584 QMap<QString,HYDROData_SequenceOfObjects> aMapOfBackRefs =
585 GetObjectsBackReferences( theModule, QStringList() << anObjName );
587 return aMapOfBackRefs[ anObjName ];
590 QMap<QString,HYDROData_SequenceOfObjects> HYDROGUI_Tool::GetObjectsBackReferences(
591 HYDROGUI_Module* theModule, const QStringList& theObjectNames )
593 QMap<QString,HYDROData_SequenceOfObjects> aResMap;
595 if( theObjectNames.isEmpty() )
598 Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( theModule->getStudyId() );
599 if( aDocument.IsNull() )
602 HYDROData_Iterator anIterator( aDocument );
603 for( ; anIterator.More(); anIterator.Next() )
605 Handle(HYDROData_Entity) anObject = anIterator.Current();
606 if( anObject.IsNull() || anObject->IsRemoved() )
609 QString anObjectName = anObject->GetName();
610 if ( theObjectNames.contains( anObjectName ) )
613 HYDROData_SequenceOfObjects aRefObjects = anObject->GetAllReferenceObjects();
614 for ( int i = 1, n = aRefObjects.Length(); i <= n; ++i )
616 Handle(HYDROData_Entity) aRefObject = aRefObjects.Value( i );
617 if( aRefObject.IsNull() || aRefObject->IsRemoved() )
620 QString aRefObjectName = aRefObject->GetName();
621 if ( !theObjectNames.contains( aRefObjectName ) )
624 aResMap[ aRefObjectName ].Append( anObject );
631 QDockWidget* HYDROGUI_Tool::WindowDock( QWidget* wid )
636 QDockWidget* dock = 0;
637 QWidget* w = wid->parentWidget();
640 dock = ::qobject_cast<QDockWidget*>( w );
641 w = w->parentWidget();
646 QColor HYDROGUI_Tool::GenerateFillingColor( HYDROGUI_Module* theModule,
647 const QStringList& theZoneNames )
649 Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( theModule->getStudyId() );
650 return GenerateFillingColor( aDocument, theZoneNames );
653 QColor HYDROGUI_Tool::GenerateFillingColor( const Handle(HYDROData_Document)& theDoc,
654 const QStringList& theZoneNames )
656 QColor aFillingColor( HYDROData_ImmersibleZone::DefaultFillingColor() );
659 int aR = 0, aG = 0, aB = 0;
660 QStringListIterator aZoneNameIter( theZoneNames );
661 while( aZoneNameIter.hasNext() )
663 const QString& aZoneName = aZoneNameIter.next();
664 Handle(HYDROData_ImmersibleZone) aRefZone =
665 Handle(HYDROData_ImmersibleZone)::DownCast(
666 HYDROData_Tool::FindObjectByName( theDoc, aZoneName, KIND_IMMERSIBLE_ZONE ) );
667 if( !aRefZone.IsNull() )
669 QColor aRefColor = aRefZone->GetFillingColor();
670 aR += aRefColor.red();
671 aG += aRefColor.green();
672 aB += aRefColor.blue();
679 aFillingColor = QColor( aR / aCounter, aG / aCounter, aB / aCounter );
682 return aFillingColor;
685 QStringList HYDROGUI_Tool::FindExistingObjectsNames( const Handle(HYDROData_Document)& theDoc,
686 const ObjectKind theObjectKind )
690 HYDROData_Iterator anIter( theDoc, theObjectKind );
691 for ( ; anIter.More(); anIter.Next() ) {
692 Handle(HYDROData_Entity) anObject = anIter.Current();
693 if( !anObject.IsNull() ) {
694 aNames.append( anObject->GetName() );
701 QString HYDROGUI_Tool::GetCoordinateString( const double theNumber )
703 return QString::number( theNumber, 'f', 2 );