Salome HOME
1) Show/Hide, Delete operations
[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
35 #include <QtxWorkstack.h>
36
37 #include <STD_TabDesktop.h>
38
39 #include <SUIT_Study.h>
40 #include <SUIT_ViewManager.h>
41 #include <SUIT_ViewWindow.h>
42
43 #include <QTextCodec>
44
45 // Definition of this id allows to use 'latin1' (Qt alias for 'ISO-8859-1')
46 // encoding instead of default 'System'
47 #define USE_LATIN1_ENCODING
48
49 QString HYDROGUI_Tool::ToQString( const TCollection_AsciiString& src )
50 {
51 #ifdef USE_LATIN1_ENCODING
52   QTextCodec* codec = QTextCodec::codecForName( "latin1" ); // alias for ISO-8859-1
53 #else
54   QTextCodec* codec = QTextCodec::codecForLocale();
55 #endif
56   QString res;
57   if ( !src.IsEmpty() )
58     res = codec ? codec->toUnicode( (char*)src.ToCString(), src.Length() ) :
59       QString( (char*)src.ToCString() );
60   return res;
61 }
62
63 QString HYDROGUI_Tool::ToQString( const TCollection_ExtendedString& src )
64 {
65   return QString( (QChar*)src.ToExtString(), src.Length() );
66 }
67
68 QString HYDROGUI_Tool::ToQString( const Handle(TCollection_HAsciiString)& src )
69 {
70   if( src.IsNull() )
71     return QString();
72   else
73     return ToQString( src->String() );
74 }
75
76 QString HYDROGUI_Tool::ToQString( const Handle(TCollection_HExtendedString)& src )
77 {
78   if( src.IsNull() )
79     return QString();
80   return ToQString( src->String() );
81 }
82
83 TCollection_AsciiString HYDROGUI_Tool::ToAsciiString( const QString& src )
84 {
85   TCollection_AsciiString res;
86   if( !src.isNull() )
87   {
88 #ifdef USE_LATIN1_ENCODING
89     QTextCodec* codec = QTextCodec::codecForName( "latin1" ); // alias for ISO-8859-1
90 #else
91     QTextCodec* codec = QTextCodec::codecForLocale();
92 #endif
93     if( codec )
94     {
95       QByteArray str = codec->fromUnicode( src );
96       res = TCollection_AsciiString( (Standard_CString)str.constData() );
97     }
98     else
99       res = TCollection_AsciiString( src.toLatin1().data() );
100   }
101   return res;
102 }
103
104 TCollection_ExtendedString HYDROGUI_Tool::ToExtString( const QString& src )
105 {
106   if( src.isEmpty() )
107     return TCollection_ExtendedString();
108
109   Standard_Integer len = src.length();
110   Standard_ExtString extStr = new Standard_ExtCharacter[ ( len + 1 ) * 2 ];
111   memcpy( (void*)extStr, src.unicode(), len * 2 );
112   ((short*)extStr)[ len ] = 0;
113
114   TCollection_ExtendedString trg( extStr );
115   delete [] extStr;
116   return trg;
117 }
118
119 Handle(TCollection_HAsciiString) HYDROGUI_Tool::ToHAsciiString( const QString& src )
120 {
121   return new TCollection_HAsciiString( ToAsciiString( src ) );
122 }
123
124 Handle(TCollection_HExtendedString) HYDROGUI_Tool::ToHExtString( const QString& src )
125 {
126   return new TCollection_HExtendedString( ToExtString( src ) );
127 }
128
129 void HYDROGUI_Tool::SetActiveViewManager( HYDROGUI_Module* theModule,
130                                           SUIT_ViewManager* theViewManager )
131 {
132   if( theViewManager )
133     if( SUIT_ViewWindow* aViewWindow = theViewManager->getActiveView() )
134       if( STD_TabDesktop* aTabDesktop = dynamic_cast<STD_TabDesktop*>( theModule->getApp()->desktop() ) )
135         if( QtxWorkstack* aWorkstack = aTabDesktop->workstack() )
136           aWorkstack->setActiveWindow( aViewWindow );
137 }
138
139 void HYDROGUI_Tool::GetPrsSubObjects( const HYDROGUI_DataModel* theModel,
140                                       const int theViewerId, // currently unused
141                                       HYDROData_SequenceOfObjects& theSeq )
142 {
143   if( !theModel )
144     return;
145
146   const int aStudyId = theModel->module()->application()->activeStudy()->id();
147
148   Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( aStudyId );
149   if( aDocument.IsNull() )
150     return;
151
152   HYDROData_Iterator anIterator( aDocument, KIND_IMAGE );
153   for( ; anIterator.More(); anIterator.Next() )
154   {
155     Handle(HYDROData_Image) anImageObj =
156       Handle(HYDROData_Image)::DownCast( anIterator.Current() );
157     if( !anImageObj.IsNull() )
158       theSeq.Append( anImageObj );
159   }
160 }
161
162 HYDROGUI_Prs* HYDROGUI_Tool::GetPresentation( const Handle(HYDROData_Object)& theObj,
163                                               const GraphicsView_ObjectList& theObjects )
164 {
165   if( !theObj.IsNull() )
166   {
167     GraphicsView_ObjectListIterator anIter( theObjects );
168     while( anIter.hasNext() )
169     {
170       if( HYDROGUI_Prs* aPrs = dynamic_cast<HYDROGUI_Prs*>( anIter.next() ) )
171       {
172         Handle(HYDROData_Object) anObj = aPrs->getObject();
173         if( !anObj.IsNull() && anObj->Label() == theObj->Label() )
174           return aPrs;
175       }
176     }
177   }
178   return NULL;
179 }
180
181 GraphicsView_ObjectList HYDROGUI_Tool::GetPrsList( GraphicsView_ViewPort* theViewPort )
182 {
183   GraphicsView_ObjectList aList;
184   if( theViewPort )
185   {
186     GraphicsView_ObjectListIterator anIter( theViewPort->getObjects() );
187     while( anIter.hasNext() )
188       if( HYDROGUI_Prs* aPrs = dynamic_cast<HYDROGUI_Prs*>( anIter.next() ) )
189         aList.append( aPrs );
190   }
191   return aList;
192 }