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 QString HYDROGUI_Tool::ToQString( const TCollection_AsciiString& src )
73 #ifdef USE_LATIN1_ENCODING
74 QTextCodec* codec = QTextCodec::codecForName( "latin1" ); // alias for ISO-8859-1
76 QTextCodec* codec = QTextCodec::codecForLocale();
80 res = codec ? codec->toUnicode( (char*)src.ToCString(), src.Length() ) :
81 QString( (char*)src.ToCString() );
85 QString HYDROGUI_Tool::ToQString( const TCollection_ExtendedString& src )
87 return QString( (QChar*)src.ToExtString(), src.Length() );
90 QString HYDROGUI_Tool::ToQString( const Handle(TCollection_HAsciiString)& src )
95 return ToQString( src->String() );
98 QString HYDROGUI_Tool::ToQString( const Handle(TCollection_HExtendedString)& src )
102 return ToQString( src->String() );
105 TCollection_AsciiString HYDROGUI_Tool::ToAsciiString( const QString& src )
107 TCollection_AsciiString res;
110 #ifdef USE_LATIN1_ENCODING
111 QTextCodec* codec = QTextCodec::codecForName( "latin1" ); // alias for ISO-8859-1
113 QTextCodec* codec = QTextCodec::codecForLocale();
117 QByteArray str = codec->fromUnicode( src );
118 res = TCollection_AsciiString( (Standard_CString)str.constData() );
121 res = TCollection_AsciiString( src.toLatin1().data() );
126 TCollection_ExtendedString HYDROGUI_Tool::ToExtString( const QString& src )
129 return TCollection_ExtendedString();
131 Standard_Integer len = src.length();
132 Standard_ExtString extStr = new Standard_ExtCharacter[ ( len + 1 ) * 2 ];
133 memcpy( (void*)extStr, src.unicode(), len * 2 );
134 ((short*)extStr)[ len ] = 0;
136 TCollection_ExtendedString trg( extStr );
141 Handle(TCollection_HAsciiString) HYDROGUI_Tool::ToHAsciiString( const QString& src )
143 return new TCollection_HAsciiString( ToAsciiString( src ) );
146 Handle(TCollection_HExtendedString) HYDROGUI_Tool::ToHExtString( const QString& src )
148 return new TCollection_HExtendedString( ToExtString( src ) );
151 int HYDROGUI_Tool::GetActiveStudyId()
153 if( SUIT_Session* aSession = SUIT_Session::session() )
154 if( SUIT_Application* anApp = aSession->activeApplication() )
155 if( SUIT_Study* aStudy = anApp->activeStudy() )
160 QString HYDROGUI_Tool::GetTempDir( const bool theToCreate )
164 char* tmpdir = getenv ( "HYDRO_TMP_DIR" );
167 // try to create folder if it does not exist
168 QFileInfo fi( tmpdir );
169 if ( !fi.exists() && theToCreate )
171 if ( QDir().mkdir( tmpdir ) )
172 QFile::setPermissions( tmpdir, (QFile::Permissions)0x4FFFF );
173 QFileInfo fi( tmpdir );
174 if ( !fi.exists() || !fi.isWritable() )
179 tmpdir = getenv ( "TEMP" );
181 tmpdir = getenv ( "TMP" );
192 QFileInfo fi( aRes );
193 if ( !fi.exists() || !fi.isWritable() )
194 aRes = QString::null;
199 void HYDROGUI_Tool::SetActiveViewManager( HYDROGUI_Module* theModule,
200 SUIT_ViewManager* theViewManager )
203 if( SUIT_ViewWindow* aViewWindow = theViewManager->getActiveView() )
204 if( STD_TabDesktop* aTabDesktop = dynamic_cast<STD_TabDesktop*>( theModule->getApp()->desktop() ) )
205 if( QtxWorkstack* aWorkstack = aTabDesktop->workstack() )
206 aWorkstack->setActiveWindow( aViewWindow );
209 bool HYDROGUI_Tool::IsObjectHasPresentation( const Handle(HYDROData_Entity)& theObject,
210 const QString& theViewerType )
213 if ( theObject.IsNull() )
216 ObjectKind anObjectKind = theObject->GetKind();
217 if ( theViewerType.isEmpty() || theViewerType == OCCViewer_Viewer::Type() )
219 if ( anObjectKind == KIND_IMAGE ||
220 anObjectKind == KIND_POLYLINEXY ||
221 anObjectKind == KIND_POLYLINE ||
222 anObjectKind == KIND_IMMERSIBLE_ZONE ||
223 anObjectKind == KIND_REGION ||
224 anObjectKind == KIND_ZONE ||
225 anObjectKind == KIND_OBSTACLE ||
226 anObjectKind == KIND_PROFILE ||
227 anObjectKind == KIND_STREAM ||
228 anObjectKind == KIND_CHANNEL ||
229 anObjectKind == KIND_DIGUE ||
230 anObjectKind == KIND_DUMMY_3D )
236 if ( theViewerType.isEmpty() || theViewerType == SVTK_Viewer::Type() )
238 if ( anObjectKind == KIND_BATHYMETRY )
242 if ( theViewerType.isEmpty() || theViewerType == GraphicsView_Viewer::Type() )
244 if ( anObjectKind == KIND_IMAGE ||
245 anObjectKind == KIND_POLYLINEXY )
252 void HYDROGUI_Tool::GetPrsSubObjects( HYDROGUI_Module* theModule,
253 HYDROData_SequenceOfObjects& theSeq )
255 Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( theModule->getStudyId() );
256 if( !aDocument.IsNull() )
258 HYDROData_Iterator anIterator( aDocument );
259 for( ; anIterator.More(); anIterator.Next() )
261 Handle(HYDROData_Entity) anObject = anIterator.Current();
262 if ( !IsObjectHasPresentation( anObject ) )
265 theSeq.Append( anObject );
270 HYDROGUI_Prs* HYDROGUI_Tool::GetPresentation( const Handle(HYDROData_Entity)& theObj,
271 const GraphicsView_ObjectList& theObjects )
273 if( !theObj.IsNull() )
275 GraphicsView_ObjectListIterator anIter( theObjects );
276 while( anIter.hasNext() )
278 if( HYDROGUI_Prs* aPrs = dynamic_cast<HYDROGUI_Prs*>( anIter.next() ) )
280 Handle(HYDROData_Entity) anObj = aPrs->getObject();
281 if( IsEqual( anObj, theObj ) )
289 GraphicsView_ObjectList HYDROGUI_Tool::GetPrsList( GraphicsView_ViewPort* theViewPort )
291 GraphicsView_ObjectList aList;
294 GraphicsView_ObjectListIterator anIter( theViewPort->getObjects() );
295 while( anIter.hasNext() )
296 if( HYDROGUI_Prs* aPrs = dynamic_cast<HYDROGUI_Prs*>( anIter.next() ) )
297 aList.append( aPrs );
302 HYDROData_SequenceOfObjects HYDROGUI_Tool::GetSelectedObjects( HYDROGUI_Module* theModule )
304 HYDROData_SequenceOfObjects aSeq;
306 HYDROGUI_DataModel* aModel = theModule->getDataModel();
308 SUIT_SelectionMgr* aSelectionMgr = theModule->getApp()->selectionMgr();
309 SUIT_DataOwnerPtrList anOwners;
310 aSelectionMgr->selected( anOwners );
312 QStringList aCollectedNameList; // to avoid duplication
313 foreach( SUIT_DataOwner* aSUITOwner, anOwners )
315 if( LightApp_DataOwner* anOwner = dynamic_cast<LightApp_DataOwner*>( aSUITOwner ) )
317 Handle(HYDROData_Entity) anObject = aModel->objectByEntry( anOwner->entry() );
318 if( !anObject.IsNull() )
320 QString aName = anObject->GetName();
321 if( !aCollectedNameList.contains( aName ) )
323 aSeq.Append( anObject );
324 aCollectedNameList.append( aName );
332 Handle(HYDROData_Entity) HYDROGUI_Tool::GetSelectedObject( HYDROGUI_Module* theModule )
334 HYDROData_SequenceOfObjects aSeq = GetSelectedObjects( theModule );
335 if( !aSeq.IsEmpty() )
340 HYDROData_SequenceOfObjects HYDROGUI_Tool::GetGeometryObjects(
341 HYDROGUI_Module* theModule )
343 Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( theModule->getStudyId() );
345 HYDROData_SequenceOfObjects aResSeq;
347 HYDROData_Iterator anIter( aDocument );
348 for ( ; anIter.More(); anIter.Next() )
350 Handle(HYDROData_Entity) anObj = anIter.Current();
351 if ( !HYDROData_Tool::IsGeometryObject( anObj ) )
354 aResSeq.Append( anObj );
360 ObjectKind HYDROGUI_Tool::GetSelectedPartition( HYDROGUI_Module* theModule )
362 HYDROGUI_DataModel* aModel = theModule->getDataModel();
364 SUIT_SelectionMgr* aSelectionMgr = theModule->getApp()->selectionMgr();
365 SUIT_DataOwnerPtrList anOwners;
366 aSelectionMgr->selected( anOwners );
368 if( anOwners.size() != 1 )
371 if( LightApp_DataOwner* anOwner = dynamic_cast<LightApp_DataOwner*>( anOwners.first().operator->() ) )
373 QString anEntry = anOwner->entry();
374 QString aPrefix = HYDROGUI_DataObject::entryPrefix();
375 if( anEntry.left( aPrefix.length() ) == aPrefix )
377 anEntry.remove( aPrefix );
378 for( ObjectKind anObjectKind = KIND_UNKNOWN + 1; anObjectKind <= KIND_LAST; anObjectKind++ )
379 if( HYDROGUI_DataModel::tr( HYDROGUI_DataModel::partitionName( anObjectKind ).toAscii() ) == anEntry )
386 QStringList HYDROGUI_Tool::GetSelectedGeomObjects( HYDROGUI_Module* theModule,
387 QList<GEOM::shape_type> theTypes )
389 QStringList anEntryList;
391 // Get active SalomeApp_Study
392 SalomeApp_Study* aStudy = NULL;
393 if ( theModule && theModule->getApp() ) {
394 aStudy = dynamic_cast<SalomeApp_Study*>( theModule->getApp()->activeStudy() );
401 SUIT_SelectionMgr* aSelectionMgr = theModule->getApp()->selectionMgr();
402 SUIT_DataOwnerPtrList anOwners;
403 aSelectionMgr->selected( anOwners );
405 // Check if the selected objects belong to GEOM and have a shape
406 foreach( SUIT_DataOwner* aSUITOwner, anOwners )
408 if( LightApp_DataOwner* anOwner = dynamic_cast<LightApp_DataOwner*>( aSUITOwner ) )
410 QString anEntry = anOwner->entry();
411 _PTR(SObject) aSObject( aStudy->studyDS()->FindObjectID(qPrintable(anEntry)) );
413 _PTR(SComponent) aSComponent = aSObject->GetFatherComponent();
414 if ( aSComponent && aSComponent->ComponentDataType() == "GEOM" ) {
415 GEOM::GEOM_Object_var aGeomObj =
416 GEOMBase::GetObjectFromIOR( aSObject->GetIOR().c_str() );
418 if ( !aGeomObj->_is_nil() && aGeomObj->IsShape() &&
419 theTypes.contains( aGeomObj->GetShapeType() ) ) {
420 anEntryList << anEntry;
430 Handle(HYDROData_Entity) HYDROGUI_Tool::FindObjectByName( HYDROGUI_Module* theModule,
431 const QString& theName,
432 const ObjectKind theObjectKind )
434 Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( theModule->getStudyId() );
435 return HYDROData_Tool::FindObjectByName( aDocument, theName, theObjectKind );
438 HYDROData_SequenceOfObjects HYDROGUI_Tool::FindObjectsByNames( HYDROGUI_Module* theModule,
439 const QStringList& theNames,
440 const ObjectKind theObjectKind )
442 Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( theModule->getStudyId() );
443 return HYDROData_Tool::FindObjectsByNames( aDocument, theNames, theObjectKind );
446 QString HYDROGUI_Tool::GenerateObjectName( HYDROGUI_Module* theModule,
447 const QString& thePrefix,
448 const QStringList& theUsedNames,
449 const bool theIsTryToUsePurePrefix)
451 Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( theModule->getStudyId() );
452 return HYDROData_Tool::GenerateObjectName( aDocument, thePrefix, theUsedNames, theIsTryToUsePurePrefix );
455 size_t HYDROGUI_Tool::GetActiveViewId( HYDROGUI_Module* theModule,
456 const QString& theViewId )
459 SUIT_ViewManager* aViewMgr = theModule->getApp()->activeViewManager();
460 if( !aViewMgr || ( !theViewId.isEmpty() && aViewMgr->getType() != theViewId ) )
463 if( SUIT_ViewModel* aViewer = aViewMgr->getViewModel() )
464 aViewId = (size_t)aViewer;
468 size_t HYDROGUI_Tool::GetActiveGraphicsViewId( HYDROGUI_Module* theModule )
470 return GetActiveViewId( theModule, GraphicsView_Viewer::Type() );
473 size_t HYDROGUI_Tool::GetActiveOCCViewId( HYDROGUI_Module* theModule )
475 return GetActiveViewId( theModule, OCCViewer_Viewer::Type() );
478 QList<size_t> getViewIdList( HYDROGUI_Module* theModule,
479 const QString& theViewId )
482 ViewManagerList aViewMgrs;
483 theModule->getApp()->viewManagers( theViewId, aViewMgrs );
484 QListIterator<SUIT_ViewManager*> anIter( aViewMgrs );
485 while( anIter.hasNext() )
487 if( SUIT_ViewManager* aViewMgr = anIter.next() )
489 if( SUIT_ViewModel* aViewer = aViewMgr->getViewModel() )
490 aList.append( (size_t)aViewer );
496 QList<size_t> HYDROGUI_Tool::GetGraphicsViewIdList( HYDROGUI_Module* theModule )
498 return getViewIdList( theModule, GraphicsView_Viewer::Type() );
501 QList<size_t> HYDROGUI_Tool::GetOCCViewIdList( HYDROGUI_Module* theModule )
503 return getViewIdList( theModule, OCCViewer_Viewer::Type() );
506 void HYDROGUI_Tool::setOCCActionShown( OCCViewer_ViewFrame* theViewFrame,
507 const int theActionId,
513 OCCViewer_ViewWindow* aView = theViewFrame->getView( OCCViewer_ViewFrame::MAIN_VIEW );
515 aView->toolMgr()->setShown( theActionId, isShown );
516 if ( theActionId == OCCViewer_ViewWindow::MaximizedId )
517 theViewFrame->onMaximizedView( aView, true );
521 void HYDROGUI_Tool::setOCCActionShown( HYDROGUI_Module* theModule,
522 const int theActionId,
526 ViewManagerList aViewMgrs;
527 theModule->getApp()->viewManagers( OCCViewer_Viewer::Type(), aViewMgrs );
528 QListIterator<SUIT_ViewManager*> anIter( aViewMgrs );
529 while( anIter.hasNext() )
531 if( SUIT_ViewManager* aViewMgr = anIter.next() )
533 OCCViewer_ViewFrame* aViewFrame = dynamic_cast<OCCViewer_ViewFrame*>
534 ( aViewMgr->getActiveView() );
536 setOCCActionShown( aViewFrame, theActionId, isShown );
541 void HYDROGUI_Tool::GetObjectReferences( const Handle(HYDROData_Entity)& theObj,
542 HYDROData_SequenceOfObjects& theRefObjects,
543 QStringList& theRefNames )
545 if( theObj.IsNull() )
548 HYDROData_SequenceOfObjects anAllRefObjects = theObj->GetAllReferenceObjects();
549 theRefObjects.Append( anAllRefObjects );
551 for( int i = 1, n = anAllRefObjects.Length(); i <= n; ++i )
553 Handle(HYDROData_Entity) aRefObj = theRefObjects.Value( i );
554 if( aRefObj.IsNull() || aRefObj->IsRemoved() )
557 QString aRefObjectName = aRefObj->GetName();
558 if( theRefNames.contains( aRefObjectName ) )
561 theRefObjects.Append( aRefObj );
562 theRefNames.append( aRefObjectName );
564 GetObjectReferences( aRefObj, theRefObjects, theRefNames );
568 HYDROData_SequenceOfObjects HYDROGUI_Tool::GetObjectBackReferences(
569 HYDROGUI_Module* theModule,
570 const Handle(HYDROData_Entity)& theObj )
572 if( theObj.IsNull() )
573 return HYDROData_SequenceOfObjects();
575 QString anObjName = theObj->GetName();
577 QMap<QString,HYDROData_SequenceOfObjects> aMapOfBackRefs =
578 GetObjectsBackReferences( theModule, QStringList() << anObjName );
580 return aMapOfBackRefs[ anObjName ];
583 QMap<QString,HYDROData_SequenceOfObjects> HYDROGUI_Tool::GetObjectsBackReferences(
584 HYDROGUI_Module* theModule, const QStringList& theObjectNames )
586 QMap<QString,HYDROData_SequenceOfObjects> aResMap;
588 if( theObjectNames.isEmpty() )
591 Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( theModule->getStudyId() );
592 if( aDocument.IsNull() )
595 HYDROData_Iterator anIterator( aDocument );
596 for( ; anIterator.More(); anIterator.Next() )
598 Handle(HYDROData_Entity) anObject = anIterator.Current();
599 if( anObject.IsNull() || anObject->IsRemoved() )
602 QString anObjectName = anObject->GetName();
603 if ( theObjectNames.contains( anObjectName ) )
606 HYDROData_SequenceOfObjects aRefObjects = anObject->GetAllReferenceObjects();
607 for ( int i = 1, n = aRefObjects.Length(); i <= n; ++i )
609 Handle(HYDROData_Entity) aRefObject = aRefObjects.Value( i );
610 if( aRefObject.IsNull() || aRefObject->IsRemoved() )
613 QString aRefObjectName = aRefObject->GetName();
614 if ( !theObjectNames.contains( aRefObjectName ) )
617 aResMap[ aRefObjectName ].Append( anObject );
624 QDockWidget* HYDROGUI_Tool::WindowDock( QWidget* wid )
629 QDockWidget* dock = 0;
630 QWidget* w = wid->parentWidget();
633 dock = ::qobject_cast<QDockWidget*>( w );
634 w = w->parentWidget();
639 QColor HYDROGUI_Tool::GenerateFillingColor( HYDROGUI_Module* theModule,
640 const QStringList& theZoneNames )
642 Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( theModule->getStudyId() );
643 return GenerateFillingColor( aDocument, theZoneNames );
646 QColor HYDROGUI_Tool::GenerateFillingColor( const Handle(HYDROData_Document)& theDoc,
647 const QStringList& theZoneNames )
649 QColor aFillingColor( HYDROData_ImmersibleZone::DefaultFillingColor() );
652 int aR = 0, aG = 0, aB = 0;
653 QStringListIterator aZoneNameIter( theZoneNames );
654 while( aZoneNameIter.hasNext() )
656 const QString& aZoneName = aZoneNameIter.next();
657 Handle(HYDROData_ImmersibleZone) aRefZone =
658 Handle(HYDROData_ImmersibleZone)::DownCast(
659 HYDROData_Tool::FindObjectByName( theDoc, aZoneName, KIND_IMMERSIBLE_ZONE ) );
660 if( !aRefZone.IsNull() )
662 QColor aRefColor = aRefZone->GetFillingColor();
663 aR += aRefColor.red();
664 aG += aRefColor.green();
665 aB += aRefColor.blue();
672 aFillingColor = QColor( aR / aCounter, aG / aCounter, aB / aCounter );
675 return aFillingColor;
678 QStringList HYDROGUI_Tool::FindExistingObjectsNames( const Handle(HYDROData_Document)& theDoc,
679 const ObjectKind theObjectKind )
683 HYDROData_Iterator anIter( theDoc, theObjectKind );
684 for ( ; anIter.More(); anIter.Next() ) {
685 Handle(HYDROData_Entity) anObject = anIter.Current();
686 if( !anObject.IsNull() ) {
687 aNames.append( anObject->GetName() );
694 QString HYDROGUI_Tool::GetCoordinateString( const double theNumber )
696 return QString::number( theNumber, 'f', 2 );