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_Study.h>
47 #include <SUIT_ViewManager.h>
48 #include <SUIT_ViewWindow.h>
51 #include <QDockWidget>
53 // Definition of this id allows to use 'latin1' (Qt alias for 'ISO-8859-1')
54 // encoding instead of default 'System'
55 #define USE_LATIN1_ENCODING
57 QString HYDROGUI_Tool::ToQString( const TCollection_AsciiString& src )
59 #ifdef USE_LATIN1_ENCODING
60 QTextCodec* codec = QTextCodec::codecForName( "latin1" ); // alias for ISO-8859-1
62 QTextCodec* codec = QTextCodec::codecForLocale();
66 res = codec ? codec->toUnicode( (char*)src.ToCString(), src.Length() ) :
67 QString( (char*)src.ToCString() );
71 QString HYDROGUI_Tool::ToQString( const TCollection_ExtendedString& src )
73 return QString( (QChar*)src.ToExtString(), src.Length() );
76 QString HYDROGUI_Tool::ToQString( const Handle(TCollection_HAsciiString)& src )
81 return ToQString( src->String() );
84 QString HYDROGUI_Tool::ToQString( const Handle(TCollection_HExtendedString)& src )
88 return ToQString( src->String() );
91 TCollection_AsciiString HYDROGUI_Tool::ToAsciiString( const QString& src )
93 TCollection_AsciiString res;
96 #ifdef USE_LATIN1_ENCODING
97 QTextCodec* codec = QTextCodec::codecForName( "latin1" ); // alias for ISO-8859-1
99 QTextCodec* codec = QTextCodec::codecForLocale();
103 QByteArray str = codec->fromUnicode( src );
104 res = TCollection_AsciiString( (Standard_CString)str.constData() );
107 res = TCollection_AsciiString( src.toLatin1().data() );
112 TCollection_ExtendedString HYDROGUI_Tool::ToExtString( const QString& src )
115 return TCollection_ExtendedString();
117 Standard_Integer len = src.length();
118 Standard_ExtString extStr = new Standard_ExtCharacter[ ( len + 1 ) * 2 ];
119 memcpy( (void*)extStr, src.unicode(), len * 2 );
120 ((short*)extStr)[ len ] = 0;
122 TCollection_ExtendedString trg( extStr );
127 Handle(TCollection_HAsciiString) HYDROGUI_Tool::ToHAsciiString( const QString& src )
129 return new TCollection_HAsciiString( ToAsciiString( src ) );
132 Handle(TCollection_HExtendedString) HYDROGUI_Tool::ToHExtString( const QString& src )
134 return new TCollection_HExtendedString( ToExtString( src ) );
137 bool HYDROGUI_Tool::IsEqual( const Handle(HYDROData_Object)& theObj1,
138 const Handle(HYDROData_Object)& theObj2 )
140 if( !theObj1.IsNull() && !theObj2.IsNull() )
141 return theObj1->Label() == theObj2->Label(); //ouv: check that the names can be used here
145 void HYDROGUI_Tool::SetActiveViewManager( HYDROGUI_Module* theModule,
146 SUIT_ViewManager* theViewManager )
149 if( SUIT_ViewWindow* aViewWindow = theViewManager->getActiveView() )
150 if( STD_TabDesktop* aTabDesktop = dynamic_cast<STD_TabDesktop*>( theModule->getApp()->desktop() ) )
151 if( QtxWorkstack* aWorkstack = aTabDesktop->workstack() )
152 aWorkstack->setActiveWindow( aViewWindow );
155 void HYDROGUI_Tool::GetPrsSubObjects( HYDROGUI_Module* theModule,
156 HYDROData_SequenceOfObjects& theSeq )
158 Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( theModule->getStudyId() );
159 if( aDocument.IsNull() )
162 HYDROData_Iterator anIterator( aDocument, KIND_IMAGE );
163 for( ; anIterator.More(); anIterator.Next() )
165 Handle(HYDROData_Object) anObject = anIterator.Current();
166 if( !anObject.IsNull() )
167 theSeq.Append( anObject );
170 anIterator = HYDROData_Iterator( aDocument, KIND_POLYLINE );
171 for( ; anIterator.More(); anIterator.Next() )
173 Handle(HYDROData_Object) anObject = anIterator.Current();
174 if( !anObject.IsNull() )
175 theSeq.Append( anObject );
178 anIterator = HYDROData_Iterator( aDocument, KIND_ZONE );
179 for( ; anIterator.More(); anIterator.Next() )
181 Handle(HYDROData_Object) anObject = anIterator.Current();
182 if( !anObject.IsNull() )
183 theSeq.Append( anObject );
187 HYDROGUI_Prs* HYDROGUI_Tool::GetPresentation( const Handle(HYDROData_Object)& theObj,
188 const GraphicsView_ObjectList& theObjects )
190 if( !theObj.IsNull() )
192 GraphicsView_ObjectListIterator anIter( theObjects );
193 while( anIter.hasNext() )
195 if( HYDROGUI_Prs* aPrs = dynamic_cast<HYDROGUI_Prs*>( anIter.next() ) )
197 Handle(HYDROData_Object) anObj = aPrs->getObject();
198 if( IsEqual( anObj, theObj ) )
206 GraphicsView_ObjectList HYDROGUI_Tool::GetPrsList( GraphicsView_ViewPort* theViewPort )
208 GraphicsView_ObjectList aList;
211 GraphicsView_ObjectListIterator anIter( theViewPort->getObjects() );
212 while( anIter.hasNext() )
213 if( HYDROGUI_Prs* aPrs = dynamic_cast<HYDROGUI_Prs*>( anIter.next() ) )
214 aList.append( aPrs );
219 HYDROData_SequenceOfObjects HYDROGUI_Tool::GetSelectedObjects( HYDROGUI_Module* theModule )
221 HYDROData_SequenceOfObjects aSeq;
223 HYDROGUI_DataModel* aModel = theModule->getDataModel();
225 SUIT_SelectionMgr* aSelectionMgr = theModule->getApp()->selectionMgr();
226 SUIT_DataOwnerPtrList anOwners;
227 aSelectionMgr->selected( anOwners );
229 QStringList aCollectedNameList; // to avoid duplication
230 foreach( SUIT_DataOwner* aSUITOwner, anOwners )
232 if( LightApp_DataOwner* anOwner = dynamic_cast<LightApp_DataOwner*>( aSUITOwner ) )
234 Handle(HYDROData_Object) anObject = aModel->objectByEntry( anOwner->entry() );
235 if( !anObject.IsNull() )
237 QString aName = anObject->GetName();
238 if( !aCollectedNameList.contains( aName ) )
240 aSeq.Append( anObject );
241 aCollectedNameList.append( aName );
249 Handle(HYDROData_Object) HYDROGUI_Tool::GetSelectedObject( HYDROGUI_Module* theModule )
251 HYDROData_SequenceOfObjects aSeq = GetSelectedObjects( theModule );
252 if( !aSeq.IsEmpty() )
257 ObjectKind HYDROGUI_Tool::GetSelectedPartition( HYDROGUI_Module* theModule )
259 HYDROGUI_DataModel* aModel = theModule->getDataModel();
261 SUIT_SelectionMgr* aSelectionMgr = theModule->getApp()->selectionMgr();
262 SUIT_DataOwnerPtrList anOwners;
263 aSelectionMgr->selected( anOwners );
265 if( anOwners.size() != 1 )
268 if( LightApp_DataOwner* anOwner = dynamic_cast<LightApp_DataOwner*>( anOwners.first().operator->() ) )
270 QString anEntry = anOwner->entry();
271 QString aPrefix = HYDROGUI_DataObject::entryPrefix();
272 if( anEntry.left( aPrefix.length() ) == aPrefix )
274 anEntry.remove( aPrefix );
275 for( ObjectKind anObjectKind = KIND_UNKNOWN + 1; anObjectKind <= KIND_LAST; anObjectKind++ )
276 if( HYDROGUI_DataModel::partitionName( anObjectKind ) == anEntry )
283 Handle(HYDROData_Object) HYDROGUI_Tool::FindObjectByName( HYDROGUI_Module* theModule,
284 const QString& theName,
285 const ObjectKind theObjectKind )
287 Handle(HYDROData_Object) anObject;
288 if ( theName.isEmpty() )
291 QStringList aNamesList;
292 aNamesList << theName;
294 HYDROData_SequenceOfObjects aSeqOfObjs = FindObjectsByNames( theModule, aNamesList, theObjectKind );
295 if( aSeqOfObjs.IsEmpty() )
298 anObject = aSeqOfObjs.First();
302 HYDROData_SequenceOfObjects HYDROGUI_Tool::FindObjectsByNames( HYDROGUI_Module* theModule,
303 const QStringList& theNames,
304 const ObjectKind theObjectKind )
306 HYDROData_SequenceOfObjects aResSeq;
307 if ( theNames.isEmpty() )
310 Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( theModule->getStudyId() );
311 if( aDocument.IsNull() )
314 QStringList aNamesList = theNames;
316 HYDROData_Iterator anIter( aDocument, theObjectKind );
317 for( ; anIter.More(); anIter.Next() )
319 Handle(HYDROData_Object) anObjectRef = anIter.Current();
320 if( anObjectRef.IsNull() )
323 QString anObjName = anObjectRef->GetName();
324 if ( anObjName.isEmpty() || !aNamesList.contains( anObjName ) )
327 aResSeq.Append( anObjectRef );
329 aNamesList.removeAll( anObjName );
330 if ( aNamesList.isEmpty() )
337 QString HYDROGUI_Tool::GenerateObjectName( HYDROGUI_Module* theModule,
338 const QString& thePrefix,
339 const QStringList& theUsedNames )
345 aName = QString( "%1_%2" ).arg( thePrefix ).arg( QString::number( anId++ ) );
347 if ( theUsedNames.contains( aName ) )
350 // check that there are no other objects with the same name in the document
351 Handle(HYDROData_Object) anObject = FindObjectByName( theModule, aName, KIND_UNKNOWN );
352 if( anObject.IsNull() )
358 size_t HYDROGUI_Tool::GetActiveViewId( HYDROGUI_Module* theModule,
359 const QString& theViewId )
362 SUIT_ViewManager* aViewMgr = theModule->getApp()->activeViewManager();
363 if( !aViewMgr || ( !theViewId.isEmpty() && aViewMgr->getType() != theViewId ) )
366 if( SUIT_ViewModel* aViewer = aViewMgr->getViewModel() )
367 aViewId = (size_t)aViewer;
371 size_t HYDROGUI_Tool::GetActiveGraphicsViewId( HYDROGUI_Module* theModule )
373 return GetActiveViewId( theModule, GraphicsView_Viewer::Type() );
376 size_t HYDROGUI_Tool::GetActiveOCCViewId( HYDROGUI_Module* theModule )
378 return GetActiveViewId( theModule, OCCViewer_Viewer::Type() );
381 QList<size_t> getViewIdList( HYDROGUI_Module* theModule,
382 const QString& theViewId )
385 ViewManagerList aViewMgrs;
386 theModule->getApp()->viewManagers( theViewId, aViewMgrs );
387 QListIterator<SUIT_ViewManager*> anIter( aViewMgrs );
388 while( anIter.hasNext() )
390 if( SUIT_ViewManager* aViewMgr = anIter.next() )
392 if( SUIT_ViewModel* aViewer = aViewMgr->getViewModel() )
393 aList.append( (size_t)aViewer );
399 QList<size_t> HYDROGUI_Tool::GetGraphicsViewIdList( HYDROGUI_Module* theModule )
401 return getViewIdList( theModule, GraphicsView_Viewer::Type() );
404 QList<size_t> HYDROGUI_Tool::GetOCCViewIdList( HYDROGUI_Module* theModule )
406 return getViewIdList( theModule, OCCViewer_Viewer::Type() );
409 void HYDROGUI_Tool::GetObjectReferences( const Handle(HYDROData_Image)& theImage,
410 HYDROData_SequenceOfObjects& theRefObjects,
411 QStringList& theRefNames )
413 if( theImage.IsNull() )
416 for( int anIndex = 0, aNbRef = theImage->NbReferences(); anIndex < aNbRef; anIndex++ )
418 Handle(HYDROData_Object) aRefObj = theImage->Reference( anIndex );
419 if( !aRefObj.IsNull() && !aRefObj->IsRemoved() )
421 QString aName = aRefObj->GetName();
422 if( !theRefNames.contains( aName ) )
424 theRefObjects.Append( aRefObj );
425 theRefNames.append( aRefObj->GetName() );
426 if( aRefObj->GetKind() == KIND_IMAGE )
428 Handle(HYDROData_Image) aRefImage = Handle(HYDROData_Image)::DownCast( aRefObj );
429 if( !aRefImage.IsNull() )
430 GetObjectReferences( aRefImage, theRefObjects, theRefNames );
437 void HYDROGUI_Tool::GetObjectBackReferences( HYDROGUI_Module* theModule,
438 const Handle(HYDROData_Object)& theObj,
439 HYDROData_SequenceOfObjects& theBackRefObjects,
440 QStringList& theBackRefNames )
442 if( theObj.IsNull() )
445 Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( theModule->getStudyId() );
446 if( aDocument.IsNull() )
449 QString aName = theObj->GetName();
451 HYDROData_Iterator anIterator( aDocument, KIND_IMAGE );
452 for( ; anIterator.More(); anIterator.Next() )
454 Handle(HYDROData_Image) anImage = Handle(HYDROData_Image)::DownCast( anIterator.Current() );
455 if( !anImage.IsNull() )
457 HYDROData_SequenceOfObjects aRefObjects;
458 QStringList aRefNames;
459 GetObjectReferences( anImage, aRefObjects, aRefNames );
460 if( aRefNames.contains( aName ) )
462 theBackRefObjects.Append( anImage );
463 theBackRefNames.append( anImage->GetName() );
470 QDockWidget* HYDROGUI_Tool::WindowDock( QWidget* wid )
475 QDockWidget* dock = 0;
476 QWidget* w = wid->parentWidget();
479 dock = ::qobject_cast<QDockWidget*>( w );
480 w = w->parentWidget();
485 QColor HYDROGUI_Tool::GenerateFillingColor( HYDROGUI_Module* theModule,
486 const QStringList& theZoneNames )
488 QColor aFillingColor( HYDROData_Zone::DefaultFillingColor() );
491 int aR = 0, aG = 0, aB = 0;
492 QStringListIterator aZoneNameIter( theZoneNames );
493 while( aZoneNameIter.hasNext() )
495 const QString& aZoneName = aZoneNameIter.next();
496 Handle(HYDROData_Zone) aRefZone = Handle(HYDROData_Zone)::DownCast(
497 FindObjectByName( theModule, aZoneName, KIND_ZONE ) );
498 if( !aRefZone.IsNull() )
500 QColor aRefColor = aRefZone->GetFillingColor();
501 aR += aRefColor.red();
502 aG += aRefColor.green();
503 aB += aRefColor.blue();
510 aFillingColor = QColor( aR / aCounter, aG / aCounter, aB / aCounter );
513 return aFillingColor;