Salome HOME
e224ff808bf4275a0bea639c0d9d41e417369ac7
[modules/hydro.git] / src / HYDROGUI / HYDROGUI_Tool.cxx
1 // Copyright (C) 2007-2013  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
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.
10 //
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.
15 //
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
19 //
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22
23 #include "HYDROGUI_Tool.h"
24
25 #include "HYDROGUI_DataModel.h"
26 #include "HYDROGUI_Module.h"
27 #include "HYDROGUI_Prs.h"
28
29 #include <HYDROData_Document.h>
30 #include <HYDROData_Iterator.h>
31
32 #include <GraphicsView_Viewer.h>
33
34 #include <LightApp_Application.h>
35 #include <LightApp_DataOwner.h>
36 #include <LightApp_SelectionMgr.h>
37
38 #include <QtxWorkstack.h>
39
40 #include <STD_TabDesktop.h>
41
42 #include <SUIT_Study.h>
43 #include <SUIT_ViewManager.h>
44 #include <SUIT_ViewWindow.h>
45
46 #include <QTextCodec>
47
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
51
52 QString HYDROGUI_Tool::ToQString( const TCollection_AsciiString& src )
53 {
54 #ifdef USE_LATIN1_ENCODING
55   QTextCodec* codec = QTextCodec::codecForName( "latin1" ); // alias for ISO-8859-1
56 #else
57   QTextCodec* codec = QTextCodec::codecForLocale();
58 #endif
59   QString res;
60   if ( !src.IsEmpty() )
61     res = codec ? codec->toUnicode( (char*)src.ToCString(), src.Length() ) :
62       QString( (char*)src.ToCString() );
63   return res;
64 }
65
66 QString HYDROGUI_Tool::ToQString( const TCollection_ExtendedString& src )
67 {
68   return QString( (QChar*)src.ToExtString(), src.Length() );
69 }
70
71 QString HYDROGUI_Tool::ToQString( const Handle(TCollection_HAsciiString)& src )
72 {
73   if( src.IsNull() )
74     return QString();
75   else
76     return ToQString( src->String() );
77 }
78
79 QString HYDROGUI_Tool::ToQString( const Handle(TCollection_HExtendedString)& src )
80 {
81   if( src.IsNull() )
82     return QString();
83   return ToQString( src->String() );
84 }
85
86 TCollection_AsciiString HYDROGUI_Tool::ToAsciiString( const QString& src )
87 {
88   TCollection_AsciiString res;
89   if( !src.isNull() )
90   {
91 #ifdef USE_LATIN1_ENCODING
92     QTextCodec* codec = QTextCodec::codecForName( "latin1" ); // alias for ISO-8859-1
93 #else
94     QTextCodec* codec = QTextCodec::codecForLocale();
95 #endif
96     if( codec )
97     {
98       QByteArray str = codec->fromUnicode( src );
99       res = TCollection_AsciiString( (Standard_CString)str.constData() );
100     }
101     else
102       res = TCollection_AsciiString( src.toLatin1().data() );
103   }
104   return res;
105 }
106
107 TCollection_ExtendedString HYDROGUI_Tool::ToExtString( const QString& src )
108 {
109   if( src.isEmpty() )
110     return TCollection_ExtendedString();
111
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;
116
117   TCollection_ExtendedString trg( extStr );
118   delete [] extStr;
119   return trg;
120 }
121
122 Handle(TCollection_HAsciiString) HYDROGUI_Tool::ToHAsciiString( const QString& src )
123 {
124   return new TCollection_HAsciiString( ToAsciiString( src ) );
125 }
126
127 Handle(TCollection_HExtendedString) HYDROGUI_Tool::ToHExtString( const QString& src )
128 {
129   return new TCollection_HExtendedString( ToExtString( src ) );
130 }
131
132 void HYDROGUI_Tool::LambertToDouble( const int theDegrees,
133                                      const int theMinutes,
134                                      const double theSeconds,
135                                      double& theCoord )
136 {
137   // ouv: check the case of negative degrees
138   theCoord = theDegrees * 3600 + theMinutes * 60 + theSeconds;
139 }
140
141 void HYDROGUI_Tool::DoubleToLambert( const double theCoord,
142                                      int& theDegrees,
143                                      int& theMinutes,
144                                      double& theSeconds )
145 {
146   // ouv: check the case of negative degrees
147   theDegrees = int( theCoord / 3600 );
148
149   double aRemainder = theCoord - theDegrees * 3600;
150   theMinutes = int( aRemainder / 60 );
151
152   theSeconds = aRemainder - theMinutes * 60;
153 }
154
155 bool HYDROGUI_Tool::IsEqual( const Handle(HYDROData_Object)& theObj1,
156                              const Handle(HYDROData_Object)& theObj2 )
157 {
158   if( !theObj1.IsNull() && !theObj2.IsNull() )
159     return theObj1->Label() == theObj2->Label(); //ouv: check that the names can be used here
160   return false;
161 }
162
163 void HYDROGUI_Tool::SetActiveViewManager( HYDROGUI_Module* theModule,
164                                           SUIT_ViewManager* theViewManager )
165 {
166   if( 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 );
171 }
172
173 void HYDROGUI_Tool::GetPrsSubObjects( HYDROGUI_Module* theModule,
174                                       HYDROData_SequenceOfObjects& theSeq )
175 {
176   Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( theModule->getStudyId() );
177   if( aDocument.IsNull() )
178     return;
179
180   HYDROData_Iterator anIterator( aDocument, KIND_IMAGE );
181   for( ; anIterator.More(); anIterator.Next() )
182   {
183     Handle(HYDROData_Object) anObject = anIterator.Current();
184     if( !anObject.IsNull() )
185       theSeq.Append( anObject );
186   }
187
188   anIterator = HYDROData_Iterator( aDocument, KIND_POLYLINE );
189   for( ; anIterator.More(); anIterator.Next() )
190   {
191     Handle(HYDROData_Object) anObject = anIterator.Current();
192     if( !anObject.IsNull() )
193       theSeq.Append( anObject );
194   }
195 }
196
197 HYDROGUI_Prs* HYDROGUI_Tool::GetPresentation( const Handle(HYDROData_Object)& theObj,
198                                               const GraphicsView_ObjectList& theObjects )
199 {
200   if( !theObj.IsNull() )
201   {
202     GraphicsView_ObjectListIterator anIter( theObjects );
203     while( anIter.hasNext() )
204     {
205       if( HYDROGUI_Prs* aPrs = dynamic_cast<HYDROGUI_Prs*>( anIter.next() ) )
206       {
207         Handle(HYDROData_Object) anObj = aPrs->getObject();
208         if( IsEqual( anObj, theObj ) )
209           return aPrs;
210       }
211     }
212   }
213   return NULL;
214 }
215
216 GraphicsView_ObjectList HYDROGUI_Tool::GetPrsList( GraphicsView_ViewPort* theViewPort )
217 {
218   GraphicsView_ObjectList aList;
219   if( theViewPort )
220   {
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 );
225   }
226   return aList;
227 }
228
229 HYDROData_SequenceOfObjects HYDROGUI_Tool::GetSelectedObjects( HYDROGUI_Module* theModule )
230 {
231   HYDROData_SequenceOfObjects aSeq;
232
233   HYDROGUI_DataModel* aModel = theModule->getDataModel();
234
235   SUIT_SelectionMgr* aSelectionMgr = theModule->getApp()->selectionMgr();
236   SUIT_DataOwnerPtrList anOwners;
237   aSelectionMgr->selected( anOwners );
238
239   QStringList aCollectedNameList; // to avoid duplication
240   foreach( SUIT_DataOwner* aSUITOwner, anOwners )
241   {
242     if( LightApp_DataOwner* anOwner = dynamic_cast<LightApp_DataOwner*>( aSUITOwner ) )
243     {
244       Handle(HYDROData_Object) anObject = aModel->objectByEntry( anOwner->entry() );
245       if( !anObject.IsNull() )
246       {
247         QString aName = anObject->GetName();
248         if( !aCollectedNameList.contains( aName ) )
249         {
250           aSeq.Append( anObject );
251           aCollectedNameList.append( aName );
252         }
253       }
254     }
255   }
256   return aSeq;
257 }
258
259 Handle(HYDROData_Object) HYDROGUI_Tool::GetSelectedObject( HYDROGUI_Module* theModule )
260 {
261   HYDROData_SequenceOfObjects aSeq = GetSelectedObjects( theModule );
262   if( !aSeq.IsEmpty() )
263     return aSeq.First();
264   return NULL;
265 }
266
267 Handle(HYDROData_Object) HYDROGUI_Tool::FindObjectByName( HYDROGUI_Module* theModule,
268                                                           const QString& theName,
269                                                           const ObjectKind theObjectKind )
270 {
271   Handle(HYDROData_Object) anObject;
272
273   Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( theModule->getStudyId() );
274   if( aDocument.IsNull() )
275     return anObject;
276
277   HYDROData_Iterator anIter( aDocument, theObjectKind );
278   for( ; anIter.More(); anIter.Next() )
279   {
280     Handle(HYDROData_Object) anObjectRef = anIter.Current();
281     if( !anObjectRef.IsNull() && anObjectRef->GetName() == theName )
282     {
283       anObject = anObjectRef;
284       break;
285     }
286   }
287   return anObject;
288 }
289
290 QString HYDROGUI_Tool::GenerateObjectName( HYDROGUI_Module* theModule,
291                                            const QString& thePrefix )
292 {
293   QString aName;
294   int anId = 1;
295   while( anId < 100 )
296   {
297     aName = QString( "%1_%2" ).arg( thePrefix ).arg( QString::number( anId++ ) );
298
299     // check that there are no other objects with the same name in the document
300     Handle(HYDROData_Object) anObject = FindObjectByName( theModule, aName, KIND_UNKNOWN );
301     if( anObject.IsNull() )
302       break;
303   }
304   return aName;
305 }
306
307 size_t HYDROGUI_Tool::GetActiveGraphicsViewId( HYDROGUI_Module* theModule )
308 {
309   size_t aViewId = 0;
310   SUIT_ViewManager* aViewMgr = theModule->getApp()->activeViewManager();
311   if( !aViewMgr || aViewMgr->getType() != GraphicsView_Viewer::Type() )
312     return aViewId;
313
314   if( SUIT_ViewModel* aViewer = aViewMgr->getViewModel() )
315     aViewId = (size_t)aViewer;
316   return aViewId;
317 }
318
319 QList<size_t> HYDROGUI_Tool::GetGraphicsViewIdList( HYDROGUI_Module* theModule )
320 {
321   QList<size_t> aList;
322   ViewManagerList aViewMgrs;
323   theModule->getApp()->viewManagers( GraphicsView_Viewer::Type(), aViewMgrs );
324   QListIterator<SUIT_ViewManager*> anIter( aViewMgrs );
325   while( anIter.hasNext() )
326   {
327     if( SUIT_ViewManager* aViewMgr = anIter.next() )
328     {
329       if( SUIT_ViewModel* aViewer = aViewMgr->getViewModel() )
330         aList.append( (size_t)aViewer );
331     }
332   }
333   return aList;
334 }
335
336 void HYDROGUI_Tool::GetObjectReferences( const Handle(HYDROData_Image)& theImage,
337                                          HYDROData_SequenceOfObjects& theRefObjects,
338                                          QStringList& theRefNames )
339 {
340   if( theImage.IsNull() )
341     return;
342
343   for( int anIndex = 0, aNbRef = theImage->NbReferences(); anIndex < aNbRef; anIndex++ )
344   {
345     Handle(HYDROData_Object) aRefObj = theImage->Reference( anIndex );
346     if( !aRefObj.IsNull() && !aRefObj->IsRemoved() )
347     {
348       QString aName = aRefObj->GetName();
349       if( !theRefNames.contains( aName ) )
350       {
351         theRefObjects.Append( aRefObj );
352         theRefNames.append( aRefObj->GetName() );
353         if( aRefObj->GetKind() == KIND_IMAGE )
354         {
355           Handle(HYDROData_Image) aRefImage = Handle(HYDROData_Image)::DownCast( aRefObj );
356           if( !aRefImage.IsNull() )
357             GetObjectReferences( aRefImage, theRefObjects, theRefNames );
358         }
359       }
360     }
361   }
362 }
363
364 void HYDROGUI_Tool::GetObjectBackReferences( HYDROGUI_Module* theModule,
365                                              const Handle(HYDROData_Object)& theObj,
366                                              HYDROData_SequenceOfObjects& theBackRefObjects,
367                                              QStringList& theBackRefNames )
368 {
369   if( theObj.IsNull() )
370     return;
371
372   Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( theModule->getStudyId() );
373   if( aDocument.IsNull() )
374     return;
375
376   QString aName = theObj->GetName();
377
378   HYDROData_Iterator anIterator( aDocument, KIND_IMAGE );
379   for( ; anIterator.More(); anIterator.Next() )
380   {
381     Handle(HYDROData_Image) anImage = Handle(HYDROData_Image)::DownCast( anIterator.Current() );
382     if( !anImage.IsNull() )
383     {
384       HYDROData_SequenceOfObjects aRefObjects;
385       QStringList aRefNames;
386       GetObjectReferences( anImage, aRefObjects, aRefNames );
387       if( aRefNames.contains( aName ) )
388       {
389         theBackRefObjects.Append( anImage );
390         theBackRefNames.append( anImage->GetName() );
391       }
392     }
393   }
394 }