Salome HOME
5cf560636b93be6c263d9eea048d6b664f0c32b6
[modules/shaper.git] / src / XGUI / XGUI_Tools.cpp
1 #include "XGUI_Tools.h"
2
3 #include <TopoDS_Shape.hxx>
4 #include <ModelAPI_Object.h>
5
6 #include <QDir>
7
8 #include <iostream>
9 #include <sstream>
10
11 namespace XGUI_Tools
12 {
13 //******************************************************************
14 QString dir(const QString& path, bool isAbs)
15 {
16   QDir aDir = QFileInfo(path).dir();
17   QString dirPath = isAbs ? aDir.absolutePath() : aDir.path();
18   if (dirPath == QString("."))
19     dirPath = QString();
20   return dirPath;
21 }
22
23 //******************************************************************
24 QString file(const QString& path, bool withExt)
25 {
26   QString fPath = path;
27   while(!fPath.isEmpty() && (fPath[fPath.length() - 1] == '\\' || fPath[fPath.length() - 1] == '/'))
28     fPath.remove(fPath.length() - 1, 1);
29
30   if (withExt)
31     return QFileInfo(fPath).fileName();
32   else
33     return QFileInfo(fPath).completeBaseName();
34 }
35
36 //******************************************************************
37 QString extension(const QString& path, bool full)
38 {
39   return full ? QFileInfo(path).completeSuffix() : QFileInfo(path).suffix();
40 }
41
42 //******************************************************************
43 QString addSlash(const QString& path)
44 {
45   QString res = path;
46   if (!res.isEmpty() && res.at(res.length() - 1) != QChar('/')
47       && res.at(res.length() - 1) != QChar('\\'))
48     res += QDir::separator();
49   return res;
50 }
51
52 //******************************************************************
53 QRect makeRect(const int x1, const int y1, const int x2, const int y2)
54 {
55   return QRect(qMin(x1, x2), qMin(y1, y2), qAbs(x2 - x1), qAbs(y2 - y1));
56 }
57
58 //******************************************************************
59 bool isModelObject(FeaturePtr theFeature)
60 {
61   return theFeature && !theFeature->data();
62 }
63
64 //******************************************************************
65 std::string featureInfo(FeaturePtr theFeature)
66 {
67   std::ostringstream aStream; 
68   if (theFeature)
69     aStream << theFeature.get() << " " << theFeature->getKind();
70   return QString(aStream.str().c_str()).toStdString();
71 }
72
73 //******************************************************************
74 FeaturePtr realFeature(const FeaturePtr theFeature)
75 {
76   if (theFeature->data()) {
77     return theFeature;
78   } else {
79     ObjectPtr aObject = boost::dynamic_pointer_cast<ModelAPI_Object>(theFeature);
80     return aObject->featureRef();
81   }
82 }
83
84 }