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>
62 #include <QDockWidget>
65 // Definition of this id allows to use 'latin1' (Qt alias for 'ISO-8859-1')
66 // encoding instead of default 'System'
67 #define USE_LATIN1_ENCODING
69 QString HYDROGUI_Tool::ToQString( const TCollection_AsciiString& src )
71 #ifdef USE_LATIN1_ENCODING
72 QTextCodec* codec = QTextCodec::codecForName( "latin1" ); // alias for ISO-8859-1
74 QTextCodec* codec = QTextCodec::codecForLocale();
78 res = codec ? codec->toUnicode( (char*)src.ToCString(), src.Length() ) :
79 QString( (char*)src.ToCString() );
83 QString HYDROGUI_Tool::ToQString( const TCollection_ExtendedString& src )
85 return QString( (QChar*)src.ToExtString(), src.Length() );
88 QString HYDROGUI_Tool::ToQString( const Handle(TCollection_HAsciiString)& src )
93 return ToQString( src->String() );
96 QString HYDROGUI_Tool::ToQString( const Handle(TCollection_HExtendedString)& src )
100 return ToQString( src->String() );
103 TCollection_AsciiString HYDROGUI_Tool::ToAsciiString( const QString& src )
105 TCollection_AsciiString res;
108 #ifdef USE_LATIN1_ENCODING
109 QTextCodec* codec = QTextCodec::codecForName( "latin1" ); // alias for ISO-8859-1
111 QTextCodec* codec = QTextCodec::codecForLocale();
115 QByteArray str = codec->fromUnicode( src );
116 res = TCollection_AsciiString( (Standard_CString)str.constData() );
119 res = TCollection_AsciiString( src.toLatin1().data() );
124 TCollection_ExtendedString HYDROGUI_Tool::ToExtString( const QString& src )
127 return TCollection_ExtendedString();
129 Standard_Integer len = src.length();
130 Standard_ExtString extStr = new Standard_ExtCharacter[ ( len + 1 ) * 2 ];
131 memcpy( (void*)extStr, src.unicode(), len * 2 );
132 ((short*)extStr)[ len ] = 0;
134 TCollection_ExtendedString trg( extStr );
139 Handle(TCollection_HAsciiString) HYDROGUI_Tool::ToHAsciiString( const QString& src )
141 return new TCollection_HAsciiString( ToAsciiString( src ) );
144 Handle(TCollection_HExtendedString) HYDROGUI_Tool::ToHExtString( const QString& src )
146 return new TCollection_HExtendedString( ToExtString( src ) );
149 int HYDROGUI_Tool::GetActiveStudyId()
151 if( SUIT_Session* aSession = SUIT_Session::session() )
152 if( SUIT_Application* anApp = aSession->activeApplication() )
153 if( SUIT_Study* aStudy = anApp->activeStudy() )
158 QString HYDROGUI_Tool::GetTempDir( const bool theToCreate )
162 char* tmpdir = getenv ( "HYDRO_TMP_DIR" );
165 // try to create folder if it does not exist
166 QFileInfo fi( tmpdir );
167 if ( !fi.exists() && theToCreate )
169 if ( QDir().mkdir( tmpdir ) )
170 QFile::setPermissions( tmpdir, (QFile::Permissions)0x4FFFF );
171 QFileInfo fi( tmpdir );
172 if ( !fi.exists() || !fi.isWritable() )
177 tmpdir = getenv ( "TEMP" );
179 tmpdir = getenv ( "TMP" );
190 QFileInfo fi( aRes );
191 if ( !fi.exists() || !fi.isWritable() )
192 aRes = QString::null;
197 void HYDROGUI_Tool::SetActiveViewManager( HYDROGUI_Module* theModule,
198 SUIT_ViewManager* theViewManager )
201 if( SUIT_ViewWindow* aViewWindow = theViewManager->getActiveView() )
202 if( STD_TabDesktop* aTabDesktop = dynamic_cast<STD_TabDesktop*>( theModule->getApp()->desktop() ) )
203 if( QtxWorkstack* aWorkstack = aTabDesktop->workstack() )
204 aWorkstack->setActiveWindow( aViewWindow );
207 void HYDROGUI_Tool::GetPrsSubObjects( HYDROGUI_Module* theModule,
208 HYDROData_SequenceOfObjects& theSeq )
210 Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( theModule->getStudyId() );
211 if( !aDocument.IsNull() )
213 HYDROData_Iterator anIterator( aDocument );
214 for( ; anIterator.More(); anIterator.Next() )
216 Handle(HYDROData_Entity) anObject = anIterator.Current();
217 if( !anObject.IsNull() && (
218 ( anObject->GetKind() == KIND_IMAGE ) ||
219 ( anObject->GetKind() == KIND_POLYLINEXY ) ||
220 ( anObject->GetKind() == KIND_POLYLINE ) ||
221 ( anObject->GetKind() == KIND_IMMERSIBLE_ZONE ) ||
222 ( anObject->GetKind() == KIND_REGION ) ||
223 ( anObject->GetKind() == KIND_BATHYMETRY ) ||
224 ( anObject->GetKind() == KIND_ZONE ) ||
225 ( anObject->GetKind() == KIND_OBSTACLE ) ||
226 ( anObject->GetKind() == KIND_PROFILE ) ||
227 ( anObject->GetKind() == KIND_STREAM ) ||
228 ( anObject->GetKind() == KIND_CHANNEL ) ) )
230 theSeq.Append( anObject );
236 HYDROGUI_Prs* HYDROGUI_Tool::GetPresentation( const Handle(HYDROData_Entity)& theObj,
237 const GraphicsView_ObjectList& theObjects )
239 if( !theObj.IsNull() )
241 GraphicsView_ObjectListIterator anIter( theObjects );
242 while( anIter.hasNext() )
244 if( HYDROGUI_Prs* aPrs = dynamic_cast<HYDROGUI_Prs*>( anIter.next() ) )
246 Handle(HYDROData_Entity) anObj = aPrs->getObject();
247 if( IsEqual( anObj, theObj ) )
255 GraphicsView_ObjectList HYDROGUI_Tool::GetPrsList( GraphicsView_ViewPort* theViewPort )
257 GraphicsView_ObjectList aList;
260 GraphicsView_ObjectListIterator anIter( theViewPort->getObjects() );
261 while( anIter.hasNext() )
262 if( HYDROGUI_Prs* aPrs = dynamic_cast<HYDROGUI_Prs*>( anIter.next() ) )
263 aList.append( aPrs );
268 HYDROData_SequenceOfObjects HYDROGUI_Tool::GetSelectedObjects( HYDROGUI_Module* theModule )
270 HYDROData_SequenceOfObjects aSeq;
272 HYDROGUI_DataModel* aModel = theModule->getDataModel();
274 SUIT_SelectionMgr* aSelectionMgr = theModule->getApp()->selectionMgr();
275 SUIT_DataOwnerPtrList anOwners;
276 aSelectionMgr->selected( anOwners );
278 QStringList aCollectedNameList; // to avoid duplication
279 foreach( SUIT_DataOwner* aSUITOwner, anOwners )
281 if( LightApp_DataOwner* anOwner = dynamic_cast<LightApp_DataOwner*>( aSUITOwner ) )
283 Handle(HYDROData_Entity) anObject = aModel->objectByEntry( anOwner->entry() );
284 if( !anObject.IsNull() )
286 QString aName = anObject->GetName();
287 if( !aCollectedNameList.contains( aName ) )
289 aSeq.Append( anObject );
290 aCollectedNameList.append( aName );
298 Handle(HYDROData_Entity) HYDROGUI_Tool::GetSelectedObject( HYDROGUI_Module* theModule )
300 HYDROData_SequenceOfObjects aSeq = GetSelectedObjects( theModule );
301 if( !aSeq.IsEmpty() )
306 HYDROData_SequenceOfObjects HYDROGUI_Tool::GetGeometryObjects(
307 HYDROGUI_Module* theModule )
309 Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( theModule->getStudyId() );
311 HYDROData_SequenceOfObjects aResSeq;
313 HYDROData_Iterator anIter( aDocument );
314 for ( ; anIter.More(); anIter.Next() )
316 Handle(HYDROData_Entity) anObj = anIter.Current();
317 if ( !HYDROData_Tool::IsGeometryObject( anObj ) )
320 aResSeq.Append( anObj );
326 ObjectKind HYDROGUI_Tool::GetSelectedPartition( HYDROGUI_Module* theModule )
328 HYDROGUI_DataModel* aModel = theModule->getDataModel();
330 SUIT_SelectionMgr* aSelectionMgr = theModule->getApp()->selectionMgr();
331 SUIT_DataOwnerPtrList anOwners;
332 aSelectionMgr->selected( anOwners );
334 if( anOwners.size() != 1 )
337 if( LightApp_DataOwner* anOwner = dynamic_cast<LightApp_DataOwner*>( anOwners.first().operator->() ) )
339 QString anEntry = anOwner->entry();
340 QString aPrefix = HYDROGUI_DataObject::entryPrefix();
341 if( anEntry.left( aPrefix.length() ) == aPrefix )
343 anEntry.remove( aPrefix );
344 for( ObjectKind anObjectKind = KIND_UNKNOWN + 1; anObjectKind <= KIND_LAST; anObjectKind++ )
345 if( HYDROGUI_DataModel::tr( HYDROGUI_DataModel::partitionName( anObjectKind ).toAscii() ) == anEntry )
352 QStringList HYDROGUI_Tool::GetSelectedGeomObjects( HYDROGUI_Module* theModule )
354 QStringList anEntryList;
356 // Get active SalomeApp_Study
357 SalomeApp_Study* aStudy = NULL;
358 if ( theModule && theModule->getApp() ) {
359 aStudy = dynamic_cast<SalomeApp_Study*>( theModule->getApp()->activeStudy() );
366 SUIT_SelectionMgr* aSelectionMgr = theModule->getApp()->selectionMgr();
367 SUIT_DataOwnerPtrList anOwners;
368 aSelectionMgr->selected( anOwners );
370 // Check if the selected objects belong to GEOM and have a shape
371 foreach( SUIT_DataOwner* aSUITOwner, anOwners )
373 if( LightApp_DataOwner* anOwner = dynamic_cast<LightApp_DataOwner*>( aSUITOwner ) )
375 QString anEntry = anOwner->entry();
376 _PTR(SObject) aSObject( aStudy->studyDS()->FindObjectID(qPrintable(anEntry)) );
378 _PTR(SComponent) aSComponent = aSObject->GetFatherComponent();
379 if ( aSComponent && aSComponent->ComponentDataType() == "GEOM" ) {
380 GEOM::GEOM_Object_var aGeomObj =
381 GEOMBase::GetObjectFromIOR( aSObject->GetIOR().c_str() );
383 if ( !aGeomObj->_is_nil() && aGeomObj->IsShape() ) {
384 anEntryList << anEntry;
394 Handle(HYDROData_Entity) HYDROGUI_Tool::FindObjectByName( HYDROGUI_Module* theModule,
395 const QString& theName,
396 const ObjectKind theObjectKind )
398 Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( theModule->getStudyId() );
399 return HYDROData_Tool::FindObjectByName( aDocument, theName, theObjectKind );
402 HYDROData_SequenceOfObjects HYDROGUI_Tool::FindObjectsByNames( HYDROGUI_Module* theModule,
403 const QStringList& theNames,
404 const ObjectKind theObjectKind )
406 Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( theModule->getStudyId() );
407 return HYDROData_Tool::FindObjectsByNames( aDocument, theNames, theObjectKind );
410 QString HYDROGUI_Tool::GenerateObjectName( HYDROGUI_Module* theModule,
411 const QString& thePrefix,
412 const QStringList& theUsedNames,
413 const bool theIsTryToUsePurePrefix)
415 Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( theModule->getStudyId() );
416 return HYDROData_Tool::GenerateObjectName( aDocument, thePrefix, theUsedNames, theIsTryToUsePurePrefix );
419 size_t HYDROGUI_Tool::GetActiveViewId( HYDROGUI_Module* theModule,
420 const QString& theViewId )
423 SUIT_ViewManager* aViewMgr = theModule->getApp()->activeViewManager();
424 if( !aViewMgr || ( !theViewId.isEmpty() && aViewMgr->getType() != theViewId ) )
427 if( SUIT_ViewModel* aViewer = aViewMgr->getViewModel() )
428 aViewId = (size_t)aViewer;
432 size_t HYDROGUI_Tool::GetActiveGraphicsViewId( HYDROGUI_Module* theModule )
434 return GetActiveViewId( theModule, GraphicsView_Viewer::Type() );
437 size_t HYDROGUI_Tool::GetActiveOCCViewId( HYDROGUI_Module* theModule )
439 return GetActiveViewId( theModule, OCCViewer_Viewer::Type() );
442 QList<size_t> getViewIdList( HYDROGUI_Module* theModule,
443 const QString& theViewId )
446 ViewManagerList aViewMgrs;
447 theModule->getApp()->viewManagers( theViewId, aViewMgrs );
448 QListIterator<SUIT_ViewManager*> anIter( aViewMgrs );
449 while( anIter.hasNext() )
451 if( SUIT_ViewManager* aViewMgr = anIter.next() )
453 if( SUIT_ViewModel* aViewer = aViewMgr->getViewModel() )
454 aList.append( (size_t)aViewer );
460 QList<size_t> HYDROGUI_Tool::GetGraphicsViewIdList( HYDROGUI_Module* theModule )
462 return getViewIdList( theModule, GraphicsView_Viewer::Type() );
465 QList<size_t> HYDROGUI_Tool::GetOCCViewIdList( HYDROGUI_Module* theModule )
467 return getViewIdList( theModule, OCCViewer_Viewer::Type() );
470 void HYDROGUI_Tool::setOCCActionShown( OCCViewer_ViewFrame* theViewFrame,
471 const int theActionId,
477 OCCViewer_ViewWindow* aView = theViewFrame->getView( OCCViewer_ViewFrame::MAIN_VIEW );
479 aView->toolMgr()->setShown( theActionId, isShown );
480 if ( theActionId == OCCViewer_ViewWindow::MaximizedId )
481 theViewFrame->onMaximizedView( aView, true );
485 void HYDROGUI_Tool::setOCCActionShown( HYDROGUI_Module* theModule,
486 const int theActionId,
490 ViewManagerList aViewMgrs;
491 theModule->getApp()->viewManagers( OCCViewer_Viewer::Type(), aViewMgrs );
492 QListIterator<SUIT_ViewManager*> anIter( aViewMgrs );
493 while( anIter.hasNext() )
495 if( SUIT_ViewManager* aViewMgr = anIter.next() )
497 OCCViewer_ViewFrame* aViewFrame = dynamic_cast<OCCViewer_ViewFrame*>
498 ( aViewMgr->getActiveView() );
500 setOCCActionShown( aViewFrame, theActionId, isShown );
505 void HYDROGUI_Tool::GetObjectReferences( const Handle(HYDROData_Entity)& theObj,
506 HYDROData_SequenceOfObjects& theRefObjects,
507 QStringList& theRefNames )
509 if( theObj.IsNull() )
512 HYDROData_SequenceOfObjects anAllRefObjects = theObj->GetAllReferenceObjects();
513 theRefObjects.Append( anAllRefObjects );
515 for( int i = 1, n = anAllRefObjects.Length(); i <= n; ++i )
517 Handle(HYDROData_Entity) aRefObj = theRefObjects.Value( i );
518 if( aRefObj.IsNull() || aRefObj->IsRemoved() )
521 QString aRefObjectName = aRefObj->GetName();
522 if( theRefNames.contains( aRefObjectName ) )
525 theRefObjects.Append( aRefObj );
526 theRefNames.append( aRefObjectName );
528 GetObjectReferences( aRefObj, theRefObjects, theRefNames );
532 HYDROData_SequenceOfObjects HYDROGUI_Tool::GetObjectBackReferences(
533 HYDROGUI_Module* theModule,
534 const Handle(HYDROData_Entity)& theObj )
536 if( theObj.IsNull() )
537 return HYDROData_SequenceOfObjects();
539 QString anObjName = theObj->GetName();
541 QMap<QString,HYDROData_SequenceOfObjects> aMapOfBackRefs =
542 GetObjectsBackReferences( theModule, QStringList() << anObjName );
544 return aMapOfBackRefs[ anObjName ];
547 QMap<QString,HYDROData_SequenceOfObjects> HYDROGUI_Tool::GetObjectsBackReferences(
548 HYDROGUI_Module* theModule, const QStringList& theObjectNames )
550 QMap<QString,HYDROData_SequenceOfObjects> aResMap;
552 if( theObjectNames.isEmpty() )
555 Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( theModule->getStudyId() );
556 if( aDocument.IsNull() )
559 HYDROData_Iterator anIterator( aDocument );
560 for( ; anIterator.More(); anIterator.Next() )
562 Handle(HYDROData_Entity) anObject = anIterator.Current();
563 if( anObject.IsNull() || anObject->IsRemoved() )
566 QString anObjectName = anObject->GetName();
567 if ( theObjectNames.contains( anObjectName ) )
570 HYDROData_SequenceOfObjects aRefObjects = anObject->GetAllReferenceObjects();
571 for ( int i = 1, n = aRefObjects.Length(); i <= n; ++i )
573 Handle(HYDROData_Entity) aRefObject = aRefObjects.Value( i );
574 if( aRefObject.IsNull() || aRefObject->IsRemoved() )
577 QString aRefObjectName = aRefObject->GetName();
578 if ( !theObjectNames.contains( aRefObjectName ) )
581 aResMap[ aRefObjectName ].Append( anObject );
588 QDockWidget* HYDROGUI_Tool::WindowDock( QWidget* wid )
593 QDockWidget* dock = 0;
594 QWidget* w = wid->parentWidget();
597 dock = ::qobject_cast<QDockWidget*>( w );
598 w = w->parentWidget();
603 QColor HYDROGUI_Tool::GenerateFillingColor( HYDROGUI_Module* theModule,
604 const QStringList& theZoneNames )
606 Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( theModule->getStudyId() );
607 return GenerateFillingColor( aDocument, theZoneNames );
610 QColor HYDROGUI_Tool::GenerateFillingColor( const Handle(HYDROData_Document)& theDoc,
611 const QStringList& theZoneNames )
613 QColor aFillingColor( HYDROData_ImmersibleZone::DefaultFillingColor() );
616 int aR = 0, aG = 0, aB = 0;
617 QStringListIterator aZoneNameIter( theZoneNames );
618 while( aZoneNameIter.hasNext() )
620 const QString& aZoneName = aZoneNameIter.next();
621 Handle(HYDROData_ImmersibleZone) aRefZone =
622 Handle(HYDROData_ImmersibleZone)::DownCast(
623 HYDROData_Tool::FindObjectByName( theDoc, aZoneName, KIND_IMMERSIBLE_ZONE ) );
624 if( !aRefZone.IsNull() )
626 QColor aRefColor = aRefZone->GetFillingColor();
627 aR += aRefColor.red();
628 aG += aRefColor.green();
629 aB += aRefColor.blue();
636 aFillingColor = QColor( aR / aCounter, aG / aCounter, aB / aCounter );
639 return aFillingColor;
642 QStringList HYDROGUI_Tool::FindExistingObjectsNames( const Handle(HYDROData_Document)& theDoc,
643 const ObjectKind theObjectKind )
647 HYDROData_Iterator anIter( theDoc, theObjectKind );
648 for ( ; anIter.More(); anIter.Next() ) {
649 Handle(HYDROData_Entity) anObject = anIter.Current();
650 if( !anObject.IsNull() ) {
651 aNames.append( anObject->GetName() );
658 QString HYDROGUI_Tool::GetCoordinateString( const double theNumber )
660 return QString::number( theNumber, 'f', 2 );