1 // Copyright (C) 2007-2015 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, or (at your option) any later version.
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>
34 #include <HYDROData_Profile.h>
38 #include <GraphicsView_Viewer.h>
40 #include <SalomeApp_Study.h>
42 #include <LightApp_Application.h>
43 #include <LightApp_DataOwner.h>
44 #include <LightApp_SelectionMgr.h>
46 #include <OCCViewer_ViewModel.h>
47 #include <OCCViewer_ViewFrame.h>
49 #include <QtxWorkstack.h>
50 #include <QtxActionToolMgr.h>
52 #include <STD_TabDesktop.h>
54 #include <SUIT_Session.h>
55 #include <SUIT_Study.h>
56 #include <SUIT_ViewManager.h>
57 #include <SUIT_ViewWindow.h>
59 #include <SALOMEDSClient.hxx>
61 #include <SVTK_ViewModel.h>
65 #include <QDockWidget>
68 // Definition of this id allows to use 'latin1' (Qt alias for 'ISO-8859-1')
69 // encoding instead of default 'System'
70 #define USE_LATIN1_ENCODING
72 // #define DEB_GROUPS 1
74 QString HYDROGUI_Tool::ToQString( const TCollection_AsciiString& src )
76 #ifdef USE_LATIN1_ENCODING
77 QTextCodec* codec = QTextCodec::codecForName( "latin1" ); // alias for ISO-8859-1
79 QTextCodec* codec = QTextCodec::codecForLocale();
83 res = codec ? codec->toUnicode( (char*)src.ToCString(), src.Length() ) :
84 QString( (char*)src.ToCString() );
88 QString HYDROGUI_Tool::ToQString( const TCollection_ExtendedString& src )
90 return QString( (QChar*)src.ToExtString(), src.Length() );
93 QString HYDROGUI_Tool::ToQString( const Handle(TCollection_HAsciiString)& src )
98 return ToQString( src->String() );
101 QString HYDROGUI_Tool::ToQString( const Handle(TCollection_HExtendedString)& src )
105 return ToQString( src->String() );
108 TCollection_AsciiString HYDROGUI_Tool::ToAsciiString( const QString& src )
110 TCollection_AsciiString res;
113 #ifdef USE_LATIN1_ENCODING
114 QTextCodec* codec = QTextCodec::codecForName( "latin1" ); // alias for ISO-8859-1
116 QTextCodec* codec = QTextCodec::codecForLocale();
120 QByteArray str = codec->fromUnicode( src );
121 res = TCollection_AsciiString( (Standard_CString)str.constData() );
124 res = TCollection_AsciiString( src.toLatin1().data() );
129 TCollection_ExtendedString HYDROGUI_Tool::ToExtString( const QString& src )
132 return TCollection_ExtendedString();
134 Standard_Integer len = src.length();
135 Standard_ExtString extStr = new Standard_ExtCharacter[ ( len + 1 ) * 2 ];
136 memcpy( (void*)extStr, src.unicode(), len * 2 );
137 ((short*)extStr)[ len ] = 0;
139 TCollection_ExtendedString trg( extStr );
144 Handle(TCollection_HAsciiString) HYDROGUI_Tool::ToHAsciiString( const QString& src )
146 return new TCollection_HAsciiString( ToAsciiString( src ) );
149 Handle(TCollection_HExtendedString) HYDROGUI_Tool::ToHExtString( const QString& src )
151 return new TCollection_HExtendedString( ToExtString( src ) );
154 int HYDROGUI_Tool::GetActiveStudyId()
156 if( SUIT_Session* aSession = SUIT_Session::session() )
157 if( SUIT_Application* anApp = aSession->activeApplication() )
158 if( SUIT_Study* aStudy = anApp->activeStudy() )
163 QString HYDROGUI_Tool::GetTempDir( const bool theToCreate )
167 char* tmpdir = getenv ( "HYDRO_TMP_DIR" );
170 // try to create folder if it does not exist
171 QFileInfo fi( tmpdir );
172 if ( !fi.exists() && theToCreate )
174 if ( QDir().mkdir( tmpdir ) )
175 QFile::setPermissions( tmpdir, (QFile::Permissions)0x4FFFF );
176 QFileInfo fi( tmpdir );
177 if ( !fi.exists() || !fi.isWritable() )
182 tmpdir = getenv ( "TEMP" );
184 tmpdir = getenv ( "TMP" );
195 QFileInfo fi( aRes );
196 if ( !fi.exists() || !fi.isWritable() )
197 aRes = QString::null;
202 void HYDROGUI_Tool::SetActiveViewManager( HYDROGUI_Module* theModule,
203 SUIT_ViewManager* theViewManager )
206 if( SUIT_ViewWindow* aViewWindow = theViewManager->getActiveView() )
207 if( STD_TabDesktop* aTabDesktop = dynamic_cast<STD_TabDesktop*>( theModule->getApp()->desktop() ) )
208 if( QtxWorkstack* aWorkstack = aTabDesktop->workstack() )
209 aWorkstack->setActiveWindow( aViewWindow );
212 bool HYDROGUI_Tool::IsObjectHasPresentation( const Handle(HYDROData_Entity)& theObject,
213 const QString& theViewerType )
216 if ( theObject.IsNull() )
219 ObjectKind anObjectKind = theObject->GetKind();
220 if ( theViewerType.isEmpty() || theViewerType == OCCViewer_Viewer::Type() )
222 if ( anObjectKind == KIND_IMAGE ||
223 anObjectKind == KIND_POLYLINEXY ||
224 anObjectKind == KIND_POLYLINE ||
225 anObjectKind == KIND_IMMERSIBLE_ZONE ||
226 anObjectKind == KIND_REGION ||
227 anObjectKind == KIND_ZONE ||
228 anObjectKind == KIND_OBSTACLE ||
229 anObjectKind == KIND_PROFILE ||
230 anObjectKind == KIND_STREAM ||
231 anObjectKind == KIND_CHANNEL ||
232 anObjectKind == KIND_DIGUE ||
233 anObjectKind == KIND_DUMMY_3D ||
234 anObjectKind == KIND_BATHYMETRY
236 || anObjectKind == KIND_SHAPES_GROUP ||
237 anObjectKind == KIND_SPLITTED_GROUP
245 if ( theViewerType.isEmpty() || theViewerType == SVTK_Viewer::Type() )
247 if ( anObjectKind == KIND_BATHYMETRY )
251 if ( theViewerType.isEmpty() || theViewerType == GraphicsView_Viewer::Type() )
253 if ( anObjectKind == KIND_IMAGE ||
254 anObjectKind == KIND_POLYLINEXY )
261 void HYDROGUI_Tool::GetPrsSubObjects( HYDROGUI_Module* theModule,
262 HYDROData_SequenceOfObjects& theSeq )
264 Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( theModule->getStudyId() );
265 if( !aDocument.IsNull() )
267 HYDROData_Iterator anIterator( aDocument );
268 for( ; anIterator.More(); anIterator.Next() )
270 Handle(HYDROData_Entity) anObject = anIterator.Current();
271 if ( !IsObjectHasPresentation( anObject ) )
274 theSeq.Append( anObject );
279 HYDROGUI_Prs* HYDROGUI_Tool::GetPresentation( const Handle(HYDROData_Entity)& theObj,
280 const GraphicsView_ObjectList& theObjects )
282 if( !theObj.IsNull() )
284 GraphicsView_ObjectListIterator anIter( theObjects );
285 while( anIter.hasNext() )
287 if( HYDROGUI_Prs* aPrs = dynamic_cast<HYDROGUI_Prs*>( anIter.next() ) )
289 Handle(HYDROData_Entity) anObj = aPrs->getObject();
290 if( IsEqual( anObj, theObj ) )
298 GraphicsView_ObjectList HYDROGUI_Tool::GetPrsList( GraphicsView_ViewPort* theViewPort )
300 GraphicsView_ObjectList aList;
303 GraphicsView_ObjectListIterator anIter( theViewPort->getObjects() );
304 while( anIter.hasNext() )
305 if( HYDROGUI_Prs* aPrs = dynamic_cast<HYDROGUI_Prs*>( anIter.next() ) )
306 aList.append( aPrs );
311 HYDROData_SequenceOfObjects HYDROGUI_Tool::GetSelectedObjects( HYDROGUI_Module* theModule )
313 HYDROData_SequenceOfObjects aSeq;
315 HYDROGUI_DataModel* aModel = theModule->getDataModel();
317 SUIT_SelectionMgr* aSelectionMgr = theModule->getApp()->selectionMgr();
318 SUIT_DataOwnerPtrList anOwners;
319 aSelectionMgr->selected( anOwners );
321 QStringList aCollectedNameList; // to avoid duplication
322 foreach( SUIT_DataOwner* aSUITOwner, anOwners )
324 if( LightApp_DataOwner* anOwner = dynamic_cast<LightApp_DataOwner*>( aSUITOwner ) )
326 Handle(HYDROData_Entity) anObject = aModel->objectByEntry( anOwner->entry() );
327 if( !anObject.IsNull() )
329 QString aName = anObject->GetName();
330 if( !aCollectedNameList.contains( aName ) )
332 aSeq.Append( anObject );
333 aCollectedNameList.append( aName );
341 Handle(HYDROData_Entity) HYDROGUI_Tool::GetSelectedObject( HYDROGUI_Module* theModule )
343 HYDROData_SequenceOfObjects aSeq = GetSelectedObjects( theModule );
344 if( !aSeq.IsEmpty() )
349 HYDROData_SequenceOfObjects HYDROGUI_Tool::GetGeometryObjects(
350 HYDROGUI_Module* theModule )
352 Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( theModule->getStudyId() );
354 HYDROData_SequenceOfObjects aResSeq;
356 HYDROData_Iterator anIter( aDocument );
357 for ( ; anIter.More(); anIter.Next() )
359 Handle(HYDROData_Entity) anObj = anIter.Current();
360 if ( !HYDROData_Tool::IsGeometryObject( anObj ) )
363 aResSeq.Append( anObj );
369 ObjectKind HYDROGUI_Tool::GetSelectedPartition( HYDROGUI_Module* theModule )
371 HYDROGUI_DataModel* aModel = theModule->getDataModel();
373 SUIT_SelectionMgr* aSelectionMgr = theModule->getApp()->selectionMgr();
374 SUIT_DataOwnerPtrList anOwners;
375 aSelectionMgr->selected( anOwners );
377 if( anOwners.size() != 1 )
380 if( LightApp_DataOwner* anOwner = dynamic_cast<LightApp_DataOwner*>( anOwners.first().operator->() ) )
382 QString anEntry = anOwner->entry();
383 QString aPrefix = HYDROGUI_DataObject::entryPrefix();
384 if( anEntry.left( aPrefix.length() ) == aPrefix )
386 anEntry.remove( aPrefix );
387 for( ObjectKind anObjectKind = KIND_UNKNOWN + 1; anObjectKind <= KIND_LAST; anObjectKind++ )
388 if( HYDROGUI_DataModel::tr( HYDROGUI_DataModel::partitionName( anObjectKind ).toAscii() ) == anEntry )
395 QStringList HYDROGUI_Tool::GetSelectedGeomObjects( HYDROGUI_Module* theModule,
396 QList<GEOM::shape_type> theTypes )
398 QStringList anEntryList;
400 // Get active SalomeApp_Study
401 SalomeApp_Study* aStudy = NULL;
402 if ( theModule && theModule->getApp() ) {
403 aStudy = dynamic_cast<SalomeApp_Study*>( theModule->getApp()->activeStudy() );
410 SUIT_SelectionMgr* aSelectionMgr = theModule->getApp()->selectionMgr();
411 SUIT_DataOwnerPtrList anOwners;
412 aSelectionMgr->selected( anOwners );
414 // Check if the selected objects belong to GEOM and have a shape
415 foreach( SUIT_DataOwner* aSUITOwner, anOwners )
417 if( LightApp_DataOwner* anOwner = dynamic_cast<LightApp_DataOwner*>( aSUITOwner ) )
419 QString anEntry = anOwner->entry();
420 _PTR(SObject) aSObject( aStudy->studyDS()->FindObjectID(qPrintable(anEntry)) );
422 _PTR(SComponent) aSComponent = aSObject->GetFatherComponent();
423 if ( aSComponent && aSComponent->ComponentDataType() == "GEOM" ) {
424 GEOM::GEOM_Object_var aGeomObj =
425 GEOMBase::GetObjectFromIOR( aSObject->GetIOR().c_str() );
427 if ( !aGeomObj->_is_nil() && aGeomObj->IsShape() &&
428 theTypes.contains( aGeomObj->GetShapeType() ) ) {
429 anEntryList << anEntry;
439 Handle(HYDROData_Entity) HYDROGUI_Tool::FindObjectByName( HYDROGUI_Module* theModule,
440 const QString& theName,
441 const ObjectKind theObjectKind )
443 Handle(HYDROData_Entity) aResObj;
445 Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( theModule->getStudyId() );
446 if ( !aDocument.IsNull() )
447 aResObj = aDocument->FindObjectByName( theName, theObjectKind );
452 HYDROData_SequenceOfObjects HYDROGUI_Tool::FindObjectsByNames( HYDROGUI_Module* theModule,
453 const QStringList& theNames,
454 const ObjectKind theObjectKind )
456 HYDROData_SequenceOfObjects aResSeq;
458 Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( theModule->getStudyId() );
459 if ( !aDocument.IsNull() )
460 aResSeq = aDocument->FindObjectsByNames( theNames, theObjectKind );
465 QString HYDROGUI_Tool::GenerateObjectName( HYDROGUI_Module* theModule,
466 const QString& thePrefix,
467 const QStringList& theUsedNames,
468 const bool theIsTryToUsePurePrefix)
470 Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( theModule->getStudyId() );
471 return HYDROData_Tool::GenerateObjectName( aDocument, thePrefix, theUsedNames, theIsTryToUsePurePrefix );
474 size_t HYDROGUI_Tool::GetActiveViewId( HYDROGUI_Module* theModule,
475 const QString& theViewId )
478 SUIT_ViewManager* aViewMgr = theModule->getApp()->activeViewManager();
479 if( !aViewMgr || ( !theViewId.isEmpty() && aViewMgr->getType() != theViewId ) )
482 if( SUIT_ViewModel* aViewer = aViewMgr->getViewModel() )
483 aViewId = (size_t)aViewer;
487 size_t HYDROGUI_Tool::GetActiveGraphicsViewId( HYDROGUI_Module* theModule )
489 return GetActiveViewId( theModule, GraphicsView_Viewer::Type() );
492 size_t HYDROGUI_Tool::GetActiveOCCViewId( HYDROGUI_Module* theModule )
494 return GetActiveViewId( theModule, OCCViewer_Viewer::Type() );
497 QList<size_t> getViewIdList( HYDROGUI_Module* theModule,
498 const QString& theViewId )
501 ViewManagerList aViewMgrs;
502 theModule->getApp()->viewManagers( theViewId, aViewMgrs );
503 QListIterator<SUIT_ViewManager*> anIter( aViewMgrs );
504 while( anIter.hasNext() )
506 if( SUIT_ViewManager* aViewMgr = anIter.next() )
508 if( SUIT_ViewModel* aViewer = aViewMgr->getViewModel() )
509 aList.append( (size_t)aViewer );
515 QList<size_t> HYDROGUI_Tool::GetGraphicsViewIdList( HYDROGUI_Module* theModule )
517 return getViewIdList( theModule, GraphicsView_Viewer::Type() );
520 QList<size_t> HYDROGUI_Tool::GetOCCViewIdList( HYDROGUI_Module* theModule )
522 return getViewIdList( theModule, OCCViewer_Viewer::Type() );
525 void HYDROGUI_Tool::setOCCActionShown( OCCViewer_ViewFrame* theViewFrame,
526 const int theActionId,
532 OCCViewer_ViewWindow* aView = theViewFrame->getView( OCCViewer_ViewFrame::MAIN_VIEW );
534 aView->toolMgr()->setShown( theActionId, isShown );
535 if ( theActionId == OCCViewer_ViewWindow::MaximizedId )
536 theViewFrame->onMaximizedView( aView, true );
540 void HYDROGUI_Tool::setOCCActionShown( HYDROGUI_Module* theModule,
541 const int theActionId,
545 ViewManagerList aViewMgrs;
546 theModule->getApp()->viewManagers( OCCViewer_Viewer::Type(), aViewMgrs );
547 QListIterator<SUIT_ViewManager*> anIter( aViewMgrs );
548 while( anIter.hasNext() )
550 if( SUIT_ViewManager* aViewMgr = anIter.next() )
552 OCCViewer_ViewFrame* aViewFrame = dynamic_cast<OCCViewer_ViewFrame*>
553 ( aViewMgr->getActiveView() );
555 setOCCActionShown( aViewFrame, theActionId, isShown );
560 void HYDROGUI_Tool::GetObjectReferences( const Handle(HYDROData_Entity)& theObj,
561 HYDROData_SequenceOfObjects& theRefObjects,
562 QStringList& theRefNames )
564 if( theObj.IsNull() )
567 HYDROData_SequenceOfObjects anAllRefObjects = theObj->GetAllReferenceObjects();
568 theRefObjects.Append( anAllRefObjects );
570 for( int i = 1, n = anAllRefObjects.Length(); i <= n; ++i )
572 Handle(HYDROData_Entity) aRefObj = theRefObjects.Value( i );
573 if( aRefObj.IsNull() || aRefObj->IsRemoved() )
576 QString aRefObjectName = aRefObj->GetName();
577 if( theRefNames.contains( aRefObjectName ) )
580 theRefObjects.Append( aRefObj );
581 theRefNames.append( aRefObjectName );
583 GetObjectReferences( aRefObj, theRefObjects, theRefNames );
587 HYDROData_SequenceOfObjects HYDROGUI_Tool::GetObjectBackReferences(
588 HYDROGUI_Module* theModule,
589 const Handle(HYDROData_Entity)& theObj )
591 if( theObj.IsNull() )
592 return HYDROData_SequenceOfObjects();
594 QString anObjName = theObj->GetName();
596 QMap<QString,HYDROData_SequenceOfObjects> aMapOfBackRefs =
597 GetObjectsBackReferences( theModule, QStringList() << anObjName );
599 return aMapOfBackRefs[ anObjName ];
602 QMap<QString,HYDROData_SequenceOfObjects> HYDROGUI_Tool::GetObjectsBackReferences(
603 HYDROGUI_Module* theModule, const QStringList& theObjectNames )
605 QMap<QString,HYDROData_SequenceOfObjects> aResMap;
607 if( theObjectNames.isEmpty() )
610 Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( theModule->getStudyId() );
611 if( aDocument.IsNull() )
614 HYDROData_Iterator anIterator( aDocument );
615 for( ; anIterator.More(); anIterator.Next() )
617 Handle(HYDROData_Entity) anObject = anIterator.Current();
618 if( anObject.IsNull() || anObject->IsRemoved() )
621 QString anObjectName = anObject->GetName();
622 if ( theObjectNames.contains( anObjectName ) )
625 HYDROData_SequenceOfObjects aRefObjects = anObject->GetAllReferenceObjects();
626 for ( int i = 1, n = aRefObjects.Length(); i <= n; ++i )
628 Handle(HYDROData_Entity) aRefObject = aRefObjects.Value( i );
629 if( aRefObject.IsNull() || aRefObject->IsRemoved() )
632 QString aRefObjectName = aRefObject->GetName();
633 if ( !theObjectNames.contains( aRefObjectName ) )
636 aResMap[ aRefObjectName ].Append( anObject );
643 QDockWidget* HYDROGUI_Tool::WindowDock( QWidget* wid )
648 QDockWidget* dock = 0;
649 QWidget* w = wid->parentWidget();
652 dock = ::qobject_cast<QDockWidget*>( w );
653 w = w->parentWidget();
658 QColor HYDROGUI_Tool::GenerateFillingColor( HYDROGUI_Module* theModule,
659 const QStringList& theZoneNames )
661 Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( theModule->getStudyId() );
662 return GenerateFillingColor( aDocument, theZoneNames );
665 QColor HYDROGUI_Tool::GenerateFillingColor( const Handle(HYDROData_Document)& theDoc,
666 const QStringList& theZoneNames )
668 QColor aFillingColor( HYDROData_ImmersibleZone::DefaultFillingColor() );
671 int aR = 0, aG = 0, aB = 0;
672 QStringListIterator aZoneNameIter( theZoneNames );
673 while( aZoneNameIter.hasNext() )
675 const QString& aZoneName = aZoneNameIter.next();
676 Handle(HYDROData_ImmersibleZone) aRefZone =
677 Handle(HYDROData_ImmersibleZone)::DownCast( theDoc->FindObjectByName( aZoneName, KIND_IMMERSIBLE_ZONE ) );
678 if( !aRefZone.IsNull() )
680 QColor aRefColor = aRefZone->GetFillingColor();
681 aR += aRefColor.red();
682 aG += aRefColor.green();
683 aB += aRefColor.blue();
690 aFillingColor = QColor( aR / aCounter, aG / aCounter, aB / aCounter );
693 return aFillingColor;
696 QStringList HYDROGUI_Tool::FindExistingObjectsNames( const Handle(HYDROData_Document)& theDoc,
697 const ObjectKind theObjectKind,
698 bool isCheckValidProfile )
702 HYDROData_Iterator anIter( theDoc, theObjectKind );
703 for ( ; anIter.More(); anIter.Next() ) {
704 Handle(HYDROData_Entity) anObject = anIter.Current();
706 bool isOK = !anObject.IsNull();
708 if( isOK && isCheckValidProfile )
710 Handle_HYDROData_Profile aProfile = Handle_HYDROData_Profile::DownCast( anObject );
711 if( !aProfile.IsNull() && !aProfile->IsValid() )
716 aNames.append( anObject->GetName() );
722 QString HYDROGUI_Tool::GetCoordinateString( const double theNumber, bool isInLocale )
726 static QLocale aLocale( QLocale::English, QLocale::France );
727 return aLocale.toString( theNumber, 'f', 2 );
730 return QString::number( theNumber, 'f', 2 );