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>
35 #include <GraphicsView_Viewer.h>
37 #include <LightApp_Application.h>
38 #include <LightApp_DataOwner.h>
39 #include <LightApp_SelectionMgr.h>
41 #include <OCCViewer_ViewModel.h>
43 #include <QtxWorkstack.h>
45 #include <STD_TabDesktop.h>
47 #include <SUIT_Session.h>
48 #include <SUIT_Study.h>
49 #include <SUIT_ViewManager.h>
50 #include <SUIT_ViewWindow.h>
54 #include <QDockWidget>
57 // Definition of this id allows to use 'latin1' (Qt alias for 'ISO-8859-1')
58 // encoding instead of default 'System'
59 #define USE_LATIN1_ENCODING
61 QString HYDROGUI_Tool::ToQString( const TCollection_AsciiString& src )
63 #ifdef USE_LATIN1_ENCODING
64 QTextCodec* codec = QTextCodec::codecForName( "latin1" ); // alias for ISO-8859-1
66 QTextCodec* codec = QTextCodec::codecForLocale();
70 res = codec ? codec->toUnicode( (char*)src.ToCString(), src.Length() ) :
71 QString( (char*)src.ToCString() );
75 QString HYDROGUI_Tool::ToQString( const TCollection_ExtendedString& src )
77 return QString( (QChar*)src.ToExtString(), src.Length() );
80 QString HYDROGUI_Tool::ToQString( const Handle(TCollection_HAsciiString)& src )
85 return ToQString( src->String() );
88 QString HYDROGUI_Tool::ToQString( const Handle(TCollection_HExtendedString)& src )
92 return ToQString( src->String() );
95 TCollection_AsciiString HYDROGUI_Tool::ToAsciiString( const QString& src )
97 TCollection_AsciiString res;
100 #ifdef USE_LATIN1_ENCODING
101 QTextCodec* codec = QTextCodec::codecForName( "latin1" ); // alias for ISO-8859-1
103 QTextCodec* codec = QTextCodec::codecForLocale();
107 QByteArray str = codec->fromUnicode( src );
108 res = TCollection_AsciiString( (Standard_CString)str.constData() );
111 res = TCollection_AsciiString( src.toLatin1().data() );
116 TCollection_ExtendedString HYDROGUI_Tool::ToExtString( const QString& src )
119 return TCollection_ExtendedString();
121 Standard_Integer len = src.length();
122 Standard_ExtString extStr = new Standard_ExtCharacter[ ( len + 1 ) * 2 ];
123 memcpy( (void*)extStr, src.unicode(), len * 2 );
124 ((short*)extStr)[ len ] = 0;
126 TCollection_ExtendedString trg( extStr );
131 Handle(TCollection_HAsciiString) HYDROGUI_Tool::ToHAsciiString( const QString& src )
133 return new TCollection_HAsciiString( ToAsciiString( src ) );
136 Handle(TCollection_HExtendedString) HYDROGUI_Tool::ToHExtString( const QString& src )
138 return new TCollection_HExtendedString( ToExtString( src ) );
141 int HYDROGUI_Tool::GetActiveStudyId()
143 if( SUIT_Session* aSession = SUIT_Session::session() )
144 if( SUIT_Application* anApp = aSession->activeApplication() )
145 if( SUIT_Study* aStudy = anApp->activeStudy() )
150 QString HYDROGUI_Tool::GetTempDir( const bool theToCreate )
154 char* tmpdir = getenv ( "HYDRO_TMP_DIR" );
157 // try to create folder if it does not exist
158 QFileInfo fi( tmpdir );
159 if ( !fi.exists() && theToCreate )
161 if ( QDir().mkdir( tmpdir ) )
162 QFile::setPermissions( tmpdir, (QFile::Permissions)0x4FFFF );
163 QFileInfo fi( tmpdir );
164 if ( !fi.exists() || !fi.isWritable() )
169 tmpdir = getenv ( "TEMP" );
171 tmpdir = getenv ( "TMP" );
182 QFileInfo fi( aRes );
183 if ( !fi.exists() || !fi.isWritable() )
184 aRes = QString::null;
189 void HYDROGUI_Tool::SetActiveViewManager( HYDROGUI_Module* theModule,
190 SUIT_ViewManager* theViewManager )
193 if( SUIT_ViewWindow* aViewWindow = theViewManager->getActiveView() )
194 if( STD_TabDesktop* aTabDesktop = dynamic_cast<STD_TabDesktop*>( theModule->getApp()->desktop() ) )
195 if( QtxWorkstack* aWorkstack = aTabDesktop->workstack() )
196 aWorkstack->setActiveWindow( aViewWindow );
199 void HYDROGUI_Tool::GetPrsSubObjects( HYDROGUI_Module* theModule,
200 HYDROData_SequenceOfObjects& theSeq )
202 Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( theModule->getStudyId() );
203 if( aDocument.IsNull() )
206 HYDROData_Iterator anIterator( aDocument, KIND_IMAGE );
207 for( ; anIterator.More(); anIterator.Next() )
209 Handle(HYDROData_Entity) anObject = anIterator.Current();
210 if( !anObject.IsNull() )
211 theSeq.Append( anObject );
214 anIterator = HYDROData_Iterator( aDocument, KIND_POLYLINE );
215 for( ; anIterator.More(); anIterator.Next() )
217 Handle(HYDROData_Entity) anObject = anIterator.Current();
218 if( !anObject.IsNull() )
219 theSeq.Append( anObject );
222 anIterator = HYDROData_Iterator( aDocument, KIND_IMMERSIBLE_ZONE );
223 for( ; anIterator.More(); anIterator.Next() )
225 Handle(HYDROData_Entity) anObject = anIterator.Current();
226 if( !anObject.IsNull() )
227 theSeq.Append( anObject );
231 HYDROGUI_Prs* HYDROGUI_Tool::GetPresentation( const Handle(HYDROData_Entity)& theObj,
232 const GraphicsView_ObjectList& theObjects )
234 if( !theObj.IsNull() )
236 GraphicsView_ObjectListIterator anIter( theObjects );
237 while( anIter.hasNext() )
239 if( HYDROGUI_Prs* aPrs = dynamic_cast<HYDROGUI_Prs*>( anIter.next() ) )
241 Handle(HYDROData_Entity) anObj = aPrs->getObject();
242 if( IsEqual( anObj, theObj ) )
250 GraphicsView_ObjectList HYDROGUI_Tool::GetPrsList( GraphicsView_ViewPort* theViewPort )
252 GraphicsView_ObjectList aList;
255 GraphicsView_ObjectListIterator anIter( theViewPort->getObjects() );
256 while( anIter.hasNext() )
257 if( HYDROGUI_Prs* aPrs = dynamic_cast<HYDROGUI_Prs*>( anIter.next() ) )
258 aList.append( aPrs );
263 HYDROData_SequenceOfObjects HYDROGUI_Tool::GetSelectedObjects( HYDROGUI_Module* theModule )
265 HYDROData_SequenceOfObjects aSeq;
267 HYDROGUI_DataModel* aModel = theModule->getDataModel();
269 SUIT_SelectionMgr* aSelectionMgr = theModule->getApp()->selectionMgr();
270 SUIT_DataOwnerPtrList anOwners;
271 aSelectionMgr->selected( anOwners );
273 QStringList aCollectedNameList; // to avoid duplication
274 foreach( SUIT_DataOwner* aSUITOwner, anOwners )
276 if( LightApp_DataOwner* anOwner = dynamic_cast<LightApp_DataOwner*>( aSUITOwner ) )
278 Handle(HYDROData_Entity) anObject = aModel->objectByEntry( anOwner->entry() );
279 if( !anObject.IsNull() )
281 QString aName = anObject->GetName();
282 if( !aCollectedNameList.contains( aName ) )
284 aSeq.Append( anObject );
285 aCollectedNameList.append( aName );
293 Handle(HYDROData_Entity) HYDROGUI_Tool::GetSelectedObject( HYDROGUI_Module* theModule )
295 HYDROData_SequenceOfObjects aSeq = GetSelectedObjects( theModule );
296 if( !aSeq.IsEmpty() )
301 HYDROData_SequenceOfObjects HYDROGUI_Tool::GetGeometryObjects(
302 HYDROGUI_Module* theModule )
304 Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( theModule->getStudyId() );
306 HYDROData_SequenceOfObjects aResSeq;
308 HYDROData_Iterator anIter( aDocument );
309 for ( ; anIter.More(); anIter.Next() )
311 Handle(HYDROData_Entity) anObj = anIter.Current();
312 if ( !HYDROData_Tool::IsGeometryObject( anObj ) )
315 aResSeq.Append( anObj );
321 ObjectKind HYDROGUI_Tool::GetSelectedPartition( HYDROGUI_Module* theModule )
323 HYDROGUI_DataModel* aModel = theModule->getDataModel();
325 SUIT_SelectionMgr* aSelectionMgr = theModule->getApp()->selectionMgr();
326 SUIT_DataOwnerPtrList anOwners;
327 aSelectionMgr->selected( anOwners );
329 if( anOwners.size() != 1 )
332 if( LightApp_DataOwner* anOwner = dynamic_cast<LightApp_DataOwner*>( anOwners.first().operator->() ) )
334 QString anEntry = anOwner->entry();
335 QString aPrefix = HYDROGUI_DataObject::entryPrefix();
336 if( anEntry.left( aPrefix.length() ) == aPrefix )
338 anEntry.remove( aPrefix );
339 for( ObjectKind anObjectKind = KIND_UNKNOWN + 1; anObjectKind <= KIND_LAST; anObjectKind++ )
340 if( HYDROGUI_DataModel::partitionName( anObjectKind ) == anEntry )
347 Handle(HYDROData_Entity) HYDROGUI_Tool::FindObjectByName( HYDROGUI_Module* theModule,
348 const QString& theName,
349 const ObjectKind theObjectKind )
351 Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( theModule->getStudyId() );
352 return HYDROData_Tool::FindObjectByName( aDocument, theName, theObjectKind );
355 HYDROData_SequenceOfObjects HYDROGUI_Tool::FindObjectsByNames( HYDROGUI_Module* theModule,
356 const QStringList& theNames,
357 const ObjectKind theObjectKind )
359 Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( theModule->getStudyId() );
360 return HYDROData_Tool::FindObjectsByNames( aDocument, theNames, theObjectKind );
363 QString HYDROGUI_Tool::GenerateObjectName( HYDROGUI_Module* theModule,
364 const QString& thePrefix,
365 const QStringList& theUsedNames )
367 Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( theModule->getStudyId() );
368 return HYDROData_Tool::GenerateObjectName( aDocument, thePrefix, theUsedNames );
371 size_t HYDROGUI_Tool::GetActiveViewId( HYDROGUI_Module* theModule,
372 const QString& theViewId )
375 SUIT_ViewManager* aViewMgr = theModule->getApp()->activeViewManager();
376 if( !aViewMgr || ( !theViewId.isEmpty() && aViewMgr->getType() != theViewId ) )
379 if( SUIT_ViewModel* aViewer = aViewMgr->getViewModel() )
380 aViewId = (size_t)aViewer;
384 size_t HYDROGUI_Tool::GetActiveGraphicsViewId( HYDROGUI_Module* theModule )
386 return GetActiveViewId( theModule, GraphicsView_Viewer::Type() );
389 size_t HYDROGUI_Tool::GetActiveOCCViewId( HYDROGUI_Module* theModule )
391 return GetActiveViewId( theModule, OCCViewer_Viewer::Type() );
394 QList<size_t> getViewIdList( HYDROGUI_Module* theModule,
395 const QString& theViewId )
398 ViewManagerList aViewMgrs;
399 theModule->getApp()->viewManagers( theViewId, aViewMgrs );
400 QListIterator<SUIT_ViewManager*> anIter( aViewMgrs );
401 while( anIter.hasNext() )
403 if( SUIT_ViewManager* aViewMgr = anIter.next() )
405 if( SUIT_ViewModel* aViewer = aViewMgr->getViewModel() )
406 aList.append( (size_t)aViewer );
412 QList<size_t> HYDROGUI_Tool::GetGraphicsViewIdList( HYDROGUI_Module* theModule )
414 return getViewIdList( theModule, GraphicsView_Viewer::Type() );
417 QList<size_t> HYDROGUI_Tool::GetOCCViewIdList( HYDROGUI_Module* theModule )
419 return getViewIdList( theModule, OCCViewer_Viewer::Type() );
422 void HYDROGUI_Tool::GetObjectReferences( const Handle(HYDROData_Image)& theImage,
423 HYDROData_SequenceOfObjects& theRefObjects,
424 QStringList& theRefNames )
426 if( theImage.IsNull() )
429 for( int anIndex = 0, aNbRef = theImage->NbReferences(); anIndex < aNbRef; anIndex++ )
431 Handle(HYDROData_Entity) aRefObj = theImage->Reference( anIndex );
432 if( !aRefObj.IsNull() && !aRefObj->IsRemoved() )
434 QString aName = aRefObj->GetName();
435 if( !theRefNames.contains( aName ) )
437 theRefObjects.Append( aRefObj );
438 theRefNames.append( aRefObj->GetName() );
439 if( aRefObj->GetKind() == KIND_IMAGE )
441 Handle(HYDROData_Image) aRefImage = Handle(HYDROData_Image)::DownCast( aRefObj );
442 if( !aRefImage.IsNull() )
443 GetObjectReferences( aRefImage, theRefObjects, theRefNames );
450 void HYDROGUI_Tool::GetObjectBackReferences( HYDROGUI_Module* theModule,
451 const Handle(HYDROData_Entity)& theObj,
452 HYDROData_SequenceOfObjects& theBackRefObjects,
453 QStringList& theBackRefNames )
455 if( theObj.IsNull() )
458 Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( theModule->getStudyId() );
459 if( aDocument.IsNull() )
462 QString aName = theObj->GetName();
464 HYDROData_Iterator anIterator( aDocument, KIND_IMAGE );
465 for( ; anIterator.More(); anIterator.Next() )
467 Handle(HYDROData_Image) anImage = Handle(HYDROData_Image)::DownCast( anIterator.Current() );
468 if( !anImage.IsNull() )
470 HYDROData_SequenceOfObjects aRefObjects;
471 QStringList aRefNames;
472 GetObjectReferences( anImage, aRefObjects, aRefNames );
473 if( aRefNames.contains( aName ) )
475 theBackRefObjects.Append( anImage );
476 theBackRefNames.append( anImage->GetName() );
483 QDockWidget* HYDROGUI_Tool::WindowDock( QWidget* wid )
488 QDockWidget* dock = 0;
489 QWidget* w = wid->parentWidget();
492 dock = ::qobject_cast<QDockWidget*>( w );
493 w = w->parentWidget();
498 QColor HYDROGUI_Tool::GenerateFillingColor( HYDROGUI_Module* theModule,
499 const QStringList& theZoneNames )
501 Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( theModule->getStudyId() );
502 return GenerateFillingColor( aDocument, theZoneNames );
505 QColor HYDROGUI_Tool::GenerateFillingColor( const Handle(HYDROData_Document)& theDoc,
506 const QStringList& theZoneNames )
508 QColor aFillingColor( HYDROData_ImmersibleZone::DefaultFillingColor() );
511 int aR = 0, aG = 0, aB = 0;
512 QStringListIterator aZoneNameIter( theZoneNames );
513 while( aZoneNameIter.hasNext() )
515 const QString& aZoneName = aZoneNameIter.next();
516 Handle(HYDROData_ImmersibleZone) aRefZone =
517 Handle(HYDROData_ImmersibleZone)::DownCast(
518 HYDROData_Tool::FindObjectByName( theDoc, aZoneName, KIND_IMMERSIBLE_ZONE ) );
519 if( !aRefZone.IsNull() )
521 QColor aRefColor = aRefZone->GetFillingColor();
522 aR += aRefColor.red();
523 aG += aRefColor.green();
524 aB += aRefColor.blue();
531 aFillingColor = QColor( aR / aCounter, aG / aCounter, aB / aCounter );
534 return aFillingColor;