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 <QtxWorkstack.h>
42 #include <STD_TabDesktop.h>
44 #include <SUIT_Study.h>
45 #include <SUIT_ViewManager.h>
46 #include <SUIT_ViewWindow.h>
49 #include <QDockWidget>
51 // Definition of this id allows to use 'latin1' (Qt alias for 'ISO-8859-1')
52 // encoding instead of default 'System'
53 #define USE_LATIN1_ENCODING
55 QString HYDROGUI_Tool::ToQString( const TCollection_AsciiString& src )
57 #ifdef USE_LATIN1_ENCODING
58 QTextCodec* codec = QTextCodec::codecForName( "latin1" ); // alias for ISO-8859-1
60 QTextCodec* codec = QTextCodec::codecForLocale();
64 res = codec ? codec->toUnicode( (char*)src.ToCString(), src.Length() ) :
65 QString( (char*)src.ToCString() );
69 QString HYDROGUI_Tool::ToQString( const TCollection_ExtendedString& src )
71 return QString( (QChar*)src.ToExtString(), src.Length() );
74 QString HYDROGUI_Tool::ToQString( const Handle(TCollection_HAsciiString)& src )
79 return ToQString( src->String() );
82 QString HYDROGUI_Tool::ToQString( const Handle(TCollection_HExtendedString)& src )
86 return ToQString( src->String() );
89 TCollection_AsciiString HYDROGUI_Tool::ToAsciiString( const QString& src )
91 TCollection_AsciiString res;
94 #ifdef USE_LATIN1_ENCODING
95 QTextCodec* codec = QTextCodec::codecForName( "latin1" ); // alias for ISO-8859-1
97 QTextCodec* codec = QTextCodec::codecForLocale();
101 QByteArray str = codec->fromUnicode( src );
102 res = TCollection_AsciiString( (Standard_CString)str.constData() );
105 res = TCollection_AsciiString( src.toLatin1().data() );
110 TCollection_ExtendedString HYDROGUI_Tool::ToExtString( const QString& src )
113 return TCollection_ExtendedString();
115 Standard_Integer len = src.length();
116 Standard_ExtString extStr = new Standard_ExtCharacter[ ( len + 1 ) * 2 ];
117 memcpy( (void*)extStr, src.unicode(), len * 2 );
118 ((short*)extStr)[ len ] = 0;
120 TCollection_ExtendedString trg( extStr );
125 Handle(TCollection_HAsciiString) HYDROGUI_Tool::ToHAsciiString( const QString& src )
127 return new TCollection_HAsciiString( ToAsciiString( src ) );
130 Handle(TCollection_HExtendedString) HYDROGUI_Tool::ToHExtString( const QString& src )
132 return new TCollection_HExtendedString( ToExtString( src ) );
135 void HYDROGUI_Tool::LambertToDouble( const int theDegrees,
136 const int theMinutes,
137 const double theSeconds,
140 // ouv: check the case of negative degrees
141 theCoord = theDegrees * 3600 + theMinutes * 60 + theSeconds;
144 void HYDROGUI_Tool::DoubleToLambert( const double theCoord,
149 // ouv: check the case of negative degrees
150 theDegrees = int( theCoord / 3600 );
152 double aRemainder = theCoord - theDegrees * 3600;
153 theMinutes = int( aRemainder / 60 );
155 theSeconds = aRemainder - theMinutes * 60;
158 bool HYDROGUI_Tool::IsEqual( const Handle(HYDROData_Object)& theObj1,
159 const Handle(HYDROData_Object)& theObj2 )
161 if( !theObj1.IsNull() && !theObj2.IsNull() )
162 return theObj1->Label() == theObj2->Label(); //ouv: check that the names can be used here
166 void HYDROGUI_Tool::SetActiveViewManager( HYDROGUI_Module* theModule,
167 SUIT_ViewManager* theViewManager )
170 if( SUIT_ViewWindow* aViewWindow = theViewManager->getActiveView() )
171 if( STD_TabDesktop* aTabDesktop = dynamic_cast<STD_TabDesktop*>( theModule->getApp()->desktop() ) )
172 if( QtxWorkstack* aWorkstack = aTabDesktop->workstack() )
173 aWorkstack->setActiveWindow( aViewWindow );
176 void HYDROGUI_Tool::GetPrsSubObjects( HYDROGUI_Module* theModule,
177 HYDROData_SequenceOfObjects& theSeq )
179 Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( theModule->getStudyId() );
180 if( aDocument.IsNull() )
183 HYDROData_Iterator anIterator( aDocument, KIND_IMAGE );
184 for( ; anIterator.More(); anIterator.Next() )
186 Handle(HYDROData_Object) anObject = anIterator.Current();
187 if( !anObject.IsNull() )
188 theSeq.Append( anObject );
191 anIterator = HYDROData_Iterator( aDocument, KIND_POLYLINE );
192 for( ; anIterator.More(); anIterator.Next() )
194 Handle(HYDROData_Object) anObject = anIterator.Current();
195 if( !anObject.IsNull() )
196 theSeq.Append( anObject );
199 anIterator = HYDROData_Iterator( aDocument, KIND_ZONE );
200 for( ; anIterator.More(); anIterator.Next() )
202 Handle(HYDROData_Object) anObject = anIterator.Current();
203 if( !anObject.IsNull() )
204 theSeq.Append( anObject );
208 HYDROGUI_Prs* HYDROGUI_Tool::GetPresentation( const Handle(HYDROData_Object)& theObj,
209 const GraphicsView_ObjectList& theObjects )
211 if( !theObj.IsNull() )
213 GraphicsView_ObjectListIterator anIter( theObjects );
214 while( anIter.hasNext() )
216 if( HYDROGUI_Prs* aPrs = dynamic_cast<HYDROGUI_Prs*>( anIter.next() ) )
218 Handle(HYDROData_Object) anObj = aPrs->getObject();
219 if( IsEqual( anObj, theObj ) )
227 GraphicsView_ObjectList HYDROGUI_Tool::GetPrsList( GraphicsView_ViewPort* theViewPort )
229 GraphicsView_ObjectList aList;
232 GraphicsView_ObjectListIterator anIter( theViewPort->getObjects() );
233 while( anIter.hasNext() )
234 if( HYDROGUI_Prs* aPrs = dynamic_cast<HYDROGUI_Prs*>( anIter.next() ) )
235 aList.append( aPrs );
240 HYDROData_SequenceOfObjects HYDROGUI_Tool::GetSelectedObjects( HYDROGUI_Module* theModule )
242 HYDROData_SequenceOfObjects aSeq;
244 HYDROGUI_DataModel* aModel = theModule->getDataModel();
246 SUIT_SelectionMgr* aSelectionMgr = theModule->getApp()->selectionMgr();
247 SUIT_DataOwnerPtrList anOwners;
248 aSelectionMgr->selected( anOwners );
250 QStringList aCollectedNameList; // to avoid duplication
251 foreach( SUIT_DataOwner* aSUITOwner, anOwners )
253 if( LightApp_DataOwner* anOwner = dynamic_cast<LightApp_DataOwner*>( aSUITOwner ) )
255 Handle(HYDROData_Object) anObject = aModel->objectByEntry( anOwner->entry() );
256 if( !anObject.IsNull() )
258 QString aName = anObject->GetName();
259 if( !aCollectedNameList.contains( aName ) )
261 aSeq.Append( anObject );
262 aCollectedNameList.append( aName );
270 Handle(HYDROData_Object) HYDROGUI_Tool::GetSelectedObject( HYDROGUI_Module* theModule )
272 HYDROData_SequenceOfObjects aSeq = GetSelectedObjects( theModule );
273 if( !aSeq.IsEmpty() )
278 ObjectKind HYDROGUI_Tool::GetSelectedPartition( HYDROGUI_Module* theModule )
280 HYDROGUI_DataModel* aModel = theModule->getDataModel();
282 SUIT_SelectionMgr* aSelectionMgr = theModule->getApp()->selectionMgr();
283 SUIT_DataOwnerPtrList anOwners;
284 aSelectionMgr->selected( anOwners );
286 if( anOwners.size() != 1 )
289 if( LightApp_DataOwner* anOwner = dynamic_cast<LightApp_DataOwner*>( anOwners.first().operator->() ) )
291 QString anEntry = anOwner->entry();
292 QString aPrefix = HYDROGUI_DataObject::entryPrefix();
293 if( anEntry.left( aPrefix.length() ) == aPrefix )
295 anEntry.remove( aPrefix );
296 for( ObjectKind anObjectKind = KIND_UNKNOWN + 1; anObjectKind <= KIND_LAST; anObjectKind++ )
297 if( HYDROGUI_DataModel::partitionName( anObjectKind ) == anEntry )
304 Handle(HYDROData_Object) HYDROGUI_Tool::FindObjectByName( HYDROGUI_Module* theModule,
305 const QString& theName,
306 const ObjectKind theObjectKind )
308 Handle(HYDROData_Object) anObject;
309 if ( theName.isEmpty() )
312 QStringList aNamesList;
313 aNamesList << theName;
315 HYDROData_SequenceOfObjects aSeqOfObjs = FindObjectsByNames( theModule, aNamesList, theObjectKind );
316 if( aSeqOfObjs.IsEmpty() )
319 anObject = aSeqOfObjs.First();
323 HYDROData_SequenceOfObjects HYDROGUI_Tool::FindObjectsByNames( HYDROGUI_Module* theModule,
324 const QStringList& theNames,
325 const ObjectKind theObjectKind )
327 HYDROData_SequenceOfObjects aResSeq;
328 if ( theNames.isEmpty() )
331 Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( theModule->getStudyId() );
332 if( aDocument.IsNull() )
335 QStringList aNamesList = theNames;
337 HYDROData_Iterator anIter( aDocument, theObjectKind );
338 for( ; anIter.More(); anIter.Next() )
340 Handle(HYDROData_Object) anObjectRef = anIter.Current();
341 if( anObjectRef.IsNull() )
344 QString anObjName = anObjectRef->GetName();
345 if ( anObjName.isEmpty() || !aNamesList.contains( anObjName ) )
348 aResSeq.Append( anObjectRef );
350 aNamesList.removeAll( anObjName );
351 if ( aNamesList.isEmpty() )
358 QString HYDROGUI_Tool::GenerateObjectName( HYDROGUI_Module* theModule,
359 const QString& thePrefix,
360 const QStringList& theUsedNames )
366 aName = QString( "%1_%2" ).arg( thePrefix ).arg( QString::number( anId++ ) );
368 if ( theUsedNames.contains( aName ) )
371 // check that there are no other objects with the same name in the document
372 Handle(HYDROData_Object) anObject = FindObjectByName( theModule, aName, KIND_UNKNOWN );
373 if( anObject.IsNull() )
379 size_t HYDROGUI_Tool::GetActiveGraphicsViewId( HYDROGUI_Module* theModule )
382 SUIT_ViewManager* aViewMgr = theModule->getApp()->activeViewManager();
383 if( !aViewMgr || aViewMgr->getType() != GraphicsView_Viewer::Type() )
386 if( SUIT_ViewModel* aViewer = aViewMgr->getViewModel() )
387 aViewId = (size_t)aViewer;
391 QList<size_t> HYDROGUI_Tool::GetGraphicsViewIdList( HYDROGUI_Module* theModule )
394 ViewManagerList aViewMgrs;
395 theModule->getApp()->viewManagers( GraphicsView_Viewer::Type(), aViewMgrs );
396 QListIterator<SUIT_ViewManager*> anIter( aViewMgrs );
397 while( anIter.hasNext() )
399 if( SUIT_ViewManager* aViewMgr = anIter.next() )
401 if( SUIT_ViewModel* aViewer = aViewMgr->getViewModel() )
402 aList.append( (size_t)aViewer );
408 void HYDROGUI_Tool::GetObjectReferences( const Handle(HYDROData_Image)& theImage,
409 HYDROData_SequenceOfObjects& theRefObjects,
410 QStringList& theRefNames )
412 if( theImage.IsNull() )
415 for( int anIndex = 0, aNbRef = theImage->NbReferences(); anIndex < aNbRef; anIndex++ )
417 Handle(HYDROData_Object) aRefObj = theImage->Reference( anIndex );
418 if( !aRefObj.IsNull() && !aRefObj->IsRemoved() )
420 QString aName = aRefObj->GetName();
421 if( !theRefNames.contains( aName ) )
423 theRefObjects.Append( aRefObj );
424 theRefNames.append( aRefObj->GetName() );
425 if( aRefObj->GetKind() == KIND_IMAGE )
427 Handle(HYDROData_Image) aRefImage = Handle(HYDROData_Image)::DownCast( aRefObj );
428 if( !aRefImage.IsNull() )
429 GetObjectReferences( aRefImage, theRefObjects, theRefNames );
436 void HYDROGUI_Tool::GetObjectBackReferences( HYDROGUI_Module* theModule,
437 const Handle(HYDROData_Object)& theObj,
438 HYDROData_SequenceOfObjects& theBackRefObjects,
439 QStringList& theBackRefNames )
441 if( theObj.IsNull() )
444 Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( theModule->getStudyId() );
445 if( aDocument.IsNull() )
448 QString aName = theObj->GetName();
450 HYDROData_Iterator anIterator( aDocument, KIND_IMAGE );
451 for( ; anIterator.More(); anIterator.Next() )
453 Handle(HYDROData_Image) anImage = Handle(HYDROData_Image)::DownCast( anIterator.Current() );
454 if( !anImage.IsNull() )
456 HYDROData_SequenceOfObjects aRefObjects;
457 QStringList aRefNames;
458 GetObjectReferences( anImage, aRefObjects, aRefNames );
459 if( aRefNames.contains( aName ) )
461 theBackRefObjects.Append( anImage );
462 theBackRefNames.append( anImage->GetName() );
469 QDockWidget* HYDROGUI_Tool::WindowDock( QWidget* wid )
474 QDockWidget* dock = 0;
475 QWidget* w = wid->parentWidget();
478 dock = ::qobject_cast<QDockWidget*>( w );
479 w = w->parentWidget();
484 QColor HYDROGUI_Tool::GenerateFillingColor( HYDROGUI_Module* theModule,
485 const QStringList& theZoneNames )
487 QColor aFillingColor( HYDROData_Zone::DefaultFillingColor() );
490 int aR = 0, aG = 0, aB = 0;
491 QStringListIterator aZoneNameIter( theZoneNames );
492 while( aZoneNameIter.hasNext() )
494 const QString& aZoneName = aZoneNameIter.next();
495 Handle(HYDROData_Zone) aRefZone = Handle(HYDROData_Zone)::DownCast(
496 FindObjectByName( theModule, aZoneName, KIND_ZONE ) );
497 if( !aRefZone.IsNull() )
499 QColor aRefColor = aRefZone->GetFillingColor();
500 aR += aRefColor.red();
501 aG += aRefColor.green();
502 aB += aRefColor.blue();
509 aFillingColor = QColor( aR / aCounter, aG / aCounter, aB / aCounter );
512 return aFillingColor;