Salome HOME
Initial version
[modules/gui.git] / src / SUIT / SUIT_Tools.cxx
1 #include "SUIT_Tools.h"
2
3 #include <qdir.h>
4
5 #include <stdio.h>
6 #include <stdarg.h>
7
8 /*!
9   Traces output to log-file.
10   If log is NULL, 'Salome_trace' file is created in temp directory.
11   Log file is written in 'append' mode.
12 */
13 void SUIT_Tools::trace( const char* lpszLog, const char* lpszFormat, ... )
14 {
15   QString tmpPath = tmpDir();
16   if ( !tmpPath.isEmpty() )
17     tmpPath += QDir::separator();
18
19   tmpPath += QString( "Salome_trace" );
20
21   FILE* pStream;
22   pStream = fopen( lpszLog ? lpszLog : tmpPath.latin1(), "a" );
23   if ( pStream ) 
24   {     
25     va_list argptr;
26     va_start( argptr, lpszFormat );
27     fprintf( pStream, "- Trace %s [%d] : %s", __FILE__, __LINE__, lpszFormat );
28     va_end( argptr );
29
30     fclose( pStream );
31   }
32 }
33
34 /*! 
35     Creates a rect with TopLeft = ( min(x1,x2), min(y1,y2) )
36     and BottomRight = ( TopLeft + (x2-x1)(y2-y1) )    
37 */      
38 QRect SUIT_Tools::makeRect( const int x1, const int y1, const int x2, const int y2 )
39 {  
40   QRect aRect;
41   aRect.setRect( QMIN( x1, x2 ), QMIN( y1, y2 ), QABS( x2 - x1 ), QABS( y2 - y1 ) );
42   return aRect;
43 }
44
45 /*!
46   Creates font from string description
47 */
48 QFont SUIT_Tools::stringToFont( const QString& fontDescription )
49 {
50   QFont font;
51   if ( fontDescription.stripWhiteSpace().isEmpty() || !font.fromString( fontDescription ) )
52     font = QFont( "Courier", 11 );
53   return font;
54 }
55
56 /*!
57   Creates font's string description
58 */
59 QString SUIT_Tools::fontToString( const QFont& font )
60 {
61   return font.toString();
62 }
63
64 void SUIT_Tools::centerWidget( QWidget* src, const QWidget* ref )
65 {
66   SUIT_Tools::alignWidget(src, ref, Qt::AlignCenter);
67 }
68
69 /*!
70   Aligns widget 'w' as refered to widget 'ref' [ static ]
71 */
72 void SUIT_Tools::alignWidget ( QWidget* src, const QWidget* ref, int alignFlags )
73 {
74   if ( !src || !ref || !alignFlags ) return;
75
76   QPoint srcOri = src->mapToGlobal( QPoint( 1, 1 ) );
77   QPoint refOri = ref->mapToGlobal( QPoint( 1, 1 ) );
78
79   int x = srcOri.x(), y = srcOri.y();
80   int refWidth = ref->width(), refHei = ref->height();
81   int srcWidth = src->width(), srcHei = src->height();
82   if ( srcWidth <= 1 )
83     srcWidth = src->sizeHint().width();
84   if ( srcHei <= 1 )
85     srcHei = src->sizeHint().height();
86
87   if ( alignFlags & AlignLeft )
88     x = refOri.x();
89   if ( alignFlags & AlignRight )
90     x = refOri.x() + refWidth - srcWidth;
91   if ( alignFlags & AlignTop )
92     y = refOri.y();
93   if ( alignFlags & AlignBottom )
94     y = refOri.y() + refHei - srcHei;
95   if ( alignFlags & AlignHCenter )
96     x = refOri.x() + ( refWidth - srcWidth ) / 2;
97   if ( alignFlags & AlignVCenter )
98     y = refOri.y() + ( refHei - srcHei ) / 2;
99
100   if ( src->parentWidget() &&        /* we move a widget inside its parent */
101       !src->inherits( "QDialog" ))   /* dialogs use global coordinates  */
102     {
103       QPoint pos = src->parentWidget()->mapFromGlobal( QPoint(x,y) );
104       x = pos.x(); y = pos.y();
105     }
106 #ifdef WNT
107   x -= 4;                             /* - frame border width ( approx. ) */
108   y -= 30;                            /* - caption height ( approx. ) */
109 #endif
110
111   src->move( x, y );
112 }