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