2 // File : QAD_Tools.cxx
3 // Created : UI team, 22.09.00
4 // Descr : Helpful functions for QAD
8 // Copyright : Open CASCADE
13 \class QAD_Tools QAD_Tools.h
14 \brief Helpful functions for QAD.
17 #include "QAD_Tools.h"
18 #include "utilities.h"
22 #include <qfileinfo.h>
28 #include <qstringlist.h>
30 static char* tempName()
32 static char tempDir[512];
33 strcpy ( tempDir, getenv ("tmp") );
36 if ( tempDir[ strlen(tempDir)-1 ] != '\\' )
37 strcat ( tempDir,"\\");
39 if ( tempDir[ strlen(tempDir)-1 ] != '/' )
40 strcat ( tempDir,"/");
42 strcat ( tempDir,"Salome_trace");
47 Returns max 'int' value [ static ]
49 inline int QAD_Tools::getMax(int v1, int v2)
51 return v1 >= v2 ? v1 : v2;
55 Returns min 'int' value [ static ]
57 inline int QAD_Tools::getMin(int v1, int v2)
59 return v1 <= v2 ? v1 : v2;
64 Creates a rect with TopLeft = ( min(x1,x2), min(y1,y2) )
65 and BottomRight = ( TopLeft + (x2-x1)(y2-y1) )
67 QRect QAD_Tools::makeRect(int x1, int y1, int x2, int y2)
70 aRect.setRect( getMin(x1, x2), getMin(y1, y2), abs(x2-x1), abs(y2-y1));
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.
79 void QAD_Tools::trace (const char* lpszLog, const char* lpszFormat, ... )
81 static char* lpszTmp = tempName();
84 pStream = fopen ( lpszLog ? lpszLog : lpszTmp, "a" );
88 va_start(argptr, lpszFormat);
89 //NRI vfprintf ( pStream, lpszFormat, argptr);
90 fprintf ( pStream, "- Trace %s [%d] : %s", __FILE__, __LINE__, lpszFormat);
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'
101 QString QAD_Tools::getDirFromPath ( const QString& path, bool absPath )
103 return QFileInfo( path ).dirPath( absPath );
108 Parses the path to select the file name with or without extension [ static ].
110 QString QAD_Tools::getFileNameFromPath( const QString& path,
114 return QFileInfo( path ).fileName();
116 return QFileInfo( path ).baseName();
120 Parses the path to select the file extension [ static ].
122 QString QAD_Tools::getFileExtensionFromPath( const QString& path )
124 return QFileInfo( path ).extension(false);
128 Adds a slash to the end of 'path' if it is not already there [ static ]
130 QString QAD_Tools::addSlash( const QString& path )
132 if (!path.isNull()) {
138 if ( path.at(path.length()-1) != slash )
145 Converts slashes from unix-like to win-like [ static ]
146 Returns null string if 'path' is empty or null.
148 QString QAD_Tools::unix2win( const QString& path )
153 QChar winSlash ('\\'), unixSlash('/');
155 (pos = path.find( unixSlash, start, false )) >= 0;
158 ret.replace( pos, 1, &winSlash, 1 );
164 Centers widget 'w' as refered to widget 'ref' [ static ]
166 void QAD_Tools::centerWidget( QWidget* src, const QWidget* ref )
168 QAD_Tools::alignWidget(src, ref, Qt::AlignCenter);
172 Aligns widget 'w' as refered to widget 'ref' [ static ]
174 void QAD_Tools::alignWidget ( QWidget* src, const QWidget* ref, int alignFlags )
176 if ( !src || !ref || !alignFlags ) return;
178 QPoint srcOri = src->mapToGlobal( QPoint( 1, 1 ) );
179 QPoint refOri = ref->mapToGlobal( QPoint( 1, 1 ) );
181 int x = srcOri.x(), y = srcOri.y();
182 int refWidth = ref->width(), refHei = ref->height();
183 int srcWidth = src->width(), srcHei = src->height();
185 srcWidth = src->sizeHint().width();
187 srcHei = src->sizeHint().height();
189 if ( alignFlags & AlignLeft )
191 if ( alignFlags & AlignRight )
192 x = refOri.x() + refWidth - srcWidth;
193 if ( alignFlags & AlignTop )
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;
202 if ( src->parentWidget() && /* we move a widget inside its parent */
203 !src->inherits( "QDialog" )) /* dialogs use global coordinates */
205 QPoint pos = src->parentWidget()->mapFromGlobal( QPoint(x,y) );
206 x = pos.x(); y = pos.y();
209 x -= 4; /* - frame border width ( approx. ) */
210 y -= 30; /* - caption height ( approx. ) */
217 Converts TCollection_ExtendedString to QString
219 QString QAD_Tools::toQString( const TCollection_ExtendedString& extString)
221 QString result = QString ( (const QChar*) extString.ToExtString(),
222 extString.Length() );
227 Converts QString to TCollection_ExtendedString
229 TCollection_ExtendedString QAD_Tools::toExtString( const QString& qString)
231 TCollection_ExtendedString result;
232 for ( int i = 0; i < (int)qString.length(); i++ )
233 result.Insert( i + 1, qString[ i ].unicode() );
238 Converts TCollection_AsciiString to QString
240 QString QAD_Tools::toQString( const TCollection_AsciiString& asciiString)
242 QString result = QString ( asciiString.ToCString() );
247 Converts QString to TCollection_AsciiString
249 TCollection_AsciiString QAD_Tools::toAsciiString( const QString& qString)
251 TCollection_AsciiString result = TCollection_AsciiString((char*)(qString.latin1()));
256 Creates font from string description
258 QFont QAD_Tools::stringToFont( const QString& fontDescription )
261 if ( !font.fromString( fontDescription ) )
262 font = QFont( "Courier", 11 );
267 Creates font's string description
269 QString QAD_Tools::fontToString( const QFont& font )
271 return font.toString();
275 Checks popup menu recursively for unnecessary separators and removes them
277 void QAD_Tools::checkPopup( QPopupMenu* popup )
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 ) );
287 QPopupMenu* child = popup->findItem( popup->idAt( i ) )->popup();
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 ) );