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>
47 #include <QtxWorkstack.h>
49 #include <STD_TabDesktop.h>
51 #include <SUIT_Session.h>
52 #include <SUIT_Study.h>
53 #include <SUIT_ViewManager.h>
54 #include <SUIT_ViewWindow.h>
56 #include <SALOMEDSClient.hxx>
60 #include <QDockWidget>
63 // Definition of this id allows to use 'latin1' (Qt alias for 'ISO-8859-1')
64 // encoding instead of default 'System'
65 #define USE_LATIN1_ENCODING
67 QString HYDROGUI_Tool::ToQString( const TCollection_AsciiString& src )
69 #ifdef USE_LATIN1_ENCODING
70 QTextCodec* codec = QTextCodec::codecForName( "latin1" ); // alias for ISO-8859-1
72 QTextCodec* codec = QTextCodec::codecForLocale();
76 res = codec ? codec->toUnicode( (char*)src.ToCString(), src.Length() ) :
77 QString( (char*)src.ToCString() );
81 QString HYDROGUI_Tool::ToQString( const TCollection_ExtendedString& src )
83 return QString( (QChar*)src.ToExtString(), src.Length() );
86 QString HYDROGUI_Tool::ToQString( const Handle(TCollection_HAsciiString)& src )
91 return ToQString( src->String() );
94 QString HYDROGUI_Tool::ToQString( const Handle(TCollection_HExtendedString)& src )
98 return ToQString( src->String() );
101 TCollection_AsciiString HYDROGUI_Tool::ToAsciiString( const QString& src )
103 TCollection_AsciiString res;
106 #ifdef USE_LATIN1_ENCODING
107 QTextCodec* codec = QTextCodec::codecForName( "latin1" ); // alias for ISO-8859-1
109 QTextCodec* codec = QTextCodec::codecForLocale();
113 QByteArray str = codec->fromUnicode( src );
114 res = TCollection_AsciiString( (Standard_CString)str.constData() );
117 res = TCollection_AsciiString( src.toLatin1().data() );
122 TCollection_ExtendedString HYDROGUI_Tool::ToExtString( const QString& src )
125 return TCollection_ExtendedString();
127 Standard_Integer len = src.length();
128 Standard_ExtString extStr = new Standard_ExtCharacter[ ( len + 1 ) * 2 ];
129 memcpy( (void*)extStr, src.unicode(), len * 2 );
130 ((short*)extStr)[ len ] = 0;
132 TCollection_ExtendedString trg( extStr );
137 Handle(TCollection_HAsciiString) HYDROGUI_Tool::ToHAsciiString( const QString& src )
139 return new TCollection_HAsciiString( ToAsciiString( src ) );
142 Handle(TCollection_HExtendedString) HYDROGUI_Tool::ToHExtString( const QString& src )
144 return new TCollection_HExtendedString( ToExtString( src ) );
147 int HYDROGUI_Tool::GetActiveStudyId()
149 if( SUIT_Session* aSession = SUIT_Session::session() )
150 if( SUIT_Application* anApp = aSession->activeApplication() )
151 if( SUIT_Study* aStudy = anApp->activeStudy() )
156 QString HYDROGUI_Tool::GetTempDir( const bool theToCreate )
160 char* tmpdir = getenv ( "HYDRO_TMP_DIR" );
163 // try to create folder if it does not exist
164 QFileInfo fi( tmpdir );
165 if ( !fi.exists() && theToCreate )
167 if ( QDir().mkdir( tmpdir ) )
168 QFile::setPermissions( tmpdir, (QFile::Permissions)0x4FFFF );
169 QFileInfo fi( tmpdir );
170 if ( !fi.exists() || !fi.isWritable() )
175 tmpdir = getenv ( "TEMP" );
177 tmpdir = getenv ( "TMP" );
188 QFileInfo fi( aRes );
189 if ( !fi.exists() || !fi.isWritable() )
190 aRes = QString::null;
195 void HYDROGUI_Tool::SetActiveViewManager( HYDROGUI_Module* theModule,
196 SUIT_ViewManager* theViewManager )
199 if( SUIT_ViewWindow* aViewWindow = theViewManager->getActiveView() )
200 if( STD_TabDesktop* aTabDesktop = dynamic_cast<STD_TabDesktop*>( theModule->getApp()->desktop() ) )
201 if( QtxWorkstack* aWorkstack = aTabDesktop->workstack() )
202 aWorkstack->setActiveWindow( aViewWindow );
205 void HYDROGUI_Tool::GetPrsSubObjects( HYDROGUI_Module* theModule,
206 HYDROData_SequenceOfObjects& theSeq )
208 Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( theModule->getStudyId() );
209 if( !aDocument.IsNull() )
211 HYDROData_Iterator anIterator( aDocument );
212 for( ; anIterator.More(); anIterator.Next() )
214 Handle(HYDROData_Entity) anObject = anIterator.Current();
215 if( !anObject.IsNull() && (
216 ( anObject->GetKind() == KIND_IMAGE ) ||
217 ( anObject->GetKind() == KIND_POLYLINEXY ) ||
218 ( anObject->GetKind() == KIND_IMMERSIBLE_ZONE ) ||
219 ( anObject->GetKind() == KIND_REGION ) ||
220 ( anObject->GetKind() == KIND_BATHYMETRY ) ||
221 ( anObject->GetKind() == KIND_ZONE ) ||
222 ( anObject->GetKind() == KIND_OBSTACLE ) ) )
224 theSeq.Append( anObject );
230 HYDROGUI_Prs* HYDROGUI_Tool::GetPresentation( const Handle(HYDROData_Entity)& theObj,
231 const GraphicsView_ObjectList& theObjects )
233 if( !theObj.IsNull() )
235 GraphicsView_ObjectListIterator anIter( theObjects );
236 while( anIter.hasNext() )
238 if( HYDROGUI_Prs* aPrs = dynamic_cast<HYDROGUI_Prs*>( anIter.next() ) )
240 Handle(HYDROData_Entity) anObj = aPrs->getObject();
241 if( IsEqual( anObj, theObj ) )
249 GraphicsView_ObjectList HYDROGUI_Tool::GetPrsList( GraphicsView_ViewPort* theViewPort )
251 GraphicsView_ObjectList aList;
254 GraphicsView_ObjectListIterator anIter( theViewPort->getObjects() );
255 while( anIter.hasNext() )
256 if( HYDROGUI_Prs* aPrs = dynamic_cast<HYDROGUI_Prs*>( anIter.next() ) )
257 aList.append( aPrs );
262 HYDROData_SequenceOfObjects HYDROGUI_Tool::GetSelectedObjects( HYDROGUI_Module* theModule )
264 HYDROData_SequenceOfObjects aSeq;
266 HYDROGUI_DataModel* aModel = theModule->getDataModel();
268 SUIT_SelectionMgr* aSelectionMgr = theModule->getApp()->selectionMgr();
269 SUIT_DataOwnerPtrList anOwners;
270 aSelectionMgr->selected( anOwners );
272 QStringList aCollectedNameList; // to avoid duplication
273 foreach( SUIT_DataOwner* aSUITOwner, anOwners )
275 if( LightApp_DataOwner* anOwner = dynamic_cast<LightApp_DataOwner*>( aSUITOwner ) )
277 Handle(HYDROData_Entity) anObject = aModel->objectByEntry( anOwner->entry() );
278 if( !anObject.IsNull() )
280 QString aName = anObject->GetName();
281 if( !aCollectedNameList.contains( aName ) )
283 aSeq.Append( anObject );
284 aCollectedNameList.append( aName );
292 Handle(HYDROData_Entity) HYDROGUI_Tool::GetSelectedObject( HYDROGUI_Module* theModule )
294 HYDROData_SequenceOfObjects aSeq = GetSelectedObjects( theModule );
295 if( !aSeq.IsEmpty() )
300 HYDROData_SequenceOfObjects HYDROGUI_Tool::GetGeometryObjects(
301 HYDROGUI_Module* theModule )
303 Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( theModule->getStudyId() );
305 HYDROData_SequenceOfObjects aResSeq;
307 HYDROData_Iterator anIter( aDocument );
308 for ( ; anIter.More(); anIter.Next() )
310 Handle(HYDROData_Entity) anObj = anIter.Current();
311 if ( !HYDROData_Tool::IsGeometryObject( anObj ) )
314 aResSeq.Append( anObj );
320 ObjectKind HYDROGUI_Tool::GetSelectedPartition( HYDROGUI_Module* theModule )
322 HYDROGUI_DataModel* aModel = theModule->getDataModel();
324 SUIT_SelectionMgr* aSelectionMgr = theModule->getApp()->selectionMgr();
325 SUIT_DataOwnerPtrList anOwners;
326 aSelectionMgr->selected( anOwners );
328 if( anOwners.size() != 1 )
331 if( LightApp_DataOwner* anOwner = dynamic_cast<LightApp_DataOwner*>( anOwners.first().operator->() ) )
333 QString anEntry = anOwner->entry();
334 QString aPrefix = HYDROGUI_DataObject::entryPrefix();
335 if( anEntry.left( aPrefix.length() ) == aPrefix )
337 anEntry.remove( aPrefix );
338 for( ObjectKind anObjectKind = KIND_UNKNOWN + 1; anObjectKind <= KIND_LAST; anObjectKind++ )
339 if( HYDROGUI_DataModel::tr( HYDROGUI_DataModel::partitionName( anObjectKind ).toAscii() ) == anEntry )
346 QStringList HYDROGUI_Tool::GetSelectedGeomObjects( HYDROGUI_Module* theModule )
348 QStringList anEntryList;
350 // Get active SalomeApp_Study
351 SalomeApp_Study* aStudy = NULL;
352 if ( theModule && theModule->getApp() ) {
353 aStudy = dynamic_cast<SalomeApp_Study*>( theModule->getApp()->activeStudy() );
360 SUIT_SelectionMgr* aSelectionMgr = theModule->getApp()->selectionMgr();
361 SUIT_DataOwnerPtrList anOwners;
362 aSelectionMgr->selected( anOwners );
364 // Check if the selected objects belong to GEOM and have a shape
365 foreach( SUIT_DataOwner* aSUITOwner, anOwners )
367 if( LightApp_DataOwner* anOwner = dynamic_cast<LightApp_DataOwner*>( aSUITOwner ) )
369 QString anEntry = anOwner->entry();
370 _PTR(SObject) aSObject( aStudy->studyDS()->FindObjectID(qPrintable(anEntry)) );
372 _PTR(SComponent) aSComponent = aSObject->GetFatherComponent();
373 if ( aSComponent && aSComponent->ComponentDataType() == "GEOM" ) {
374 GEOM::GEOM_Object_var aGeomObj =
375 GEOMBase::GetObjectFromIOR( aSObject->GetIOR().c_str() );
377 if ( !aGeomObj->_is_nil() && aGeomObj->IsShape() ) {
378 anEntryList << anEntry;
388 Handle(HYDROData_Entity) HYDROGUI_Tool::FindObjectByName( HYDROGUI_Module* theModule,
389 const QString& theName,
390 const ObjectKind theObjectKind )
392 Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( theModule->getStudyId() );
393 return HYDROData_Tool::FindObjectByName( aDocument, theName, theObjectKind );
396 HYDROData_SequenceOfObjects HYDROGUI_Tool::FindObjectsByNames( HYDROGUI_Module* theModule,
397 const QStringList& theNames,
398 const ObjectKind theObjectKind )
400 Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( theModule->getStudyId() );
401 return HYDROData_Tool::FindObjectsByNames( aDocument, theNames, theObjectKind );
404 QString HYDROGUI_Tool::GenerateObjectName( HYDROGUI_Module* theModule,
405 const QString& thePrefix,
406 const QStringList& theUsedNames,
407 const bool theIsTryToUsePurePrefix)
409 Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( theModule->getStudyId() );
410 return HYDROData_Tool::GenerateObjectName( aDocument, thePrefix, theUsedNames, theIsTryToUsePurePrefix );
413 size_t HYDROGUI_Tool::GetActiveViewId( HYDROGUI_Module* theModule,
414 const QString& theViewId )
417 SUIT_ViewManager* aViewMgr = theModule->getApp()->activeViewManager();
418 if( !aViewMgr || ( !theViewId.isEmpty() && aViewMgr->getType() != theViewId ) )
421 if( SUIT_ViewModel* aViewer = aViewMgr->getViewModel() )
422 aViewId = (size_t)aViewer;
426 size_t HYDROGUI_Tool::GetActiveGraphicsViewId( HYDROGUI_Module* theModule )
428 return GetActiveViewId( theModule, GraphicsView_Viewer::Type() );
431 size_t HYDROGUI_Tool::GetActiveOCCViewId( HYDROGUI_Module* theModule )
433 return GetActiveViewId( theModule, OCCViewer_Viewer::Type() );
436 QList<size_t> getViewIdList( HYDROGUI_Module* theModule,
437 const QString& theViewId )
440 ViewManagerList aViewMgrs;
441 theModule->getApp()->viewManagers( theViewId, aViewMgrs );
442 QListIterator<SUIT_ViewManager*> anIter( aViewMgrs );
443 while( anIter.hasNext() )
445 if( SUIT_ViewManager* aViewMgr = anIter.next() )
447 if( SUIT_ViewModel* aViewer = aViewMgr->getViewModel() )
448 aList.append( (size_t)aViewer );
454 QList<size_t> HYDROGUI_Tool::GetGraphicsViewIdList( HYDROGUI_Module* theModule )
456 return getViewIdList( theModule, GraphicsView_Viewer::Type() );
459 QList<size_t> HYDROGUI_Tool::GetOCCViewIdList( HYDROGUI_Module* theModule )
461 return getViewIdList( theModule, OCCViewer_Viewer::Type() );
464 void HYDROGUI_Tool::GetObjectReferences( const Handle(HYDROData_Image)& theImage,
465 HYDROData_SequenceOfObjects& theRefObjects,
466 QStringList& theRefNames )
468 if( theImage.IsNull() )
471 for( int anIndex = 0, aNbRef = theImage->NbReferences(); anIndex < aNbRef; anIndex++ )
473 Handle(HYDROData_Entity) aRefObj = theImage->Reference( anIndex );
474 if( !aRefObj.IsNull() && !aRefObj->IsRemoved() )
476 QString aName = aRefObj->GetName();
477 if( !theRefNames.contains( aName ) )
479 theRefObjects.Append( aRefObj );
480 theRefNames.append( aRefObj->GetName() );
481 if( aRefObj->GetKind() == KIND_IMAGE )
483 Handle(HYDROData_Image) aRefImage = Handle(HYDROData_Image)::DownCast( aRefObj );
484 if( !aRefImage.IsNull() )
485 GetObjectReferences( aRefImage, theRefObjects, theRefNames );
492 void HYDROGUI_Tool::GetObjectBackReferences( HYDROGUI_Module* theModule,
493 const Handle(HYDROData_Entity)& theObj,
494 HYDROData_SequenceOfObjects& theBackRefObjects,
495 QStringList& theBackRefNames )
497 if( theObj.IsNull() )
500 Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( theModule->getStudyId() );
501 if( aDocument.IsNull() )
504 QString aName = theObj->GetName();
506 HYDROData_Iterator anIterator( aDocument, KIND_IMAGE );
507 for( ; anIterator.More(); anIterator.Next() )
509 Handle(HYDROData_Image) anImage = Handle(HYDROData_Image)::DownCast( anIterator.Current() );
510 if( !anImage.IsNull() )
512 HYDROData_SequenceOfObjects aRefObjects;
513 QStringList aRefNames;
514 GetObjectReferences( anImage, aRefObjects, aRefNames );
515 if( aRefNames.contains( aName ) )
517 theBackRefObjects.Append( anImage );
518 theBackRefNames.append( anImage->GetName() );
525 QDockWidget* HYDROGUI_Tool::WindowDock( QWidget* wid )
530 QDockWidget* dock = 0;
531 QWidget* w = wid->parentWidget();
534 dock = ::qobject_cast<QDockWidget*>( w );
535 w = w->parentWidget();
540 QColor HYDROGUI_Tool::GenerateFillingColor( HYDROGUI_Module* theModule,
541 const QStringList& theZoneNames )
543 Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( theModule->getStudyId() );
544 return GenerateFillingColor( aDocument, theZoneNames );
547 QColor HYDROGUI_Tool::GenerateFillingColor( const Handle(HYDROData_Document)& theDoc,
548 const QStringList& theZoneNames )
550 QColor aFillingColor( HYDROData_ImmersibleZone::DefaultFillingColor() );
553 int aR = 0, aG = 0, aB = 0;
554 QStringListIterator aZoneNameIter( theZoneNames );
555 while( aZoneNameIter.hasNext() )
557 const QString& aZoneName = aZoneNameIter.next();
558 Handle(HYDROData_ImmersibleZone) aRefZone =
559 Handle(HYDROData_ImmersibleZone)::DownCast(
560 HYDROData_Tool::FindObjectByName( theDoc, aZoneName, KIND_IMMERSIBLE_ZONE ) );
561 if( !aRefZone.IsNull() )
563 QColor aRefColor = aRefZone->GetFillingColor();
564 aR += aRefColor.red();
565 aG += aRefColor.green();
566 aB += aRefColor.blue();
573 aFillingColor = QColor( aR / aCounter, aG / aCounter, aB / aCounter );
576 return aFillingColor;
579 QStringList HYDROGUI_Tool::FindExistingObjectsNames( const Handle(HYDROData_Document)& theDoc,
580 const ObjectKind theObjectKind )
584 HYDROData_Iterator anIter( theDoc, theObjectKind );
585 for ( ; anIter.More(); anIter.Next() ) {
586 Handle(HYDROData_Entity) anObject = anIter.Current();
587 if( !anObject.IsNull() ) {
588 aNames.append( anObject->GetName() );