]> SALOME platform Git repositories - modules/hydro.git/blob - src/HYDROGUI/HYDROGUI_Tool.cxx
Salome HOME
Python console has been added for HYDRO module (Bug #22).
[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_Module.h"
27 #include "HYDROGUI_Prs.h"
28
29 #include <HYDROData_Document.h>
30 #include <HYDROData_Iterator.h>
31
32 #include <GraphicsView_Viewer.h>
33
34 #include <LightApp_Application.h>
35 #include <LightApp_DataOwner.h>
36 #include <LightApp_SelectionMgr.h>
37
38 #include <QtxWorkstack.h>
39
40 #include <STD_TabDesktop.h>
41
42 #include <SUIT_Study.h>
43 #include <SUIT_ViewManager.h>
44 #include <SUIT_ViewWindow.h>
45
46 #include <QTextCodec>
47 #include <QDockWidget>
48
49 // Definition of this id allows to use 'latin1' (Qt alias for 'ISO-8859-1')
50 // encoding instead of default 'System'
51 #define USE_LATIN1_ENCODING
52
53 QString HYDROGUI_Tool::ToQString( const TCollection_AsciiString& src )
54 {
55 #ifdef USE_LATIN1_ENCODING
56   QTextCodec* codec = QTextCodec::codecForName( "latin1" ); // alias for ISO-8859-1
57 #else
58   QTextCodec* codec = QTextCodec::codecForLocale();
59 #endif
60   QString res;
61   if ( !src.IsEmpty() )
62     res = codec ? codec->toUnicode( (char*)src.ToCString(), src.Length() ) :
63       QString( (char*)src.ToCString() );
64   return res;
65 }
66
67 QString HYDROGUI_Tool::ToQString( const TCollection_ExtendedString& src )
68 {
69   return QString( (QChar*)src.ToExtString(), src.Length() );
70 }
71
72 QString HYDROGUI_Tool::ToQString( const Handle(TCollection_HAsciiString)& src )
73 {
74   if( src.IsNull() )
75     return QString();
76   else
77     return ToQString( src->String() );
78 }
79
80 QString HYDROGUI_Tool::ToQString( const Handle(TCollection_HExtendedString)& src )
81 {
82   if( src.IsNull() )
83     return QString();
84   return ToQString( src->String() );
85 }
86
87 TCollection_AsciiString HYDROGUI_Tool::ToAsciiString( const QString& src )
88 {
89   TCollection_AsciiString res;
90   if( !src.isNull() )
91   {
92 #ifdef USE_LATIN1_ENCODING
93     QTextCodec* codec = QTextCodec::codecForName( "latin1" ); // alias for ISO-8859-1
94 #else
95     QTextCodec* codec = QTextCodec::codecForLocale();
96 #endif
97     if( codec )
98     {
99       QByteArray str = codec->fromUnicode( src );
100       res = TCollection_AsciiString( (Standard_CString)str.constData() );
101     }
102     else
103       res = TCollection_AsciiString( src.toLatin1().data() );
104   }
105   return res;
106 }
107
108 TCollection_ExtendedString HYDROGUI_Tool::ToExtString( const QString& src )
109 {
110   if( src.isEmpty() )
111     return TCollection_ExtendedString();
112
113   Standard_Integer len = src.length();
114   Standard_ExtString extStr = new Standard_ExtCharacter[ ( len + 1 ) * 2 ];
115   memcpy( (void*)extStr, src.unicode(), len * 2 );
116   ((short*)extStr)[ len ] = 0;
117
118   TCollection_ExtendedString trg( extStr );
119   delete [] extStr;
120   return trg;
121 }
122
123 Handle(TCollection_HAsciiString) HYDROGUI_Tool::ToHAsciiString( const QString& src )
124 {
125   return new TCollection_HAsciiString( ToAsciiString( src ) );
126 }
127
128 Handle(TCollection_HExtendedString) HYDROGUI_Tool::ToHExtString( const QString& src )
129 {
130   return new TCollection_HExtendedString( ToExtString( src ) );
131 }
132
133 void HYDROGUI_Tool::LambertToDouble( const int theDegrees,
134                                      const int theMinutes,
135                                      const double theSeconds,
136                                      double& theCoord )
137 {
138   // ouv: check the case of negative degrees
139   theCoord = theDegrees * 3600 + theMinutes * 60 + theSeconds;
140 }
141
142 void HYDROGUI_Tool::DoubleToLambert( const double theCoord,
143                                      int& theDegrees,
144                                      int& theMinutes,
145                                      double& theSeconds )
146 {
147   // ouv: check the case of negative degrees
148   theDegrees = int( theCoord / 3600 );
149
150   double aRemainder = theCoord - theDegrees * 3600;
151   theMinutes = int( aRemainder / 60 );
152
153   theSeconds = aRemainder - theMinutes * 60;
154 }
155
156 bool HYDROGUI_Tool::IsEqual( const Handle(HYDROData_Object)& theObj1,
157                              const Handle(HYDROData_Object)& theObj2 )
158 {
159   if( !theObj1.IsNull() && !theObj2.IsNull() )
160     return theObj1->Label() == theObj2->Label(); //ouv: check that the names can be used here
161   return false;
162 }
163
164 void HYDROGUI_Tool::SetActiveViewManager( HYDROGUI_Module* theModule,
165                                           SUIT_ViewManager* theViewManager )
166 {
167   if( theViewManager )
168     if( SUIT_ViewWindow* aViewWindow = theViewManager->getActiveView() )
169       if( STD_TabDesktop* aTabDesktop = dynamic_cast<STD_TabDesktop*>( theModule->getApp()->desktop() ) )
170         if( QtxWorkstack* aWorkstack = aTabDesktop->workstack() )
171           aWorkstack->setActiveWindow( aViewWindow );
172 }
173
174 void HYDROGUI_Tool::GetPrsSubObjects( HYDROGUI_Module* theModule,
175                                       HYDROData_SequenceOfObjects& theSeq )
176 {
177   Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( theModule->getStudyId() );
178   if( aDocument.IsNull() )
179     return;
180
181   HYDROData_Iterator anIterator( aDocument, KIND_IMAGE );
182   for( ; anIterator.More(); anIterator.Next() )
183   {
184     Handle(HYDROData_Object) anObject = anIterator.Current();
185     if( !anObject.IsNull() )
186       theSeq.Append( anObject );
187   }
188
189   anIterator = HYDROData_Iterator( aDocument, KIND_POLYLINE );
190   for( ; anIterator.More(); anIterator.Next() )
191   {
192     Handle(HYDROData_Object) anObject = anIterator.Current();
193     if( !anObject.IsNull() )
194       theSeq.Append( anObject );
195   }
196 }
197
198 HYDROGUI_Prs* HYDROGUI_Tool::GetPresentation( const Handle(HYDROData_Object)& theObj,
199                                               const GraphicsView_ObjectList& theObjects )
200 {
201   if( !theObj.IsNull() )
202   {
203     GraphicsView_ObjectListIterator anIter( theObjects );
204     while( anIter.hasNext() )
205     {
206       if( HYDROGUI_Prs* aPrs = dynamic_cast<HYDROGUI_Prs*>( anIter.next() ) )
207       {
208         Handle(HYDROData_Object) anObj = aPrs->getObject();
209         if( IsEqual( anObj, theObj ) )
210           return aPrs;
211       }
212     }
213   }
214   return NULL;
215 }
216
217 GraphicsView_ObjectList HYDROGUI_Tool::GetPrsList( GraphicsView_ViewPort* theViewPort )
218 {
219   GraphicsView_ObjectList aList;
220   if( theViewPort )
221   {
222     GraphicsView_ObjectListIterator anIter( theViewPort->getObjects() );
223     while( anIter.hasNext() )
224       if( HYDROGUI_Prs* aPrs = dynamic_cast<HYDROGUI_Prs*>( anIter.next() ) )
225         aList.append( aPrs );
226   }
227   return aList;
228 }
229
230 HYDROData_SequenceOfObjects HYDROGUI_Tool::GetSelectedObjects( HYDROGUI_Module* theModule )
231 {
232   HYDROData_SequenceOfObjects aSeq;
233
234   HYDROGUI_DataModel* aModel = theModule->getDataModel();
235
236   SUIT_SelectionMgr* aSelectionMgr = theModule->getApp()->selectionMgr();
237   SUIT_DataOwnerPtrList anOwners;
238   aSelectionMgr->selected( anOwners );
239
240   QStringList aCollectedNameList; // to avoid duplication
241   foreach( SUIT_DataOwner* aSUITOwner, anOwners )
242   {
243     if( LightApp_DataOwner* anOwner = dynamic_cast<LightApp_DataOwner*>( aSUITOwner ) )
244     {
245       Handle(HYDROData_Object) anObject = aModel->objectByEntry( anOwner->entry() );
246       if( !anObject.IsNull() )
247       {
248         QString aName = anObject->GetName();
249         if( !aCollectedNameList.contains( aName ) )
250         {
251           aSeq.Append( anObject );
252           aCollectedNameList.append( aName );
253         }
254       }
255     }
256   }
257   return aSeq;
258 }
259
260 Handle(HYDROData_Object) HYDROGUI_Tool::GetSelectedObject( HYDROGUI_Module* theModule )
261 {
262   HYDROData_SequenceOfObjects aSeq = GetSelectedObjects( theModule );
263   if( !aSeq.IsEmpty() )
264     return aSeq.First();
265   return NULL;
266 }
267
268 Handle(HYDROData_Object) HYDROGUI_Tool::FindObjectByName( HYDROGUI_Module* theModule,
269                                                           const QString& theName,
270                                                           const ObjectKind theObjectKind )
271 {
272   Handle(HYDROData_Object) anObject;
273
274   Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( theModule->getStudyId() );
275   if( aDocument.IsNull() )
276     return anObject;
277
278   HYDROData_Iterator anIter( aDocument, theObjectKind );
279   for( ; anIter.More(); anIter.Next() )
280   {
281     Handle(HYDROData_Object) anObjectRef = anIter.Current();
282     if( !anObjectRef.IsNull() && anObjectRef->GetName() == theName )
283     {
284       anObject = anObjectRef;
285       break;
286     }
287   }
288   return anObject;
289 }
290
291 QString HYDROGUI_Tool::GenerateObjectName( HYDROGUI_Module* theModule,
292                                            const QString& thePrefix )
293 {
294   QString aName;
295   int anId = 1;
296   while( anId < 100 )
297   {
298     aName = QString( "%1_%2" ).arg( thePrefix ).arg( QString::number( anId++ ) );
299
300     // check that there are no other objects with the same name in the document
301     Handle(HYDROData_Object) anObject = FindObjectByName( theModule, aName, KIND_UNKNOWN );
302     if( anObject.IsNull() )
303       break;
304   }
305   return aName;
306 }
307
308 size_t HYDROGUI_Tool::GetActiveGraphicsViewId( HYDROGUI_Module* theModule )
309 {
310   size_t aViewId = 0;
311   SUIT_ViewManager* aViewMgr = theModule->getApp()->activeViewManager();
312   if( !aViewMgr || aViewMgr->getType() != GraphicsView_Viewer::Type() )
313     return aViewId;
314
315   if( SUIT_ViewModel* aViewer = aViewMgr->getViewModel() )
316     aViewId = (size_t)aViewer;
317   return aViewId;
318 }
319
320 QList<size_t> HYDROGUI_Tool::GetGraphicsViewIdList( HYDROGUI_Module* theModule )
321 {
322   QList<size_t> aList;
323   ViewManagerList aViewMgrs;
324   theModule->getApp()->viewManagers( GraphicsView_Viewer::Type(), aViewMgrs );
325   QListIterator<SUIT_ViewManager*> anIter( aViewMgrs );
326   while( anIter.hasNext() )
327   {
328     if( SUIT_ViewManager* aViewMgr = anIter.next() )
329     {
330       if( SUIT_ViewModel* aViewer = aViewMgr->getViewModel() )
331         aList.append( (size_t)aViewer );
332     }
333   }
334   return aList;
335 }
336
337 void HYDROGUI_Tool::GetObjectReferences( const Handle(HYDROData_Image)& theImage,
338                                          HYDROData_SequenceOfObjects& theRefObjects,
339                                          QStringList& theRefNames )
340 {
341   if( theImage.IsNull() )
342     return;
343
344   for( int anIndex = 0, aNbRef = theImage->NbReferences(); anIndex < aNbRef; anIndex++ )
345   {
346     Handle(HYDROData_Object) aRefObj = theImage->Reference( anIndex );
347     if( !aRefObj.IsNull() && !aRefObj->IsRemoved() )
348     {
349       QString aName = aRefObj->GetName();
350       if( !theRefNames.contains( aName ) )
351       {
352         theRefObjects.Append( aRefObj );
353         theRefNames.append( aRefObj->GetName() );
354         if( aRefObj->GetKind() == KIND_IMAGE )
355         {
356           Handle(HYDROData_Image) aRefImage = Handle(HYDROData_Image)::DownCast( aRefObj );
357           if( !aRefImage.IsNull() )
358             GetObjectReferences( aRefImage, theRefObjects, theRefNames );
359         }
360       }
361     }
362   }
363 }
364
365 void HYDROGUI_Tool::GetObjectBackReferences( HYDROGUI_Module* theModule,
366                                              const Handle(HYDROData_Object)& theObj,
367                                              HYDROData_SequenceOfObjects& theBackRefObjects,
368                                              QStringList& theBackRefNames )
369 {
370   if( theObj.IsNull() )
371     return;
372
373   Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( theModule->getStudyId() );
374   if( aDocument.IsNull() )
375     return;
376
377   QString aName = theObj->GetName();
378
379   HYDROData_Iterator anIterator( aDocument, KIND_IMAGE );
380   for( ; anIterator.More(); anIterator.Next() )
381   {
382     Handle(HYDROData_Image) anImage = Handle(HYDROData_Image)::DownCast( anIterator.Current() );
383     if( !anImage.IsNull() )
384     {
385       HYDROData_SequenceOfObjects aRefObjects;
386       QStringList aRefNames;
387       GetObjectReferences( anImage, aRefObjects, aRefNames );
388       if( aRefNames.contains( aName ) )
389       {
390         theBackRefObjects.Append( anImage );
391         theBackRefNames.append( anImage->GetName() );
392       }
393     }
394   }
395 }
396
397
398 QDockWidget* HYDROGUI_Tool::WindowDock( QWidget* wid )
399 {
400   if ( !wid )
401     return 0;
402
403   QDockWidget* dock = 0;
404   QWidget* w = wid->parentWidget();
405   while ( w && !dock )
406   {
407     dock = ::qobject_cast<QDockWidget*>( w );
408     w = w->parentWidget();
409   }
410   return dock;
411 }