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