Salome HOME
6d4009a4a9d72ad11fdf0a98b3555a49bdf212f9
[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 "XGUI_ModuleConnector.h"
6 #include "XGUI_Workshop.h"
7
8 #include "ModuleBase_IWorkshop.h"
9
10 #include <TopoDS_Shape.hxx>
11 #include <ModelAPI_Object.h>
12 #include <ModelAPI_Result.h>
13 #include <ModelAPI_ResultParameter.h>
14 #include <ModelAPI_Feature.h>
15 #include <ModelAPI_Session.h>
16 #include <ModelAPI_Document.h>
17 #include <ModelAPI_ResultPart.h>
18 #include <ModelAPI_CompositeFeature.h>
19 #include <ModelAPI_Tools.h>
20 #include <Events_Error.h>
21
22 #include <GeomAPI_Shape.h>
23
24 #include <QDir>
25 #include <QMessageBox>
26
27 #include <iostream>
28 #include <sstream>
29
30 namespace XGUI_Tools {
31 //******************************************************************
32 QString dir(const QString& path, bool isAbs)
33 {
34   QDir aDir = QFileInfo(path).dir();
35   QString dirPath = isAbs ? aDir.absolutePath() : aDir.path();
36   if (dirPath == QString("."))
37     dirPath = QString();
38   return dirPath;
39 }
40
41 //******************************************************************
42 QString file(const QString& path, bool withExt)
43 {
44   QString fPath = path;
45   while (!fPath.isEmpty() && (fPath[fPath.length() - 1] == '\\' || fPath[fPath.length() - 1] == '/'))
46     fPath.remove(fPath.length() - 1, 1);
47
48   if (withExt)
49     return QFileInfo(fPath).fileName();
50   else
51     return QFileInfo(fPath).completeBaseName();
52 }
53
54 //******************************************************************
55 QString addSlash(const QString& path)
56 {
57   QString res = path;
58   if (!res.isEmpty() && res.at(res.length() - 1) != QChar('/')
59       && res.at(res.length() - 1) != QChar('\\'))
60     res += QDir::separator();
61   return res;
62 }
63
64 //******************************************************************
65 QString unionOfObjectNames(const QObjectPtrList& theObjects, const QString& theSeparator)
66 {
67   QStringList aObjectNames;
68   foreach (ObjectPtr aObj, theObjects) {
69     if (!aObj->data()->isValid())
70       continue;
71     aObjectNames << QString::fromStdString(aObj->data()->name());
72   }
73   return aObjectNames.join(", ");
74 }
75
76 //******************************************************************
77 bool isModelObject(FeaturePtr theFeature)
78 {
79   return theFeature && !theFeature->data();
80 }
81
82 //******************************************************************
83 std::string featureInfo(FeaturePtr theFeature)
84 {
85   std::ostringstream aStream;
86   if (theFeature)
87     aStream << theFeature.get() << " " << theFeature->getKind();
88   return QString(aStream.str().c_str()).toStdString();
89 }
90
91 //******************************************************************
92 /*FeaturePtr realFeature(const FeaturePtr theFeature)
93  {
94  if (theFeature->data()) {
95  return theFeature;
96  } else {
97  ObjectPtr aObject = std::dynamic_pointer_cast<ModelAPI_Object>(theFeature);
98  return aObject->featureRef();
99  }
100  }*/
101
102 //******************************************************************
103 bool canRemoveOrRename(QWidget* theParent, const QObjectPtrList& theObjects)
104 {
105   bool aResult = true;
106   QString aNotActivatedNames;
107   if (!XGUI_Tools::allDocumentsActivated(aNotActivatedNames)) {
108     DocumentPtr aModuleDoc = ModelAPI_Session::get()->moduleDocument();
109     bool aFoundPartSetObject = false;
110     foreach (ObjectPtr aObj, theObjects) {
111       if (aObj->groupName() == ModelAPI_ResultPart::group())
112         continue;
113       aFoundPartSetObject = aObj->document() == aModuleDoc;
114     }
115     if (aFoundPartSetObject) {
116       QMessageBox::StandardButton aRes = QMessageBox::warning(theParent, QObject::tr("Warning"),
117                QObject::tr("Selected objects can be used in Part documents which are not loaded: \
118 %1. Whould you like to continue?").arg(aNotActivatedNames),
119                QMessageBox::No | QMessageBox::Yes, QMessageBox::No);
120       aResult = aRes == QMessageBox::Yes;
121     }
122   }
123   return aResult;
124 }
125
126 //******************************************************************
127 bool canRename(const ObjectPtr& theObject, const QString& theName)
128 {
129   if (std::dynamic_pointer_cast<ModelAPI_ResultParameter>(theObject).get()) {
130     double aValue;
131     ResultParameterPtr aParam;
132     if (ModelAPI_Tools::findVariable(theObject->document(), qPrintable(theName), aValue, aParam)) {
133       QString aErrMsg(QObject::tr("Selected parameter can not be renamed to: %1. \
134  There is a parameter with the same name. Its value is: %2.").arg(qPrintable(theName)).arg(aValue));
135       // We can not use here a dialog box for message - it will crash editing process in ObjectBrowser
136       Events_Error::send(aErrMsg.toStdString());
137       return false;
138     }
139   }
140
141   return true;
142 }
143
144 //******************************************************************
145 bool allDocumentsActivated(QString& theNotActivatedNames)
146 {
147   bool anAllPartActivated = true;
148   QStringList aRefNames;
149
150   DocumentPtr aRootDoc = ModelAPI_Session::get()->moduleDocument();
151   int aSize = aRootDoc->size(ModelAPI_ResultPart::group());
152   for (int i = 0; i < aSize; i++) {
153     ObjectPtr aObject = aRootDoc->object(ModelAPI_ResultPart::group(), i);
154     ResultPartPtr aPart = std::dynamic_pointer_cast<ModelAPI_ResultPart>(aObject);
155     if (!aPart->isActivated()) {
156       anAllPartActivated = false;
157       aRefNames.append(aObject->data()->name().c_str());
158     }
159   }
160   theNotActivatedNames = aRefNames.join(", ");
161   return anAllPartActivated;
162 }
163
164 //**************************************************************
165
166 XGUI_Workshop* workshop(ModuleBase_IWorkshop* theWorkshop)
167 {
168   XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(theWorkshop);
169   return aConnector->workshop();
170 }
171
172 }