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 ||
233 anObjectKind == KIND_BATHYMETRY
235 || anObjectKind == KIND_SHAPES_GROUP ||
236 anObjectKind == KIND_SPLITTED_GROUP
244 if ( theViewerType.isEmpty() || theViewerType == SVTK_Viewer::Type() )
246 if ( anObjectKind == KIND_BATHYMETRY )
250 if ( theViewerType.isEmpty() || theViewerType == GraphicsView_Viewer::Type() )
252 if ( anObjectKind == KIND_IMAGE ||
253 anObjectKind == KIND_POLYLINEXY )
260 void HYDROGUI_Tool::GetPrsSubObjects( HYDROGUI_Module* theModule,
261 HYDROData_SequenceOfObjects& theSeq )
263 Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( theModule->getStudyId() );
264 if( !aDocument.IsNull() )
266 HYDROData_Iterator anIterator( aDocument );
267 for( ; anIterator.More(); anIterator.Next() )
269 Handle(HYDROData_Entity) anObject = anIterator.Current();
270 if ( !IsObjectHasPresentation( anObject ) )
273 theSeq.Append( anObject );
278 HYDROGUI_Prs* HYDROGUI_Tool::GetPresentation( const Handle(HYDROData_Entity)& theObj,
279 const GraphicsView_ObjectList& theObjects )
281 if( !theObj.IsNull() )
283 GraphicsView_ObjectListIterator anIter( theObjects );
284 while( anIter.hasNext() )
286 if( HYDROGUI_Prs* aPrs = dynamic_cast<HYDROGUI_Prs*>( anIter.next() ) )
288 Handle(HYDROData_Entity) anObj = aPrs->getObject();
289 if( IsEqual( anObj, theObj ) )
297 GraphicsView_ObjectList HYDROGUI_Tool::GetPrsList( GraphicsView_ViewPort* theViewPort )
299 GraphicsView_ObjectList aList;
302 GraphicsView_ObjectListIterator anIter( theViewPort->getObjects() );
303 while( anIter.hasNext() )
304 if( HYDROGUI_Prs* aPrs = dynamic_cast<HYDROGUI_Prs*>( anIter.next() ) )
305 aList.append( aPrs );
310 HYDROData_SequenceOfObjects HYDROGUI_Tool::GetSelectedObjects( HYDROGUI_Module* theModule )
312 HYDROData_SequenceOfObjects aSeq;
314 HYDROGUI_DataModel* aModel = theModule->getDataModel();
316 SUIT_SelectionMgr* aSelectionMgr = theModule->getApp()->selectionMgr();
317 SUIT_DataOwnerPtrList anOwners;
318 aSelectionMgr->selected( anOwners );
320 QStringList aCollectedNameList; // to avoid duplication
321 foreach( SUIT_DataOwner* aSUITOwner, anOwners )
323 if( LightApp_DataOwner* anOwner = dynamic_cast<LightApp_DataOwner*>( aSUITOwner ) )
325 Handle(HYDROData_Entity) anObject = aModel->objectByEntry( anOwner->entry() );
326 if( !anObject.IsNull() )
328 QString aName = anObject->GetName();
329 if( !aCollectedNameList.contains( aName ) )
331 aSeq.Append( anObject );
332 aCollectedNameList.append( aName );
340 Handle(HYDROData_Entity) HYDROGUI_Tool::GetSelectedObject( HYDROGUI_Module* theModule )
342 HYDROData_SequenceOfObjects aSeq = GetSelectedObjects( theModule );
343 if( !aSeq.IsEmpty() )
348 HYDROData_SequenceOfObjects HYDROGUI_Tool::GetGeometryObjects(
349 HYDROGUI_Module* theModule )
351 Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( theModule->getStudyId() );
353 HYDROData_SequenceOfObjects aResSeq;
355 HYDROData_Iterator anIter( aDocument );
356 for ( ; anIter.More(); anIter.Next() )
358 Handle(HYDROData_Entity) anObj = anIter.Current();
359 if ( !HYDROData_Tool::IsGeometryObject( anObj ) )
362 aResSeq.Append( anObj );
368 ObjectKind HYDROGUI_Tool::GetSelectedPartition( HYDROGUI_Module* theModule )
370 HYDROGUI_DataModel* aModel = theModule->getDataModel();
372 SUIT_SelectionMgr* aSelectionMgr = theModule->getApp()->selectionMgr();
373 SUIT_DataOwnerPtrList anOwners;
374 aSelectionMgr->selected( anOwners );
376 if( anOwners.size() != 1 )
379 if( LightApp_DataOwner* anOwner = dynamic_cast<LightApp_DataOwner*>( anOwners.first().operator->() ) )
381 QString anEntry = anOwner->entry();
382 QString aPrefix = HYDROGUI_DataObject::entryPrefix();
383 if( anEntry.left( aPrefix.length() ) == aPrefix )
385 anEntry.remove( aPrefix );
386 for( ObjectKind anObjectKind = KIND_UNKNOWN + 1; anObjectKind <= KIND_LAST; anObjectKind++ )
387 if( HYDROGUI_DataModel::tr( HYDROGUI_DataModel::partitionName( anObjectKind ).toAscii() ) == anEntry )
394 QStringList HYDROGUI_Tool::GetSelectedGeomObjects( HYDROGUI_Module* theModule,
395 QList<GEOM::shape_type> theTypes )
397 QStringList anEntryList;
399 // Get active SalomeApp_Study
400 SalomeApp_Study* aStudy = NULL;
401 if ( theModule && theModule->getApp() ) {
402 aStudy = dynamic_cast<SalomeApp_Study*>( theModule->getApp()->activeStudy() );
409 SUIT_SelectionMgr* aSelectionMgr = theModule->getApp()->selectionMgr();
410 SUIT_DataOwnerPtrList anOwners;
411 aSelectionMgr->selected( anOwners );
413 // Check if the selected objects belong to GEOM and have a shape
414 foreach( SUIT_DataOwner* aSUITOwner, anOwners )
416 if( LightApp_DataOwner* anOwner = dynamic_cast<LightApp_DataOwner*>( aSUITOwner ) )
418 QString anEntry = anOwner->entry();
419 _PTR(SObject) aSObject( aStudy->studyDS()->FindObjectID(qPrintable(anEntry)) );
421 _PTR(SComponent) aSComponent = aSObject->GetFatherComponent();
422 if ( aSComponent && aSComponent->ComponentDataType() == "GEOM" ) {
423 GEOM::GEOM_Object_var aGeomObj =
424 GEOMBase::GetObjectFromIOR( aSObject->GetIOR().c_str() );
426 if ( !aGeomObj->_is_nil() && aGeomObj->IsShape() &&
427 theTypes.contains( aGeomObj->GetShapeType() ) ) {
428 anEntryList << anEntry;
438 Handle(HYDROData_Entity) HYDROGUI_Tool::FindObjectByName( HYDROGUI_Module* theModule,
439 const QString& theName,
440 const ObjectKind theObjectKind )
442 Handle(HYDROData_Entity) aResObj;
444 Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( theModule->getStudyId() );
445 if ( !aDocument.IsNull() )
446 aResObj = aDocument->FindObjectByName( theName, theObjectKind );
451 HYDROData_SequenceOfObjects HYDROGUI_Tool::FindObjectsByNames( HYDROGUI_Module* theModule,
452 const QStringList& theNames,
453 const ObjectKind theObjectKind )
455 HYDROData_SequenceOfObjects aResSeq;
457 Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( theModule->getStudyId() );
458 if ( !aDocument.IsNull() )
459 aResSeq = aDocument->FindObjectsByNames( theNames, theObjectKind );
464 QString HYDROGUI_Tool::GenerateObjectName( HYDROGUI_Module* theModule,
465 const QString& thePrefix,
466 const QStringList& theUsedNames,
467 const bool theIsTryToUsePurePrefix)
469 Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( theModule->getStudyId() );
470 return HYDROData_Tool::GenerateObjectName( aDocument, thePrefix, theUsedNames, theIsTryToUsePurePrefix );
473 size_t HYDROGUI_Tool::GetActiveViewId( HYDROGUI_Module* theModule,
474 const QString& theViewId )
477 SUIT_ViewManager* aViewMgr = theModule->getApp()->activeViewManager();
478 if( !aViewMgr || ( !theViewId.isEmpty() && aViewMgr->getType() != theViewId ) )
481 if( SUIT_ViewModel* aViewer = aViewMgr->getViewModel() )
482 aViewId = (size_t)aViewer;
486 size_t HYDROGUI_Tool::GetActiveGraphicsViewId( HYDROGUI_Module* theModule )
488 return GetActiveViewId( theModule, GraphicsView_Viewer::Type() );
491 size_t HYDROGUI_Tool::GetActiveOCCViewId( HYDROGUI_Module* theModule )
493 return GetActiveViewId( theModule, OCCViewer_Viewer::Type() );
496 QList<size_t> getViewIdList( HYDROGUI_Module* theModule,
497 const QString& theViewId )
500 ViewManagerList aViewMgrs;
501 theModule->getApp()->viewManagers( theViewId, aViewMgrs );
502 QListIterator<SUIT_ViewManager*> anIter( aViewMgrs );
503 while( anIter.hasNext() )
505 if( SUIT_ViewManager* aViewMgr = anIter.next() )
507 if( SUIT_ViewModel* aViewer = aViewMgr->getViewModel() )
508 aList.append( (size_t)aViewer );
514 QList<size_t> HYDROGUI_Tool::GetGraphicsViewIdList( HYDROGUI_Module* theModule )
516 return getViewIdList( theModule, GraphicsView_Viewer::Type() );
519 QList<size_t> HYDROGUI_Tool::GetOCCViewIdList( HYDROGUI_Module* theModule )
521 return getViewIdList( theModule, OCCViewer_Viewer::Type() );
524 void HYDROGUI_Tool::setOCCActionShown( OCCViewer_ViewFrame* theViewFrame,
525 const int theActionId,
531 OCCViewer_ViewWindow* aView = theViewFrame->getView( OCCViewer_ViewFrame::MAIN_VIEW );
533 aView->toolMgr()->setShown( theActionId, isShown );
534 if ( theActionId == OCCViewer_ViewWindow::MaximizedId )
535 theViewFrame->onMaximizedView( aView, true );
539 void HYDROGUI_Tool::setOCCActionShown( HYDROGUI_Module* theModule,
540 const int theActionId,
544 ViewManagerList aViewMgrs;
545 theModule->getApp()->viewManagers( OCCViewer_Viewer::Type(), aViewMgrs );
546 QListIterator<SUIT_ViewManager*> anIter( aViewMgrs );
547 while( anIter.hasNext() )
549 if( SUIT_ViewManager* aViewMgr = anIter.next() )
551 OCCViewer_ViewFrame* aViewFrame = dynamic_cast<OCCViewer_ViewFrame*>
552 ( aViewMgr->getActiveView() );
554 setOCCActionShown( aViewFrame, theActionId, isShown );
559 void HYDROGUI_Tool::GetObjectReferences( const Handle(HYDROData_Entity)& theObj,
560 HYDROData_SequenceOfObjects& theRefObjects,
561 QStringList& theRefNames )
563 if( theObj.IsNull() )
566 HYDROData_SequenceOfObjects anAllRefObjects = theObj->GetAllReferenceObjects();
567 theRefObjects.Append( anAllRefObjects );
569 for( int i = 1, n = anAllRefObjects.Length(); i <= n; ++i )
571 Handle(HYDROData_Entity) aRefObj = theRefObjects.Value( i );
572 if( aRefObj.IsNull() || aRefObj->IsRemoved() )
575 QString aRefObjectName = aRefObj->GetName();
576 if( theRefNames.contains( aRefObjectName ) )
579 theRefObjects.Append( aRefObj );
580 theRefNames.append( aRefObjectName );
582 GetObjectReferences( aRefObj, theRefObjects, theRefNames );
586 HYDROData_SequenceOfObjects HYDROGUI_Tool::GetObjectBackReferences(
587 HYDROGUI_Module* theModule,
588 const Handle(HYDROData_Entity)& theObj )
590 if( theObj.IsNull() )
591 return HYDROData_SequenceOfObjects();
593 QString anObjName = theObj->GetName();
595 QMap<QString,HYDROData_SequenceOfObjects> aMapOfBackRefs =
596 GetObjectsBackReferences( theModule, QStringList() << anObjName );
598 return aMapOfBackRefs[ anObjName ];
601 QMap<QString,HYDROData_SequenceOfObjects> HYDROGUI_Tool::GetObjectsBackReferences(
602 HYDROGUI_Module* theModule, const QStringList& theObjectNames )
604 QMap<QString,HYDROData_SequenceOfObjects> aResMap;
606 if( theObjectNames.isEmpty() )
609 Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( theModule->getStudyId() );
610 if( aDocument.IsNull() )
613 HYDROData_Iterator anIterator( aDocument );
614 for( ; anIterator.More(); anIterator.Next() )
616 Handle(HYDROData_Entity) anObject = anIterator.Current();
617 if( anObject.IsNull() || anObject->IsRemoved() )
620 QString anObjectName = anObject->GetName();
621 if ( theObjectNames.contains( anObjectName ) )
624 HYDROData_SequenceOfObjects aRefObjects = anObject->GetAllReferenceObjects();
625 for ( int i = 1, n = aRefObjects.Length(); i <= n; ++i )
627 Handle(HYDROData_Entity) aRefObject = aRefObjects.Value( i );
628 if( aRefObject.IsNull() || aRefObject->IsRemoved() )
631 QString aRefObjectName = aRefObject->GetName();
632 if ( !theObjectNames.contains( aRefObjectName ) )
635 aResMap[ aRefObjectName ].Append( anObject );
642 QDockWidget* HYDROGUI_Tool::WindowDock( QWidget* wid )
647 QDockWidget* dock = 0;
648 QWidget* w = wid->parentWidget();
651 dock = ::qobject_cast<QDockWidget*>( w );
652 w = w->parentWidget();
657 QColor HYDROGUI_Tool::GenerateFillingColor( HYDROGUI_Module* theModule,
658 const QStringList& theZoneNames )
660 Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( theModule->getStudyId() );
661 return GenerateFillingColor( aDocument, theZoneNames );
664 QColor HYDROGUI_Tool::GenerateFillingColor( const Handle(HYDROData_Document)& theDoc,
665 const QStringList& theZoneNames )
667 QColor aFillingColor( HYDROData_ImmersibleZone::DefaultFillingColor() );
670 int aR = 0, aG = 0, aB = 0;
671 QStringListIterator aZoneNameIter( theZoneNames );
672 while( aZoneNameIter.hasNext() )
674 const QString& aZoneName = aZoneNameIter.next();
675 Handle(HYDROData_ImmersibleZone) aRefZone =
676 Handle(HYDROData_ImmersibleZone)::DownCast( theDoc->FindObjectByName( aZoneName, KIND_IMMERSIBLE_ZONE ) );
677 if( !aRefZone.IsNull() )
679 QColor aRefColor = aRefZone->GetFillingColor();
680 aR += aRefColor.red();
681 aG += aRefColor.green();
682 aB += aRefColor.blue();
689 aFillingColor = QColor( aR / aCounter, aG / aCounter, aB / aCounter );
692 return aFillingColor;
695 QStringList HYDROGUI_Tool::FindExistingObjectsNames( const Handle(HYDROData_Document)& theDoc,
696 const ObjectKind theObjectKind )
700 HYDROData_Iterator anIter( theDoc, theObjectKind );
701 for ( ; anIter.More(); anIter.Next() ) {
702 Handle(HYDROData_Entity) anObject = anIter.Current();
703 if( !anObject.IsNull() ) {
704 aNames.append( anObject->GetName() );
711 QString HYDROGUI_Tool::GetCoordinateString( const double theNumber, bool isInLocale )
715 static QLocale aLocale( QLocale::English, QLocale::France );
716 return aLocale.toString( theNumber, 'f', 2 );
719 return QString::number( theNumber, 'f', 2 );