1 // SALOME SALOMEGUI : implementation of desktop and GUI kernel
3 // Copyright (C) 2003 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
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.
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.
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
20 // See http://www.opencascade.org/SALOME/ or email : webmaster.salome@opencascade.org
24 // File : QAD_Tools.cxx
30 \class QAD_Tools QAD_Tools.h
31 \brief Helpful functions for QAD.
34 #include "QAD_Tools.h"
35 #include "utilities.h"
39 #include <qfileinfo.h>
45 #include <qstringlist.h>
48 static char* tempName()
50 static char tempDir[512];
51 strcpy ( tempDir, getenv ("tmp") );
54 if ( tempDir[ strlen(tempDir)-1 ] != '\\' )
55 strcat ( tempDir,"\\");
57 if ( tempDir[ strlen(tempDir)-1 ] != '/' )
58 strcat ( tempDir,"/");
60 strcat ( tempDir,"Salome_trace");
65 Returns max 'int' value [ static ]
67 inline int QAD_Tools::getMax(int v1, int v2)
69 return v1 >= v2 ? v1 : v2;
73 Returns min 'int' value [ static ]
75 inline int QAD_Tools::getMin(int v1, int v2)
77 return v1 <= v2 ? v1 : v2;
82 Creates a rect with TopLeft = ( min(x1,x2), min(y1,y2) )
83 and BottomRight = ( TopLeft + (x2-x1)(y2-y1) )
85 QRect QAD_Tools::makeRect(int x1, int y1, int x2, int y2)
88 aRect.setRect( getMin(x1, x2), getMin(y1, y2), abs(x2-x1), abs(y2-y1));
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.
97 void QAD_Tools::trace (const char* lpszLog, const char* lpszFormat, ... )
99 static char* lpszTmp = tempName();
102 pStream = fopen ( lpszLog ? lpszLog : lpszTmp, "a" );
106 va_start(argptr, lpszFormat);
107 //NRI vfprintf ( pStream, lpszFormat, argptr);
108 fprintf ( pStream, "- Trace %s [%d] : %s", __FILE__, __LINE__, lpszFormat);
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'
119 QString QAD_Tools::getDirFromPath ( const QString& path, bool absPath )
121 return QFileInfo( path ).dirPath( absPath );
126 Parses the path to select the file name with or without extension [ static ].
128 QString QAD_Tools::getFileNameFromPath( const QString& path,
132 return QFileInfo( path ).fileName();
134 return QFileInfo( path ).baseName();
138 Parses the path to select the file extension [ static ].
140 QString QAD_Tools::getFileExtensionFromPath( const QString& path )
142 return QFileInfo( path ).extension(false);
146 Adds a slash to the end of 'path' if it is not already there [ static ]
148 QString QAD_Tools::addSlash( const QString& path )
150 if (!path.isNull()) {
156 if ( path.at(path.length()-1) != slash )
163 Converts slashes from unix-like to win-like [ static ]
164 Returns null string if 'path' is empty or null.
166 QString QAD_Tools::unix2win( const QString& path )
171 QChar winSlash ('\\'), unixSlash('/');
173 (pos = path.find( unixSlash, start, false )) >= 0;
176 ret.replace( pos, 1, &winSlash, 1 );
182 Centers widget 'w' as refered to widget 'ref' [ static ]
184 void QAD_Tools::centerWidget( QWidget* src, const QWidget* ref )
186 QAD_Tools::alignWidget(src, ref, Qt::AlignCenter);
190 Aligns widget 'w' as refered to widget 'ref' [ static ]
192 void QAD_Tools::alignWidget ( QWidget* src, const QWidget* ref, int alignFlags )
194 if ( !src || !ref || !alignFlags ) return;
196 QPoint srcOri = src->mapToGlobal( QPoint( 1, 1 ) );
197 QPoint refOri = ref->mapToGlobal( QPoint( 1, 1 ) );
199 int x = srcOri.x(), y = srcOri.y();
200 int refWidth = ref->width(), refHei = ref->height();
201 int srcWidth = src->width(), srcHei = src->height();
203 srcWidth = src->sizeHint().width();
205 srcHei = src->sizeHint().height();
207 if ( alignFlags & AlignLeft )
209 if ( alignFlags & AlignRight )
210 x = refOri.x() + refWidth - srcWidth;
211 if ( alignFlags & AlignTop )
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;
220 if ( src->parentWidget() && /* we move a widget inside its parent */
221 !src->inherits( "QDialog" )) /* dialogs use global coordinates */
223 QPoint pos = src->parentWidget()->mapFromGlobal( QPoint(x,y) );
224 x = pos.x(); y = pos.y();
227 x -= 4; /* - frame border width ( approx. ) */
228 y -= 30; /* - caption height ( approx. ) */
235 Converts TCollection_ExtendedString to QString
237 QString QAD_Tools::toQString( const TCollection_ExtendedString& extString)
239 QString result = QString ( (const QChar*) extString.ToExtString(),
240 extString.Length() );
245 Converts QString to TCollection_ExtendedString
247 TCollection_ExtendedString QAD_Tools::toExtString( const QString& qString)
249 TCollection_ExtendedString result;
250 for ( int i = 0; i < (int)qString.length(); i++ )
251 result.Insert( i + 1, qString[ i ].unicode() );
256 Converts TCollection_AsciiString to QString
258 QString QAD_Tools::toQString( const TCollection_AsciiString& asciiString)
260 QString result = QString ( asciiString.ToCString() );
265 Converts QString to TCollection_AsciiString
267 TCollection_AsciiString QAD_Tools::toAsciiString( const QString& qString)
269 TCollection_AsciiString result = TCollection_AsciiString((char*)(qString.latin1()));
274 Creates font from string description
276 QFont QAD_Tools::stringToFont( const QString& fontDescription )
279 if ( fontDescription.stripWhiteSpace().isEmpty() || !font.fromString( fontDescription ) )
280 font = QFont( "Courier", 11 );
285 Creates font's string description
287 QString QAD_Tools::fontToString( const QFont& font )
289 return font.toString();
293 Checks popup menu recursively for unnecessary separators and removes them
295 void QAD_Tools::checkPopup( QPopupMenu* popup )
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 ) );
305 QPopupMenu* child = popup->findItem( popup->idAt( i ) )->popup();
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 ) );