Salome HOME
Update from BR_V5_DEV 13Feb2009
[modules/gui.git] / src / SUIT / SUIT_Tools.cxx
1 //  Copyright (C) 2007-2008  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 #include "SUIT_Tools.h"
23
24 #include <QDir>
25
26 #include <stdio.h>
27 #include <stdarg.h>
28
29 /*!
30   Traces output to log-file.
31   If log is NULL, 'Salome_trace' file is created in temp directory.
32   Log file is written in 'append' mode.
33 */
34 void SUIT_Tools::trace( const char* lpszLog, const char* lpszFormat, ... )
35 {
36   QString tmpPath = tmpDir();
37   if ( !tmpPath.isEmpty() )
38     tmpPath += QDir::separator();
39
40   tmpPath += QString( "Salome_trace" );
41
42   FILE* pStream;
43   pStream = fopen( lpszLog ? lpszLog : (const char*)tmpPath.toLatin1(), "a" );
44   if ( pStream ) 
45   {     
46     va_list argptr;
47     va_start( argptr, lpszFormat );
48     fprintf( pStream, "- Trace %s [%d] : %s", __FILE__, __LINE__, lpszFormat );
49     va_end( argptr );
50
51     fclose( pStream );
52   }
53 }
54
55 /*! 
56     Creates a rect with TopLeft = ( min(x1,x2), min(y1,y2) )
57     and BottomRight = ( TopLeft + (x2-x1)(y2-y1) )    
58 */      
59 QRect SUIT_Tools::makeRect( const int x1, const int y1, const int x2, const int y2 )
60 {  
61   return QRect( qMin( x1, x2 ), qMin( y1, y2 ), qAbs( x2 - x1 ), qAbs( y2 - y1 ) );
62 }
63
64 /*!
65   Creates font from string description
66 */
67 QFont SUIT_Tools::stringToFont( const QString& fontDescription )
68 {
69   QFont font;
70   if ( fontDescription.trimmed().isEmpty() || !font.fromString( fontDescription ) )
71     font = QFont( "Courier", 11 );
72   return font;
73 }
74
75 /*!
76   Creates font's string description
77 */
78 QString SUIT_Tools::fontToString( const QFont& font )
79 {
80   return font.toString();
81 }
82
83 /*!
84   Center widget 'src' relative to widget 'ref'.
85 */
86 void SUIT_Tools::centerWidget( QWidget* src, const QWidget* ref )
87 {
88   SUIT_Tools::alignWidget( src, ref, Qt::AlignCenter );
89 }