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