Salome HOME
Mouse interaction with Viewer is added
[modules/shaper.git] / src / XGUI / XGUI_Tools.cpp
1
2 #include "XGUI_Tools.h"
3
4 #include <QDir>
5
6
7 //******************************************************************
8 QString library( const QString& str )
9 {
10   QString path = dir( str, false );
11   QString name = file( str, false );
12   QString ext  = extension( str );
13
14 #ifndef WIN32
15   if ( !name.startsWith( "lib" ) )
16     name = QString( "lib" ) + name;
17 #endif
18
19 #ifdef WIN32
20   QString libExt( "dll" );
21 #else
22   QString libExt( "so" );
23 #endif
24
25   if ( ext.toLower() != QString( "so" ) && ext.toLower() != QString( "dll" ) )
26   {
27     if ( !name.isEmpty() && !ext.isEmpty() )
28       name += QString( "." );
29     name += ext;
30   }
31
32   ext = libExt;
33
34   QString fileName = addSlash( path ) + name + QString( "." ) + ext;
35
36   return fileName;
37 }
38
39 //******************************************************************
40 QString dir( const QString& path, bool isAbs )
41 {
42   QDir aDir = QFileInfo( path ).dir();
43   QString dirPath = isAbs ? aDir.absolutePath() : aDir.path();
44   if ( dirPath == QString( "." ) )
45     dirPath = QString();
46   return dirPath;
47 }
48
49
50 //******************************************************************
51 QString file( const QString& path, bool withExt )
52 {
53   QString fPath = path;
54   while ( !fPath.isEmpty() && ( fPath[fPath.length() - 1] =='\\' || fPath[fPath.length() - 1] == '/' ) )
55     fPath.remove( fPath.length() - 1, 1 );
56
57   if ( withExt )
58     return QFileInfo( fPath ).fileName();
59   else
60     return QFileInfo( fPath ).completeBaseName();
61 }
62
63 //******************************************************************
64 QString extension( const QString& path, bool full )
65 {
66   return full ? QFileInfo( path ).completeSuffix() : QFileInfo( path ).suffix();
67 }
68
69 //******************************************************************
70 QString addSlash( const QString& path )
71 {
72   QString res = path;
73   if ( !res.isEmpty() && res.at( res.length() - 1 ) != QChar( '/' ) &&
74        res.at( res.length() - 1 ) != QChar( '\\' ) )
75   res += QDir::separator();
76   return res;
77 }
78
79 //******************************************************************
80 QRect makeRect( const int x1, const int y1, const int x2, const int y2 )
81 {  
82   return QRect( qMin( x1, x2 ), qMin( y1, y2 ), qAbs( x2 - x1 ), qAbs( y2 - y1 ) );
83 }