Salome HOME
Copyrights update
[modules/gui.git] / src / SUIT / SUIT_Tools.cxx
1 // Copyright (C) 2005  OPEN CASCADE, CEA/DEN, EDF R&D, PRINCIPIA R&D
2 // 
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either 
6 // version 2.1 of the License.
7 // 
8 // This library is distributed in the hope that it will be useful 
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of 
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU 
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public  
14 // License along with this library; if not, write to the Free Software 
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 //
17 // See http://www.salome-platform.org/
18 //
19 #include "SUIT_Tools.h"
20
21 #include <qdir.h>
22
23 #include <stdio.h>
24 #include <stdarg.h>
25
26 /*!
27   Traces output to log-file.
28   If log is NULL, 'Salome_trace' file is created in temp directory.
29   Log file is written in 'append' mode.
30 */
31 void SUIT_Tools::trace( const char* lpszLog, const char* lpszFormat, ... )
32 {
33   QString tmpPath = tmpDir();
34   if ( !tmpPath.isEmpty() )
35     tmpPath += QDir::separator();
36
37   tmpPath += QString( "Salome_trace" );
38
39   FILE* pStream;
40   pStream = fopen( lpszLog ? lpszLog : tmpPath.latin1(), "a" );
41   if ( pStream ) 
42   {     
43     va_list argptr;
44     va_start( argptr, lpszFormat );
45     fprintf( pStream, "- Trace %s [%d] : %s", __FILE__, __LINE__, lpszFormat );
46     va_end( argptr );
47
48     fclose( pStream );
49   }
50 }
51
52 /*! 
53     Creates a rect with TopLeft = ( min(x1,x2), min(y1,y2) )
54     and BottomRight = ( TopLeft + (x2-x1)(y2-y1) )    
55 */      
56 QRect SUIT_Tools::makeRect( const int x1, const int y1, const int x2, const int y2 )
57 {  
58   return QRect( QMIN( x1, x2 ), QMIN( y1, y2 ), QABS( x2 - x1 ), QABS( y2 - y1 ) );
59 }
60
61 /*!
62   Creates font from string description
63 */
64 QFont SUIT_Tools::stringToFont( const QString& fontDescription )
65 {
66   QFont font;
67   if ( fontDescription.stripWhiteSpace().isEmpty() || !font.fromString( fontDescription ) )
68     font = QFont( "Courier", 11 );
69   return font;
70 }
71
72 /*!
73   Creates font's string description
74 */
75 QString SUIT_Tools::fontToString( const QFont& font )
76 {
77   return font.toString();
78 }
79
80 /*!
81   Center widget 'src' relative to widget 'ref'.
82 */
83 void SUIT_Tools::centerWidget( QWidget* src, const QWidget* ref )
84 {
85   SUIT_Tools::alignWidget( src, ref, Qt::AlignCenter );
86 }