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_Zone.h>
34 #include <GraphicsView_Viewer.h>
36 #include <LightApp_Application.h>
37 #include <LightApp_DataOwner.h>
38 #include <LightApp_SelectionMgr.h>
40 #include <OCCViewer_ViewModel.h>
42 #include <QtxWorkstack.h>
44 #include <STD_TabDesktop.h>
46 #include <SUIT_Session.h>
47 #include <SUIT_Study.h>
48 #include <SUIT_ViewManager.h>
49 #include <SUIT_ViewWindow.h>
53 #include <QDockWidget>
56 // Definition of this id allows to use 'latin1' (Qt alias for 'ISO-8859-1')
57 // encoding instead of default 'System'
58 #define USE_LATIN1_ENCODING
60 QString HYDROGUI_Tool::ToQString( const TCollection_AsciiString& src )
62 #ifdef USE_LATIN1_ENCODING
63 QTextCodec* codec = QTextCodec::codecForName( "latin1" ); // alias for ISO-8859-1
65 QTextCodec* codec = QTextCodec::codecForLocale();
69 res = codec ? codec->toUnicode( (char*)src.ToCString(), src.Length() ) :
70 QString( (char*)src.ToCString() );
74 QString HYDROGUI_Tool::ToQString( const TCollection_ExtendedString& src )
76 return QString( (QChar*)src.ToExtString(), src.Length() );
79 QString HYDROGUI_Tool::ToQString( const Handle(TCollection_HAsciiString)& src )
84 return ToQString( src->String() );
87 QString HYDROGUI_Tool::ToQString( const Handle(TCollection_HExtendedString)& src )
91 return ToQString( src->String() );
94 TCollection_AsciiString HYDROGUI_Tool::ToAsciiString( const QString& src )
96 TCollection_AsciiString res;
99 #ifdef USE_LATIN1_ENCODING
100 QTextCodec* codec = QTextCodec::codecForName( "latin1" ); // alias for ISO-8859-1
102 QTextCodec* codec = QTextCodec::codecForLocale();
106 QByteArray str = codec->fromUnicode( src );
107 res = TCollection_AsciiString( (Standard_CString)str.constData() );
110 res = TCollection_AsciiString( src.toLatin1().data() );
115 TCollection_ExtendedString HYDROGUI_Tool::ToExtString( const QString& src )
118 return TCollection_ExtendedString();
120 Standard_Integer len = src.length();
121 Standard_ExtString extStr = new Standard_ExtCharacter[ ( len + 1 ) * 2 ];
122 memcpy( (void*)extStr, src.unicode(), len * 2 );
123 ((short*)extStr)[ len ] = 0;
125 TCollection_ExtendedString trg( extStr );
130 Handle(TCollection_HAsciiString) HYDROGUI_Tool::ToHAsciiString( const QString& src )
132 return new TCollection_HAsciiString( ToAsciiString( src ) );
135 Handle(TCollection_HExtendedString) HYDROGUI_Tool::ToHExtString( const QString& src )
137 return new TCollection_HExtendedString( ToExtString( src ) );
140 int HYDROGUI_Tool::GetActiveStudyId()
142 if( SUIT_Session* aSession = SUIT_Session::session() )
143 if( SUIT_Application* anApp = aSession->activeApplication() )
144 if( SUIT_Study* aStudy = anApp->activeStudy() )
149 QString HYDROGUI_Tool::GetTempDir( const bool theToCreate )
153 char* tmpdir = getenv ( "HYDRO_TMP_DIR" );
156 // try to create folder if it does not exist
157 QFileInfo fi( tmpdir );
158 if ( !fi.exists() && theToCreate )
160 if ( QDir().mkdir( tmpdir ) )
161 QFile::setPermissions( tmpdir, (QFile::Permissions)0x4FFFF );
162 QFileInfo fi( tmpdir );
163 if ( !fi.exists() || !fi.isWritable() )
168 tmpdir = getenv ( "TEMP" );
170 tmpdir = getenv ( "TMP" );
181 QFileInfo fi( aRes );
182 if ( !fi.exists() || !fi.isWritable() )
183 aRes = QString::null;
188 bool HYDROGUI_Tool::IsEqual( const Handle(HYDROData_Object)& theObj1,
189 const Handle(HYDROData_Object)& theObj2 )
191 if( !theObj1.IsNull() && !theObj2.IsNull() )
192 return theObj1->Label() == theObj2->Label(); //ouv: check that the names can be used here
196 void HYDROGUI_Tool::SetActiveViewManager( HYDROGUI_Module* theModule,
197 SUIT_ViewManager* theViewManager )
200 if( SUIT_ViewWindow* aViewWindow = theViewManager->getActiveView() )
201 if( STD_TabDesktop* aTabDesktop = dynamic_cast<STD_TabDesktop*>( theModule->getApp()->desktop() ) )
202 if( QtxWorkstack* aWorkstack = aTabDesktop->workstack() )
203 aWorkstack->setActiveWindow( aViewWindow );
206 void HYDROGUI_Tool::GetPrsSubObjects( HYDROGUI_Module* theModule,
207 HYDROData_SequenceOfObjects& theSeq )
209 Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( theModule->getStudyId() );
210 if( aDocument.IsNull() )
213 HYDROData_Iterator anIterator( aDocument, KIND_IMAGE );
214 for( ; anIterator.More(); anIterator.Next() )
216 Handle(HYDROData_Object) anObject = anIterator.Current();
217 if( !anObject.IsNull() )
218 theSeq.Append( anObject );
221 anIterator = HYDROData_Iterator( aDocument, KIND_POLYLINE );
222 for( ; anIterator.More(); anIterator.Next() )
224 Handle(HYDROData_Object) anObject = anIterator.Current();
225 if( !anObject.IsNull() )
226 theSeq.Append( anObject );
229 anIterator = HYDROData_Iterator( aDocument, KIND_ZONE );
230 for( ; anIterator.More(); anIterator.Next() )
232 Handle(HYDROData_Object) anObject = anIterator.Current();
233 if( !anObject.IsNull() )
234 theSeq.Append( anObject );
238 HYDROGUI_Prs* HYDROGUI_Tool::GetPresentation( const Handle(HYDROData_Object)& theObj,
239 const GraphicsView_ObjectList& theObjects )
241 if( !theObj.IsNull() )
243 GraphicsView_ObjectListIterator anIter( theObjects );
244 while( anIter.hasNext() )
246 if( HYDROGUI_Prs* aPrs = dynamic_cast<HYDROGUI_Prs*>( anIter.next() ) )
248 Handle(HYDROData_Object) anObj = aPrs->getObject();
249 if( IsEqual( anObj, theObj ) )
257 GraphicsView_ObjectList HYDROGUI_Tool::GetPrsList( GraphicsView_ViewPort* theViewPort )
259 GraphicsView_ObjectList aList;
262 GraphicsView_ObjectListIterator anIter( theViewPort->getObjects() );
263 while( anIter.hasNext() )
264 if( HYDROGUI_Prs* aPrs = dynamic_cast<HYDROGUI_Prs*>( anIter.next() ) )
265 aList.append( aPrs );
270 HYDROData_SequenceOfObjects HYDROGUI_Tool::GetSelectedObjects( HYDROGUI_Module* theModule )
272 HYDROData_SequenceOfObjects aSeq;
274 HYDROGUI_DataModel* aModel = theModule->getDataModel();
276 SUIT_SelectionMgr* aSelectionMgr = theModule->getApp()->selectionMgr();
277 SUIT_DataOwnerPtrList anOwners;
278 aSelectionMgr->selected( anOwners );
280 QStringList aCollectedNameList; // to avoid duplication
281 foreach( SUIT_DataOwner* aSUITOwner, anOwners )
283 if( LightApp_DataOwner* anOwner = dynamic_cast<LightApp_DataOwner*>( aSUITOwner ) )
285 Handle(HYDROData_Object) anObject = aModel->objectByEntry( anOwner->entry() );
286 if( !anObject.IsNull() )
288 QString aName = anObject->GetName();
289 if( !aCollectedNameList.contains( aName ) )
291 aSeq.Append( anObject );
292 aCollectedNameList.append( aName );
300 Handle(HYDROData_Object) HYDROGUI_Tool::GetSelectedObject( HYDROGUI_Module* theModule )
302 HYDROData_SequenceOfObjects aSeq = GetSelectedObjects( theModule );
303 if( !aSeq.IsEmpty() )
308 ObjectKind HYDROGUI_Tool::GetSelectedPartition( HYDROGUI_Module* theModule )
310 HYDROGUI_DataModel* aModel = theModule->getDataModel();
312 SUIT_SelectionMgr* aSelectionMgr = theModule->getApp()->selectionMgr();
313 SUIT_DataOwnerPtrList anOwners;
314 aSelectionMgr->selected( anOwners );
316 if( anOwners.size() != 1 )
319 if( LightApp_DataOwner* anOwner = dynamic_cast<LightApp_DataOwner*>( anOwners.first().operator->() ) )
321 QString anEntry = anOwner->entry();
322 QString aPrefix = HYDROGUI_DataObject::entryPrefix();
323 if( anEntry.left( aPrefix.length() ) == aPrefix )
325 anEntry.remove( aPrefix );
326 for( ObjectKind anObjectKind = KIND_UNKNOWN + 1; anObjectKind <= KIND_LAST; anObjectKind++ )
327 if( HYDROGUI_DataModel::partitionName( anObjectKind ) == anEntry )
334 Handle(HYDROData_Object) HYDROGUI_Tool::FindObjectByName( HYDROGUI_Module* theModule,
335 const QString& theName,
336 const ObjectKind theObjectKind )
338 Handle(HYDROData_Object) anObject;
339 if ( theName.isEmpty() )
342 QStringList aNamesList;
343 aNamesList << theName;
345 HYDROData_SequenceOfObjects aSeqOfObjs = FindObjectsByNames( theModule, aNamesList, theObjectKind );
346 if( aSeqOfObjs.IsEmpty() )
349 anObject = aSeqOfObjs.First();
353 HYDROData_SequenceOfObjects HYDROGUI_Tool::FindObjectsByNames( HYDROGUI_Module* theModule,
354 const QStringList& theNames,
355 const ObjectKind theObjectKind )
357 HYDROData_SequenceOfObjects aResSeq;
358 if ( theNames.isEmpty() )
361 Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( theModule->getStudyId() );
362 if( aDocument.IsNull() )
365 QStringList aNamesList = theNames;
367 HYDROData_Iterator anIter( aDocument, theObjectKind );
368 for( ; anIter.More(); anIter.Next() )
370 Handle(HYDROData_Object) anObjectRef = anIter.Current();
371 if( anObjectRef.IsNull() )
374 QString anObjName = anObjectRef->GetName();
375 if ( anObjName.isEmpty() || !aNamesList.contains( anObjName ) )
378 aResSeq.Append( anObjectRef );
380 aNamesList.removeAll( anObjName );
381 if ( aNamesList.isEmpty() )
388 QString HYDROGUI_Tool::GenerateObjectName( HYDROGUI_Module* theModule,
389 const QString& thePrefix,
390 const QStringList& theUsedNames )
396 aName = QString( "%1_%2" ).arg( thePrefix ).arg( QString::number( anId++ ) );
398 if ( theUsedNames.contains( aName ) )
401 // check that there are no other objects with the same name in the document
402 Handle(HYDROData_Object) anObject = FindObjectByName( theModule, aName, KIND_UNKNOWN );
403 if( anObject.IsNull() )
409 size_t HYDROGUI_Tool::GetActiveViewId( HYDROGUI_Module* theModule,
410 const QString& theViewId )
413 SUIT_ViewManager* aViewMgr = theModule->getApp()->activeViewManager();
414 if( !aViewMgr || ( !theViewId.isEmpty() && aViewMgr->getType() != theViewId ) )
417 if( SUIT_ViewModel* aViewer = aViewMgr->getViewModel() )
418 aViewId = (size_t)aViewer;
422 size_t HYDROGUI_Tool::GetActiveGraphicsViewId( HYDROGUI_Module* theModule )
424 return GetActiveViewId( theModule, GraphicsView_Viewer::Type() );
427 size_t HYDROGUI_Tool::GetActiveOCCViewId( HYDROGUI_Module* theModule )
429 return GetActiveViewId( theModule, OCCViewer_Viewer::Type() );
432 QList<size_t> getViewIdList( HYDROGUI_Module* theModule,
433 const QString& theViewId )
436 ViewManagerList aViewMgrs;
437 theModule->getApp()->viewManagers( theViewId, aViewMgrs );
438 QListIterator<SUIT_ViewManager*> anIter( aViewMgrs );
439 while( anIter.hasNext() )
441 if( SUIT_ViewManager* aViewMgr = anIter.next() )
443 if( SUIT_ViewModel* aViewer = aViewMgr->getViewModel() )
444 aList.append( (size_t)aViewer );
450 QList<size_t> HYDROGUI_Tool::GetGraphicsViewIdList( HYDROGUI_Module* theModule )
452 return getViewIdList( theModule, GraphicsView_Viewer::Type() );
455 QList<size_t> HYDROGUI_Tool::GetOCCViewIdList( HYDROGUI_Module* theModule )
457 return getViewIdList( theModule, OCCViewer_Viewer::Type() );
460 void HYDROGUI_Tool::GetObjectReferences( const Handle(HYDROData_Image)& theImage,
461 HYDROData_SequenceOfObjects& theRefObjects,
462 QStringList& theRefNames )
464 if( theImage.IsNull() )
467 for( int anIndex = 0, aNbRef = theImage->NbReferences(); anIndex < aNbRef; anIndex++ )
469 Handle(HYDROData_Object) aRefObj = theImage->Reference( anIndex );
470 if( !aRefObj.IsNull() && !aRefObj->IsRemoved() )
472 QString aName = aRefObj->GetName();
473 if( !theRefNames.contains( aName ) )
475 theRefObjects.Append( aRefObj );
476 theRefNames.append( aRefObj->GetName() );
477 if( aRefObj->GetKind() == KIND_IMAGE )
479 Handle(HYDROData_Image) aRefImage = Handle(HYDROData_Image)::DownCast( aRefObj );
480 if( !aRefImage.IsNull() )
481 GetObjectReferences( aRefImage, theRefObjects, theRefNames );
488 void HYDROGUI_Tool::GetObjectBackReferences( HYDROGUI_Module* theModule,
489 const Handle(HYDROData_Object)& theObj,
490 HYDROData_SequenceOfObjects& theBackRefObjects,
491 QStringList& theBackRefNames )
493 if( theObj.IsNull() )
496 Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( theModule->getStudyId() );
497 if( aDocument.IsNull() )
500 QString aName = theObj->GetName();
502 HYDROData_Iterator anIterator( aDocument, KIND_IMAGE );
503 for( ; anIterator.More(); anIterator.Next() )
505 Handle(HYDROData_Image) anImage = Handle(HYDROData_Image)::DownCast( anIterator.Current() );
506 if( !anImage.IsNull() )
508 HYDROData_SequenceOfObjects aRefObjects;
509 QStringList aRefNames;
510 GetObjectReferences( anImage, aRefObjects, aRefNames );
511 if( aRefNames.contains( aName ) )
513 theBackRefObjects.Append( anImage );
514 theBackRefNames.append( anImage->GetName() );
521 QDockWidget* HYDROGUI_Tool::WindowDock( QWidget* wid )
526 QDockWidget* dock = 0;
527 QWidget* w = wid->parentWidget();
530 dock = ::qobject_cast<QDockWidget*>( w );
531 w = w->parentWidget();
536 QColor HYDROGUI_Tool::GenerateFillingColor( HYDROGUI_Module* theModule,
537 const QStringList& theZoneNames )
539 QColor aFillingColor( HYDROData_Zone::DefaultFillingColor() );
542 int aR = 0, aG = 0, aB = 0;
543 QStringListIterator aZoneNameIter( theZoneNames );
544 while( aZoneNameIter.hasNext() )
546 const QString& aZoneName = aZoneNameIter.next();
547 Handle(HYDROData_Zone) aRefZone = Handle(HYDROData_Zone)::DownCast(
548 FindObjectByName( theModule, aZoneName, KIND_ZONE ) );
549 if( !aRefZone.IsNull() )
551 QColor aRefColor = aRefZone->GetFillingColor();
552 aR += aRefColor.red();
553 aG += aRefColor.green();
554 aB += aRefColor.blue();
561 aFillingColor = QColor( aR / aCounter, aG / aCounter, aB / aCounter );
564 return aFillingColor;