Salome HOME
Initialisation de la base KERNEL avec la version operationnelle de KERNEL_SRC issue...
[modules/kernel.git] / src / SALOMEGUI / QAD_Tools.cxx
1 using namespace std;
2 //  File      : QAD_Tools.cxx
3 //  Created   : UI team, 22.09.00
4 //  Descr     : Helpful functions for QAD
5 //  Author    : UI team
6 //  Project   : SALOME
7 //  Module    : SALOMEGUI
8 //  Copyright : Open CASCADE
9 //  $Header$
10
11
12 /*!
13   \class QAD_Tools QAD_Tools.h
14   \brief Helpful functions for QAD.
15 */
16
17 #include "QAD_Tools.h"
18 #include "utilities.h"
19
20 // QT Inlcude
21 #include <qwidget.h>
22 #include <qfileinfo.h>
23 #include <stdlib.h>
24 #include <stdio.h>
25 #include <stdarg.h>
26 #include <string.h>
27 #include <ctype.h>
28 #include <qstringlist.h>
29
30 static char* tempName()
31 {
32   static char tempDir[512];
33   strcpy ( tempDir, getenv ("tmp") );
34
35 #if defined WNT
36   if ( tempDir[ strlen(tempDir)-1 ] != '\\' )
37     strcat ( tempDir,"\\");
38 #else
39   if ( tempDir[ strlen(tempDir)-1 ] != '/' )
40     strcat ( tempDir,"/");
41 #endif        
42   strcat ( tempDir,"Salome_trace");        
43   return tempDir;
44 }
45
46 /*!
47     Returns max 'int' value [ static ]
48 */
49 inline int QAD_Tools::getMax(int v1, int v2)
50
51   return v1 >= v2 ? v1 : v2; 
52 }
53
54 /*!
55     Returns min 'int' value [ static ]
56 */
57 inline int QAD_Tools::getMin(int v1, int v2)
58
59   return v1 <= v2 ? v1 : v2;
60 }
61
62 /*! 
63     [ static ]
64     Creates a rect with TopLeft = ( min(x1,x2), min(y1,y2) )
65     and BottomRight = ( TopLeft + (x2-x1)(y2-y1) )    
66 */      
67 QRect QAD_Tools::makeRect(int x1, int y1, int x2, int y2)
68 {  
69   QRect aRect;
70   aRect.setRect( getMin(x1, x2), getMin(y1, y2), abs(x2-x1), abs(y2-y1));
71   return aRect;
72 }
73
74 /*!
75     Traces output to log-file. [ static ] 
76     If log is NULL, 'Salome_trace' file is created in temp directory.
77     Log file is written in 'append' mode.
78 */
79 void  QAD_Tools::trace (const char* lpszLog, const char* lpszFormat, ... )
80 {
81   static  char* lpszTmp = tempName();
82
83   FILE* pStream;
84   pStream = fopen ( lpszLog ? lpszLog : lpszTmp, "a" );
85   if ( pStream ) 
86     {   
87       va_list argptr;
88       va_start(argptr, lpszFormat);                             
89       //NRI      vfprintf ( pStream, lpszFormat, argptr);       
90       fprintf ( pStream, "- Trace %s [%d] : %s", __FILE__, __LINE__, lpszFormat);       
91       va_end(argptr);   
92       fclose ( pStream );
93     }
94 }
95
96 /*!
97     Parses the path to select the dir name only [ static ].
98     Returns NULL if the path is empty (null).
99     NB: Unix-style slashes are assumed in 'path'
100 */
101 QString QAD_Tools::getDirFromPath ( const QString& path, bool absPath )
102 {
103   return QFileInfo( path ).dirPath( absPath );
104 }
105
106
107 /*!
108     Parses the path to select the file name with or without extension [ static ].
109 */
110 QString QAD_Tools::getFileNameFromPath( const QString& path, 
111                                         bool withExtension )
112 {
113   if ( withExtension )
114     return QFileInfo( path ).fileName();    
115   else 
116     return QFileInfo( path ).baseName();
117 }
118
119 /*!
120     Parses the path to select the file extension [ static ].
121 */
122 QString QAD_Tools::getFileExtensionFromPath( const QString& path )
123 {    
124   return QFileInfo( path ).extension(false);
125 }
126
127 /*!
128     Adds a slash to the end of 'path' if it is not already there [ static ]
129 */
130 QString QAD_Tools::addSlash( const QString& path )
131 {
132   if (!path.isNull()) {
133 #ifdef WNT
134     QChar slash ('\\');
135 #else
136     QChar slash ('/');
137 #endif
138     if ( path.at(path.length()-1) != slash )
139       return path + slash;
140   }
141   return path;
142 }
143
144 /*!
145     Converts slashes from unix-like to win-like [ static ]
146     Returns null string if 'path' is empty or null. 
147 */
148 QString QAD_Tools::unix2win( const QString& path ) 
149 {
150   QString ret = path;
151 #ifdef WNT
152   int pos;
153   QChar winSlash ('\\'), unixSlash('/');    
154   for ( int start = 0; 
155        (pos = path.find( unixSlash, start, false )) >= 0; 
156        start = pos + 1 ) 
157     {        
158       ret.replace( pos, 1, &winSlash, 1 );
159     }
160 #endif
161   return ret;
162 }
163 /*!
164     Centers widget 'w' as refered to widget 'ref' [ static ]
165 */
166 void QAD_Tools::centerWidget( QWidget* src, const QWidget* ref )
167 {
168   QAD_Tools::alignWidget(src, ref, Qt::AlignCenter);
169 }
170
171 /*!
172   Aligns widget 'w' as refered to widget 'ref' [ static ] 
173 */
174 void QAD_Tools::alignWidget ( QWidget* src, const QWidget* ref, int alignFlags )
175 {
176   if ( !src || !ref || !alignFlags ) return;
177
178   QPoint srcOri = src->mapToGlobal( QPoint( 1, 1 ) );
179   QPoint refOri = ref->mapToGlobal( QPoint( 1, 1 ) );
180   
181   int x = srcOri.x(), y = srcOri.y();
182   int refWidth = ref->width(), refHei = ref->height();
183   int srcWidth = src->width(), srcHei = src->height();       
184   if ( srcWidth <= 1 ) 
185     srcWidth = src->sizeHint().width();
186   if ( srcHei <= 1 ) 
187     srcHei = src->sizeHint().height();
188   
189   if ( alignFlags & AlignLeft )         
190     x = refOri.x();
191   if ( alignFlags & AlignRight )
192     x = refOri.x() + refWidth - srcWidth;    
193   if ( alignFlags & AlignTop )
194     y = refOri.y();
195   if ( alignFlags & AlignBottom )
196     y = refOri.y() + refHei - srcHei;
197   if ( alignFlags & AlignHCenter ) 
198     x = refOri.x() + ( refWidth - srcWidth ) / 2;    
199   if ( alignFlags & AlignVCenter ) 
200     y = refOri.y() + ( refHei - srcHei ) / 2;
201   
202   if ( src->parentWidget() &&        /* we move a widget inside its parent */
203       !src->inherits( "QDialog" ))   /* dialogs use global coordinates  */
204     {
205       QPoint pos = src->parentWidget()->mapFromGlobal( QPoint(x,y) ); 
206       x = pos.x(); y = pos.y();
207     }
208 #ifdef WNT
209   x -= 4;                             /* - frame border width ( approx. ) */
210   y -= 30;                            /* - caption height ( approx. ) */
211 #endif
212
213   src->move( x, y );
214 }
215
216 /*!
217   Converts TCollection_ExtendedString to QString
218 */
219 QString QAD_Tools::toQString( const TCollection_ExtendedString& extString)
220 {
221   QString result = QString ( (const QChar*) extString.ToExtString(),
222                                             extString.Length() );
223   return result;
224 }
225
226 /*!
227   Converts QString to TCollection_ExtendedString
228 */
229 TCollection_ExtendedString QAD_Tools::toExtString( const QString& qString)
230 {
231   TCollection_ExtendedString result;
232   for ( int i = 0; i < (int)qString.length(); i++ )
233     result.Insert( i + 1, qString[ i ].unicode() );
234   return result;
235 }
236
237 /*!
238   Converts TCollection_AsciiString to QString
239 */
240 QString QAD_Tools::toQString( const TCollection_AsciiString& asciiString)
241 {
242   QString result = QString ( asciiString.ToCString() );
243   return result;
244 }
245
246 /*!
247   Converts QString to TCollection_AsciiString
248 */
249 TCollection_AsciiString QAD_Tools::toAsciiString( const QString& qString)
250 {
251   TCollection_AsciiString result = TCollection_AsciiString((char*)(qString.latin1()));
252   return result;
253 }
254
255 /*!
256   Creates font from string description
257 */
258 QFont QAD_Tools::stringToFont( const QString& fontDescription )
259 {
260   QFont font;
261   if ( !font.fromString( fontDescription ) )
262     font = QFont( "Courier", 11 );
263   return font;
264 }
265
266 /*!
267   Creates font's string description
268 */
269 QString QAD_Tools::fontToString( const QFont& font )
270 {
271   return font.toString();
272 }
273
274 /*!
275   Checks popup menu recursively for unnecessary separators and removes them
276 */
277 void QAD_Tools::checkPopup( QPopupMenu* popup )
278 {
279   if ( popup->count() > 0 ) {
280     QValueList<int> idRemove;
281     for ( int i = 1; i < popup->count(); i++ ) {
282       if ( popup->findItem( popup->idAt( i ) )->isSeparator() ) {
283         if ( popup->findItem( popup->idAt( i-1 ) )->isSeparator() )
284           idRemove.append( popup->idAt( i ) );
285       }
286       else { 
287         QPopupMenu* child = popup->findItem( popup->idAt( i ) )->popup();
288         if ( child ) {
289           checkPopup( child );
290         }
291       }
292     }
293     for ( int i = 0; i < idRemove.count(); i++ )
294       popup->removeItem( idRemove[i] );
295     if ( popup->count() > 0 && popup->findItem( popup->idAt( 0 ) )->isSeparator() )
296       popup->removeItem( popup->idAt( 0 ) );
297     if ( popup->count() > 0 && popup->findItem( popup->idAt( popup->count()-1 ) )->isSeparator() )
298       popup->removeItem( popup->idAt( popup->count()-1 ) );
299   }
300 }