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 void HYDROGUI_Tool::LambertToDouble( const int theDegrees,
138 const int theMinutes,
139 const double theSeconds,
142 // ouv: check the case of negative degrees
143 theCoord = theDegrees * 3600 + theMinutes * 60 + theSeconds;
146 void HYDROGUI_Tool::DoubleToLambert( const double theCoord,
151 // ouv: check the case of negative degrees
152 theDegrees = int( theCoord / 3600 );
154 double aRemainder = theCoord - theDegrees * 3600;
155 theMinutes = int( aRemainder / 60 );
157 theSeconds = aRemainder - theMinutes * 60;
160 bool HYDROGUI_Tool::IsEqual( const Handle(HYDROData_Object)& theObj1,
161 const Handle(HYDROData_Object)& theObj2 )
163 if( !theObj1.IsNull() && !theObj2.IsNull() )
164 return theObj1->Label() == theObj2->Label(); //ouv: check that the names can be used here
168 void HYDROGUI_Tool::SetActiveViewManager( HYDROGUI_Module* theModule,
169 SUIT_ViewManager* theViewManager )
172 if( SUIT_ViewWindow* aViewWindow = theViewManager->getActiveView() )
173 if( STD_TabDesktop* aTabDesktop = dynamic_cast<STD_TabDesktop*>( theModule->getApp()->desktop() ) )
174 if( QtxWorkstack* aWorkstack = aTabDesktop->workstack() )
175 aWorkstack->setActiveWindow( aViewWindow );
178 void HYDROGUI_Tool::GetPrsSubObjects( HYDROGUI_Module* theModule,
179 HYDROData_SequenceOfObjects& theSeq )
181 Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( theModule->getStudyId() );
182 if( aDocument.IsNull() )
185 HYDROData_Iterator anIterator( aDocument, KIND_IMAGE );
186 for( ; anIterator.More(); anIterator.Next() )
188 Handle(HYDROData_Object) anObject = anIterator.Current();
189 if( !anObject.IsNull() )
190 theSeq.Append( anObject );
193 anIterator = HYDROData_Iterator( aDocument, KIND_POLYLINE );
194 for( ; anIterator.More(); anIterator.Next() )
196 Handle(HYDROData_Object) anObject = anIterator.Current();
197 if( !anObject.IsNull() )
198 theSeq.Append( anObject );
201 anIterator = HYDROData_Iterator( aDocument, KIND_ZONE );
202 for( ; anIterator.More(); anIterator.Next() )
204 Handle(HYDROData_Object) anObject = anIterator.Current();
205 if( !anObject.IsNull() )
206 theSeq.Append( anObject );
210 HYDROGUI_Prs* HYDROGUI_Tool::GetPresentation( const Handle(HYDROData_Object)& theObj,
211 const GraphicsView_ObjectList& theObjects )
213 if( !theObj.IsNull() )
215 GraphicsView_ObjectListIterator anIter( theObjects );
216 while( anIter.hasNext() )
218 if( HYDROGUI_Prs* aPrs = dynamic_cast<HYDROGUI_Prs*>( anIter.next() ) )
220 Handle(HYDROData_Object) anObj = aPrs->getObject();
221 if( IsEqual( anObj, theObj ) )
229 GraphicsView_ObjectList HYDROGUI_Tool::GetPrsList( GraphicsView_ViewPort* theViewPort )
231 GraphicsView_ObjectList aList;
234 GraphicsView_ObjectListIterator anIter( theViewPort->getObjects() );
235 while( anIter.hasNext() )
236 if( HYDROGUI_Prs* aPrs = dynamic_cast<HYDROGUI_Prs*>( anIter.next() ) )
237 aList.append( aPrs );
242 HYDROData_SequenceOfObjects HYDROGUI_Tool::GetSelectedObjects( HYDROGUI_Module* theModule )
244 HYDROData_SequenceOfObjects aSeq;
246 HYDROGUI_DataModel* aModel = theModule->getDataModel();
248 SUIT_SelectionMgr* aSelectionMgr = theModule->getApp()->selectionMgr();
249 SUIT_DataOwnerPtrList anOwners;
250 aSelectionMgr->selected( anOwners );
252 QStringList aCollectedNameList; // to avoid duplication
253 foreach( SUIT_DataOwner* aSUITOwner, anOwners )
255 if( LightApp_DataOwner* anOwner = dynamic_cast<LightApp_DataOwner*>( aSUITOwner ) )
257 Handle(HYDROData_Object) anObject = aModel->objectByEntry( anOwner->entry() );
258 if( !anObject.IsNull() )
260 QString aName = anObject->GetName();
261 if( !aCollectedNameList.contains( aName ) )
263 aSeq.Append( anObject );
264 aCollectedNameList.append( aName );
272 Handle(HYDROData_Object) HYDROGUI_Tool::GetSelectedObject( HYDROGUI_Module* theModule )
274 HYDROData_SequenceOfObjects aSeq = GetSelectedObjects( theModule );
275 if( !aSeq.IsEmpty() )
280 ObjectKind HYDROGUI_Tool::GetSelectedPartition( HYDROGUI_Module* theModule )
282 HYDROGUI_DataModel* aModel = theModule->getDataModel();
284 SUIT_SelectionMgr* aSelectionMgr = theModule->getApp()->selectionMgr();
285 SUIT_DataOwnerPtrList anOwners;
286 aSelectionMgr->selected( anOwners );
288 if( anOwners.size() != 1 )
291 if( LightApp_DataOwner* anOwner = dynamic_cast<LightApp_DataOwner*>( anOwners.first().operator->() ) )
293 QString anEntry = anOwner->entry();
294 QString aPrefix = HYDROGUI_DataObject::entryPrefix();
295 if( anEntry.left( aPrefix.length() ) == aPrefix )
297 anEntry.remove( aPrefix );
298 for( ObjectKind anObjectKind = KIND_UNKNOWN + 1; anObjectKind <= KIND_LAST; anObjectKind++ )
299 if( HYDROGUI_DataModel::partitionName( anObjectKind ) == anEntry )
306 Handle(HYDROData_Object) HYDROGUI_Tool::FindObjectByName( HYDROGUI_Module* theModule,
307 const QString& theName,
308 const ObjectKind theObjectKind )
310 Handle(HYDROData_Object) anObject;
311 if ( theName.isEmpty() )
314 QStringList aNamesList;
315 aNamesList << theName;
317 HYDROData_SequenceOfObjects aSeqOfObjs = FindObjectsByNames( theModule, aNamesList, theObjectKind );
318 if( aSeqOfObjs.IsEmpty() )
321 anObject = aSeqOfObjs.First();
325 HYDROData_SequenceOfObjects HYDROGUI_Tool::FindObjectsByNames( HYDROGUI_Module* theModule,
326 const QStringList& theNames,
327 const ObjectKind theObjectKind )
329 HYDROData_SequenceOfObjects aResSeq;
330 if ( theNames.isEmpty() )
333 Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( theModule->getStudyId() );
334 if( aDocument.IsNull() )
337 QStringList aNamesList = theNames;
339 HYDROData_Iterator anIter( aDocument, theObjectKind );
340 for( ; anIter.More(); anIter.Next() )
342 Handle(HYDROData_Object) anObjectRef = anIter.Current();
343 if( anObjectRef.IsNull() )
346 QString anObjName = anObjectRef->GetName();
347 if ( anObjName.isEmpty() || !aNamesList.contains( anObjName ) )
350 aResSeq.Append( anObjectRef );
352 aNamesList.removeAll( anObjName );
353 if ( aNamesList.isEmpty() )
360 QString HYDROGUI_Tool::GenerateObjectName( HYDROGUI_Module* theModule,
361 const QString& thePrefix,
362 const QStringList& theUsedNames )
368 aName = QString( "%1_%2" ).arg( thePrefix ).arg( QString::number( anId++ ) );
370 if ( theUsedNames.contains( aName ) )
373 // check that there are no other objects with the same name in the document
374 Handle(HYDROData_Object) anObject = FindObjectByName( theModule, aName, KIND_UNKNOWN );
375 if( anObject.IsNull() )
381 size_t HYDROGUI_Tool::GetActiveViewId( HYDROGUI_Module* theModule,
382 const QString& theViewId )
385 SUIT_ViewManager* aViewMgr = theModule->getApp()->activeViewManager();
386 if( !aViewMgr || ( !theViewId.isEmpty() && aViewMgr->getType() != theViewId ) )
389 if( SUIT_ViewModel* aViewer = aViewMgr->getViewModel() )
390 aViewId = (size_t)aViewer;
394 size_t HYDROGUI_Tool::GetActiveGraphicsViewId( HYDROGUI_Module* theModule )
396 return GetActiveViewId( theModule, GraphicsView_Viewer::Type() );
399 size_t HYDROGUI_Tool::GetActiveOCCViewId( HYDROGUI_Module* theModule )
401 return GetActiveViewId( theModule, OCCViewer_Viewer::Type() );
404 QList<size_t> getViewIdList( HYDROGUI_Module* theModule,
405 const QString& theViewId )
408 ViewManagerList aViewMgrs;
409 theModule->getApp()->viewManagers( theViewId, aViewMgrs );
410 QListIterator<SUIT_ViewManager*> anIter( aViewMgrs );
411 while( anIter.hasNext() )
413 if( SUIT_ViewManager* aViewMgr = anIter.next() )
415 if( SUIT_ViewModel* aViewer = aViewMgr->getViewModel() )
416 aList.append( (size_t)aViewer );
422 QList<size_t> HYDROGUI_Tool::GetGraphicsViewIdList( HYDROGUI_Module* theModule )
424 return getViewIdList( theModule, GraphicsView_Viewer::Type() );
427 QList<size_t> HYDROGUI_Tool::GetOCCViewIdList( HYDROGUI_Module* theModule )
429 return getViewIdList( theModule, OCCViewer_Viewer::Type() );
432 void HYDROGUI_Tool::GetObjectReferences( const Handle(HYDROData_Image)& theImage,
433 HYDROData_SequenceOfObjects& theRefObjects,
434 QStringList& theRefNames )
436 if( theImage.IsNull() )
439 for( int anIndex = 0, aNbRef = theImage->NbReferences(); anIndex < aNbRef; anIndex++ )
441 Handle(HYDROData_Object) aRefObj = theImage->Reference( anIndex );
442 if( !aRefObj.IsNull() && !aRefObj->IsRemoved() )
444 QString aName = aRefObj->GetName();
445 if( !theRefNames.contains( aName ) )
447 theRefObjects.Append( aRefObj );
448 theRefNames.append( aRefObj->GetName() );
449 if( aRefObj->GetKind() == KIND_IMAGE )
451 Handle(HYDROData_Image) aRefImage = Handle(HYDROData_Image)::DownCast( aRefObj );
452 if( !aRefImage.IsNull() )
453 GetObjectReferences( aRefImage, theRefObjects, theRefNames );
460 void HYDROGUI_Tool::GetObjectBackReferences( HYDROGUI_Module* theModule,
461 const Handle(HYDROData_Object)& theObj,
462 HYDROData_SequenceOfObjects& theBackRefObjects,
463 QStringList& theBackRefNames )
465 if( theObj.IsNull() )
468 Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( theModule->getStudyId() );
469 if( aDocument.IsNull() )
472 QString aName = theObj->GetName();
474 HYDROData_Iterator anIterator( aDocument, KIND_IMAGE );
475 for( ; anIterator.More(); anIterator.Next() )
477 Handle(HYDROData_Image) anImage = Handle(HYDROData_Image)::DownCast( anIterator.Current() );
478 if( !anImage.IsNull() )
480 HYDROData_SequenceOfObjects aRefObjects;
481 QStringList aRefNames;
482 GetObjectReferences( anImage, aRefObjects, aRefNames );
483 if( aRefNames.contains( aName ) )
485 theBackRefObjects.Append( anImage );
486 theBackRefNames.append( anImage->GetName() );
493 QDockWidget* HYDROGUI_Tool::WindowDock( QWidget* wid )
498 QDockWidget* dock = 0;
499 QWidget* w = wid->parentWidget();
502 dock = ::qobject_cast<QDockWidget*>( w );
503 w = w->parentWidget();
508 QColor HYDROGUI_Tool::GenerateFillingColor( HYDROGUI_Module* theModule,
509 const QStringList& theZoneNames )
511 QColor aFillingColor( HYDROData_Zone::DefaultFillingColor() );
514 int aR = 0, aG = 0, aB = 0;
515 QStringListIterator aZoneNameIter( theZoneNames );
516 while( aZoneNameIter.hasNext() )
518 const QString& aZoneName = aZoneNameIter.next();
519 Handle(HYDROData_Zone) aRefZone = Handle(HYDROData_Zone)::DownCast(
520 FindObjectByName( theModule, aZoneName, KIND_ZONE ) );
521 if( !aRefZone.IsNull() )
523 QColor aRefColor = aRefZone->GetFillingColor();
524 aR += aRefColor.red();
525 aG += aRefColor.green();
526 aB += aRefColor.blue();
533 aFillingColor = QColor( aR / aCounter, aG / aCounter, aB / aCounter );
536 return aFillingColor;