Salome HOME
a159f81ae8dc8412df68844eb7d58b36fcc75272
[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_Image.h>
31 #include <HYDROData_Iterator.h>
32
33 #include <LightApp_Application.h>
34 #include <LightApp_DataOwner.h>
35 #include <LightApp_SelectionMgr.h>
36
37 #include <QtxWorkstack.h>
38
39 #include <STD_TabDesktop.h>
40
41 #include <SUIT_Study.h>
42 #include <SUIT_ViewManager.h>
43 #include <SUIT_ViewWindow.h>
44
45 #include <QTextCodec>
46
47 // Definition of this id allows to use 'latin1' (Qt alias for 'ISO-8859-1')
48 // encoding instead of default 'System'
49 #define USE_LATIN1_ENCODING
50
51 QString HYDROGUI_Tool::ToQString( const TCollection_AsciiString& src )
52 {
53 #ifdef USE_LATIN1_ENCODING
54   QTextCodec* codec = QTextCodec::codecForName( "latin1" ); // alias for ISO-8859-1
55 #else
56   QTextCodec* codec = QTextCodec::codecForLocale();
57 #endif
58   QString res;
59   if ( !src.IsEmpty() )
60     res = codec ? codec->toUnicode( (char*)src.ToCString(), src.Length() ) :
61       QString( (char*)src.ToCString() );
62   return res;
63 }
64
65 QString HYDROGUI_Tool::ToQString( const TCollection_ExtendedString& src )
66 {
67   return QString( (QChar*)src.ToExtString(), src.Length() );
68 }
69
70 QString HYDROGUI_Tool::ToQString( const Handle(TCollection_HAsciiString)& src )
71 {
72   if( src.IsNull() )
73     return QString();
74   else
75     return ToQString( src->String() );
76 }
77
78 QString HYDROGUI_Tool::ToQString( const Handle(TCollection_HExtendedString)& src )
79 {
80   if( src.IsNull() )
81     return QString();
82   return ToQString( src->String() );
83 }
84
85 TCollection_AsciiString HYDROGUI_Tool::ToAsciiString( const QString& src )
86 {
87   TCollection_AsciiString res;
88   if( !src.isNull() )
89   {
90 #ifdef USE_LATIN1_ENCODING
91     QTextCodec* codec = QTextCodec::codecForName( "latin1" ); // alias for ISO-8859-1
92 #else
93     QTextCodec* codec = QTextCodec::codecForLocale();
94 #endif
95     if( codec )
96     {
97       QByteArray str = codec->fromUnicode( src );
98       res = TCollection_AsciiString( (Standard_CString)str.constData() );
99     }
100     else
101       res = TCollection_AsciiString( src.toLatin1().data() );
102   }
103   return res;
104 }
105
106 TCollection_ExtendedString HYDROGUI_Tool::ToExtString( const QString& src )
107 {
108   if( src.isEmpty() )
109     return TCollection_ExtendedString();
110
111   Standard_Integer len = src.length();
112   Standard_ExtString extStr = new Standard_ExtCharacter[ ( len + 1 ) * 2 ];
113   memcpy( (void*)extStr, src.unicode(), len * 2 );
114   ((short*)extStr)[ len ] = 0;
115
116   TCollection_ExtendedString trg( extStr );
117   delete [] extStr;
118   return trg;
119 }
120
121 Handle(TCollection_HAsciiString) HYDROGUI_Tool::ToHAsciiString( const QString& src )
122 {
123   return new TCollection_HAsciiString( ToAsciiString( src ) );
124 }
125
126 Handle(TCollection_HExtendedString) HYDROGUI_Tool::ToHExtString( const QString& src )
127 {
128   return new TCollection_HExtendedString( ToExtString( src ) );
129 }
130
131 void HYDROGUI_Tool::LambertToDouble( const int theDegrees,
132                                      const int theMinutes,
133                                      const double theSeconds,
134                                      double& theCoord )
135 {
136   // ouv: check the case of negative degrees
137   theCoord = theDegrees * 3600 + theMinutes * 60 + theSeconds;
138 }
139
140 void HYDROGUI_Tool::DoubleToLambert( const double theCoord,
141                                      int& theDegrees,
142                                      int& theMinutes,
143                                      double& theSeconds )
144 {
145   // ouv: check the case of negative degrees
146   theDegrees = int( theCoord / 3600 );
147
148   double aRemainder = theCoord - theDegrees * 3600;
149   theMinutes = int( aRemainder / 60 );
150
151   theSeconds = aRemainder - theMinutes * 60;
152 }
153
154 void HYDROGUI_Tool::SetActiveViewManager( HYDROGUI_Module* theModule,
155                                           SUIT_ViewManager* theViewManager )
156 {
157   if( theViewManager )
158     if( SUIT_ViewWindow* aViewWindow = theViewManager->getActiveView() )
159       if( STD_TabDesktop* aTabDesktop = dynamic_cast<STD_TabDesktop*>( theModule->getApp()->desktop() ) )
160         if( QtxWorkstack* aWorkstack = aTabDesktop->workstack() )
161           aWorkstack->setActiveWindow( aViewWindow );
162 }
163
164 void HYDROGUI_Tool::GetPrsSubObjects( HYDROGUI_Module* theModule,
165                                       const int theViewerId, // currently unused
166                                       HYDROData_SequenceOfObjects& theSeq )
167 {
168   Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( theModule->getStudyId() );
169   if( aDocument.IsNull() )
170     return;
171
172   HYDROData_Iterator anIterator( aDocument, KIND_IMAGE );
173   for( ; anIterator.More(); anIterator.Next() )
174   {
175     Handle(HYDROData_Image) anImageObj =
176       Handle(HYDROData_Image)::DownCast( anIterator.Current() );
177     if( !anImageObj.IsNull() )
178       theSeq.Append( anImageObj );
179   }
180 }
181
182 HYDROGUI_Prs* HYDROGUI_Tool::GetPresentation( const Handle(HYDROData_Object)& theObj,
183                                               const GraphicsView_ObjectList& theObjects )
184 {
185   if( !theObj.IsNull() )
186   {
187     GraphicsView_ObjectListIterator anIter( theObjects );
188     while( anIter.hasNext() )
189     {
190       if( HYDROGUI_Prs* aPrs = dynamic_cast<HYDROGUI_Prs*>( anIter.next() ) )
191       {
192         Handle(HYDROData_Object) anObj = aPrs->getObject();
193         if( !anObj.IsNull() && anObj->Label() == theObj->Label() )
194           return aPrs;
195       }
196     }
197   }
198   return NULL;
199 }
200
201 GraphicsView_ObjectList HYDROGUI_Tool::GetPrsList( GraphicsView_ViewPort* theViewPort )
202 {
203   GraphicsView_ObjectList aList;
204   if( theViewPort )
205   {
206     GraphicsView_ObjectListIterator anIter( theViewPort->getObjects() );
207     while( anIter.hasNext() )
208       if( HYDROGUI_Prs* aPrs = dynamic_cast<HYDROGUI_Prs*>( anIter.next() ) )
209         aList.append( aPrs );
210   }
211   return aList;
212 }
213
214 HYDROData_SequenceOfObjects HYDROGUI_Tool::GetSelectedObjects( HYDROGUI_Module* theModule )
215 {
216   HYDROData_SequenceOfObjects aSeq;
217
218   HYDROGUI_DataModel* aModel = theModule->getDataModel();
219
220   SUIT_SelectionMgr* aSelectionMgr = theModule->getApp()->selectionMgr();
221   SUIT_DataOwnerPtrList anOwners;
222   aSelectionMgr->selected( anOwners );
223
224   foreach( SUIT_DataOwner* aSUITOwner, anOwners )
225   {
226     if( LightApp_DataOwner* anOwner = dynamic_cast<LightApp_DataOwner*>( aSUITOwner ) )
227     {
228       Handle(HYDROData_Object) anObject = aModel->objectByEntry( anOwner->entry(), KIND_UNKNOWN );
229       if( !anObject.IsNull() )
230         aSeq.Append( anObject );
231     }
232   }
233   return aSeq;
234 }
235
236 Handle(HYDROData_Object) HYDROGUI_Tool::GetSelectedObject( HYDROGUI_Module* theModule )
237 {
238   HYDROData_SequenceOfObjects aSeq = GetSelectedObjects( theModule );
239   if( !aSeq.IsEmpty() )
240     return aSeq.First();
241   return NULL;
242 }
243
244 Handle(HYDROData_Object) HYDROGUI_Tool::FindObjectByName( HYDROGUI_Module* theModule,
245                                                           const QString& theName,
246                                                           const ObjectKind theObjectKind )
247 {
248   Handle(HYDROData_Object) anObject;
249
250   Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( theModule->getStudyId() );
251   if( aDocument.IsNull() )
252     return anObject;
253
254   HYDROData_Iterator anIter( aDocument, theObjectKind );
255   for( ; anIter.More(); anIter.Next() )
256   {
257     Handle(HYDROData_Object) anObjectRef = anIter.Current();
258     if( !anObjectRef.IsNull() && anObjectRef->GetName() == theName )
259     {
260       anObject = anObjectRef;
261       break;
262     }
263   }
264   return anObject;
265 }
266
267 QString HYDROGUI_Tool::GenerateObjectName( HYDROGUI_Module* theModule,
268                                            const QString& thePrefix )
269 {
270   QString aName;
271   int anId = 1;
272   while( anId < 100 )
273   {
274     aName = QString( "%1_%2" ).arg( thePrefix ).arg( QString::number( anId++ ) );
275
276     // check that there are no other objects with the same name in the document
277     Handle(HYDROData_Object) anObject = FindObjectByName( theModule, aName, KIND_UNKNOWN );
278     if( anObject.IsNull() )
279       break;
280   }
281   return aName;
282 }