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 )
388 QStringList anEntryList;
390 // Get active SalomeApp_Study
391 SalomeApp_Study* aStudy = NULL;
392 if ( theModule && theModule->getApp() ) {
393 aStudy = dynamic_cast<SalomeApp_Study*>( theModule->getApp()->activeStudy() );
400 SUIT_SelectionMgr* aSelectionMgr = theModule->getApp()->selectionMgr();
401 SUIT_DataOwnerPtrList anOwners;
402 aSelectionMgr->selected( anOwners );
404 // Check if the selected objects belong to GEOM and have a shape
405 foreach( SUIT_DataOwner* aSUITOwner, anOwners )
407 if( LightApp_DataOwner* anOwner = dynamic_cast<LightApp_DataOwner*>( aSUITOwner ) )
409 QString anEntry = anOwner->entry();
410 _PTR(SObject) aSObject( aStudy->studyDS()->FindObjectID(qPrintable(anEntry)) );
412 _PTR(SComponent) aSComponent = aSObject->GetFatherComponent();
413 if ( aSComponent && aSComponent->ComponentDataType() == "GEOM" ) {
414 GEOM::GEOM_Object_var aGeomObj =
415 GEOMBase::GetObjectFromIOR( aSObject->GetIOR().c_str() );
417 if ( !aGeomObj->_is_nil() && aGeomObj->IsShape() ) {
418 anEntryList << anEntry;
428 Handle(HYDROData_Entity) HYDROGUI_Tool::FindObjectByName( HYDROGUI_Module* theModule,
429 const QString& theName,
430 const ObjectKind theObjectKind )
432 Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( theModule->getStudyId() );
433 return HYDROData_Tool::FindObjectByName( aDocument, theName, theObjectKind );
436 HYDROData_SequenceOfObjects HYDROGUI_Tool::FindObjectsByNames( HYDROGUI_Module* theModule,
437 const QStringList& theNames,
438 const ObjectKind theObjectKind )
440 Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( theModule->getStudyId() );
441 return HYDROData_Tool::FindObjectsByNames( aDocument, theNames, theObjectKind );
444 QString HYDROGUI_Tool::GenerateObjectName( HYDROGUI_Module* theModule,
445 const QString& thePrefix,
446 const QStringList& theUsedNames,
447 const bool theIsTryToUsePurePrefix)
449 Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( theModule->getStudyId() );
450 return HYDROData_Tool::GenerateObjectName( aDocument, thePrefix, theUsedNames, theIsTryToUsePurePrefix );
453 size_t HYDROGUI_Tool::GetActiveViewId( HYDROGUI_Module* theModule,
454 const QString& theViewId )
457 SUIT_ViewManager* aViewMgr = theModule->getApp()->activeViewManager();
458 if( !aViewMgr || ( !theViewId.isEmpty() && aViewMgr->getType() != theViewId ) )
461 if( SUIT_ViewModel* aViewer = aViewMgr->getViewModel() )
462 aViewId = (size_t)aViewer;
466 size_t HYDROGUI_Tool::GetActiveGraphicsViewId( HYDROGUI_Module* theModule )
468 return GetActiveViewId( theModule, GraphicsView_Viewer::Type() );
471 size_t HYDROGUI_Tool::GetActiveOCCViewId( HYDROGUI_Module* theModule )
473 return GetActiveViewId( theModule, OCCViewer_Viewer::Type() );
476 QList<size_t> getViewIdList( HYDROGUI_Module* theModule,
477 const QString& theViewId )
480 ViewManagerList aViewMgrs;
481 theModule->getApp()->viewManagers( theViewId, aViewMgrs );
482 QListIterator<SUIT_ViewManager*> anIter( aViewMgrs );
483 while( anIter.hasNext() )
485 if( SUIT_ViewManager* aViewMgr = anIter.next() )
487 if( SUIT_ViewModel* aViewer = aViewMgr->getViewModel() )
488 aList.append( (size_t)aViewer );
494 QList<size_t> HYDROGUI_Tool::GetGraphicsViewIdList( HYDROGUI_Module* theModule )
496 return getViewIdList( theModule, GraphicsView_Viewer::Type() );
499 QList<size_t> HYDROGUI_Tool::GetOCCViewIdList( HYDROGUI_Module* theModule )
501 return getViewIdList( theModule, OCCViewer_Viewer::Type() );
504 void HYDROGUI_Tool::setOCCActionShown( OCCViewer_ViewFrame* theViewFrame,
505 const int theActionId,
511 OCCViewer_ViewWindow* aView = theViewFrame->getView( OCCViewer_ViewFrame::MAIN_VIEW );
513 aView->toolMgr()->setShown( theActionId, isShown );
514 if ( theActionId == OCCViewer_ViewWindow::MaximizedId )
515 theViewFrame->onMaximizedView( aView, true );
519 void HYDROGUI_Tool::setOCCActionShown( HYDROGUI_Module* theModule,
520 const int theActionId,
524 ViewManagerList aViewMgrs;
525 theModule->getApp()->viewManagers( OCCViewer_Viewer::Type(), aViewMgrs );
526 QListIterator<SUIT_ViewManager*> anIter( aViewMgrs );
527 while( anIter.hasNext() )
529 if( SUIT_ViewManager* aViewMgr = anIter.next() )
531 OCCViewer_ViewFrame* aViewFrame = dynamic_cast<OCCViewer_ViewFrame*>
532 ( aViewMgr->getActiveView() );
534 setOCCActionShown( aViewFrame, theActionId, isShown );
539 void HYDROGUI_Tool::GetObjectReferences( const Handle(HYDROData_Entity)& theObj,
540 HYDROData_SequenceOfObjects& theRefObjects,
541 QStringList& theRefNames )
543 if( theObj.IsNull() )
546 HYDROData_SequenceOfObjects anAllRefObjects = theObj->GetAllReferenceObjects();
547 theRefObjects.Append( anAllRefObjects );
549 for( int i = 1, n = anAllRefObjects.Length(); i <= n; ++i )
551 Handle(HYDROData_Entity) aRefObj = theRefObjects.Value( i );
552 if( aRefObj.IsNull() || aRefObj->IsRemoved() )
555 QString aRefObjectName = aRefObj->GetName();
556 if( theRefNames.contains( aRefObjectName ) )
559 theRefObjects.Append( aRefObj );
560 theRefNames.append( aRefObjectName );
562 GetObjectReferences( aRefObj, theRefObjects, theRefNames );
566 HYDROData_SequenceOfObjects HYDROGUI_Tool::GetObjectBackReferences(
567 HYDROGUI_Module* theModule,
568 const Handle(HYDROData_Entity)& theObj )
570 if( theObj.IsNull() )
571 return HYDROData_SequenceOfObjects();
573 QString anObjName = theObj->GetName();
575 QMap<QString,HYDROData_SequenceOfObjects> aMapOfBackRefs =
576 GetObjectsBackReferences( theModule, QStringList() << anObjName );
578 return aMapOfBackRefs[ anObjName ];
581 QMap<QString,HYDROData_SequenceOfObjects> HYDROGUI_Tool::GetObjectsBackReferences(
582 HYDROGUI_Module* theModule, const QStringList& theObjectNames )
584 QMap<QString,HYDROData_SequenceOfObjects> aResMap;
586 if( theObjectNames.isEmpty() )
589 Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( theModule->getStudyId() );
590 if( aDocument.IsNull() )
593 HYDROData_Iterator anIterator( aDocument );
594 for( ; anIterator.More(); anIterator.Next() )
596 Handle(HYDROData_Entity) anObject = anIterator.Current();
597 if( anObject.IsNull() || anObject->IsRemoved() )
600 QString anObjectName = anObject->GetName();
601 if ( theObjectNames.contains( anObjectName ) )
604 HYDROData_SequenceOfObjects aRefObjects = anObject->GetAllReferenceObjects();
605 for ( int i = 1, n = aRefObjects.Length(); i <= n; ++i )
607 Handle(HYDROData_Entity) aRefObject = aRefObjects.Value( i );
608 if( aRefObject.IsNull() || aRefObject->IsRemoved() )
611 QString aRefObjectName = aRefObject->GetName();
612 if ( !theObjectNames.contains( aRefObjectName ) )
615 aResMap[ aRefObjectName ].Append( anObject );
622 QDockWidget* HYDROGUI_Tool::WindowDock( QWidget* wid )
627 QDockWidget* dock = 0;
628 QWidget* w = wid->parentWidget();
631 dock = ::qobject_cast<QDockWidget*>( w );
632 w = w->parentWidget();
637 QColor HYDROGUI_Tool::GenerateFillingColor( HYDROGUI_Module* theModule,
638 const QStringList& theZoneNames )
640 Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( theModule->getStudyId() );
641 return GenerateFillingColor( aDocument, theZoneNames );
644 QColor HYDROGUI_Tool::GenerateFillingColor( const Handle(HYDROData_Document)& theDoc,
645 const QStringList& theZoneNames )
647 QColor aFillingColor( HYDROData_ImmersibleZone::DefaultFillingColor() );
650 int aR = 0, aG = 0, aB = 0;
651 QStringListIterator aZoneNameIter( theZoneNames );
652 while( aZoneNameIter.hasNext() )
654 const QString& aZoneName = aZoneNameIter.next();
655 Handle(HYDROData_ImmersibleZone) aRefZone =
656 Handle(HYDROData_ImmersibleZone)::DownCast(
657 HYDROData_Tool::FindObjectByName( theDoc, aZoneName, KIND_IMMERSIBLE_ZONE ) );
658 if( !aRefZone.IsNull() )
660 QColor aRefColor = aRefZone->GetFillingColor();
661 aR += aRefColor.red();
662 aG += aRefColor.green();
663 aB += aRefColor.blue();
670 aFillingColor = QColor( aR / aCounter, aG / aCounter, aB / aCounter );
673 return aFillingColor;
676 QStringList HYDROGUI_Tool::FindExistingObjectsNames( const Handle(HYDROData_Document)& theDoc,
677 const ObjectKind theObjectKind )
681 HYDROData_Iterator anIter( theDoc, theObjectKind );
682 for ( ; anIter.More(); anIter.Next() ) {
683 Handle(HYDROData_Entity) anObject = anIter.Current();
684 if( !anObject.IsNull() ) {
685 aNames.append( anObject->GetName() );
692 QString HYDROGUI_Tool::GetCoordinateString( const double theNumber )
694 return QString::number( theNumber, 'f', 2 );