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>
33 #include <GraphicsView_Viewer.h>
35 #include <LightApp_Application.h>
36 #include <LightApp_DataOwner.h>
37 #include <LightApp_SelectionMgr.h>
39 #include <QtxWorkstack.h>
41 #include <STD_TabDesktop.h>
43 #include <SUIT_Study.h>
44 #include <SUIT_ViewManager.h>
45 #include <SUIT_ViewWindow.h>
48 #include <QDockWidget>
50 // Definition of this id allows to use 'latin1' (Qt alias for 'ISO-8859-1')
51 // encoding instead of default 'System'
52 #define USE_LATIN1_ENCODING
54 QString HYDROGUI_Tool::ToQString( const TCollection_AsciiString& src )
56 #ifdef USE_LATIN1_ENCODING
57 QTextCodec* codec = QTextCodec::codecForName( "latin1" ); // alias for ISO-8859-1
59 QTextCodec* codec = QTextCodec::codecForLocale();
63 res = codec ? codec->toUnicode( (char*)src.ToCString(), src.Length() ) :
64 QString( (char*)src.ToCString() );
68 QString HYDROGUI_Tool::ToQString( const TCollection_ExtendedString& src )
70 return QString( (QChar*)src.ToExtString(), src.Length() );
73 QString HYDROGUI_Tool::ToQString( const Handle(TCollection_HAsciiString)& src )
78 return ToQString( src->String() );
81 QString HYDROGUI_Tool::ToQString( const Handle(TCollection_HExtendedString)& src )
85 return ToQString( src->String() );
88 TCollection_AsciiString HYDROGUI_Tool::ToAsciiString( const QString& src )
90 TCollection_AsciiString res;
93 #ifdef USE_LATIN1_ENCODING
94 QTextCodec* codec = QTextCodec::codecForName( "latin1" ); // alias for ISO-8859-1
96 QTextCodec* codec = QTextCodec::codecForLocale();
100 QByteArray str = codec->fromUnicode( src );
101 res = TCollection_AsciiString( (Standard_CString)str.constData() );
104 res = TCollection_AsciiString( src.toLatin1().data() );
109 TCollection_ExtendedString HYDROGUI_Tool::ToExtString( const QString& src )
112 return TCollection_ExtendedString();
114 Standard_Integer len = src.length();
115 Standard_ExtString extStr = new Standard_ExtCharacter[ ( len + 1 ) * 2 ];
116 memcpy( (void*)extStr, src.unicode(), len * 2 );
117 ((short*)extStr)[ len ] = 0;
119 TCollection_ExtendedString trg( extStr );
124 Handle(TCollection_HAsciiString) HYDROGUI_Tool::ToHAsciiString( const QString& src )
126 return new TCollection_HAsciiString( ToAsciiString( src ) );
129 Handle(TCollection_HExtendedString) HYDROGUI_Tool::ToHExtString( const QString& src )
131 return new TCollection_HExtendedString( ToExtString( src ) );
134 void HYDROGUI_Tool::LambertToDouble( const int theDegrees,
135 const int theMinutes,
136 const double theSeconds,
139 // ouv: check the case of negative degrees
140 theCoord = theDegrees * 3600 + theMinutes * 60 + theSeconds;
143 void HYDROGUI_Tool::DoubleToLambert( const double theCoord,
148 // ouv: check the case of negative degrees
149 theDegrees = int( theCoord / 3600 );
151 double aRemainder = theCoord - theDegrees * 3600;
152 theMinutes = int( aRemainder / 60 );
154 theSeconds = aRemainder - theMinutes * 60;
157 bool HYDROGUI_Tool::IsEqual( const Handle(HYDROData_Object)& theObj1,
158 const Handle(HYDROData_Object)& theObj2 )
160 if( !theObj1.IsNull() && !theObj2.IsNull() )
161 return theObj1->Label() == theObj2->Label(); //ouv: check that the names can be used here
165 void HYDROGUI_Tool::SetActiveViewManager( HYDROGUI_Module* theModule,
166 SUIT_ViewManager* theViewManager )
169 if( SUIT_ViewWindow* aViewWindow = theViewManager->getActiveView() )
170 if( STD_TabDesktop* aTabDesktop = dynamic_cast<STD_TabDesktop*>( theModule->getApp()->desktop() ) )
171 if( QtxWorkstack* aWorkstack = aTabDesktop->workstack() )
172 aWorkstack->setActiveWindow( aViewWindow );
175 void HYDROGUI_Tool::GetPrsSubObjects( HYDROGUI_Module* theModule,
176 HYDROData_SequenceOfObjects& theSeq )
178 Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( theModule->getStudyId() );
179 if( aDocument.IsNull() )
182 HYDROData_Iterator anIterator( aDocument, KIND_IMAGE );
183 for( ; anIterator.More(); anIterator.Next() )
185 Handle(HYDROData_Object) anObject = anIterator.Current();
186 if( !anObject.IsNull() )
187 theSeq.Append( anObject );
190 anIterator = HYDROData_Iterator( aDocument, KIND_POLYLINE );
191 for( ; anIterator.More(); anIterator.Next() )
193 Handle(HYDROData_Object) anObject = anIterator.Current();
194 if( !anObject.IsNull() )
195 theSeq.Append( anObject );
199 HYDROGUI_Prs* HYDROGUI_Tool::GetPresentation( const Handle(HYDROData_Object)& theObj,
200 const GraphicsView_ObjectList& theObjects )
202 if( !theObj.IsNull() )
204 GraphicsView_ObjectListIterator anIter( theObjects );
205 while( anIter.hasNext() )
207 if( HYDROGUI_Prs* aPrs = dynamic_cast<HYDROGUI_Prs*>( anIter.next() ) )
209 Handle(HYDROData_Object) anObj = aPrs->getObject();
210 if( IsEqual( anObj, theObj ) )
218 GraphicsView_ObjectList HYDROGUI_Tool::GetPrsList( GraphicsView_ViewPort* theViewPort )
220 GraphicsView_ObjectList aList;
223 GraphicsView_ObjectListIterator anIter( theViewPort->getObjects() );
224 while( anIter.hasNext() )
225 if( HYDROGUI_Prs* aPrs = dynamic_cast<HYDROGUI_Prs*>( anIter.next() ) )
226 aList.append( aPrs );
231 HYDROData_SequenceOfObjects HYDROGUI_Tool::GetSelectedObjects( HYDROGUI_Module* theModule )
233 HYDROData_SequenceOfObjects aSeq;
235 HYDROGUI_DataModel* aModel = theModule->getDataModel();
237 SUIT_SelectionMgr* aSelectionMgr = theModule->getApp()->selectionMgr();
238 SUIT_DataOwnerPtrList anOwners;
239 aSelectionMgr->selected( anOwners );
241 QStringList aCollectedNameList; // to avoid duplication
242 foreach( SUIT_DataOwner* aSUITOwner, anOwners )
244 if( LightApp_DataOwner* anOwner = dynamic_cast<LightApp_DataOwner*>( aSUITOwner ) )
246 Handle(HYDROData_Object) anObject = aModel->objectByEntry( anOwner->entry() );
247 if( !anObject.IsNull() )
249 QString aName = anObject->GetName();
250 if( !aCollectedNameList.contains( aName ) )
252 aSeq.Append( anObject );
253 aCollectedNameList.append( aName );
261 Handle(HYDROData_Object) HYDROGUI_Tool::GetSelectedObject( HYDROGUI_Module* theModule )
263 HYDROData_SequenceOfObjects aSeq = GetSelectedObjects( theModule );
264 if( !aSeq.IsEmpty() )
269 ObjectKind HYDROGUI_Tool::GetSelectedPartition( HYDROGUI_Module* theModule )
271 HYDROGUI_DataModel* aModel = theModule->getDataModel();
273 SUIT_SelectionMgr* aSelectionMgr = theModule->getApp()->selectionMgr();
274 SUIT_DataOwnerPtrList anOwners;
275 aSelectionMgr->selected( anOwners );
277 if( anOwners.size() != 1 )
280 if( LightApp_DataOwner* anOwner = dynamic_cast<LightApp_DataOwner*>( anOwners.first().operator->() ) )
282 QString anEntry = anOwner->entry();
283 QString aPrefix = HYDROGUI_DataObject::entryPrefix();
284 if( anEntry.left( aPrefix.length() ) == aPrefix )
286 anEntry.remove( aPrefix );
287 for( ObjectKind anObjectKind = KIND_UNKNOWN + 1; anObjectKind <= KIND_LAST; anObjectKind++ )
288 if( HYDROGUI_DataModel::partitionName( anObjectKind ) == anEntry )
295 Handle(HYDROData_Object) HYDROGUI_Tool::FindObjectByName( HYDROGUI_Module* theModule,
296 const QString& theName,
297 const ObjectKind theObjectKind )
299 Handle(HYDROData_Object) anObject;
301 Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( theModule->getStudyId() );
302 if( aDocument.IsNull() )
305 HYDROData_Iterator anIter( aDocument, theObjectKind );
306 for( ; anIter.More(); anIter.Next() )
308 Handle(HYDROData_Object) anObjectRef = anIter.Current();
309 if( !anObjectRef.IsNull() && anObjectRef->GetName() == theName )
311 anObject = anObjectRef;
318 QString HYDROGUI_Tool::GenerateObjectName( HYDROGUI_Module* theModule,
319 const QString& thePrefix )
325 aName = QString( "%1_%2" ).arg( thePrefix ).arg( QString::number( anId++ ) );
327 // check that there are no other objects with the same name in the document
328 Handle(HYDROData_Object) anObject = FindObjectByName( theModule, aName, KIND_UNKNOWN );
329 if( anObject.IsNull() )
335 size_t HYDROGUI_Tool::GetActiveGraphicsViewId( HYDROGUI_Module* theModule )
338 SUIT_ViewManager* aViewMgr = theModule->getApp()->activeViewManager();
339 if( !aViewMgr || aViewMgr->getType() != GraphicsView_Viewer::Type() )
342 if( SUIT_ViewModel* aViewer = aViewMgr->getViewModel() )
343 aViewId = (size_t)aViewer;
347 QList<size_t> HYDROGUI_Tool::GetGraphicsViewIdList( HYDROGUI_Module* theModule )
350 ViewManagerList aViewMgrs;
351 theModule->getApp()->viewManagers( GraphicsView_Viewer::Type(), aViewMgrs );
352 QListIterator<SUIT_ViewManager*> anIter( aViewMgrs );
353 while( anIter.hasNext() )
355 if( SUIT_ViewManager* aViewMgr = anIter.next() )
357 if( SUIT_ViewModel* aViewer = aViewMgr->getViewModel() )
358 aList.append( (size_t)aViewer );
364 void HYDROGUI_Tool::GetObjectReferences( const Handle(HYDROData_Image)& theImage,
365 HYDROData_SequenceOfObjects& theRefObjects,
366 QStringList& theRefNames )
368 if( theImage.IsNull() )
371 for( int anIndex = 0, aNbRef = theImage->NbReferences(); anIndex < aNbRef; anIndex++ )
373 Handle(HYDROData_Object) aRefObj = theImage->Reference( anIndex );
374 if( !aRefObj.IsNull() && !aRefObj->IsRemoved() )
376 QString aName = aRefObj->GetName();
377 if( !theRefNames.contains( aName ) )
379 theRefObjects.Append( aRefObj );
380 theRefNames.append( aRefObj->GetName() );
381 if( aRefObj->GetKind() == KIND_IMAGE )
383 Handle(HYDROData_Image) aRefImage = Handle(HYDROData_Image)::DownCast( aRefObj );
384 if( !aRefImage.IsNull() )
385 GetObjectReferences( aRefImage, theRefObjects, theRefNames );
392 void HYDROGUI_Tool::GetObjectBackReferences( HYDROGUI_Module* theModule,
393 const Handle(HYDROData_Object)& theObj,
394 HYDROData_SequenceOfObjects& theBackRefObjects,
395 QStringList& theBackRefNames )
397 if( theObj.IsNull() )
400 Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( theModule->getStudyId() );
401 if( aDocument.IsNull() )
404 QString aName = theObj->GetName();
406 HYDROData_Iterator anIterator( aDocument, KIND_IMAGE );
407 for( ; anIterator.More(); anIterator.Next() )
409 Handle(HYDROData_Image) anImage = Handle(HYDROData_Image)::DownCast( anIterator.Current() );
410 if( !anImage.IsNull() )
412 HYDROData_SequenceOfObjects aRefObjects;
413 QStringList aRefNames;
414 GetObjectReferences( anImage, aRefObjects, aRefNames );
415 if( aRefNames.contains( aName ) )
417 theBackRefObjects.Append( anImage );
418 theBackRefNames.append( anImage->GetName() );
425 QDockWidget* HYDROGUI_Tool::WindowDock( QWidget* wid )
430 QDockWidget* dock = 0;
431 QWidget* w = wid->parentWidget();
434 dock = ::qobject_cast<QDockWidget*>( w );
435 w = w->parentWidget();