Salome HOME
ac76bb18ebcd670e219e17700373a5f7b4c0f7f3
[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 #include <HYDROData_Zone.h>
33
34 #include <GraphicsView_Viewer.h>
35
36 #include <LightApp_Application.h>
37 #include <LightApp_DataOwner.h>
38 #include <LightApp_SelectionMgr.h>
39
40 #include <OCCViewer_ViewModel.h>
41
42 #include <QtxWorkstack.h>
43
44 #include <STD_TabDesktop.h>
45
46 #include <SUIT_Study.h>
47 #include <SUIT_ViewManager.h>
48 #include <SUIT_ViewWindow.h>
49
50 #include <QTextCodec>
51 #include <QDockWidget>
52
53 // Definition of this id allows to use 'latin1' (Qt alias for 'ISO-8859-1')
54 // encoding instead of default 'System'
55 #define USE_LATIN1_ENCODING
56
57 QString HYDROGUI_Tool::ToQString( const TCollection_AsciiString& src )
58 {
59 #ifdef USE_LATIN1_ENCODING
60   QTextCodec* codec = QTextCodec::codecForName( "latin1" ); // alias for ISO-8859-1
61 #else
62   QTextCodec* codec = QTextCodec::codecForLocale();
63 #endif
64   QString res;
65   if ( !src.IsEmpty() )
66     res = codec ? codec->toUnicode( (char*)src.ToCString(), src.Length() ) :
67       QString( (char*)src.ToCString() );
68   return res;
69 }
70
71 QString HYDROGUI_Tool::ToQString( const TCollection_ExtendedString& src )
72 {
73   return QString( (QChar*)src.ToExtString(), src.Length() );
74 }
75
76 QString HYDROGUI_Tool::ToQString( const Handle(TCollection_HAsciiString)& src )
77 {
78   if( src.IsNull() )
79     return QString();
80   else
81     return ToQString( src->String() );
82 }
83
84 QString HYDROGUI_Tool::ToQString( const Handle(TCollection_HExtendedString)& src )
85 {
86   if( src.IsNull() )
87     return QString();
88   return ToQString( src->String() );
89 }
90
91 TCollection_AsciiString HYDROGUI_Tool::ToAsciiString( const QString& src )
92 {
93   TCollection_AsciiString res;
94   if( !src.isNull() )
95   {
96 #ifdef USE_LATIN1_ENCODING
97     QTextCodec* codec = QTextCodec::codecForName( "latin1" ); // alias for ISO-8859-1
98 #else
99     QTextCodec* codec = QTextCodec::codecForLocale();
100 #endif
101     if( codec )
102     {
103       QByteArray str = codec->fromUnicode( src );
104       res = TCollection_AsciiString( (Standard_CString)str.constData() );
105     }
106     else
107       res = TCollection_AsciiString( src.toLatin1().data() );
108   }
109   return res;
110 }
111
112 TCollection_ExtendedString HYDROGUI_Tool::ToExtString( const QString& src )
113 {
114   if( src.isEmpty() )
115     return TCollection_ExtendedString();
116
117   Standard_Integer len = src.length();
118   Standard_ExtString extStr = new Standard_ExtCharacter[ ( len + 1 ) * 2 ];
119   memcpy( (void*)extStr, src.unicode(), len * 2 );
120   ((short*)extStr)[ len ] = 0;
121
122   TCollection_ExtendedString trg( extStr );
123   delete [] extStr;
124   return trg;
125 }
126
127 Handle(TCollection_HAsciiString) HYDROGUI_Tool::ToHAsciiString( const QString& src )
128 {
129   return new TCollection_HAsciiString( ToAsciiString( src ) );
130 }
131
132 Handle(TCollection_HExtendedString) HYDROGUI_Tool::ToHExtString( const QString& src )
133 {
134   return new TCollection_HExtendedString( ToExtString( src ) );
135 }
136
137 void HYDROGUI_Tool::LambertToDouble( const int theDegrees,
138                                      const int theMinutes,
139                                      const double theSeconds,
140                                      double& theCoord )
141 {
142   // ouv: check the case of negative degrees
143   theCoord = theDegrees * 3600 + theMinutes * 60 + theSeconds;
144 }
145
146 void HYDROGUI_Tool::DoubleToLambert( const double theCoord,
147                                      int& theDegrees,
148                                      int& theMinutes,
149                                      double& theSeconds )
150 {
151   // ouv: check the case of negative degrees
152   theDegrees = int( theCoord / 3600 );
153
154   double aRemainder = theCoord - theDegrees * 3600;
155   theMinutes = int( aRemainder / 60 );
156
157   theSeconds = aRemainder - theMinutes * 60;
158 }
159
160 bool HYDROGUI_Tool::IsEqual( const Handle(HYDROData_Object)& theObj1,
161                              const Handle(HYDROData_Object)& theObj2 )
162 {
163   if( !theObj1.IsNull() && !theObj2.IsNull() )
164     return theObj1->Label() == theObj2->Label(); //ouv: check that the names can be used here
165   return false;
166 }
167
168 void HYDROGUI_Tool::SetActiveViewManager( HYDROGUI_Module* theModule,
169                                           SUIT_ViewManager* theViewManager )
170 {
171   if( theViewManager )
172     if( SUIT_ViewWindow* aViewWindow = theViewManager->getActiveView() )
173       if( STD_TabDesktop* aTabDesktop = dynamic_cast<STD_TabDesktop*>( theModule->getApp()->desktop() ) )
174         if( QtxWorkstack* aWorkstack = aTabDesktop->workstack() )
175           aWorkstack->setActiveWindow( aViewWindow );
176 }
177
178 void HYDROGUI_Tool::GetPrsSubObjects( HYDROGUI_Module* theModule,
179                                       HYDROData_SequenceOfObjects& theSeq )
180 {
181   Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( theModule->getStudyId() );
182   if( aDocument.IsNull() )
183     return;
184
185   HYDROData_Iterator anIterator( aDocument, KIND_IMAGE );
186   for( ; anIterator.More(); anIterator.Next() )
187   {
188     Handle(HYDROData_Object) anObject = anIterator.Current();
189     if( !anObject.IsNull() )
190       theSeq.Append( anObject );
191   }
192
193   anIterator = HYDROData_Iterator( aDocument, KIND_POLYLINE );
194   for( ; anIterator.More(); anIterator.Next() )
195   {
196     Handle(HYDROData_Object) anObject = anIterator.Current();
197     if( !anObject.IsNull() )
198       theSeq.Append( anObject );
199   }
200
201   anIterator = HYDROData_Iterator( aDocument, KIND_ZONE );
202   for( ; anIterator.More(); anIterator.Next() )
203   {
204     Handle(HYDROData_Object) anObject = anIterator.Current();
205     if( !anObject.IsNull() )
206       theSeq.Append( anObject );
207   }
208 }
209
210 HYDROGUI_Prs* HYDROGUI_Tool::GetPresentation( const Handle(HYDROData_Object)& theObj,
211                                               const GraphicsView_ObjectList& theObjects )
212 {
213   if( !theObj.IsNull() )
214   {
215     GraphicsView_ObjectListIterator anIter( theObjects );
216     while( anIter.hasNext() )
217     {
218       if( HYDROGUI_Prs* aPrs = dynamic_cast<HYDROGUI_Prs*>( anIter.next() ) )
219       {
220         Handle(HYDROData_Object) anObj = aPrs->getObject();
221         if( IsEqual( anObj, theObj ) )
222           return aPrs;
223       }
224     }
225   }
226   return NULL;
227 }
228
229 GraphicsView_ObjectList HYDROGUI_Tool::GetPrsList( GraphicsView_ViewPort* theViewPort )
230 {
231   GraphicsView_ObjectList aList;
232   if( theViewPort )
233   {
234     GraphicsView_ObjectListIterator anIter( theViewPort->getObjects() );
235     while( anIter.hasNext() )
236       if( HYDROGUI_Prs* aPrs = dynamic_cast<HYDROGUI_Prs*>( anIter.next() ) )
237         aList.append( aPrs );
238   }
239   return aList;
240 }
241
242 HYDROData_SequenceOfObjects HYDROGUI_Tool::GetSelectedObjects( HYDROGUI_Module* theModule )
243 {
244   HYDROData_SequenceOfObjects aSeq;
245
246   HYDROGUI_DataModel* aModel = theModule->getDataModel();
247
248   SUIT_SelectionMgr* aSelectionMgr = theModule->getApp()->selectionMgr();
249   SUIT_DataOwnerPtrList anOwners;
250   aSelectionMgr->selected( anOwners );
251
252   QStringList aCollectedNameList; // to avoid duplication
253   foreach( SUIT_DataOwner* aSUITOwner, anOwners )
254   {
255     if( LightApp_DataOwner* anOwner = dynamic_cast<LightApp_DataOwner*>( aSUITOwner ) )
256     {
257       Handle(HYDROData_Object) anObject = aModel->objectByEntry( anOwner->entry() );
258       if( !anObject.IsNull() )
259       {
260         QString aName = anObject->GetName();
261         if( !aCollectedNameList.contains( aName ) )
262         {
263           aSeq.Append( anObject );
264           aCollectedNameList.append( aName );
265         }
266       }
267     }
268   }
269   return aSeq;
270 }
271
272 Handle(HYDROData_Object) HYDROGUI_Tool::GetSelectedObject( HYDROGUI_Module* theModule )
273 {
274   HYDROData_SequenceOfObjects aSeq = GetSelectedObjects( theModule );
275   if( !aSeq.IsEmpty() )
276     return aSeq.First();
277   return NULL;
278 }
279
280 ObjectKind HYDROGUI_Tool::GetSelectedPartition( HYDROGUI_Module* theModule )
281 {
282   HYDROGUI_DataModel* aModel = theModule->getDataModel();
283
284   SUIT_SelectionMgr* aSelectionMgr = theModule->getApp()->selectionMgr();
285   SUIT_DataOwnerPtrList anOwners;
286   aSelectionMgr->selected( anOwners );
287
288   if( anOwners.size() != 1 )
289     return KIND_UNKNOWN;
290
291   if( LightApp_DataOwner* anOwner = dynamic_cast<LightApp_DataOwner*>( anOwners.first().operator->() ) )
292   {
293     QString anEntry = anOwner->entry();
294     QString aPrefix = HYDROGUI_DataObject::entryPrefix();
295     if( anEntry.left( aPrefix.length() ) == aPrefix )
296     {
297       anEntry.remove( aPrefix );
298       for( ObjectKind anObjectKind = KIND_UNKNOWN + 1; anObjectKind <= KIND_LAST; anObjectKind++ )
299         if( HYDROGUI_DataModel::partitionName( anObjectKind ) == anEntry )
300           return anObjectKind;
301     }
302   }
303   return KIND_UNKNOWN;
304 }
305
306 Handle(HYDROData_Object) HYDROGUI_Tool::FindObjectByName( HYDROGUI_Module* theModule,
307                                                           const QString&   theName,
308                                                           const ObjectKind theObjectKind )
309 {
310   Handle(HYDROData_Object) anObject;
311   if ( theName.isEmpty() )
312     return anObject;
313
314   QStringList aNamesList;
315   aNamesList << theName;
316
317   HYDROData_SequenceOfObjects aSeqOfObjs = FindObjectsByNames( theModule, aNamesList, theObjectKind );
318   if( aSeqOfObjs.IsEmpty() )
319     return anObject;
320   
321   anObject = aSeqOfObjs.First();
322   return anObject;
323 }
324
325 HYDROData_SequenceOfObjects HYDROGUI_Tool::FindObjectsByNames( HYDROGUI_Module*   theModule,
326                                                                const QStringList& theNames,
327                                                                const ObjectKind   theObjectKind )
328 {
329   HYDROData_SequenceOfObjects aResSeq;
330   if ( theNames.isEmpty() )
331     return aResSeq;
332
333   Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( theModule->getStudyId() );
334   if( aDocument.IsNull() )
335     return aResSeq;
336
337   QStringList aNamesList = theNames;
338
339   HYDROData_Iterator anIter( aDocument, theObjectKind );
340   for( ; anIter.More(); anIter.Next() )
341   {
342     Handle(HYDROData_Object) anObjectRef = anIter.Current();
343     if( anObjectRef.IsNull() )
344       continue;
345
346     QString anObjName = anObjectRef->GetName();
347     if ( anObjName.isEmpty() || !aNamesList.contains( anObjName ) )
348       continue;
349
350     aResSeq.Append( anObjectRef );
351
352     aNamesList.removeAll( anObjName );
353     if ( aNamesList.isEmpty() )
354       break;
355   }
356
357   return aResSeq;
358 }
359
360 QString HYDROGUI_Tool::GenerateObjectName( HYDROGUI_Module*   theModule,
361                                            const QString&     thePrefix,
362                                            const QStringList& theUsedNames )
363 {
364   QString aName;
365   int anId = 1;
366   while( anId < 100 )
367   {
368     aName = QString( "%1_%2" ).arg( thePrefix ).arg( QString::number( anId++ ) );
369
370     if ( theUsedNames.contains( aName ) )
371       continue;
372
373     // check that there are no other objects with the same name in the document
374     Handle(HYDROData_Object) anObject = FindObjectByName( theModule, aName, KIND_UNKNOWN );
375     if( anObject.IsNull() )
376       break;
377   }
378   return aName;
379 }
380
381 size_t HYDROGUI_Tool::GetActiveViewId( HYDROGUI_Module* theModule,
382                                        const QString&   theViewId )
383 {
384   size_t aViewId = 0;
385   SUIT_ViewManager* aViewMgr = theModule->getApp()->activeViewManager();
386   if( !aViewMgr || ( !theViewId.isEmpty() && aViewMgr->getType() != theViewId ) )
387     return aViewId;
388
389   if( SUIT_ViewModel* aViewer = aViewMgr->getViewModel() )
390     aViewId = (size_t)aViewer;
391   return aViewId;
392 }
393
394 size_t HYDROGUI_Tool::GetActiveGraphicsViewId( HYDROGUI_Module* theModule )
395 {
396   return GetActiveViewId( theModule, GraphicsView_Viewer::Type() );
397 }
398
399 size_t HYDROGUI_Tool::GetActiveOCCViewId( HYDROGUI_Module* theModule )
400 {
401   return GetActiveViewId( theModule, OCCViewer_Viewer::Type() );
402 }
403
404 QList<size_t> getViewIdList( HYDROGUI_Module* theModule,
405                              const QString&   theViewId )
406 {
407   QList<size_t> aList;
408   ViewManagerList aViewMgrs;
409   theModule->getApp()->viewManagers( theViewId, aViewMgrs );
410   QListIterator<SUIT_ViewManager*> anIter( aViewMgrs );
411   while( anIter.hasNext() )
412   {
413     if( SUIT_ViewManager* aViewMgr = anIter.next() )
414     {
415       if( SUIT_ViewModel* aViewer = aViewMgr->getViewModel() )
416         aList.append( (size_t)aViewer );
417     }
418   }
419   return aList;
420 }
421
422 QList<size_t> HYDROGUI_Tool::GetGraphicsViewIdList( HYDROGUI_Module* theModule )
423 {
424   return getViewIdList( theModule, GraphicsView_Viewer::Type() );
425 }
426
427 QList<size_t> HYDROGUI_Tool::GetOCCViewIdList( HYDROGUI_Module* theModule )
428 {
429   return getViewIdList( theModule, OCCViewer_Viewer::Type() );
430 }
431
432 void HYDROGUI_Tool::GetObjectReferences( const Handle(HYDROData_Image)& theImage,
433                                          HYDROData_SequenceOfObjects& theRefObjects,
434                                          QStringList& theRefNames )
435 {
436   if( theImage.IsNull() )
437     return;
438
439   for( int anIndex = 0, aNbRef = theImage->NbReferences(); anIndex < aNbRef; anIndex++ )
440   {
441     Handle(HYDROData_Object) aRefObj = theImage->Reference( anIndex );
442     if( !aRefObj.IsNull() && !aRefObj->IsRemoved() )
443     {
444       QString aName = aRefObj->GetName();
445       if( !theRefNames.contains( aName ) )
446       {
447         theRefObjects.Append( aRefObj );
448         theRefNames.append( aRefObj->GetName() );
449         if( aRefObj->GetKind() == KIND_IMAGE )
450         {
451           Handle(HYDROData_Image) aRefImage = Handle(HYDROData_Image)::DownCast( aRefObj );
452           if( !aRefImage.IsNull() )
453             GetObjectReferences( aRefImage, theRefObjects, theRefNames );
454         }
455       }
456     }
457   }
458 }
459
460 void HYDROGUI_Tool::GetObjectBackReferences( HYDROGUI_Module* theModule,
461                                              const Handle(HYDROData_Object)& theObj,
462                                              HYDROData_SequenceOfObjects& theBackRefObjects,
463                                              QStringList& theBackRefNames )
464 {
465   if( theObj.IsNull() )
466     return;
467
468   Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( theModule->getStudyId() );
469   if( aDocument.IsNull() )
470     return;
471
472   QString aName = theObj->GetName();
473
474   HYDROData_Iterator anIterator( aDocument, KIND_IMAGE );
475   for( ; anIterator.More(); anIterator.Next() )
476   {
477     Handle(HYDROData_Image) anImage = Handle(HYDROData_Image)::DownCast( anIterator.Current() );
478     if( !anImage.IsNull() )
479     {
480       HYDROData_SequenceOfObjects aRefObjects;
481       QStringList aRefNames;
482       GetObjectReferences( anImage, aRefObjects, aRefNames );
483       if( aRefNames.contains( aName ) )
484       {
485         theBackRefObjects.Append( anImage );
486         theBackRefNames.append( anImage->GetName() );
487       }
488     }
489   }
490 }
491
492
493 QDockWidget* HYDROGUI_Tool::WindowDock( QWidget* wid )
494 {
495   if ( !wid )
496     return 0;
497
498   QDockWidget* dock = 0;
499   QWidget* w = wid->parentWidget();
500   while ( w && !dock )
501   {
502     dock = ::qobject_cast<QDockWidget*>( w );
503     w = w->parentWidget();
504   }
505   return dock;
506 }
507
508 QColor HYDROGUI_Tool::GenerateFillingColor( HYDROGUI_Module*   theModule,
509                                             const QStringList& theZoneNames )
510 {
511   QColor aFillingColor( HYDROData_Zone::DefaultFillingColor() );
512
513   int aCounter = 0;
514   int aR = 0, aG = 0, aB = 0;
515   QStringListIterator aZoneNameIter( theZoneNames );
516   while( aZoneNameIter.hasNext() )
517   {
518     const QString& aZoneName = aZoneNameIter.next();
519     Handle(HYDROData_Zone) aRefZone = Handle(HYDROData_Zone)::DownCast(
520       FindObjectByName( theModule, aZoneName, KIND_ZONE ) );
521     if( !aRefZone.IsNull() )
522     {
523       QColor aRefColor = aRefZone->GetFillingColor();
524       aR += aRefColor.red();
525       aG += aRefColor.green();
526       aB += aRefColor.blue();
527       aCounter++;
528     }
529   }
530   
531   if( aCounter > 0 )
532   {
533     aFillingColor = QColor( aR / aCounter, aG / aCounter, aB / aCounter );
534   }
535
536   return aFillingColor;
537 }
538