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_IMMERSIBLE_ZONE ) ||
221 ( anObject->GetKind() == KIND_REGION ) ||
222 ( anObject->GetKind() == KIND_BATHYMETRY ) ||
223 ( anObject->GetKind() == KIND_ZONE ) ||
224 ( anObject->GetKind() == KIND_OBSTACLE ) ||
225 ( anObject->GetKind() == KIND_PROFILE ) ) )
227 theSeq.Append( anObject );
233 HYDROGUI_Prs* HYDROGUI_Tool::GetPresentation( const Handle(HYDROData_Entity)& theObj,
234 const GraphicsView_ObjectList& theObjects )
236 if( !theObj.IsNull() )
238 GraphicsView_ObjectListIterator anIter( theObjects );
239 while( anIter.hasNext() )
241 if( HYDROGUI_Prs* aPrs = dynamic_cast<HYDROGUI_Prs*>( anIter.next() ) )
243 Handle(HYDROData_Entity) anObj = aPrs->getObject();
244 if( IsEqual( anObj, theObj ) )
252 GraphicsView_ObjectList HYDROGUI_Tool::GetPrsList( GraphicsView_ViewPort* theViewPort )
254 GraphicsView_ObjectList aList;
257 GraphicsView_ObjectListIterator anIter( theViewPort->getObjects() );
258 while( anIter.hasNext() )
259 if( HYDROGUI_Prs* aPrs = dynamic_cast<HYDROGUI_Prs*>( anIter.next() ) )
260 aList.append( aPrs );
265 HYDROData_SequenceOfObjects HYDROGUI_Tool::GetSelectedObjects( HYDROGUI_Module* theModule )
267 HYDROData_SequenceOfObjects aSeq;
269 HYDROGUI_DataModel* aModel = theModule->getDataModel();
271 SUIT_SelectionMgr* aSelectionMgr = theModule->getApp()->selectionMgr();
272 SUIT_DataOwnerPtrList anOwners;
273 aSelectionMgr->selected( anOwners );
275 QStringList aCollectedNameList; // to avoid duplication
276 foreach( SUIT_DataOwner* aSUITOwner, anOwners )
278 if( LightApp_DataOwner* anOwner = dynamic_cast<LightApp_DataOwner*>( aSUITOwner ) )
280 Handle(HYDROData_Entity) anObject = aModel->objectByEntry( anOwner->entry() );
281 if( !anObject.IsNull() )
283 QString aName = anObject->GetName();
284 if( !aCollectedNameList.contains( aName ) )
286 aSeq.Append( anObject );
287 aCollectedNameList.append( aName );
295 Handle(HYDROData_Entity) HYDROGUI_Tool::GetSelectedObject( HYDROGUI_Module* theModule )
297 HYDROData_SequenceOfObjects aSeq = GetSelectedObjects( theModule );
298 if( !aSeq.IsEmpty() )
303 HYDROData_SequenceOfObjects HYDROGUI_Tool::GetGeometryObjects(
304 HYDROGUI_Module* theModule )
306 Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( theModule->getStudyId() );
308 HYDROData_SequenceOfObjects aResSeq;
310 HYDROData_Iterator anIter( aDocument );
311 for ( ; anIter.More(); anIter.Next() )
313 Handle(HYDROData_Entity) anObj = anIter.Current();
314 if ( !HYDROData_Tool::IsGeometryObject( anObj ) )
317 aResSeq.Append( anObj );
323 ObjectKind HYDROGUI_Tool::GetSelectedPartition( HYDROGUI_Module* theModule )
325 HYDROGUI_DataModel* aModel = theModule->getDataModel();
327 SUIT_SelectionMgr* aSelectionMgr = theModule->getApp()->selectionMgr();
328 SUIT_DataOwnerPtrList anOwners;
329 aSelectionMgr->selected( anOwners );
331 if( anOwners.size() != 1 )
334 if( LightApp_DataOwner* anOwner = dynamic_cast<LightApp_DataOwner*>( anOwners.first().operator->() ) )
336 QString anEntry = anOwner->entry();
337 QString aPrefix = HYDROGUI_DataObject::entryPrefix();
338 if( anEntry.left( aPrefix.length() ) == aPrefix )
340 anEntry.remove( aPrefix );
341 for( ObjectKind anObjectKind = KIND_UNKNOWN + 1; anObjectKind <= KIND_LAST; anObjectKind++ )
342 if( HYDROGUI_DataModel::tr( HYDROGUI_DataModel::partitionName( anObjectKind ).toAscii() ) == anEntry )
349 QStringList HYDROGUI_Tool::GetSelectedGeomObjects( HYDROGUI_Module* theModule )
351 QStringList anEntryList;
353 // Get active SalomeApp_Study
354 SalomeApp_Study* aStudy = NULL;
355 if ( theModule && theModule->getApp() ) {
356 aStudy = dynamic_cast<SalomeApp_Study*>( theModule->getApp()->activeStudy() );
363 SUIT_SelectionMgr* aSelectionMgr = theModule->getApp()->selectionMgr();
364 SUIT_DataOwnerPtrList anOwners;
365 aSelectionMgr->selected( anOwners );
367 // Check if the selected objects belong to GEOM and have a shape
368 foreach( SUIT_DataOwner* aSUITOwner, anOwners )
370 if( LightApp_DataOwner* anOwner = dynamic_cast<LightApp_DataOwner*>( aSUITOwner ) )
372 QString anEntry = anOwner->entry();
373 _PTR(SObject) aSObject( aStudy->studyDS()->FindObjectID(qPrintable(anEntry)) );
375 _PTR(SComponent) aSComponent = aSObject->GetFatherComponent();
376 if ( aSComponent && aSComponent->ComponentDataType() == "GEOM" ) {
377 GEOM::GEOM_Object_var aGeomObj =
378 GEOMBase::GetObjectFromIOR( aSObject->GetIOR().c_str() );
380 if ( !aGeomObj->_is_nil() && aGeomObj->IsShape() ) {
381 anEntryList << anEntry;
391 Handle(HYDROData_Entity) HYDROGUI_Tool::FindObjectByName( HYDROGUI_Module* theModule,
392 const QString& theName,
393 const ObjectKind theObjectKind )
395 Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( theModule->getStudyId() );
396 return HYDROData_Tool::FindObjectByName( aDocument, theName, theObjectKind );
399 HYDROData_SequenceOfObjects HYDROGUI_Tool::FindObjectsByNames( HYDROGUI_Module* theModule,
400 const QStringList& theNames,
401 const ObjectKind theObjectKind )
403 Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( theModule->getStudyId() );
404 return HYDROData_Tool::FindObjectsByNames( aDocument, theNames, theObjectKind );
407 QString HYDROGUI_Tool::GenerateObjectName( HYDROGUI_Module* theModule,
408 const QString& thePrefix,
409 const QStringList& theUsedNames,
410 const bool theIsTryToUsePurePrefix)
412 Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( theModule->getStudyId() );
413 return HYDROData_Tool::GenerateObjectName( aDocument, thePrefix, theUsedNames, theIsTryToUsePurePrefix );
416 size_t HYDROGUI_Tool::GetActiveViewId( HYDROGUI_Module* theModule,
417 const QString& theViewId )
420 SUIT_ViewManager* aViewMgr = theModule->getApp()->activeViewManager();
421 if( !aViewMgr || ( !theViewId.isEmpty() && aViewMgr->getType() != theViewId ) )
424 if( SUIT_ViewModel* aViewer = aViewMgr->getViewModel() )
425 aViewId = (size_t)aViewer;
429 size_t HYDROGUI_Tool::GetActiveGraphicsViewId( HYDROGUI_Module* theModule )
431 return GetActiveViewId( theModule, GraphicsView_Viewer::Type() );
434 size_t HYDROGUI_Tool::GetActiveOCCViewId( HYDROGUI_Module* theModule )
436 return GetActiveViewId( theModule, OCCViewer_Viewer::Type() );
439 QList<size_t> getViewIdList( HYDROGUI_Module* theModule,
440 const QString& theViewId )
443 ViewManagerList aViewMgrs;
444 theModule->getApp()->viewManagers( theViewId, aViewMgrs );
445 QListIterator<SUIT_ViewManager*> anIter( aViewMgrs );
446 while( anIter.hasNext() )
448 if( SUIT_ViewManager* aViewMgr = anIter.next() )
450 if( SUIT_ViewModel* aViewer = aViewMgr->getViewModel() )
451 aList.append( (size_t)aViewer );
457 QList<size_t> HYDROGUI_Tool::GetGraphicsViewIdList( HYDROGUI_Module* theModule )
459 return getViewIdList( theModule, GraphicsView_Viewer::Type() );
462 QList<size_t> HYDROGUI_Tool::GetOCCViewIdList( HYDROGUI_Module* theModule )
464 return getViewIdList( theModule, OCCViewer_Viewer::Type() );
467 void HYDROGUI_Tool::setOCCActionShown( OCCViewer_ViewFrame* theViewFrame,
468 const int theActionId,
474 OCCViewer_ViewWindow* aView = theViewFrame->getView( OCCViewer_ViewFrame::MAIN_VIEW );
476 aView->toolMgr()->setShown( theActionId, isShown );
477 if ( theActionId == OCCViewer_ViewWindow::MaximizedId )
478 theViewFrame->onMaximizedView( aView, true );
482 void HYDROGUI_Tool::setOCCActionShown( HYDROGUI_Module* theModule,
483 const int theActionId,
487 ViewManagerList aViewMgrs;
488 theModule->getApp()->viewManagers( OCCViewer_Viewer::Type(), aViewMgrs );
489 QListIterator<SUIT_ViewManager*> anIter( aViewMgrs );
490 while( anIter.hasNext() )
492 if( SUIT_ViewManager* aViewMgr = anIter.next() )
494 OCCViewer_ViewFrame* aViewFrame = dynamic_cast<OCCViewer_ViewFrame*>
495 ( aViewMgr->getActiveView() );
497 setOCCActionShown( aViewFrame, theActionId, isShown );
502 void HYDROGUI_Tool::GetObjectReferences( const Handle(HYDROData_Image)& theImage,
503 HYDROData_SequenceOfObjects& theRefObjects,
504 QStringList& theRefNames )
506 if( theImage.IsNull() )
509 for( int anIndex = 0, aNbRef = theImage->NbReferences(); anIndex < aNbRef; anIndex++ )
511 Handle(HYDROData_Entity) aRefObj = theImage->Reference( anIndex );
512 if( !aRefObj.IsNull() && !aRefObj->IsRemoved() )
514 QString aName = aRefObj->GetName();
515 if( !theRefNames.contains( aName ) )
517 theRefObjects.Append( aRefObj );
518 theRefNames.append( aRefObj->GetName() );
519 if( aRefObj->GetKind() == KIND_IMAGE )
521 Handle(HYDROData_Image) aRefImage = Handle(HYDROData_Image)::DownCast( aRefObj );
522 if( !aRefImage.IsNull() )
523 GetObjectReferences( aRefImage, theRefObjects, theRefNames );
530 void HYDROGUI_Tool::GetObjectBackReferences( HYDROGUI_Module* theModule,
531 const Handle(HYDROData_Entity)& theObj,
532 HYDROData_SequenceOfObjects& theBackRefObjects,
533 QStringList& theBackRefNames )
535 if( theObj.IsNull() )
538 Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( theModule->getStudyId() );
539 if( aDocument.IsNull() )
542 QString aName = theObj->GetName();
544 HYDROData_Iterator anIterator( aDocument, KIND_IMAGE );
545 for( ; anIterator.More(); anIterator.Next() )
547 Handle(HYDROData_Image) anImage = Handle(HYDROData_Image)::DownCast( anIterator.Current() );
548 if( !anImage.IsNull() )
550 HYDROData_SequenceOfObjects aRefObjects;
551 QStringList aRefNames;
552 GetObjectReferences( anImage, aRefObjects, aRefNames );
553 if( aRefNames.contains( aName ) )
555 theBackRefObjects.Append( anImage );
556 theBackRefNames.append( anImage->GetName() );
563 QDockWidget* HYDROGUI_Tool::WindowDock( QWidget* wid )
568 QDockWidget* dock = 0;
569 QWidget* w = wid->parentWidget();
572 dock = ::qobject_cast<QDockWidget*>( w );
573 w = w->parentWidget();
578 QColor HYDROGUI_Tool::GenerateFillingColor( HYDROGUI_Module* theModule,
579 const QStringList& theZoneNames )
581 Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( theModule->getStudyId() );
582 return GenerateFillingColor( aDocument, theZoneNames );
585 QColor HYDROGUI_Tool::GenerateFillingColor( const Handle(HYDROData_Document)& theDoc,
586 const QStringList& theZoneNames )
588 QColor aFillingColor( HYDROData_ImmersibleZone::DefaultFillingColor() );
591 int aR = 0, aG = 0, aB = 0;
592 QStringListIterator aZoneNameIter( theZoneNames );
593 while( aZoneNameIter.hasNext() )
595 const QString& aZoneName = aZoneNameIter.next();
596 Handle(HYDROData_ImmersibleZone) aRefZone =
597 Handle(HYDROData_ImmersibleZone)::DownCast(
598 HYDROData_Tool::FindObjectByName( theDoc, aZoneName, KIND_IMMERSIBLE_ZONE ) );
599 if( !aRefZone.IsNull() )
601 QColor aRefColor = aRefZone->GetFillingColor();
602 aR += aRefColor.red();
603 aG += aRefColor.green();
604 aB += aRefColor.blue();
611 aFillingColor = QColor( aR / aCounter, aG / aCounter, aB / aCounter );
614 return aFillingColor;
617 QStringList HYDROGUI_Tool::FindExistingObjectsNames( const Handle(HYDROData_Document)& theDoc,
618 const ObjectKind theObjectKind )
622 HYDROData_Iterator anIter( theDoc, theObjectKind );
623 for ( ; anIter.More(); anIter.Next() ) {
624 Handle(HYDROData_Entity) anObject = anIter.Current();
625 if( !anObject.IsNull() ) {
626 aNames.append( anObject->GetName() );