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