]> SALOME platform Git repositories - modules/hydro.git/blob - src/HYDROGUI/HYDROGUI_Tool.cxx
Salome HOME
Feature 32: Image positionning: cartesian coordinates.
[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 bool HYDROGUI_Tool::IsEqual( const Handle(HYDROData_Object)& theObj1,
138                              const Handle(HYDROData_Object)& theObj2 )
139 {
140   if( !theObj1.IsNull() && !theObj2.IsNull() )
141     return theObj1->Label() == theObj2->Label(); //ouv: check that the names can be used here
142   return false;
143 }
144
145 void HYDROGUI_Tool::SetActiveViewManager( HYDROGUI_Module* theModule,
146                                           SUIT_ViewManager* theViewManager )
147 {
148   if( theViewManager )
149     if( SUIT_ViewWindow* aViewWindow = theViewManager->getActiveView() )
150       if( STD_TabDesktop* aTabDesktop = dynamic_cast<STD_TabDesktop*>( theModule->getApp()->desktop() ) )
151         if( QtxWorkstack* aWorkstack = aTabDesktop->workstack() )
152           aWorkstack->setActiveWindow( aViewWindow );
153 }
154
155 void HYDROGUI_Tool::GetPrsSubObjects( HYDROGUI_Module* theModule,
156                                       HYDROData_SequenceOfObjects& theSeq )
157 {
158   Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( theModule->getStudyId() );
159   if( aDocument.IsNull() )
160     return;
161
162   HYDROData_Iterator anIterator( aDocument, KIND_IMAGE );
163   for( ; anIterator.More(); anIterator.Next() )
164   {
165     Handle(HYDROData_Object) anObject = anIterator.Current();
166     if( !anObject.IsNull() )
167       theSeq.Append( anObject );
168   }
169
170   anIterator = HYDROData_Iterator( aDocument, KIND_POLYLINE );
171   for( ; anIterator.More(); anIterator.Next() )
172   {
173     Handle(HYDROData_Object) anObject = anIterator.Current();
174     if( !anObject.IsNull() )
175       theSeq.Append( anObject );
176   }
177
178   anIterator = HYDROData_Iterator( aDocument, KIND_ZONE );
179   for( ; anIterator.More(); anIterator.Next() )
180   {
181     Handle(HYDROData_Object) anObject = anIterator.Current();
182     if( !anObject.IsNull() )
183       theSeq.Append( anObject );
184   }
185 }
186
187 HYDROGUI_Prs* HYDROGUI_Tool::GetPresentation( const Handle(HYDROData_Object)& theObj,
188                                               const GraphicsView_ObjectList& theObjects )
189 {
190   if( !theObj.IsNull() )
191   {
192     GraphicsView_ObjectListIterator anIter( theObjects );
193     while( anIter.hasNext() )
194     {
195       if( HYDROGUI_Prs* aPrs = dynamic_cast<HYDROGUI_Prs*>( anIter.next() ) )
196       {
197         Handle(HYDROData_Object) anObj = aPrs->getObject();
198         if( IsEqual( anObj, theObj ) )
199           return aPrs;
200       }
201     }
202   }
203   return NULL;
204 }
205
206 GraphicsView_ObjectList HYDROGUI_Tool::GetPrsList( GraphicsView_ViewPort* theViewPort )
207 {
208   GraphicsView_ObjectList aList;
209   if( theViewPort )
210   {
211     GraphicsView_ObjectListIterator anIter( theViewPort->getObjects() );
212     while( anIter.hasNext() )
213       if( HYDROGUI_Prs* aPrs = dynamic_cast<HYDROGUI_Prs*>( anIter.next() ) )
214         aList.append( aPrs );
215   }
216   return aList;
217 }
218
219 HYDROData_SequenceOfObjects HYDROGUI_Tool::GetSelectedObjects( HYDROGUI_Module* theModule )
220 {
221   HYDROData_SequenceOfObjects aSeq;
222
223   HYDROGUI_DataModel* aModel = theModule->getDataModel();
224
225   SUIT_SelectionMgr* aSelectionMgr = theModule->getApp()->selectionMgr();
226   SUIT_DataOwnerPtrList anOwners;
227   aSelectionMgr->selected( anOwners );
228
229   QStringList aCollectedNameList; // to avoid duplication
230   foreach( SUIT_DataOwner* aSUITOwner, anOwners )
231   {
232     if( LightApp_DataOwner* anOwner = dynamic_cast<LightApp_DataOwner*>( aSUITOwner ) )
233     {
234       Handle(HYDROData_Object) anObject = aModel->objectByEntry( anOwner->entry() );
235       if( !anObject.IsNull() )
236       {
237         QString aName = anObject->GetName();
238         if( !aCollectedNameList.contains( aName ) )
239         {
240           aSeq.Append( anObject );
241           aCollectedNameList.append( aName );
242         }
243       }
244     }
245   }
246   return aSeq;
247 }
248
249 Handle(HYDROData_Object) HYDROGUI_Tool::GetSelectedObject( HYDROGUI_Module* theModule )
250 {
251   HYDROData_SequenceOfObjects aSeq = GetSelectedObjects( theModule );
252   if( !aSeq.IsEmpty() )
253     return aSeq.First();
254   return NULL;
255 }
256
257 ObjectKind HYDROGUI_Tool::GetSelectedPartition( HYDROGUI_Module* theModule )
258 {
259   HYDROGUI_DataModel* aModel = theModule->getDataModel();
260
261   SUIT_SelectionMgr* aSelectionMgr = theModule->getApp()->selectionMgr();
262   SUIT_DataOwnerPtrList anOwners;
263   aSelectionMgr->selected( anOwners );
264
265   if( anOwners.size() != 1 )
266     return KIND_UNKNOWN;
267
268   if( LightApp_DataOwner* anOwner = dynamic_cast<LightApp_DataOwner*>( anOwners.first().operator->() ) )
269   {
270     QString anEntry = anOwner->entry();
271     QString aPrefix = HYDROGUI_DataObject::entryPrefix();
272     if( anEntry.left( aPrefix.length() ) == aPrefix )
273     {
274       anEntry.remove( aPrefix );
275       for( ObjectKind anObjectKind = KIND_UNKNOWN + 1; anObjectKind <= KIND_LAST; anObjectKind++ )
276         if( HYDROGUI_DataModel::partitionName( anObjectKind ) == anEntry )
277           return anObjectKind;
278     }
279   }
280   return KIND_UNKNOWN;
281 }
282
283 Handle(HYDROData_Object) HYDROGUI_Tool::FindObjectByName( HYDROGUI_Module* theModule,
284                                                           const QString&   theName,
285                                                           const ObjectKind theObjectKind )
286 {
287   Handle(HYDROData_Object) anObject;
288   if ( theName.isEmpty() )
289     return anObject;
290
291   QStringList aNamesList;
292   aNamesList << theName;
293
294   HYDROData_SequenceOfObjects aSeqOfObjs = FindObjectsByNames( theModule, aNamesList, theObjectKind );
295   if( aSeqOfObjs.IsEmpty() )
296     return anObject;
297   
298   anObject = aSeqOfObjs.First();
299   return anObject;
300 }
301
302 HYDROData_SequenceOfObjects HYDROGUI_Tool::FindObjectsByNames( HYDROGUI_Module*   theModule,
303                                                                const QStringList& theNames,
304                                                                const ObjectKind   theObjectKind )
305 {
306   HYDROData_SequenceOfObjects aResSeq;
307   if ( theNames.isEmpty() )
308     return aResSeq;
309
310   Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( theModule->getStudyId() );
311   if( aDocument.IsNull() )
312     return aResSeq;
313
314   QStringList aNamesList = theNames;
315
316   HYDROData_Iterator anIter( aDocument, theObjectKind );
317   for( ; anIter.More(); anIter.Next() )
318   {
319     Handle(HYDROData_Object) anObjectRef = anIter.Current();
320     if( anObjectRef.IsNull() )
321       continue;
322
323     QString anObjName = anObjectRef->GetName();
324     if ( anObjName.isEmpty() || !aNamesList.contains( anObjName ) )
325       continue;
326
327     aResSeq.Append( anObjectRef );
328
329     aNamesList.removeAll( anObjName );
330     if ( aNamesList.isEmpty() )
331       break;
332   }
333
334   return aResSeq;
335 }
336
337 QString HYDROGUI_Tool::GenerateObjectName( HYDROGUI_Module*   theModule,
338                                            const QString&     thePrefix,
339                                            const QStringList& theUsedNames )
340 {
341   QString aName;
342   int anId = 1;
343   while( anId < 100 )
344   {
345     aName = QString( "%1_%2" ).arg( thePrefix ).arg( QString::number( anId++ ) );
346
347     if ( theUsedNames.contains( aName ) )
348       continue;
349
350     // check that there are no other objects with the same name in the document
351     Handle(HYDROData_Object) anObject = FindObjectByName( theModule, aName, KIND_UNKNOWN );
352     if( anObject.IsNull() )
353       break;
354   }
355   return aName;
356 }
357
358 size_t HYDROGUI_Tool::GetActiveViewId( HYDROGUI_Module* theModule,
359                                        const QString&   theViewId )
360 {
361   size_t aViewId = 0;
362   SUIT_ViewManager* aViewMgr = theModule->getApp()->activeViewManager();
363   if( !aViewMgr || ( !theViewId.isEmpty() && aViewMgr->getType() != theViewId ) )
364     return aViewId;
365
366   if( SUIT_ViewModel* aViewer = aViewMgr->getViewModel() )
367     aViewId = (size_t)aViewer;
368   return aViewId;
369 }
370
371 size_t HYDROGUI_Tool::GetActiveGraphicsViewId( HYDROGUI_Module* theModule )
372 {
373   return GetActiveViewId( theModule, GraphicsView_Viewer::Type() );
374 }
375
376 size_t HYDROGUI_Tool::GetActiveOCCViewId( HYDROGUI_Module* theModule )
377 {
378   return GetActiveViewId( theModule, OCCViewer_Viewer::Type() );
379 }
380
381 QList<size_t> getViewIdList( HYDROGUI_Module* theModule,
382                              const QString&   theViewId )
383 {
384   QList<size_t> aList;
385   ViewManagerList aViewMgrs;
386   theModule->getApp()->viewManagers( theViewId, aViewMgrs );
387   QListIterator<SUIT_ViewManager*> anIter( aViewMgrs );
388   while( anIter.hasNext() )
389   {
390     if( SUIT_ViewManager* aViewMgr = anIter.next() )
391     {
392       if( SUIT_ViewModel* aViewer = aViewMgr->getViewModel() )
393         aList.append( (size_t)aViewer );
394     }
395   }
396   return aList;
397 }
398
399 QList<size_t> HYDROGUI_Tool::GetGraphicsViewIdList( HYDROGUI_Module* theModule )
400 {
401   return getViewIdList( theModule, GraphicsView_Viewer::Type() );
402 }
403
404 QList<size_t> HYDROGUI_Tool::GetOCCViewIdList( HYDROGUI_Module* theModule )
405 {
406   return getViewIdList( theModule, OCCViewer_Viewer::Type() );
407 }
408
409 void HYDROGUI_Tool::GetObjectReferences( const Handle(HYDROData_Image)& theImage,
410                                          HYDROData_SequenceOfObjects& theRefObjects,
411                                          QStringList& theRefNames )
412 {
413   if( theImage.IsNull() )
414     return;
415
416   for( int anIndex = 0, aNbRef = theImage->NbReferences(); anIndex < aNbRef; anIndex++ )
417   {
418     Handle(HYDROData_Object) aRefObj = theImage->Reference( anIndex );
419     if( !aRefObj.IsNull() && !aRefObj->IsRemoved() )
420     {
421       QString aName = aRefObj->GetName();
422       if( !theRefNames.contains( aName ) )
423       {
424         theRefObjects.Append( aRefObj );
425         theRefNames.append( aRefObj->GetName() );
426         if( aRefObj->GetKind() == KIND_IMAGE )
427         {
428           Handle(HYDROData_Image) aRefImage = Handle(HYDROData_Image)::DownCast( aRefObj );
429           if( !aRefImage.IsNull() )
430             GetObjectReferences( aRefImage, theRefObjects, theRefNames );
431         }
432       }
433     }
434   }
435 }
436
437 void HYDROGUI_Tool::GetObjectBackReferences( HYDROGUI_Module* theModule,
438                                              const Handle(HYDROData_Object)& theObj,
439                                              HYDROData_SequenceOfObjects& theBackRefObjects,
440                                              QStringList& theBackRefNames )
441 {
442   if( theObj.IsNull() )
443     return;
444
445   Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( theModule->getStudyId() );
446   if( aDocument.IsNull() )
447     return;
448
449   QString aName = theObj->GetName();
450
451   HYDROData_Iterator anIterator( aDocument, KIND_IMAGE );
452   for( ; anIterator.More(); anIterator.Next() )
453   {
454     Handle(HYDROData_Image) anImage = Handle(HYDROData_Image)::DownCast( anIterator.Current() );
455     if( !anImage.IsNull() )
456     {
457       HYDROData_SequenceOfObjects aRefObjects;
458       QStringList aRefNames;
459       GetObjectReferences( anImage, aRefObjects, aRefNames );
460       if( aRefNames.contains( aName ) )
461       {
462         theBackRefObjects.Append( anImage );
463         theBackRefNames.append( anImage->GetName() );
464       }
465     }
466   }
467 }
468
469
470 QDockWidget* HYDROGUI_Tool::WindowDock( QWidget* wid )
471 {
472   if ( !wid )
473     return 0;
474
475   QDockWidget* dock = 0;
476   QWidget* w = wid->parentWidget();
477   while ( w && !dock )
478   {
479     dock = ::qobject_cast<QDockWidget*>( w );
480     w = w->parentWidget();
481   }
482   return dock;
483 }
484
485 QColor HYDROGUI_Tool::GenerateFillingColor( HYDROGUI_Module*   theModule,
486                                             const QStringList& theZoneNames )
487 {
488   QColor aFillingColor( HYDROData_Zone::DefaultFillingColor() );
489
490   int aCounter = 0;
491   int aR = 0, aG = 0, aB = 0;
492   QStringListIterator aZoneNameIter( theZoneNames );
493   while( aZoneNameIter.hasNext() )
494   {
495     const QString& aZoneName = aZoneNameIter.next();
496     Handle(HYDROData_Zone) aRefZone = Handle(HYDROData_Zone)::DownCast(
497       FindObjectByName( theModule, aZoneName, KIND_ZONE ) );
498     if( !aRefZone.IsNull() )
499     {
500       QColor aRefColor = aRefZone->GetFillingColor();
501       aR += aRefColor.red();
502       aG += aRefColor.green();
503       aB += aRefColor.blue();
504       aCounter++;
505     }
506   }
507   
508   if( aCounter > 0 )
509   {
510     aFillingColor = QColor( aR / aCounter, aG / aCounter, aB / aCounter );
511   }
512
513   return aFillingColor;
514 }
515