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.
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.
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
16 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
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>
28 #include <QDockWidget>
32 #include <QStringList>
36 // Definition of this id allows to use 'latin1' (Qt alias for 'ISO-8859-1')
37 // encoding instead of default 'System'
38 #define USE_LATIN1_ENCODING
40 // #define DEB_GROUPS 1
42 QString HYDROGUI_Tool::ToQString( const TCollection_AsciiString& src )
44 #ifdef USE_LATIN1_ENCODING
45 QTextCodec* codec = QTextCodec::codecForName( "latin1" ); // alias for ISO-8859-1
47 QTextCodec* codec = QTextCodec::codecForLocale();
51 res = codec ? codec->toUnicode( (char*)src.ToCString(), src.Length() ) :
52 QString( (char*)src.ToCString() );
56 QString HYDROGUI_Tool::ToQString( const TCollection_ExtendedString& src )
58 return QString( (QChar*)src.ToExtString(), src.Length() );
61 QString HYDROGUI_Tool::ToQString( const Handle(TCollection_HAsciiString)& src )
66 return ToQString( src->String() );
69 QString HYDROGUI_Tool::ToQString( const Handle(TCollection_HExtendedString)& src )
73 return ToQString( src->String() );
76 TCollection_AsciiString HYDROGUI_Tool::ToAsciiString( const QString& src )
78 TCollection_AsciiString res;
81 #ifdef USE_LATIN1_ENCODING
82 QTextCodec* codec = QTextCodec::codecForName( "latin1" ); // alias for ISO-8859-1
84 QTextCodec* codec = QTextCodec::codecForLocale();
88 QByteArray str = codec->fromUnicode( src );
89 res = TCollection_AsciiString( (Standard_CString)str.constData() );
92 res = TCollection_AsciiString( src.toLatin1().data() );
97 TCollection_ExtendedString HYDROGUI_Tool::ToExtString( const QString& src )
100 return TCollection_ExtendedString();
102 Standard_Integer len = src.length();
103 Standard_ExtString extStr = new Standard_ExtCharacter[ ( len + 1 ) * 2 ];
104 memcpy( (void*)extStr, src.unicode(), len * 2 );
105 ((short*)extStr)[ len ] = 0;
107 TCollection_ExtendedString trg( extStr );
112 Handle(TCollection_HAsciiString) HYDROGUI_Tool::ToHAsciiString( const QString& src )
114 return new TCollection_HAsciiString( ToAsciiString( src ) );
117 Handle(TCollection_HExtendedString) HYDROGUI_Tool::ToHExtString( const QString& src )
119 return new TCollection_HExtendedString( ToExtString( src ) );
122 QString HYDROGUI_Tool::GetTempDir( const bool theToCreate )
126 char* tmpdir = getenv ( "HYDRO_TMP_DIR" );
129 // try to create folder if it does not exist
130 QFileInfo fi( tmpdir );
131 if ( !fi.exists() && theToCreate )
133 if ( QDir().mkdir( tmpdir ) )
134 QFile::setPermissions( tmpdir, (QFile::Permissions)0x4FFFF );
135 QFileInfo fi( tmpdir );
136 if ( !fi.exists() || !fi.isWritable() )
141 tmpdir = getenv ( "TEMP" );
143 tmpdir = getenv ( "TMP" );
149 tmpdir = strdup( "/tmp" );
154 QFileInfo fi( aRes );
155 if ( !fi.exists() || !fi.isWritable() )
156 aRes = QString::null;
161 void HYDROGUI_Tool::GetObjectReferences( const Handle(HYDROData_Entity)& theObj,
162 HYDROData_SequenceOfObjects& theRefObjects,
163 QStringList& theRefNames )
165 if( theObj.IsNull() )
168 HYDROData_SequenceOfObjects anAllRefObjects = theObj->GetAllReferenceObjects();
169 theRefObjects.Append( anAllRefObjects );
171 for( int i = 1, n = anAllRefObjects.Length(); i <= n; ++i )
173 Handle(HYDROData_Entity) aRefObj = theRefObjects.Value( i );
174 if( aRefObj.IsNull() || aRefObj->IsRemoved() )
177 QString aRefObjectName = aRefObj->GetName();
178 if( theRefNames.contains( aRefObjectName ) )
181 theRefObjects.Append( aRefObj );
182 theRefNames.append( aRefObjectName );
184 GetObjectReferences( aRefObj, theRefObjects, theRefNames );
188 HYDROData_SequenceOfObjects HYDROGUI_Tool::GetObjectBackReferences( const Handle(HYDROData_Entity)& theObj )
190 if( theObj.IsNull() )
191 return HYDROData_SequenceOfObjects();
193 QString anObjName = theObj->GetName();
195 Handle(HYDROData_Document) aDoc = HYDROData_Document::Document( theObj->Label() );
196 QMap<QString,HYDROData_SequenceOfObjects> aMapOfBackRefs =
197 GetObjectsBackReferences( aDoc, QStringList() << anObjName );
199 return aMapOfBackRefs[ anObjName ];
202 QMap<QString,HYDROData_SequenceOfObjects> HYDROGUI_Tool::GetObjectsBackReferences(
203 const Handle(HYDROData_Document)& theDocument, const QStringList& theObjectNames )
205 QMap<QString,HYDROData_SequenceOfObjects> aResMap;
207 if( theObjectNames.isEmpty() )
210 if( theDocument.IsNull() )
213 HYDROData_Iterator anIterator( theDocument );
214 for( ; anIterator.More(); anIterator.Next() )
216 Handle(HYDROData_Entity) anObject = anIterator.Current();
217 if( anObject.IsNull() || anObject->IsRemoved() )
220 QString anObjectName = anObject->GetName();
221 if ( theObjectNames.contains( anObjectName ) )
224 HYDROData_SequenceOfObjects aRefObjects = anObject->GetAllReferenceObjects();
225 for ( int i = 1, n = aRefObjects.Length(); i <= n; ++i )
227 Handle(HYDROData_Entity) aRefObject = aRefObjects.Value( i );
228 if( aRefObject.IsNull() || aRefObject->IsRemoved() )
231 QString aRefObjectName = aRefObject->GetName();
232 if ( !theObjectNames.contains( aRefObjectName ) )
235 aResMap[ aRefObjectName ].Append( anObject );
242 QDockWidget* HYDROGUI_Tool::WindowDock( QWidget* wid )
247 QDockWidget* dock = 0;
248 QWidget* w = wid->parentWidget();
251 dock = ::qobject_cast<QDockWidget*>( w );
252 w = w->parentWidget();
257 QStringList HYDROGUI_Tool::FindExistingObjectsNames( const Handle(HYDROData_Document)& theDoc,
258 const ObjectKind theObjectKind,
259 bool isCheckValidProfile )
263 HYDROData_Iterator anIter( theDoc, theObjectKind );
264 for ( ; anIter.More(); anIter.Next() ) {
265 Handle(HYDROData_Entity) anObject = anIter.Current();
267 bool isOK = !anObject.IsNull();
269 if( isOK && isCheckValidProfile )
271 Handle(HYDROData_Profile) aProfile = Handle(HYDROData_Profile)::DownCast( anObject );
272 if( !aProfile.IsNull() && !aProfile->IsValid() )
277 aNames.append( anObject->GetName() );
283 QString HYDROGUI_Tool::GetCoordinateString( const double theNumber, bool isInLocale )
287 static QLocale aLocale( QLocale::English, QLocale::France );
288 return aLocale.toString( theNumber, 'f', 2 );
291 return QString::number( theNumber, 'f', 2 );
294 Handle(Image_PixMap) HYDROGUI_Tool::Pixmap( const QImage& theImage )
296 Handle(Image_PixMap) pix;
297 if ( theImage.isNull() || theImage.format() == QImage::Format_Invalid )
300 Handle(Image_PixMap) tmpPix = new Image_PixMap();
301 tmpPix->SetTopDown( false );
302 QImage anImage = theImage.mirrored();
303 if ( !anImage.hasAlphaChannel() && anImage.allGray() )
305 tmpPix->InitTrash( Image_PixMap::ImgGray, anImage.width(), anImage.height(), anImage.width() );
306 for ( int r = 0; r < anImage.height(); r++ )
308 Standard_Byte* aRowData = tmpPix->ChangeRow( anImage.height() - r - 1 );
309 for ( int p = 0; p < anImage.width(); p++ )
310 aRowData[p] = qRed( anImage.pixel( p, r ) );
315 Image_PixMap::ImgFormat aFormat;
316 if ( anImage.hasAlphaChannel() )
318 if ( anImage.format() != QImage::Format_RGBA8888 )
319 anImage = anImage.convertToFormat( QImage::Format_RGBA8888 );
320 aFormat = Image_PixMap::ImgRGBA;
324 if ( anImage.format() != QImage::Format_RGB888 )
325 anImage = anImage.convertToFormat( QImage::Format_RGB888 );
326 aFormat = Image_PixMap::ImgRGB;
329 tmpPix->InitWrapper( aFormat, (Standard_Byte*)anImage.bits(), anImage.width(), anImage.height(), anImage.bytesPerLine() );
332 if ( !tmpPix.IsNull() )
334 pix = new Image_PixMap();
335 pix->InitCopy( *tmpPix.operator->() );
336 pix->SetTopDown( tmpPix->IsTopDown() );