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