Salome HOME
47de402703e2f0cc611ff9659b1ac3bca33a5a01
[modules/shaper.git] / src / XGUI / XGUI_Tools.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D -->
2
3 #include "XGUI_Tools.h"
4
5 #include <TopoDS_Shape.hxx>
6 #include <ModelAPI_Object.h>
7 #include <ModelAPI_Result.h>
8 #include <ModelAPI_ResultParameter.h>
9 #include <ModelAPI_Feature.h>
10 #include <ModelAPI_Session.h>
11 #include <ModelAPI_Document.h>
12 #include <ModelAPI_ResultPart.h>
13
14 #include <GeomAPI_Shape.h>
15
16 #include <QDir>
17 #include <QMessageBox>
18
19 #include <iostream>
20 #include <sstream>
21
22 namespace XGUI_Tools {
23 //******************************************************************
24 QString dir(const QString& path, bool isAbs)
25 {
26   QDir aDir = QFileInfo(path).dir();
27   QString dirPath = isAbs ? aDir.absolutePath() : aDir.path();
28   if (dirPath == QString("."))
29     dirPath = QString();
30   return dirPath;
31 }
32
33 //******************************************************************
34 QString file(const QString& path, bool withExt)
35 {
36   QString fPath = path;
37   while (!fPath.isEmpty() && (fPath[fPath.length() - 1] == '\\' || fPath[fPath.length() - 1] == '/'))
38     fPath.remove(fPath.length() - 1, 1);
39
40   if (withExt)
41     return QFileInfo(fPath).fileName();
42   else
43     return QFileInfo(fPath).completeBaseName();
44 }
45
46 //******************************************************************
47 QString addSlash(const QString& path)
48 {
49   QString res = path;
50   if (!res.isEmpty() && res.at(res.length() - 1) != QChar('/')
51       && res.at(res.length() - 1) != QChar('\\'))
52     res += QDir::separator();
53   return res;
54 }
55
56 //******************************************************************
57 bool isModelObject(FeaturePtr theFeature)
58 {
59   return theFeature && !theFeature->data();
60 }
61
62 //******************************************************************
63 std::string featureInfo(FeaturePtr theFeature)
64 {
65   std::ostringstream aStream;
66   if (theFeature)
67     aStream << theFeature.get() << " " << theFeature->getKind();
68   return QString(aStream.str().c_str()).toStdString();
69 }
70
71 //******************************************************************
72 /*FeaturePtr realFeature(const FeaturePtr theFeature)
73  {
74  if (theFeature->data()) {
75  return theFeature;
76  } else {
77  ObjectPtr aObject = std::dynamic_pointer_cast<ModelAPI_Object>(theFeature);
78  return aObject->featureRef();
79  }
80  }*/
81
82 //******************************************************************
83 bool canRemoveOrRename(QWidget* theParent, const QObjectPtrList& theObjects)
84 {
85   bool aResult = true;
86   QString aNotActivatedNames;
87   if (!XGUI_Tools::allDocumentsActivated(aNotActivatedNames)) {
88     DocumentPtr aModuleDoc = ModelAPI_Session::get()->moduleDocument();
89     bool aFoundPartSetObject = false;
90     foreach (ObjectPtr aObj, theObjects) {
91       if (aObj->groupName() == ModelAPI_ResultPart::group())
92         continue;
93       aFoundPartSetObject = aObj->document() == aModuleDoc;
94     }
95     if (aFoundPartSetObject) {
96       QMessageBox::StandardButton aRes = QMessageBox::warning(theParent, QObject::tr("Warning"),
97                QObject::tr("Selected objects can be used in Part documents which are not loaded: \
98 %1. Whould you like to continue?").arg(aNotActivatedNames),
99                QMessageBox::No | QMessageBox::Yes, QMessageBox::No);
100       aResult = aRes == QMessageBox::Yes;
101     }
102   }
103   return aResult;
104 }
105
106 //******************************************************************
107 bool allDocumentsActivated(QString& theNotActivatedNames)
108 {
109   bool anAllPartActivated = true;
110   QStringList aRefNames;
111
112   DocumentPtr aRootDoc = ModelAPI_Session::get()->moduleDocument();
113   int aSize = aRootDoc->size(ModelAPI_ResultPart::group());
114   for (int i = 0; i < aSize; i++) {
115     ObjectPtr aObject = aRootDoc->object(ModelAPI_ResultPart::group(), i);
116     ResultPartPtr aPart = std::dynamic_pointer_cast<ModelAPI_ResultPart>(aObject);
117     if (!aPart->isActivated()) {
118       anAllPartActivated = false;
119       aRefNames.append(aObject->data()->name().c_str());
120     }
121   }
122   theNotActivatedNames = aRefNames.join(", ");
123   return anAllPartActivated;
124 }
125
126 }