Salome HOME
e030e1db13e33016a05b5d17269e9143e0cb509e
[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
199 HYDROGUI_Prs* HYDROGUI_Tool::GetPresentation( const Handle(HYDROData_Object)& theObj,
200                                               const GraphicsView_ObjectList& theObjects )
201 {
202   if( !theObj.IsNull() )
203   {
204     GraphicsView_ObjectListIterator anIter( theObjects );
205     while( anIter.hasNext() )
206     {
207       if( HYDROGUI_Prs* aPrs = dynamic_cast<HYDROGUI_Prs*>( anIter.next() ) )
208       {
209         Handle(HYDROData_Object) anObj = aPrs->getObject();
210         if( IsEqual( anObj, theObj ) )
211           return aPrs;
212       }
213     }
214   }
215   return NULL;
216 }
217
218 GraphicsView_ObjectList HYDROGUI_Tool::GetPrsList( GraphicsView_ViewPort* theViewPort )
219 {
220   GraphicsView_ObjectList aList;
221   if( theViewPort )
222   {
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 );
227   }
228   return aList;
229 }
230
231 HYDROData_SequenceOfObjects HYDROGUI_Tool::GetSelectedObjects( HYDROGUI_Module* theModule )
232 {
233   HYDROData_SequenceOfObjects aSeq;
234
235   HYDROGUI_DataModel* aModel = theModule->getDataModel();
236
237   SUIT_SelectionMgr* aSelectionMgr = theModule->getApp()->selectionMgr();
238   SUIT_DataOwnerPtrList anOwners;
239   aSelectionMgr->selected( anOwners );
240
241   QStringList aCollectedNameList; // to avoid duplication
242   foreach( SUIT_DataOwner* aSUITOwner, anOwners )
243   {
244     if( LightApp_DataOwner* anOwner = dynamic_cast<LightApp_DataOwner*>( aSUITOwner ) )
245     {
246       Handle(HYDROData_Object) anObject = aModel->objectByEntry( anOwner->entry() );
247       if( !anObject.IsNull() )
248       {
249         QString aName = anObject->GetName();
250         if( !aCollectedNameList.contains( aName ) )
251         {
252           aSeq.Append( anObject );
253           aCollectedNameList.append( aName );
254         }
255       }
256     }
257   }
258   return aSeq;
259 }
260
261 Handle(HYDROData_Object) HYDROGUI_Tool::GetSelectedObject( HYDROGUI_Module* theModule )
262 {
263   HYDROData_SequenceOfObjects aSeq = GetSelectedObjects( theModule );
264   if( !aSeq.IsEmpty() )
265     return aSeq.First();
266   return NULL;
267 }
268
269 ObjectKind HYDROGUI_Tool::GetSelectedPartition( HYDROGUI_Module* theModule )
270 {
271   HYDROGUI_DataModel* aModel = theModule->getDataModel();
272
273   SUIT_SelectionMgr* aSelectionMgr = theModule->getApp()->selectionMgr();
274   SUIT_DataOwnerPtrList anOwners;
275   aSelectionMgr->selected( anOwners );
276
277   if( anOwners.size() != 1 )
278     return KIND_UNKNOWN;
279
280   if( LightApp_DataOwner* anOwner = dynamic_cast<LightApp_DataOwner*>( anOwners.first().operator->() ) )
281   {
282     QString anEntry = anOwner->entry();
283     QString aPrefix = HYDROGUI_DataObject::entryPrefix();
284     if( anEntry.left( aPrefix.length() ) == aPrefix )
285     {
286       anEntry.remove( aPrefix );
287       for( ObjectKind anObjectKind = KIND_UNKNOWN + 1; anObjectKind <= KIND_LAST; anObjectKind++ )
288         if( HYDROGUI_DataModel::partitionName( anObjectKind ) == anEntry )
289           return anObjectKind;
290     }
291   }
292   return KIND_UNKNOWN;
293 }
294
295 Handle(HYDROData_Object) HYDROGUI_Tool::FindObjectByName( HYDROGUI_Module* theModule,
296                                                           const QString& theName,
297                                                           const ObjectKind theObjectKind )
298 {
299   Handle(HYDROData_Object) anObject;
300
301   Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( theModule->getStudyId() );
302   if( aDocument.IsNull() )
303     return anObject;
304
305   HYDROData_Iterator anIter( aDocument, theObjectKind );
306   for( ; anIter.More(); anIter.Next() )
307   {
308     Handle(HYDROData_Object) anObjectRef = anIter.Current();
309     if( !anObjectRef.IsNull() && anObjectRef->GetName() == theName )
310     {
311       anObject = anObjectRef;
312       break;
313     }
314   }
315   return anObject;
316 }
317
318 QString HYDROGUI_Tool::GenerateObjectName( HYDROGUI_Module* theModule,
319                                            const QString& thePrefix )
320 {
321   QString aName;
322   int anId = 1;
323   while( anId < 100 )
324   {
325     aName = QString( "%1_%2" ).arg( thePrefix ).arg( QString::number( anId++ ) );
326
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() )
330       break;
331   }
332   return aName;
333 }
334
335 size_t HYDROGUI_Tool::GetActiveGraphicsViewId( HYDROGUI_Module* theModule )
336 {
337   size_t aViewId = 0;
338   SUIT_ViewManager* aViewMgr = theModule->getApp()->activeViewManager();
339   if( !aViewMgr || aViewMgr->getType() != GraphicsView_Viewer::Type() )
340     return aViewId;
341
342   if( SUIT_ViewModel* aViewer = aViewMgr->getViewModel() )
343     aViewId = (size_t)aViewer;
344   return aViewId;
345 }
346
347 QList<size_t> HYDROGUI_Tool::GetGraphicsViewIdList( HYDROGUI_Module* theModule )
348 {
349   QList<size_t> aList;
350   ViewManagerList aViewMgrs;
351   theModule->getApp()->viewManagers( GraphicsView_Viewer::Type(), aViewMgrs );
352   QListIterator<SUIT_ViewManager*> anIter( aViewMgrs );
353   while( anIter.hasNext() )
354   {
355     if( SUIT_ViewManager* aViewMgr = anIter.next() )
356     {
357       if( SUIT_ViewModel* aViewer = aViewMgr->getViewModel() )
358         aList.append( (size_t)aViewer );
359     }
360   }
361   return aList;
362 }
363
364 void HYDROGUI_Tool::GetObjectReferences( const Handle(HYDROData_Image)& theImage,
365                                          HYDROData_SequenceOfObjects& theRefObjects,
366                                          QStringList& theRefNames )
367 {
368   if( theImage.IsNull() )
369     return;
370
371   for( int anIndex = 0, aNbRef = theImage->NbReferences(); anIndex < aNbRef; anIndex++ )
372   {
373     Handle(HYDROData_Object) aRefObj = theImage->Reference( anIndex );
374     if( !aRefObj.IsNull() && !aRefObj->IsRemoved() )
375     {
376       QString aName = aRefObj->GetName();
377       if( !theRefNames.contains( aName ) )
378       {
379         theRefObjects.Append( aRefObj );
380         theRefNames.append( aRefObj->GetName() );
381         if( aRefObj->GetKind() == KIND_IMAGE )
382         {
383           Handle(HYDROData_Image) aRefImage = Handle(HYDROData_Image)::DownCast( aRefObj );
384           if( !aRefImage.IsNull() )
385             GetObjectReferences( aRefImage, theRefObjects, theRefNames );
386         }
387       }
388     }
389   }
390 }
391
392 void HYDROGUI_Tool::GetObjectBackReferences( HYDROGUI_Module* theModule,
393                                              const Handle(HYDROData_Object)& theObj,
394                                              HYDROData_SequenceOfObjects& theBackRefObjects,
395                                              QStringList& theBackRefNames )
396 {
397   if( theObj.IsNull() )
398     return;
399
400   Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( theModule->getStudyId() );
401   if( aDocument.IsNull() )
402     return;
403
404   QString aName = theObj->GetName();
405
406   HYDROData_Iterator anIterator( aDocument, KIND_IMAGE );
407   for( ; anIterator.More(); anIterator.Next() )
408   {
409     Handle(HYDROData_Image) anImage = Handle(HYDROData_Image)::DownCast( anIterator.Current() );
410     if( !anImage.IsNull() )
411     {
412       HYDROData_SequenceOfObjects aRefObjects;
413       QStringList aRefNames;
414       GetObjectReferences( anImage, aRefObjects, aRefNames );
415       if( aRefNames.contains( aName ) )
416       {
417         theBackRefObjects.Append( anImage );
418         theBackRefNames.append( anImage->GetName() );
419       }
420     }
421   }
422 }
423
424
425 QDockWidget* HYDROGUI_Tool::WindowDock( QWidget* wid )
426 {
427   if ( !wid )
428     return 0;
429
430   QDockWidget* dock = 0;
431   QWidget* w = wid->parentWidget();
432   while ( w && !dock )
433   {
434     dock = ::qobject_cast<QDockWidget*>( w );
435     w = w->parentWidget();
436   }
437   return dock;
438 }