2 // Author: Sergey TELKOV
11 #include <qgroupbox.h>
12 #include <qfileinfo.h>
13 #include <qpopupmenu.h>
14 #include <qobjectlist.h>
15 #include <qwidgetlist.h>
16 #include <qapplication.h>
23 Name: setTabOrder [static public]
24 Desc: Set tab order for specified list of widgets. Last parameter should be null pointer.
27 void Qtx::setTabOrder( QWidget* first, ... )
30 va_start( wids, first );
37 widList.append( cur );
38 cur = va_arg( wids, QWidget* );
41 setTabOrder( widList );
45 Name: setTabOrder [static public]
46 Desc: Set tab order for specified list of widgets.
49 void Qtx::setTabOrder( const QWidgetList& widgets )
51 if ( widgets.count() < 2 )
55 for ( QWidgetListIt it( widgets ); it.current(); ++it )
57 QWidget* next = it.current();
59 QWidget::setTabOrder( prev, next );
65 Name: alignWidget [static public]
66 Desc: Align widget 'src' relative to widget 'ref' acording to alignment flags.
68 Qtx::AlignLeft - Align left side of 'src' to left side of 'ref'.
69 Qtx::AlignRight - Align right side of 'src' to right side of 'ref'.
70 Qtx::AlignTop - Align top side of 'src' to top side of 'ref'.
71 Qtx::AlignBottom - Align bottom side of 'src' to bottom side of 'ref'.
72 Qtx::AlignHCenter - Align 'src' to center of 'ref' in horizontal dimension.
73 Qtx::AlignVCenter - Align 'src' to center of 'ref' in vertical dimension.
74 Qtx::AlignCenter - Align 'src' to center of 'ref' in both dimensions.
75 Qtx::AlignOutLeft - Align right side of 'src' to left side of 'ref'.
76 Qtx::AlignOutRight - Align left side of 'src' to right side of 'ref'.
77 Qtx::AlignOutTop - Align bottom side of 'src' to top side of 'ref'.
78 Qtx::AlignOutBottom - Align top side of 'src' to bottom side of 'ref'.
81 void Qtx::alignWidget( QWidget* src, const QWidget* ref, const int alignFlags )
83 if ( !src || !ref || !alignFlags )
86 QPoint srcOri = src->pos();
87 QPoint refOri = ref->pos();
88 if ( src->parentWidget() && !src->isTopLevel() )
89 srcOri = src->parentWidget()->mapToGlobal( srcOri );
90 if ( ref->parentWidget() && !ref->isTopLevel() )
91 refOri = ref->parentWidget()->mapToGlobal( refOri );
93 int x = srcOri.x(), y = srcOri.y();
94 int refWidth = ref->frameGeometry().width(), refHei = ref->frameGeometry().height();
95 int srcWidth = src->frameGeometry().width(), srcHei = src->frameGeometry().height();
98 srcWidth = src->sizeHint().width();
100 srcHei = src->sizeHint().height();
103 if ( ref->isTopLevel() && ref->isMaximized() &&
104 src->isTopLevel() && !src->isMaximized() )
105 border = ( src->frameGeometry().width() - src->width() ) / 2;
107 if ( alignFlags & Qtx::AlignLeft )
108 x = refOri.x() + border;
109 if ( alignFlags & Qtx::AlignOutLeft )
110 x = refOri.x() - srcWidth - border;
111 if ( alignFlags & Qtx::AlignRight )
112 x = refOri.x() + refWidth - srcWidth - border;
113 if ( alignFlags & Qtx::AlignOutRight )
114 x = refOri.x() + refWidth + border;
115 if ( alignFlags & Qtx::AlignTop )
116 y = refOri.y() + border;
117 if ( alignFlags & Qtx::AlignOutTop )
118 y = refOri.y() - srcHei - border;
119 if ( alignFlags & Qtx::AlignBottom )
120 y = refOri.y() + refHei - srcHei - border;
121 if ( alignFlags & Qtx::AlignOutBottom )
122 y = refOri.y() + refHei + border;
123 if ( alignFlags & Qtx::AlignHCenter )
124 x = refOri.x() + ( refWidth - srcWidth ) / 2;
125 if ( alignFlags & Qtx::AlignVCenter )
126 y = refOri.y() + ( refHei - srcHei ) / 2;
128 if ( src->parentWidget() && !src->isTopLevel() )
130 QPoint pos = src->parentWidget()->mapFromGlobal( QPoint( x, y ) );
135 QWidget* desk = QApplication::desktop();
136 if ( desk && x + srcWidth + border > desk->width() )
137 x = desk->width() - srcWidth - border;
138 if ( desk && y + srcHei + border > desk->height() )
139 y = desk->height() - srcHei - border;
148 Name: simplifySeparators [static public]
149 Desc: Checks toolbar for unnecessary separators and removes them
151 void Qtx::simplifySeparators( QToolBar* toolbar )
156 const QObjectList* objList = toolbar->children();
162 bool isPrevSep = true;
163 for ( QObjectListIt it( *objList ); it.current(); ++it )
165 bool isSep = it.current()->isA( "QToolBarSeparator" );
166 if ( isPrevSep && isSep )
167 delList.append( it.current() );
171 for ( QObjectListIt itr( delList ); itr.current(); ++itr )
172 delete itr.current();
174 if ( toolbar->children() && !toolbar->children()->isEmpty() &&
175 toolbar->children()->getFirst()->isA( "QToolBarSeparator" ) )
176 delete toolbar->children()->getFirst();
178 if ( toolbar->children() && !toolbar->children()->isEmpty() &&
179 toolbar->children()->getLast()->isA( "QToolBarSeparator" ) )
180 delete toolbar->children()->getLast();
184 Name: simplifySeparators [static public]
185 Desc: Checks popup menu recursively for unnecessary separators and removes them
187 void Qtx::simplifySeparators( QPopupMenu* popup, const bool recursive )
189 if ( !popup || !popup->count() )
193 for ( uint i = 1; i < popup->count(); i++ )
195 if ( popup->findItem( popup->idAt( i ) )->isSeparator() &&
196 popup->findItem( popup->idAt( i - 1 ) )->isSeparator() )
197 idRemove.append( popup->idAt( i ) );
200 simplifySeparators( popup->findItem( popup->idAt( i ) )->popup() );
203 for ( QIntList::const_iterator it = idRemove.begin(); it != idRemove.end(); ++it )
204 popup->removeItem( *it );
206 if ( popup->count() > 0 && popup->findItem( popup->idAt( 0 ) )->isSeparator() )
207 popup->removeItem( popup->idAt( 0 ) );
209 if ( popup->count() > 0 && popup->findItem( popup->idAt( popup->count() - 1 ) )->isSeparator() )
210 popup->removeItem( popup->idAt( popup->count() - 1 ) );
214 Name: isParent [static public]
215 Desc: Returns 'true' if specified 'parent' is parent object of given 'child'.
217 bool Qtx::isParent( QObject* child, QObject* parent )
219 if ( !child || !parent )
223 QObject* obj = child;
224 while ( !res && obj )
233 Name: dir [static public]
234 Desc: Returns dir name or null string.
236 QString Qtx::dir( const QString& path, const bool abs )
238 QString dirPath = QFileInfo( path ).dirPath( abs );
239 if ( dirPath == QString( "." ) )
240 dirPath = QString::null;
245 Name: file [static public]
246 Desc: Returns file with or without extension.
248 QString Qtx::file( const QString& path, bool withExt )
251 return QFileInfo( path ).fileName();
253 return QFileInfo( path ).baseName();
257 Name: extension [static public]
258 Desc: Returns the file extension only or null string.
260 QString Qtx::extension( const QString& path )
262 return QFileInfo( path ).extension();
266 Name: library [static public]
267 Desc: Generate library file name.
268 Append required prefix (lib) and suffix (.dll/.so) to the library file name.
270 QString Qtx::library( const QString& str )
272 QString path = dir( str, false );
273 QString name = file( str, false );
274 QString ext = extension( str );
277 if ( !name.startsWith( "lib" ) )
278 name = QString( "lib" ) + name;
282 QString libExt( "dll" );
284 QString libExt( "so" );
287 if ( ext.lower() != QString( "so" ) && ext.lower() != QString( "dll" ) )
289 if ( !name.isEmpty() && !ext.isEmpty() )
290 name += QString( "." );
296 QString fileName = addSlash( path ) + name + QString( "." ) + ext;
302 Name: tmpDir [static public]
303 Desc: Returns path to temporary directory.
305 QString Qtx::tmpDir()
307 char* tmpdir = ::getenv( "TEMP" );
309 tmpdir = ::getenv ( "TMP" );
318 return QString( tmpdir );
322 Name: mkDir [static public]
323 Desc: Creates directory with intermediate perent directories.
324 Returns true in successfull case.
326 bool Qtx::mkDir( const QString& dirPath )
328 QString path = QDir::convertSeparators( dirPath );
331 while ( !path.isEmpty() && path.at( path.length() - 1 ) == QDir::separator() )
332 path.remove( path.length() - 1, 1 );
334 if ( path.at( path.length() - 1 ) == ':' )
335 return QFileInfo( path ).exists();
338 QFileInfo fInfo( path );
339 if ( fInfo.exists() )
340 return fInfo.isDir();
342 if ( !mkDir( fInfo.dirPath() ) )
345 return QDir( fInfo.dirPath() ).mkdir( fInfo.fileName() );
349 Name: rmDir [static public]
350 Desc: Removes directory with its subdirectories and files.
351 Returns true in successfull case.
353 bool Qtx::rmDir( const QString& thePath )
355 QFileInfo fi( thePath );
361 stat = QFile::remove( thePath );
362 else if ( fi.isDir() )
364 QDir aDir( thePath );
365 const QFileInfoList* anEntries = aDir.entryInfoList();
368 for ( QPtrListIterator<QFileInfo> it( *anEntries ); it.current(); ++it )
370 if ( it.current()->fileName() == "." || it.current()->fileName() == ".." )
372 stat = stat && rmDir( it.current()->absFilePath() );
375 stat = stat && aDir.rmdir( thePath );
381 Name: addSlash [static public]
382 Desc: Adds a slash to the end of 'path' if it is not already there.
384 QString Qtx::addSlash( const QString& path )
387 if ( !res.isEmpty() && res.at( res.length() - 1 ) != QChar( '/' ) &&
388 res.at( res.length() - 1 ) != QChar( '\\' ) )
389 res += QDir::separator();
394 Name: dos2unix [static public]
395 Desc: Convert text file. Replace symbols "LF/CR" by symbol "LF".
397 bool Qtx::dos2unix( const QString& absName )
399 FILE* src = ::fopen( absName, "rb" );
403 /* we'll use temporary file */
404 char temp[512] = { '\0' };
405 QString dir = Qtx::dir( absName );
406 FILE* tgt = ::fopen( strcpy( temp, ::tempnam( dir, "__x" ) ), "wb" );
410 /* temp -> result of conversion */
411 const char CR = 0x0d;
412 const char LF = 0x0a;
413 bool waitingLF = false;
418 char inbuf[512], outbuf[512];
421 int nbread = ::fread( inbuf, 1, sizeof( inbuf ), src );
422 for ( int incnt = 0; incnt < nbread; incnt++ )
427 if ( inbuf[incnt] == LF )
428 outbuf[outcnt++] = LF;
430 outbuf[outcnt++] = CR;
432 else if ( inbuf[incnt] == CR )
435 outbuf[outcnt++] = inbuf[incnt];
438 /* check last sym in buffer */
439 waitingLF = ( inbuf[nbread - 1] == CR );
441 /* write converted buffer to temp file */
442 int nbwri = ::fwrite( outbuf, 1, outcnt, tgt );
443 if ( nbwri != outcnt )
447 QFile::remove( QString( temp ) );
450 if ( nbread != sizeof( inbuf ) )
451 break; /* converted ok */
456 /* rename temp -> src */
457 if ( !QFile::remove( absName ) )
460 return QDir().rename( QString( temp ), absName );
464 Name: rgbSet [static public]
465 Desc: Pack the specified color into one integer RGB set.
467 int Qtx::rgbSet( const QColor& c )
469 return rgbSet( c.red(), c.green(), c.blue() );
473 Name: rgbSet [static public]
474 Desc: Pack the specified color components into one integer RGB set.
476 int Qtx::rgbSet( const int r, const int g, const int b )
478 return ( ( ( 0xff & r ) << 16 ) + ( ( 0xff & g ) << 8 ) + ( 0xff & b ) );
482 Name: rgbSet [static public]
483 Desc: Unpack the specified integer RGB set into the color.
485 void Qtx::rgbSet( const int rgb, QColor& c )
488 rgbSet( rgb, r, g, b );
489 c = QColor( r, g, b );
493 Name: rgbSet [static public]
494 Desc: Unpack the specified integer RGB set into the color components.
496 void Qtx::rgbSet( const int rgb, int& r, int& g, int& b )
498 r = ( rgb >> 16 ) & 0xff;
499 g = ( rgb >> 8 ) & 0xff;
504 Name: scaleColor [static public]
505 Desc: Returns the color specified by the index between min (blue) and max (red).
507 QColor Qtx::scaleColor( const int index, const int min, const int max )
509 static const int HUE[10] = {230, 210, 195, 180, 160, 80, 60, 50, 30, 0};
515 double aPosition = 9.0 * ( index - min ) / ( max - min );
516 if ( aPosition > 0.0 )
518 if ( aPosition >= 9.0 )
522 int idx = (int)aPosition;
523 hue = HUE[idx] + int( ( aPosition - idx ) * ( HUE[idx + 1] - HUE[idx] ) );
528 return QColor( hue, 255, 255, QColor::Hsv );
532 Name: scaleColors [static public]
533 Desc: Returns the 'num' number of colors from blue to red.
535 void Qtx::scaleColors( const int num, QValueList<QColor>& lst )
538 for ( int i = 0; i < num; i++ )
539 lst.append( scaleColor( i, 0, num - 1 ) );