]> SALOME platform Git repositories - modules/hydro.git/blob - src/HYDROGUI/HYDROGUI_Tool.cxx
Salome HOME
Merge branch 'BR_PY3'
[modules/hydro.git] / src / HYDROGUI / HYDROGUI_Tool.cxx
1 // Copyright (C) 2014-2015  EDF-R&D
2 // This library is free software; you can redistribute it and/or
3 // modify it under the terms of the GNU Lesser General Public
4 // License as published by the Free Software Foundation; either
5 // version 2.1 of the License, or (at your option) any later version.
6 //
7 // This library is distributed in the hope that it will be useful,
8 // but WITHOUT ANY WARRANTY; without even the implied warranty of
9 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
10 // Lesser General Public License for more details.
11 //
12 // You should have received a copy of the GNU Lesser General Public
13 // License along with this library; if not, write to the Free Software
14 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
15 //
16 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
17 //
18
19 #include <HYDROGUI_Tool.h>
20 #include <HYDROData_Document.h>
21 #include <HYDROData_Iterator.h>
22 #include <HYDROData_Profile.h>
23 #include <TCollection_ExtendedString.hxx>
24 #include <TCollection_HAsciiString.hxx>
25 #include <TCollection_HExtendedString.hxx>
26 #include <Image_PixMap.hxx>
27 #include <QDir>
28 #include <QDockWidget>
29 #include <QFile>
30 #include <QFileInfo>
31 #include <QLocale>
32 #include <QStringList>
33 #include <QTextCodec>
34 #include <QWidget>
35 #include <QHeaderView>
36
37 // Definition of this id allows to use 'latin1' (Qt alias for 'ISO-8859-1')
38 // encoding instead of default 'System'
39 #define USE_LATIN1_ENCODING
40
41 // #define DEB_GROUPS 1
42
43 QString HYDROGUI_Tool::ToQString( const TCollection_AsciiString& src )
44 {
45 #ifdef USE_LATIN1_ENCODING
46   QTextCodec* codec = QTextCodec::codecForName( "latin1" ); // alias for ISO-8859-1
47 #else
48   QTextCodec* codec = QTextCodec::codecForLocale();
49 #endif
50   QString res;
51   if ( !src.IsEmpty() )
52     res = codec ? codec->toUnicode( (char*)src.ToCString(), src.Length() ) :
53       QString( (char*)src.ToCString() );
54   return res;
55 }
56
57 QString HYDROGUI_Tool::ToQString( const TCollection_ExtendedString& src )
58 {
59   return QString( (QChar*)src.ToExtString(), src.Length() );
60 }
61
62 QString HYDROGUI_Tool::ToQString( const Handle(TCollection_HAsciiString)& src )
63 {
64   if( src.IsNull() )
65     return QString();
66   else
67     return ToQString( src->String() );
68 }
69
70 QString HYDROGUI_Tool::ToQString( const Handle(TCollection_HExtendedString)& src )
71 {
72   if( src.IsNull() )
73     return QString();
74   return ToQString( src->String() );
75 }
76
77 TCollection_AsciiString HYDROGUI_Tool::ToAsciiString( const QString& src )
78 {
79   TCollection_AsciiString res;
80   if( !src.isNull() )
81   {
82 #ifdef USE_LATIN1_ENCODING
83     QTextCodec* codec = QTextCodec::codecForName( "latin1" ); // alias for ISO-8859-1
84 #else
85     QTextCodec* codec = QTextCodec::codecForLocale();
86 #endif
87     if( codec )
88     {
89       QByteArray str = codec->fromUnicode( src );
90       res = TCollection_AsciiString( (Standard_CString)str.constData() );
91     }
92     else
93       res = TCollection_AsciiString( src.toLatin1().data() );
94   }
95   return res;
96 }
97
98 TCollection_ExtendedString HYDROGUI_Tool::ToExtString( const QString& src )
99 {
100   if( src.isEmpty() )
101     return TCollection_ExtendedString();
102
103   Standard_Integer len = src.length();
104   Standard_ExtString extStr = new Standard_ExtCharacter[ ( len + 1 ) * 2 ];
105   memcpy( (void*)extStr, src.unicode(), len * 2 );
106   ((short*)extStr)[ len ] = 0;
107
108   TCollection_ExtendedString trg( extStr );
109   delete [] extStr;
110   return trg;
111 }
112
113 Handle(TCollection_HAsciiString) HYDROGUI_Tool::ToHAsciiString( const QString& src )
114 {
115   return new TCollection_HAsciiString( ToAsciiString( src ) );
116 }
117
118 Handle(TCollection_HExtendedString) HYDROGUI_Tool::ToHExtString( const QString& src )
119 {
120   return new TCollection_HExtendedString( ToExtString( src ) );
121 }
122
123 QString HYDROGUI_Tool::GetTempDir( const bool theToCreate )
124 {
125   QString aRes;
126
127   char* tmpdir = getenv ( "HYDRO_TMP_DIR" );
128   if ( tmpdir )
129   {
130     // try to create folder if it does not exist
131     QFileInfo fi( tmpdir );
132     if ( !fi.exists() && theToCreate )
133     {
134       if ( QDir().mkdir( tmpdir ) )
135         QFile::setPermissions( tmpdir, (QFile::Permissions)0x4FFFF );
136        QFileInfo fi( tmpdir );
137        if ( !fi.exists() || !fi.isWritable() )
138          tmpdir = 0;
139     }
140   }
141   if ( !tmpdir )
142     tmpdir = getenv ( "TEMP" );
143   if ( !tmpdir )
144     tmpdir = getenv ( "TMP" );
145   if ( !tmpdir )
146   {
147 #ifdef WNT
148     tmpdir = "C:\\";
149 #else
150     tmpdir = strdup( "/tmp" );
151 #endif
152   }
153   aRes = tmpdir;
154   
155   QFileInfo fi( aRes );
156   if ( !fi.exists() || !fi.isWritable() )
157     aRes = QString::null;
158
159   return aRes;
160 }
161
162 void HYDROGUI_Tool::GetObjectReferences( const Handle(HYDROData_Entity)& theObj,
163                                          HYDROData_SequenceOfObjects& theRefObjects,
164                                          QStringList& theRefNames )
165 {
166   if( theObj.IsNull() )
167     return;
168
169   HYDROData_SequenceOfObjects anAllRefObjects = theObj->GetAllReferenceObjects();
170   theRefObjects.Append( anAllRefObjects );
171
172   for( int i = 1, n = anAllRefObjects.Length(); i <= n; ++i )
173   {
174     Handle(HYDROData_Entity) aRefObj = theRefObjects.Value( i );
175     if( aRefObj.IsNull() || aRefObj->IsRemoved() )
176       continue;
177
178     QString aRefObjectName = aRefObj->GetName();
179     if( theRefNames.contains( aRefObjectName ) )
180       continue;
181
182     theRefObjects.Append( aRefObj );
183     theRefNames.append( aRefObjectName );
184
185     GetObjectReferences( aRefObj, theRefObjects, theRefNames );
186   }
187 }
188
189 HYDROData_SequenceOfObjects HYDROGUI_Tool::GetObjectBackReferences( const Handle(HYDROData_Entity)& theObj )
190 {
191   if( theObj.IsNull() )
192     return HYDROData_SequenceOfObjects();
193
194   QString anObjName = theObj->GetName();
195
196   Handle(HYDROData_Document) aDoc = HYDROData_Document::Document( theObj->Label() );
197   QMap<QString,HYDROData_SequenceOfObjects> aMapOfBackRefs =
198     GetObjectsBackReferences( aDoc, QStringList() << anObjName );
199
200   return aMapOfBackRefs[ anObjName ];
201 }
202
203 QMap<QString,HYDROData_SequenceOfObjects> HYDROGUI_Tool::GetObjectsBackReferences(
204   const Handle(HYDROData_Document)& theDocument, const QStringList& theObjectNames )
205 {
206   QMap<QString,HYDROData_SequenceOfObjects> aResMap;
207
208   if( theObjectNames.isEmpty() )
209     return aResMap;
210
211   if( theDocument.IsNull() )
212     return aResMap;
213
214   HYDROData_Iterator anIterator( theDocument );
215   for( ; anIterator.More(); anIterator.Next() )
216   {
217     Handle(HYDROData_Entity) anObject = anIterator.Current();
218     if( anObject.IsNull() || anObject->IsRemoved() )
219       continue;
220
221     QString anObjectName = anObject->GetName();
222     if ( theObjectNames.contains( anObjectName ) )
223       continue;
224
225     HYDROData_SequenceOfObjects aRefObjects = anObject->GetAllReferenceObjects();
226     for ( int i = 1, n = aRefObjects.Length(); i <= n; ++i )
227     {
228       Handle(HYDROData_Entity) aRefObject = aRefObjects.Value( i );
229       if( aRefObject.IsNull() || aRefObject->IsRemoved() )
230         continue;
231
232       QString aRefObjectName = aRefObject->GetName();
233       if ( !theObjectNames.contains( aRefObjectName ) )
234         continue;
235
236       aResMap[ aRefObjectName ].Append( anObject );
237     }
238   }
239
240   return aResMap;
241 }
242
243 QDockWidget* HYDROGUI_Tool::WindowDock( QWidget* wid )
244 {
245   if ( !wid )
246     return 0;
247
248   QDockWidget* dock = 0;
249   QWidget* w = wid->parentWidget();
250   while ( w && !dock )
251   {
252     dock = ::qobject_cast<QDockWidget*>( w );
253     w = w->parentWidget();
254   }
255   return dock;
256 }
257
258 QStringList HYDROGUI_Tool::FindExistingObjectsNames( const Handle(HYDROData_Document)& theDoc, 
259                                                      const ObjectKind theObjectKind,
260                                                      bool isCheckValidProfile )
261 {
262   QStringList aNames;
263
264   HYDROData_Iterator anIter( theDoc, theObjectKind );
265   for ( ; anIter.More(); anIter.Next() ) {
266     Handle(HYDROData_Entity) anObject = anIter.Current();
267
268     bool isOK = !anObject.IsNull();
269     
270     if( isOK && isCheckValidProfile )
271     {
272       Handle(HYDROData_Profile) aProfile = Handle(HYDROData_Profile)::DownCast( anObject );
273       if( !aProfile.IsNull() && !aProfile->IsValid() )
274         isOK = false;
275     }
276       
277     if( isOK )
278       aNames.append( anObject->GetName() );
279   }
280
281   return aNames;
282 }
283
284 QString HYDROGUI_Tool::GetCoordinateString( const double theNumber, bool isInLocale )
285 {
286   if( isInLocale )
287   {
288     static QLocale aLocale( QLocale::English, QLocale::France );
289     return aLocale.toString( theNumber, 'f', 2 );
290   }
291   else
292     return QString::number( theNumber, 'f', 2 );
293 }
294
295 Handle(Image_PixMap) HYDROGUI_Tool::Pixmap( const QImage& theImage )
296 {
297     Handle(Image_PixMap) pix;
298     if ( theImage.isNull() || theImage.format() == QImage::Format_Invalid )
299         return pix;
300
301     Handle(Image_PixMap) tmpPix = new Image_PixMap();
302     tmpPix->SetTopDown( false );
303     QImage anImage = theImage.mirrored();
304     if ( !anImage.hasAlphaChannel() && anImage.allGray() )
305     {
306         tmpPix->InitTrash( Image_PixMap::ImgGray, anImage.width(), anImage.height(), anImage.width() );
307         for ( int r = 0; r < anImage.height(); r++ )
308         {
309             Standard_Byte* aRowData = tmpPix->ChangeRow( anImage.height() - r - 1 );
310             for  ( int p = 0; p < anImage.width(); p++ )
311                 aRowData[p] = qRed( anImage.pixel( p, r ) );
312         }
313     }
314     else
315     {
316         Image_PixMap::ImgFormat aFormat;
317         if ( anImage.hasAlphaChannel() )
318         {
319           if ( anImage.format() != QImage::Format_RGBA8888 )
320                 anImage = anImage.convertToFormat( QImage::Format_RGBA8888 );
321             aFormat = Image_PixMap::ImgRGBA;
322         }
323         else
324         {
325             if ( anImage.format() != QImage::Format_RGB888 )
326                 anImage = anImage.convertToFormat( QImage::Format_RGB888 );
327             aFormat = Image_PixMap::ImgRGB;
328         }
329
330         tmpPix->InitWrapper( aFormat, (Standard_Byte*)anImage.bits(), anImage.width(), anImage.height(), anImage.bytesPerLine() );
331     }
332
333     if ( !tmpPix.IsNull() )
334     {
335         pix = new Image_PixMap();
336         pix->InitCopy( *tmpPix.operator->() );
337         pix->SetTopDown( tmpPix->IsTopDown() );
338     }
339
340     return pix;
341 }
342
343 void HYDROGUI_Tool::setResizeMode( QHeaderView* theHeader, int theSection, int theMode )
344 {
345 #if QT_VERSION < QT_VERSION_CHECK(5, 0, 0)
346   theHeader->setResizeMode( theSection, (QHeaderView::ResizeMode)theMode );
347 #else
348   theHeader->setSectionResizeMode( theSection, (QHeaderView::ResizeMode)theMode );
349 #endif
350 }
351
352 void HYDROGUI_Tool::setResizeMode( QHeaderView* theHeader, int theMode )
353 {
354 #if QT_VERSION < QT_VERSION_CHECK(5, 0, 0)
355   theHeader->setResizeMode( (QHeaderView::ResizeMode)theMode );
356 #else
357   theHeader->setSectionResizeMode( (QHeaderView::ResizeMode)theMode );
358 #endif
359 }