// Copyright (C) 2005 OPEN CASCADE, CEA/DEN, EDF R&D, PRINCIPIA R&D
-//
+//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
-// License as published by the Free Software Foundation; either
+// License as published by the Free Software Foundation; either
// version 2.1 of the License.
-//
-// This library is distributed in the hope that it will be useful
-// but WITHOUT ANY WARRANTY; without even the implied warranty of
-// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+//
+// This library is distributed in the hope that it will be useful
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
-// You should have received a copy of the GNU Lesser General Public
-// License along with this library; if not, write to the Free Software
+// You should have received a copy of the GNU Lesser General Public
+// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
//
// See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
#include "Qtx.h"
-#include <qdir.h>
-#include <qbitmap.h>
-#include <qstring.h>
-#include <qwidget.h>
-#include <qlayout.h>
-#include <qpainter.h>
-#include <qtoolbar.h>
-#include <qgroupbox.h>
-#include <qfileinfo.h>
-#include <qpopupmenu.h>
-#include <qobjectlist.h>
-#include <qwidgetlist.h>
-#include <qapplication.h>
+#include <QtCore/qdir.h>
+#include <QtCore/qstring.h>
+#include <QtCore/qfileinfo.h>
+
+#include <QtGui/qmenu.h>
+#include <QtGui/qbitmap.h>
+#include <QtGui/qwidget.h>
+#include <QtGui/qlayout.h>
+#include <QtGui/qpainter.h>
+#include <QtGui/qtoolbar.h>
+#include <QtGui/qapplication.h>
+#include <QtGui/qdesktopwidget.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
/*!
- Name: setTabOrder [static public]
- Desc: Set tab order for specified list of widgets. Last parameter should be null pointer.
-*/
+ \brief Convert character array (ASCII string) to the QString.
+ \param str character array
+ \param len array length, if < 0, the array should be zero-terminated
+ \return QString object
+ */
+QString Qtx::toQString( const char* str, const int len )
+{
+ return toQString( (unsigned char*)str, len );
+}
+
+/*!
+ \brief Convert integer array (UNICODE string) to the QString.
+ \param str integer array
+ \param len array length, if < 0, the array should be zero-terminated
+ \return QString object
+ */
+QString Qtx::toQString( const short* str, const int len )
+{
+ return toQString( (unsigned short*)str, len );
+}
+
+/*!
+ \brief Convert character array (ASCII string) to the QString.
+ \param str character array
+ \param len array length, if < 0, the array should be zero-terminated
+ \return QString object
+ */
+QString Qtx::toQString( const unsigned char* str, const int len )
+{
+ QString res;
+ const unsigned char* s = str;
+ while ( len < 0 || res.length() < len )
+ {
+ if ( *s == '\0' )
+ break;
+
+ res.append( QChar( *s ) );
+ s++;
+ }
+ return res;
+}
+
+/*!
+ \brief Convert integer array (UNICODE string) to the QString.
+ \param str integer array
+ \param len array length, if < 0, the array should be zero-terminated
+ \return QString object
+ */
+QString Qtx::toQString( const unsigned short* str, const int len )
+{
+ QString res;
+ const unsigned short* s = str;
+ while ( len < 0 || res.length() < len )
+ {
+ if ( *s == '\0' )
+ break;
+ res.append( QChar( *s ) );
+ s++;
+ }
+ return res;
+}
+
+/*!
+ \brief Set tab order for specified list of widgets.
+
+ The function has arbitrary number of parameters, each should be
+ hovewer of QWidget* type. Last parameter should be null pointer.
+
+ \param first first widget in the sequence
+*/
void Qtx::setTabOrder( QWidget* first, ... )
{
va_list wids;
- va_start( wids, first );
+ va_start( wids, first );
- QWidgetList widList;
+ QWidgetList widList;
- QWidget* cur = first;
- while ( cur )
- {
- widList.append( cur );
- cur = va_arg( wids, QWidget* );
+ QWidget* cur = first;
+ while ( cur )
+ {
+ widList.append( cur );
+ cur = va_arg( wids, QWidget* );
}
- setTabOrder( widList );
+ setTabOrder( widList );
}
/*!
- Name: setTabOrder [static public]
- Desc: Set tab order for specified list of widgets.
+ \brief Set tab order for specified list of widgets.
+ \param widgets list of widgets
*/
-
void Qtx::setTabOrder( const QWidgetList& widgets )
{
if ( widgets.count() < 2 )
return;
QWidget* prev = 0;
- for ( QWidgetListIt it( widgets ); it.current(); ++it )
+ for ( QWidgetList::const_iterator it = widgets.begin(); it!= widgets.end(); ++it )
{
- QWidget* next = it.current();
+ QWidget* next = *it;
if ( prev && next )
QWidget::setTabOrder( prev, next );
prev = next;
}
/*!
- Name: alignWidget [static public]
- Desc: Align widget 'src' relative to widget 'ref' acording to alignment flags.
- Alignment flags:
- Qtx::AlignLeft - Align left side of 'src' to left side of 'ref'.
- Qtx::AlignRight - Align right side of 'src' to right side of 'ref'.
- Qtx::AlignTop - Align top side of 'src' to top side of 'ref'.
- Qtx::AlignBottom - Align bottom side of 'src' to bottom side of 'ref'.
- Qtx::AlignHCenter - Align 'src' to center of 'ref' in horizontal dimension.
- Qtx::AlignVCenter - Align 'src' to center of 'ref' in vertical dimension.
- Qtx::AlignCenter - Align 'src' to center of 'ref' in both dimensions.
- Qtx::AlignOutLeft - Align right side of 'src' to left side of 'ref'.
- Qtx::AlignOutRight - Align left side of 'src' to right side of 'ref'.
- Qtx::AlignOutTop - Align bottom side of 'src' to top side of 'ref'.
- Qtx::AlignOutBottom - Align top side of 'src' to bottom side of 'ref'.
+ \brief Align widget \a src relative to widget \a ref acording to alignment flags.
+
+ Alignment flags:
+ - Qtx::AlignLeft : align left side of \a src to the left side of \a ref
+ - Qtx::AlignRight : align right side of \a src to the right side of \a ref
+ - Qtx::AlignTop : align top side of \a src to the top side of \a ref
+ - Qtx::AlignBottom : align bottom side of \a src to the bottom side of \a ref
+ - Qtx::AlignHCenter : align \a src to the center of \a ref in horizontal dimension
+ - Qtx::AlignVCenter : align \a src to the center of \a ref in vertical dimension
+ - Qtx::AlignCenter : align \a src to the center of \a ref in both dimensions
+ - Qtx::AlignOutLeft : align right side of \a src to the left side of \a ref
+ - Qtx::AlignOutRight : align left side of \a src to the right side of \a ref
+ - Qtx::AlignOutTop : align bottom side of \a src to the top side of \a ref
+ - Qtx::AlignOutBottom : align top side of \a src to the bottom side of \a ref
+
+ \param src source widget (being aligned)
+ \param ref reference widget (source widget being to)
+ \param alignFlags alignment flags
*/
-
void Qtx::alignWidget( QWidget* src, const QWidget* ref, const int alignFlags )
{
- if ( !src || !ref || !alignFlags )
- return;
-
- QPoint srcOri = src->pos();
- QPoint refOri = ref->pos();
- if ( src->parentWidget() && !src->isTopLevel() )
- srcOri = src->parentWidget()->mapToGlobal( srcOri );
- if ( ref->parentWidget() && !ref->isTopLevel() )
- refOri = ref->parentWidget()->mapToGlobal( refOri );
-
- int x = srcOri.x(), y = srcOri.y();
- int refWidth = ref->frameGeometry().width(), refHei = ref->frameGeometry().height();
- int srcWidth = src->frameGeometry().width(), srcHei = src->frameGeometry().height();
-
- if ( srcWidth <= 0 )
- srcWidth = src->sizeHint().width();
+ if ( !src || !ref || !alignFlags )
+ return;
+
+ QPoint srcOri = src->pos();
+ QPoint refOri = ref->pos();
+ if ( src->parentWidget() && !src->isTopLevel() )
+ srcOri = src->parentWidget()->mapToGlobal( srcOri );
+ if ( ref->parentWidget() && !ref->isTopLevel() )
+ refOri = ref->parentWidget()->mapToGlobal( refOri );
+
+ int x = srcOri.x(), y = srcOri.y();
+ int refWidth = ref->frameGeometry().width(), refHei = ref->frameGeometry().height();
+ int srcWidth = src->frameGeometry().width(), srcHei = src->frameGeometry().height();
+
+ if ( srcWidth <= 0 )
+ srcWidth = src->sizeHint().width();
if ( srcHei <= 0 )
srcHei = src->sizeHint().height();
- int border = 0;
+ int border = 0;
if ( ref->isTopLevel() && ref->isMaximized() &&
src->isTopLevel() && !src->isMaximized() )
border = ( src->frameGeometry().width() - src->width() ) / 2;
- if ( alignFlags & Qtx::AlignLeft )
- x = refOri.x() + border;
- if ( alignFlags & Qtx::AlignOutLeft )
- x = refOri.x() - srcWidth - border;
- if ( alignFlags & Qtx::AlignRight )
- x = refOri.x() + refWidth - srcWidth - border;
- if ( alignFlags & Qtx::AlignOutRight )
- x = refOri.x() + refWidth + border;
- if ( alignFlags & Qtx::AlignTop )
- y = refOri.y() + border;
- if ( alignFlags & Qtx::AlignOutTop )
- y = refOri.y() - srcHei - border;
- if ( alignFlags & Qtx::AlignBottom )
- y = refOri.y() + refHei - srcHei - border;
- if ( alignFlags & Qtx::AlignOutBottom )
- y = refOri.y() + refHei + border;
- if ( alignFlags & Qtx::AlignHCenter )
- x = refOri.x() + ( refWidth - srcWidth ) / 2;
- if ( alignFlags & Qtx::AlignVCenter )
- y = refOri.y() + ( refHei - srcHei ) / 2;
-
- if ( src->parentWidget() && !src->isTopLevel() )
- {
- QPoint pos = src->parentWidget()->mapFromGlobal( QPoint( x, y ) );
- x = pos.x();
- y = pos.y();
- }
-
- QWidget* desk = QApplication::desktop();
- if ( desk && x + srcWidth + border > desk->width() )
- x = desk->width() - srcWidth - border;
- if ( desk && y + srcHei + border > desk->height() )
- y = desk->height() - srcHei - border;
-
- x = QMAX( x, 0 );
- y = QMAX( y, 0 );
-
- src->move( x, y );
+ if ( alignFlags & Qtx::AlignLeft )
+ x = refOri.x() + border;
+ if ( alignFlags & Qtx::AlignOutLeft )
+ x = refOri.x() - srcWidth - border;
+ if ( alignFlags & Qtx::AlignRight )
+ x = refOri.x() + refWidth - srcWidth - border;
+ if ( alignFlags & Qtx::AlignOutRight )
+ x = refOri.x() + refWidth + border;
+ if ( alignFlags & Qtx::AlignTop )
+ y = refOri.y() + border;
+ if ( alignFlags & Qtx::AlignOutTop )
+ y = refOri.y() - srcHei - border;
+ if ( alignFlags & Qtx::AlignBottom )
+ y = refOri.y() + refHei - srcHei - border;
+ if ( alignFlags & Qtx::AlignOutBottom )
+ y = refOri.y() + refHei + border;
+ if ( alignFlags & Qtx::AlignHCenter )
+ x = refOri.x() + ( refWidth - srcWidth ) / 2;
+ if ( alignFlags & Qtx::AlignVCenter )
+ y = refOri.y() + ( refHei - srcHei ) / 2;
+
+ if ( src->parentWidget() && !src->isTopLevel() )
+ {
+ QPoint pos = src->parentWidget()->mapFromGlobal( QPoint( x, y ) );
+ x = pos.x();
+ y = pos.y();
+ }
+
+ QWidget* desk = QApplication::desktop();
+ if ( desk && x + srcWidth + border > desk->width() )
+ x = desk->width() - srcWidth - border;
+ if ( desk && y + srcHei + border > desk->height() )
+ y = desk->height() - srcHei - border;
+
+ x = qMax( x, 0 );
+ y = qMax( y, 0 );
+
+ src->move( x, y );
}
-/*!
- Name: simplifySeparators [static public]
- Desc: Checks toolbar for unnecessary separators and removes them
-*/
+/* VSR: obsolete
void Qtx::simplifySeparators( QToolBar* toolbar )
{
if ( !toolbar )
return;
- const QObjectList* objList = toolbar->children();
- if ( !objList )
- return;
+ const QObjectList& objList = toolbar->children();
QObjectList delList;
bool isPrevSep = true;
- for ( QObjectListIt it( *objList ); it.current(); ++it )
+ QObject* lastVis = 0; // last visible
+ for ( QObjectList::const_iterator it = objList.begin(); it != objList.end(); ++it )
{
- bool isSep = it.current()->isA( "QToolBarSeparator" );
+ QObject* obj = *it;
+ if ( !obj || !obj->isWidgetType() )
+ continue;
+ bool isSep = obj->inherits( "QToolBarSeparator" );
+ if ( !isSep && !((QWidget*)obj)->isVisibleTo( toolbar ) )
+ continue;
if ( isPrevSep && isSep )
- delList.append( it.current() );
- isPrevSep = isSep;
+ delList.append( obj );
+ else
+ {
+ isPrevSep = isSep;
+ lastVis = obj;
+ }
}
+ // remove last visible separator
+ if ( lastVis && lastVis->inherits( "QToolBarSeparator" ) )
+ delList.append( lastVis );
- for ( QObjectListIt itr( delList ); itr.current(); ++itr )
- delete itr.current();
-
- if ( toolbar->children() && !toolbar->children()->isEmpty() &&
- toolbar->children()->getFirst()->isA( "QToolBarSeparator" ) )
- delete toolbar->children()->getFirst();
-
- if ( toolbar->children() && !toolbar->children()->isEmpty() &&
- toolbar->children()->getLast()->isA( "QToolBarSeparator" ) )
- delete toolbar->children()->getLast();
+ for ( QObjectList::iterator itr = delList.begin(); itr != delList.end(); ++itr )
+ delete *itr;
}
+*/
/*!
- Name: simplifySeparators [static public]
- Desc: Checks popup menu recursively for unnecessary separators and removes them
+ \brief Remove (recursively) unnecessary separators from the menu or toolbar.
+ \param wid widget, should be of QMenu* or QToolBar* class
*/
-void Qtx::simplifySeparators( QPopupMenu* popup, const bool recursive )
+void Qtx::simplifySeparators( QWidget* wid, const bool recursive )
{
- if ( !popup || !popup->count() )
+ if ( !wid )
+ return;
+
+ QList<QAction*> items = wid->actions();
+ if ( items.isEmpty() )
return;
- QIntList idRemove;
- for ( uint i = 1; i < popup->count(); i++ )
+ QList<QAction*> toRemove;
+ for ( int i = 1; i < items.count(); i++ )
{
- if ( popup->findItem( popup->idAt( i ) )->isSeparator() &&
- popup->findItem( popup->idAt( i - 1 ) )->isSeparator() )
- idRemove.append( popup->idAt( i ) );
+ if ( items[i]->isSeparator() && items[i - 1]->isSeparator() )
+ toRemove.append( items[i] );
- if ( recursive )
- simplifySeparators( popup->findItem( popup->idAt( i ) )->popup() );
+ if ( recursive && items[i]->menu() )
+ simplifySeparators( items[i]->menu(), recursive );
}
- for ( QIntList::const_iterator it = idRemove.begin(); it != idRemove.end(); ++it )
- popup->removeItem( *it );
+ for ( QList<QAction*>::iterator it = toRemove.begin(); it != toRemove.end(); ++it )
+ wid->removeAction( *it );
- if ( popup->count() > 0 && popup->findItem( popup->idAt( 0 ) )->isSeparator() )
- popup->removeItem( popup->idAt( 0 ) );
+ items = wid->actions();
+ if ( !items.isEmpty() && items[0]->isSeparator() )
+ wid->removeAction( items[0] );
- if ( popup->count() > 0 && popup->findItem( popup->idAt( popup->count() - 1 ) )->isSeparator() )
- popup->removeItem( popup->idAt( popup->count() - 1 ) );
+ items = wid->actions();
+ if ( !items.isEmpty() && items[items.count() - 1]->isSeparator() )
+ wid->removeAction( items[items.count() - 1] );
}
/*!
- Name: isParent [static public]
- Desc: Returns 'true' if specified 'parent' is parent object of given 'child'.
+ \brief Return \c true if specified \a parent is a parent object
+ of given \a child (in terms of QObject).
+
+ This function works recursively. It means that \a true is also
+ returned if \a parent is a grand-father, grand-grand-father, etc
+ of \a child. If the same object is given as both \a parent and
+ \a child, \c true is also returned.
+
+ \param child child object
+ \param parent parent object
+ \return \c true if the \a parent is a parent of \a child
*/
bool Qtx::isParent( QObject* child, QObject* parent )
{
}
/*!
- Name: dir [static public]
- Desc: Returns dir name or null string.
+ \brief Return directory part of the file path.
+
+ If the file path does not include directory part (the file is in the
+ current directory), null string is returned.
+
+ \param path file path
+ \param abs if true (default) \a path parameter is treated as absolute file path
+ \return directory part of the file path
*/
QString Qtx::dir( const QString& path, const bool abs )
{
- QString dirPath = QFileInfo( path ).dirPath( abs );
+ QDir aDir = QFileInfo( path ).dir();
+ QString dirPath = abs ? aDir.absolutePath() : aDir.path();
if ( dirPath == QString( "." ) )
- dirPath = QString::null;
+ dirPath = QString();
return dirPath;
}
/*!
- Name: file [static public]
- Desc: Returns file with or without extension.
+ \brief Return file name part of the file path.
+
+ \param path file path
+ \param withExt if true (default) complete file name (with all
+ extension except the last) is returned, otherwise only base name
+ is returned
+ \return file name part of the file path
*/
QString Qtx::file( const QString& path, bool withExt )
{
+ QString fPath = path;
+ while ( !fPath.isEmpty() && ( fPath[fPath.length() - 1] == '\\' || fPath[fPath.length() - 1] == '/' ) )
+ fPath.remove( fPath.length() - 1, 1 );
+
if ( withExt )
- return QFileInfo( path ).fileName();
+ return QFileInfo( fPath ).fileName();
else
- return QFileInfo( path ).baseName();
+ return QFileInfo( fPath ).baseName();
}
/*!
- Name: extension [static public]
- Desc: Returns the file extension only or null string.
+ \brief Return extension part of the file path.
+
+ \param path file path
+ \param full if true complete extension (all extensions, dot separated)
+ is returned, otherwise (default) only last extension is returned
+ \return extension part of the file path
*/
-QString Qtx::extension( const QString& path )
+QString Qtx::extension( const QString& path, const bool full )
{
- return QFileInfo( path ).extension(false); // after the last dot
+ return full ? QFileInfo( path ).completeSuffix() : QFileInfo( path ).suffix();
}
/*!
- Name: library [static public]
- Desc: Generate library file name.
- Append required prefix (lib) and suffix (.dll/.so) to the library file name.
+ \brief Convert the given parameter to the platform-specific library name.
+
+ The function appends platform-specific prefix (lib) and suffix (.dll/.so)
+ to the library file name.
+ For example, if \a str = "mylib", "libmylib.so" is returned for Linux and
+ mylib.dll for Windows.
+
+ \param str short library name
+ \return full library name
*/
QString Qtx::library( const QString& str )
{
QString libExt( "so" );
#endif
- if ( ext.lower() != QString( "so" ) && ext.lower() != QString( "dll" ) )
+ if ( ext.toLower() != QString( "so" ) && ext.toLower() != QString( "dll" ) )
{
if ( !name.isEmpty() && !ext.isEmpty() )
name += QString( "." );
}
/*!
- Name: tmpDir [static public]
- Desc: Returns path to temporary directory.
+ \brief Get the temporary directory name.
+ \return temporary directory (platform specific)
*/
QString Qtx::tmpDir()
{
- char* tmpdir = ::getenv( "TEMP" );
- if ( !tmpdir )
- tmpdir = ::getenv ( "TMP" );
- if ( !tmpdir )
- {
+ char* tmpdir = ::getenv( "TEMP" );
+ if ( !tmpdir )
+ tmpdir = ::getenv ( "TMP" );
+ if ( !tmpdir )
+ {
#ifdef WIN32
- tmpdir = "C:\\";
+ tmpdir = "C:\\";
#else
- tmpdir = "/tmp";
+ tmpdir = "/tmp";
#endif
- }
- return QString( tmpdir );
+ }
+ return QString( tmpdir );
}
/*!
- Name: mkDir [static public]
- Desc: Creates directory with intermediate perent directories.
- Returns true in successfull case.
+ \brief Create directory recursively including all intermediate sub directories.
+ \return \c true if the directory is successfully created and \c false otherwise
*/
bool Qtx::mkDir( const QString& dirPath )
{
- QString path = QDir::convertSeparators( dirPath );
-
-#ifdef WIN32
- while ( !path.isEmpty() && path.at( path.length() - 1 ) == QDir::separator() )
- path.remove( path.length() - 1, 1 );
-
- if ( path.at( path.length() - 1 ) == ':' )
- return QFileInfo( path ).exists();
-#endif
-
- QFileInfo fInfo( path );
- if ( fInfo.exists() )
- return fInfo.isDir();
-
- if ( !mkDir( fInfo.dirPath() ) )
- return false;
-
- return QDir( fInfo.dirPath() ).mkdir( fInfo.fileName() );
+ return QDir().mkpath( dirPath );
}
/*!
- Name: rmDir [static public]
- Desc: Removes directory with its subdirectories and files.
- Returns true in successfull case.
+ \brief Remove directory recursively including all subdirectories and files.
+ \return \c true if the directory is successfully removed and \c false otherwise
*/
bool Qtx::rmDir( const QString& thePath )
{
- QFileInfo fi( thePath );
- if ( !fi.exists() )
- return true;
-
- bool stat = true;
- if ( fi.isFile() )
- stat = QFile::remove( thePath );
- else if ( fi.isDir() )
- {
- QDir aDir( thePath );
- const QFileInfoList* anEntries = aDir.entryInfoList();
- if ( anEntries )
- {
- for ( QPtrListIterator<QFileInfo> it( *anEntries ); it.current(); ++it )
- {
- if ( it.current()->fileName() == "." || it.current()->fileName() == ".." )
- continue;
- stat = stat && rmDir( it.current()->absFilePath() );
- }
- }
- stat = stat && aDir.rmdir( thePath );
- }
- return stat;
+ QFileInfo fi( thePath );
+ if ( !fi.exists() )
+ return true;
+
+ bool stat = true;
+ if ( fi.isFile() )
+ stat = QFile::remove( thePath );
+ else if ( fi.isDir() )
+ {
+ QDir aDir( thePath );
+ QFileInfoList anEntries = aDir.entryInfoList();
+ for ( QFileInfoList::iterator it = anEntries.begin(); it != anEntries.end(); ++it )
+ {
+ QFileInfo inf = *it;
+ if ( inf.fileName() == "." || inf.fileName() == ".." )
+ continue;
+ stat = stat && rmDir( inf.absoluteFilePath() );
+ }
+ stat = stat && aDir.rmdir( thePath );
+ }
+ return stat;
}
/*!
- Name: addSlash [static public]
- Desc: Adds a slash to the end of 'path' if it is not already there.
+ \brief Add a slash (platform-specific) to the end of \a path
+ if it is not already there.
+ \param path directory path
+ \return modified path (with slash added to the end)
*/
QString Qtx::addSlash( const QString& path )
{
- QString res = path;
+ QString res = path;
if ( !res.isEmpty() && res.at( res.length() - 1 ) != QChar( '/' ) &&
- res.at( res.length() - 1 ) != QChar( '\\' ) )
+ res.at( res.length() - 1 ) != QChar( '\\' ) )
res += QDir::separator();
return res;
}
/*!
- Name: dos2unix [static public]
- Desc: Convert text file. Replace symbols "LF/CR" by symbol "LF".
+ \brief Convert text file from DOS format to UNIX.
+
+ The function replaces "LF/CR" symbols sequence by "LF" symbol.
+
+ \param absName file name
+ \return \c true if the file is converted successfully and \c false in
+ case of any error
*/
bool Qtx::dos2unix( const QString& absName )
{
- FILE* src = ::fopen( absName, "rb" );
+ FILE* src = ::fopen( absName.toLatin1(), "rb" );
if ( !src )
- return false;
+ return false;
/* we'll use temporary file */
char temp[512] = { '\0' };
QString dir = Qtx::dir( absName );
- FILE* tgt = ::fopen( strcpy( temp, ::tempnam( dir, "__x" ) ), "wb" );
+ FILE* tgt = ::fopen( strcpy( temp, ::tempnam( dir.toLatin1(), "__x" ) ), "wb" );
if ( !tgt )
- return false;
+ return false;
/* temp -> result of conversion */
const char CR = 0x0d;
break; /* converted ok */
}
::fclose( src );
- ::fclose( tgt );
+ ::fclose( tgt );
/* rename temp -> src */
if ( !QFile::remove( absName ) )
}
/*!
- Name: rgbSet [static public]
- Desc: Pack the specified color into one integer RGB set.
+ \brief Pack the specified color into integer RGB set.
+ \param c unpacked color
+ \return packed color
*/
int Qtx::rgbSet( const QColor& c )
{
}
/*!
- Name: rgbSet [static public]
- Desc: Pack the specified color components into one integer RGB set.
+ \brief Pack the specified RGB color components into integer RGB set.
+ \param r red component
+ \param g green component
+ \param b blue component
+ \return packed color
*/
int Qtx::rgbSet( const int r, const int g, const int b )
{
}
/*!
- Name: rgbSet [static public]
- Desc: Unpack the specified integer RGB set into the color.
+ \brief Unpack the specified integer RGB set to the color.
+ \param rgb packed color
+ \return unpacked color (QColor)
*/
-void Qtx::rgbSet( const int rgb, QColor& c )
+QColor Qtx::rgbSet( const int rgb )
{
int r, g, b;
rgbSet( rgb, r, g, b );
- c = QColor( r, g, b );
+ return QColor( r, g, b );
}
/*!
- Name: rgbSet [static public]
- Desc: Unpack the specified integer RGB set into the color components.
+ \brief Unpack the specified integer RGB set to the three RGB components.
+ \param rgb packed color
+ \param r returned unpacked red component
+ \param g returned unpacked green component
+ \param b returned unpacked blue component
*/
void Qtx::rgbSet( const int rgb, int& r, int& g, int& b )
{
}
/*!
- Name: scaleColor [static public]
- Desc: Returns the color specified by the index between min (blue) and max (red).
+ \brief Return the color specified by the index between min (blue) and max (red).
+ \param index color index
+ \param min required minimum hue value
+ \param max required maximum hue value
+ \return resulting color
*/
QColor Qtx::scaleColor( const int index, const int min, const int max )
{
int hue = HUE[0];
- if ( min != max )
+ if ( min != max )
{
double aPosition = 9.0 * ( index - min ) / ( max - min );
if ( aPosition > 0.0 )
}
}
- return QColor( hue, 255, 255, QColor::Hsv );
+ return QColor::fromHsv( hue, 255, 255 );
}
/*!
- Name: scaleColors [static public]
- Desc: Returns the 'num' number of colors from blue to red.
+ \brief Generate required number of colors aligned from blue to red.
+ \param num required number of colors
+ \param lst returned set of colors
*/
-void Qtx::scaleColors( const int num, QValueList<QColor>& lst )
+void Qtx::scaleColors( const int num, QColorList& lst )
{
lst.clear();
for ( int i = 0; i < num; i++ )
}
/*!
- Name: grayscale [static public]
- Desc: Convert color image to grayscale image.
+ \brief Convert given image to the grayscale format.
+ \param img initial image
+ \return converted to the grayscale image
*/
QImage Qtx::grayscale( const QImage& img )
{
}
/*!
- Name: grayscale [static public]
- Desc: Convert color pixmap to grayscale pixmap.
+ \brief Convert given pixmap to the grayscale format.
+ \param pix initial pixmap
+ \return converted to the grayscale pixmap
*/
QPixmap Qtx::grayscale( const QPixmap& pix )
{
QPixmap res;
- res.convertFromImage( grayscale( pix.convertToImage() ) );
+ res.fromImage( grayscale( pix.toImage() ) );
return res;
}
/*!
- Name: transparentImage [static public]
- Desc: Create transparent image with specified width \aw, height \ah and color depth \ad.
+ \brief Create transparent image.
+ \param w required image width
+ \param h required image height
+ \param d required image depth
+ \return generated image
*/
QImage Qtx::transparentImage( const int w, const int h, const int d )
{
- QImage img;
- if ( img.create( w, h, d < 0 ? QPixmap::defaultDepth() : d ) )
+ QImage::Format fmt;
+ switch ( d )
+ {
+ case 1:
+ fmt = QImage::Format_Mono;
+ break;
+ case 8:
+ fmt = QImage::Format_Indexed8;
+ break;
+ case 16:
+ case 24:
+ case 32:
+ default:
+ fmt = QImage::Format_ARGB32;
+ break;
+ }
+
+ QImage img( w, h, fmt );
+ if ( !img.isNull() )
{
- img.setAlphaBuffer( true );
+// img.setAlphaBuffer( true );
for ( int i = 0; i < img.height(); i++ )
for ( int j = 0; j < img.width(); j++ )
img.setPixel( j, i, qRgba( 0, 0, 0, 0 ) );
}
/*!
- Name: transparentPixmap [static public]
- Desc: Create transparent pixmap with specified width \aw, height \ah and color depth \ad.
+/*!
+ \brief Create transparent pixmap.
+ \param w required image width
+ \param h required pixmap height
+ \param d required pixmap depth
+ \return generated pixmap
*/
QPixmap Qtx::transparentPixmap( const int w, const int h, const int d )
{
QPixmap pix;
QImage img = transparentImage( w, h, d );
if ( !img.isNull() )
- pix.convertFromImage( img );
+ pix.fromImage( img );
return pix;
}
/*!
- Name: composite [static public]
- Desc: Create composite pixmap. Pixmap 'pix' draws over pixmap 'dest' with coordinates
- specified relative upper left corner of 'dest'. If 'dest' not given then new empty
- pixmap with appropriate size created.
+ \brief Create composite pixmap.
+
+ Pixmap \a pix is drawn over pixmap \a dest with coordinates
+ specified relatively to the upper left corner of \a dest.
+ If \a dest is not given, the new empty pixmap with appropriate size created instead.
+
+ \param pix source pixmap
+ \param x horizontal shift
+ \param y vertical shift
+ \param dest background pixmap
+ \return resulting pixmap
*/
QPixmap Qtx::composite( const QPixmap& pix, const int x, const int y, const QPixmap& dest )
{
if ( pix.isNull() )
return dest;
- int width = QMAX( pix.width() + x, dest.width() );
- int height = QMAX( pix.height() + y, dest.height() );
+ int width = qMax( pix.width() + x, dest.width() );
+ int height = qMax( pix.height() + y, dest.height() );
QPixmap res( width, height );
QImage img = transparentImage( width, height, 32 );
QPainter p;
p.begin( &res );
- p.fillRect( 0, 0, width, height, QBrush( white ) );
+ p.fillRect( 0, 0, width, height, QBrush( Qt::white ) );
if ( !dest.isNull() )
{
p.drawPixmap( 0, 0, dest );
- QImage temp = dest.convertToImage();
+ QImage temp = dest.toImage();
for ( int i = 0; i < temp.width() && i < img.width(); i++ )
{
for ( int j = 0; j < temp.height() && j < img.height(); j++ )
{
- if ( temp.hasAlphaBuffer() )
+ if ( temp.hasAlphaChannel() )
img.setPixel( i, j, temp.pixel( i, j ) );
else
{
}
p.drawPixmap( x, y, pix );
- QImage temp = pix.convertToImage();
+ QImage temp = pix.toImage();
for ( int c = x; c < temp.width() + x && c < img.width(); c++ )
{
for ( int r = y; r < temp.height() + y && r < img.height(); r++ )
}
QBitmap bmp( width, height );
- bmp.convertFromImage( img, Qt::ColorMode_Mask | Qt::ThresholdDither );
+ bmp.fromImage( img, Qt::ColorMode_Mask | Qt::ThresholdDither );
res.setMask( bmp );
return res;