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