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>
35 #include <QHeaderView>
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
41 // #define DEB_GROUPS 1
43 QString HYDROGUI_Tool::ToQString( const TCollection_AsciiString& src )
45 #ifdef USE_LATIN1_ENCODING
46 QTextCodec* codec = QTextCodec::codecForName( "latin1" ); // alias for ISO-8859-1
48 QTextCodec* codec = QTextCodec::codecForLocale();
52 res = codec ? codec->toUnicode( (char*)src.ToCString(), src.Length() ) :
53 QString( (char*)src.ToCString() );
57 QString HYDROGUI_Tool::ToQString( const TCollection_ExtendedString& src )
59 return QString( (QChar*)src.ToExtString(), src.Length() );
62 QString HYDROGUI_Tool::ToQString( const Handle(TCollection_HAsciiString)& src )
67 return ToQString( src->String() );
70 QString HYDROGUI_Tool::ToQString( const Handle(TCollection_HExtendedString)& src )
74 return ToQString( src->String() );
77 TCollection_AsciiString HYDROGUI_Tool::ToAsciiString( const QString& src )
79 TCollection_AsciiString res;
82 #ifdef USE_LATIN1_ENCODING
83 QTextCodec* codec = QTextCodec::codecForName( "latin1" ); // alias for ISO-8859-1
85 QTextCodec* codec = QTextCodec::codecForLocale();
89 QByteArray str = codec->fromUnicode( src );
90 res = TCollection_AsciiString( (Standard_CString)str.constData() );
93 res = TCollection_AsciiString( src.toLatin1().data() );
98 TCollection_ExtendedString HYDROGUI_Tool::ToExtString( const QString& src )
101 return TCollection_ExtendedString();
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;
108 TCollection_ExtendedString trg( extStr );
113 Handle(TCollection_HAsciiString) HYDROGUI_Tool::ToHAsciiString( const QString& src )
115 return new TCollection_HAsciiString( ToAsciiString( src ) );
118 Handle(TCollection_HExtendedString) HYDROGUI_Tool::ToHExtString( const QString& src )
120 return new TCollection_HExtendedString( ToExtString( src ) );
123 QString HYDROGUI_Tool::GetTempDir( const bool theToCreate )
127 char* tmpdir = getenv ( "HYDRO_TMP_DIR" );
130 // try to create folder if it does not exist
131 QFileInfo fi( tmpdir );
132 if ( !fi.exists() && theToCreate )
134 if ( QDir().mkdir( tmpdir ) )
135 QFile::setPermissions( tmpdir, (QFile::Permissions)0x4FFFF );
136 QFileInfo fi( tmpdir );
137 if ( !fi.exists() || !fi.isWritable() )
142 tmpdir = getenv ( "TEMP" );
144 tmpdir = getenv ( "TMP" );
150 tmpdir = strdup( "/tmp" );
155 QFileInfo fi( aRes );
156 if ( !fi.exists() || !fi.isWritable() )
157 aRes = QString::null;
162 void HYDROGUI_Tool::GetObjectReferences( const Handle(HYDROData_Entity)& theObj,
163 HYDROData_SequenceOfObjects& theRefObjects,
164 QStringList& theRefNames )
166 if( theObj.IsNull() )
169 HYDROData_SequenceOfObjects anAllRefObjects = theObj->GetAllReferenceObjects();
170 theRefObjects.Append( anAllRefObjects );
172 for( int i = 1, n = anAllRefObjects.Length(); i <= n; ++i )
174 Handle(HYDROData_Entity) aRefObj = theRefObjects.Value( i );
175 if( aRefObj.IsNull() || aRefObj->IsRemoved() )
178 QString aRefObjectName = aRefObj->GetName();
179 if( theRefNames.contains( aRefObjectName ) )
182 theRefObjects.Append( aRefObj );
183 theRefNames.append( aRefObjectName );
185 GetObjectReferences( aRefObj, theRefObjects, theRefNames );
189 HYDROData_SequenceOfObjects HYDROGUI_Tool::GetObjectBackReferences( const Handle(HYDROData_Entity)& theObj )
191 if( theObj.IsNull() )
192 return HYDROData_SequenceOfObjects();
194 QString anObjName = theObj->GetName();
196 Handle(HYDROData_Document) aDoc = HYDROData_Document::Document( theObj->Label() );
197 QMap<QString,HYDROData_SequenceOfObjects> aMapOfBackRefs =
198 GetObjectsBackReferences( aDoc, QStringList() << anObjName );
200 return aMapOfBackRefs[ anObjName ];
203 QMap<QString,HYDROData_SequenceOfObjects> HYDROGUI_Tool::GetObjectsBackReferences(
204 const Handle(HYDROData_Document)& theDocument, const QStringList& theObjectNames )
206 QMap<QString,HYDROData_SequenceOfObjects> aResMap;
208 if( theObjectNames.isEmpty() )
211 if( theDocument.IsNull() )
214 HYDROData_Iterator anIterator( theDocument );
215 for( ; anIterator.More(); anIterator.Next() )
217 Handle(HYDROData_Entity) anObject = anIterator.Current();
218 if( anObject.IsNull() || anObject->IsRemoved() )
221 QString anObjectName = anObject->GetName();
222 if ( theObjectNames.contains( anObjectName ) )
225 HYDROData_SequenceOfObjects aRefObjects = anObject->GetAllReferenceObjects();
226 for ( int i = 1, n = aRefObjects.Length(); i <= n; ++i )
228 Handle(HYDROData_Entity) aRefObject = aRefObjects.Value( i );
229 if( aRefObject.IsNull() || aRefObject->IsRemoved() )
232 QString aRefObjectName = aRefObject->GetName();
233 if ( !theObjectNames.contains( aRefObjectName ) )
236 aResMap[ aRefObjectName ].Append( anObject );
243 QDockWidget* HYDROGUI_Tool::WindowDock( QWidget* wid )
248 QDockWidget* dock = 0;
249 QWidget* w = wid->parentWidget();
252 dock = ::qobject_cast<QDockWidget*>( w );
253 w = w->parentWidget();
258 QStringList HYDROGUI_Tool::FindExistingObjectsNames( const Handle(HYDROData_Document)& theDoc,
259 const ObjectKind theObjectKind,
260 bool isCheckValidProfile )
264 HYDROData_Iterator anIter( theDoc, theObjectKind );
265 for ( ; anIter.More(); anIter.Next() ) {
266 Handle(HYDROData_Entity) anObject = anIter.Current();
268 bool isOK = !anObject.IsNull();
270 if( isOK && isCheckValidProfile )
272 Handle(HYDROData_Profile) aProfile = Handle(HYDROData_Profile)::DownCast( anObject );
273 if( !aProfile.IsNull() && !aProfile->IsValid() )
278 aNames.append( anObject->GetName() );
284 QString HYDROGUI_Tool::GetCoordinateString( const double theNumber, bool isInLocale )
288 static QLocale aLocale( QLocale::English, QLocale::France );
289 return aLocale.toString( theNumber, 'f', 2 );
292 return QString::number( theNumber, 'f', 2 );
295 Handle(Image_PixMap) HYDROGUI_Tool::Pixmap( const QImage& theImage )
297 Handle(Image_PixMap) pix;
298 if ( theImage.isNull() || theImage.format() == QImage::Format_Invalid )
301 Handle(Image_PixMap) tmpPix = new Image_PixMap();
302 tmpPix->SetTopDown( false );
303 QImage anImage = theImage.mirrored();
304 if ( !anImage.hasAlphaChannel() && anImage.allGray() )
306 tmpPix->InitTrash( Image_PixMap::ImgGray, anImage.width(), anImage.height(), anImage.width() );
307 for ( int r = 0; r < anImage.height(); r++ )
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 ) );
316 Image_PixMap::ImgFormat aFormat;
317 if ( anImage.hasAlphaChannel() )
319 if ( anImage.format() != QImage::Format_RGBA8888 )
320 anImage = anImage.convertToFormat( QImage::Format_RGBA8888 );
321 aFormat = Image_PixMap::ImgRGBA;
325 if ( anImage.format() != QImage::Format_RGB888 )
326 anImage = anImage.convertToFormat( QImage::Format_RGB888 );
327 aFormat = Image_PixMap::ImgRGB;
330 tmpPix->InitWrapper( aFormat, (Standard_Byte*)anImage.bits(), anImage.width(), anImage.height(), anImage.bytesPerLine() );
333 if ( !tmpPix.IsNull() )
335 pix = new Image_PixMap();
336 pix->InitCopy( *tmpPix.operator->() );
337 pix->SetTopDown( tmpPix->IsTopDown() );
343 void HYDROGUI_Tool::setResizeMode( QHeaderView* theHeader, int theSection, int theMode )
345 #if QT_VERSION < QT_VERSION_CHECK(5, 0, 0)
346 theHeader->setResizeMode( theSection, (QHeaderView::ResizeMode)theMode );
348 theHeader->setSectionResizeMode( theSection, (QHeaderView::ResizeMode)theMode );
352 void HYDROGUI_Tool::setResizeMode( QHeaderView* theHeader, int theMode )
354 #if QT_VERSION < QT_VERSION_CHECK(5, 0, 0)
355 theHeader->setResizeMode( (QHeaderView::ResizeMode)theMode );
357 theHeader->setSectionResizeMode( (QHeaderView::ResizeMode)theMode );