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_Module.h"
27 #include "HYDROGUI_Prs.h"
29 #include <HYDROData_Document.h>
30 #include <HYDROData_Iterator.h>
32 #include <GraphicsView_Viewer.h>
34 #include <LightApp_Application.h>
35 #include <LightApp_DataOwner.h>
36 #include <LightApp_SelectionMgr.h>
38 #include <QtxWorkstack.h>
40 #include <STD_TabDesktop.h>
42 #include <SUIT_Study.h>
43 #include <SUIT_ViewManager.h>
44 #include <SUIT_ViewWindow.h>
48 // Definition of this id allows to use 'latin1' (Qt alias for 'ISO-8859-1')
49 // encoding instead of default 'System'
50 #define USE_LATIN1_ENCODING
52 QString HYDROGUI_Tool::ToQString( const TCollection_AsciiString& src )
54 #ifdef USE_LATIN1_ENCODING
55 QTextCodec* codec = QTextCodec::codecForName( "latin1" ); // alias for ISO-8859-1
57 QTextCodec* codec = QTextCodec::codecForLocale();
61 res = codec ? codec->toUnicode( (char*)src.ToCString(), src.Length() ) :
62 QString( (char*)src.ToCString() );
66 QString HYDROGUI_Tool::ToQString( const TCollection_ExtendedString& src )
68 return QString( (QChar*)src.ToExtString(), src.Length() );
71 QString HYDROGUI_Tool::ToQString( const Handle(TCollection_HAsciiString)& src )
76 return ToQString( src->String() );
79 QString HYDROGUI_Tool::ToQString( const Handle(TCollection_HExtendedString)& src )
83 return ToQString( src->String() );
86 TCollection_AsciiString HYDROGUI_Tool::ToAsciiString( const QString& src )
88 TCollection_AsciiString res;
91 #ifdef USE_LATIN1_ENCODING
92 QTextCodec* codec = QTextCodec::codecForName( "latin1" ); // alias for ISO-8859-1
94 QTextCodec* codec = QTextCodec::codecForLocale();
98 QByteArray str = codec->fromUnicode( src );
99 res = TCollection_AsciiString( (Standard_CString)str.constData() );
102 res = TCollection_AsciiString( src.toLatin1().data() );
107 TCollection_ExtendedString HYDROGUI_Tool::ToExtString( const QString& src )
110 return TCollection_ExtendedString();
112 Standard_Integer len = src.length();
113 Standard_ExtString extStr = new Standard_ExtCharacter[ ( len + 1 ) * 2 ];
114 memcpy( (void*)extStr, src.unicode(), len * 2 );
115 ((short*)extStr)[ len ] = 0;
117 TCollection_ExtendedString trg( extStr );
122 Handle(TCollection_HAsciiString) HYDROGUI_Tool::ToHAsciiString( const QString& src )
124 return new TCollection_HAsciiString( ToAsciiString( src ) );
127 Handle(TCollection_HExtendedString) HYDROGUI_Tool::ToHExtString( const QString& src )
129 return new TCollection_HExtendedString( ToExtString( src ) );
132 void HYDROGUI_Tool::LambertToDouble( const int theDegrees,
133 const int theMinutes,
134 const double theSeconds,
137 // ouv: check the case of negative degrees
138 theCoord = theDegrees * 3600 + theMinutes * 60 + theSeconds;
141 void HYDROGUI_Tool::DoubleToLambert( const double theCoord,
146 // ouv: check the case of negative degrees
147 theDegrees = int( theCoord / 3600 );
149 double aRemainder = theCoord - theDegrees * 3600;
150 theMinutes = int( aRemainder / 60 );
152 theSeconds = aRemainder - theMinutes * 60;
155 bool HYDROGUI_Tool::IsEqual( const Handle(HYDROData_Object)& theObj1,
156 const Handle(HYDROData_Object)& theObj2 )
158 if( !theObj1.IsNull() && !theObj2.IsNull() )
159 return theObj1->Label() == theObj2->Label(); //ouv: check that the names can be used here
163 void HYDROGUI_Tool::SetActiveViewManager( HYDROGUI_Module* theModule,
164 SUIT_ViewManager* theViewManager )
167 if( SUIT_ViewWindow* aViewWindow = theViewManager->getActiveView() )
168 if( STD_TabDesktop* aTabDesktop = dynamic_cast<STD_TabDesktop*>( theModule->getApp()->desktop() ) )
169 if( QtxWorkstack* aWorkstack = aTabDesktop->workstack() )
170 aWorkstack->setActiveWindow( aViewWindow );
173 void HYDROGUI_Tool::GetPrsSubObjects( HYDROGUI_Module* theModule,
174 HYDROData_SequenceOfObjects& theSeq )
176 Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( theModule->getStudyId() );
177 if( aDocument.IsNull() )
180 HYDROData_Iterator anIterator( aDocument, KIND_IMAGE );
181 for( ; anIterator.More(); anIterator.Next() )
183 Handle(HYDROData_Object) anObject = anIterator.Current();
184 if( !anObject.IsNull() )
185 theSeq.Append( anObject );
188 anIterator = HYDROData_Iterator( aDocument, KIND_POLYLINE );
189 for( ; anIterator.More(); anIterator.Next() )
191 Handle(HYDROData_Object) anObject = anIterator.Current();
192 if( !anObject.IsNull() )
193 theSeq.Append( anObject );
197 HYDROGUI_Prs* HYDROGUI_Tool::GetPresentation( const Handle(HYDROData_Object)& theObj,
198 const GraphicsView_ObjectList& theObjects )
200 if( !theObj.IsNull() )
202 GraphicsView_ObjectListIterator anIter( theObjects );
203 while( anIter.hasNext() )
205 if( HYDROGUI_Prs* aPrs = dynamic_cast<HYDROGUI_Prs*>( anIter.next() ) )
207 Handle(HYDROData_Object) anObj = aPrs->getObject();
208 if( IsEqual( anObj, theObj ) )
216 GraphicsView_ObjectList HYDROGUI_Tool::GetPrsList( GraphicsView_ViewPort* theViewPort )
218 GraphicsView_ObjectList aList;
221 GraphicsView_ObjectListIterator anIter( theViewPort->getObjects() );
222 while( anIter.hasNext() )
223 if( HYDROGUI_Prs* aPrs = dynamic_cast<HYDROGUI_Prs*>( anIter.next() ) )
224 aList.append( aPrs );
229 HYDROData_SequenceOfObjects HYDROGUI_Tool::GetSelectedObjects( HYDROGUI_Module* theModule )
231 HYDROData_SequenceOfObjects aSeq;
233 HYDROGUI_DataModel* aModel = theModule->getDataModel();
235 SUIT_SelectionMgr* aSelectionMgr = theModule->getApp()->selectionMgr();
236 SUIT_DataOwnerPtrList anOwners;
237 aSelectionMgr->selected( anOwners );
239 foreach( SUIT_DataOwner* aSUITOwner, anOwners )
241 if( LightApp_DataOwner* anOwner = dynamic_cast<LightApp_DataOwner*>( aSUITOwner ) )
243 Handle(HYDROData_Object) anObject = aModel->objectByEntry( anOwner->entry(), KIND_UNKNOWN );
244 if( !anObject.IsNull() )
245 aSeq.Append( anObject );
251 Handle(HYDROData_Object) HYDROGUI_Tool::GetSelectedObject( HYDROGUI_Module* theModule )
253 HYDROData_SequenceOfObjects aSeq = GetSelectedObjects( theModule );
254 if( !aSeq.IsEmpty() )
259 Handle(HYDROData_Object) HYDROGUI_Tool::FindObjectByName( HYDROGUI_Module* theModule,
260 const QString& theName,
261 const ObjectKind theObjectKind )
263 Handle(HYDROData_Object) anObject;
265 Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( theModule->getStudyId() );
266 if( aDocument.IsNull() )
269 HYDROData_Iterator anIter( aDocument, theObjectKind );
270 for( ; anIter.More(); anIter.Next() )
272 Handle(HYDROData_Object) anObjectRef = anIter.Current();
273 if( !anObjectRef.IsNull() && anObjectRef->GetName() == theName )
275 anObject = anObjectRef;
282 QString HYDROGUI_Tool::GenerateObjectName( HYDROGUI_Module* theModule,
283 const QString& thePrefix )
289 aName = QString( "%1_%2" ).arg( thePrefix ).arg( QString::number( anId++ ) );
291 // check that there are no other objects with the same name in the document
292 Handle(HYDROData_Object) anObject = FindObjectByName( theModule, aName, KIND_UNKNOWN );
293 if( anObject.IsNull() )
299 size_t HYDROGUI_Tool::GetActiveGraphicsViewId( HYDROGUI_Module* theModule )
302 SUIT_ViewManager* aViewMgr = theModule->getApp()->activeViewManager();
303 if( !aViewMgr || aViewMgr->getType() != GraphicsView_Viewer::Type() )
306 if( SUIT_ViewModel* aViewer = aViewMgr->getViewModel() )
307 aViewId = (size_t)aViewer;
311 QList<size_t> HYDROGUI_Tool::GetGraphicsViewIdList( HYDROGUI_Module* theModule )
314 ViewManagerList aViewMgrs;
315 theModule->getApp()->viewManagers( GraphicsView_Viewer::Type(), aViewMgrs );
316 QListIterator<SUIT_ViewManager*> anIter( aViewMgrs );
317 while( anIter.hasNext() )
319 if( SUIT_ViewManager* aViewMgr = anIter.next() )
321 if( SUIT_ViewModel* aViewer = aViewMgr->getViewModel() )
322 aList.append( (size_t)aViewer );