Salome HOME
Merge branch 'V9_2_2_BR'
[modules/shaper.git] / src / XGUI / XGUI_Tools.cpp
1 // Copyright (C) 2014-2019  CEA/DEN, EDF R&D
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19
20 #include "XGUI_Tools.h"
21
22 #include "XGUI_ModuleConnector.h"
23 #include "XGUI_Workshop.h"
24
25 #include "ModuleBase_IWorkshop.h"
26 #include "ModuleBase_Tools.h"
27
28 #include <TopoDS_Shape.hxx>
29 #include <ModelAPI_Object.h>
30 #include <ModelAPI_Result.h>
31 #include <ModelAPI_ResultParameter.h>
32 #include <ModelAPI_Feature.h>
33 #include <ModelAPI_Session.h>
34 #include <ModelAPI_Document.h>
35 #include <ModelAPI_ResultPart.h>
36 #include <ModelAPI_CompositeFeature.h>
37 #include <ModelAPI_Tools.h>
38 #include <ModelAPI_ResultConstruction.h>
39 #include <ModelAPI_ResultBody.h>
40 #include <Events_InfoMessage.h>
41
42 #include <GeomAPI_Shape.h>
43 #include <GeomAlgoAPI_CompoundBuilder.h>
44
45 #include <TopoDS_Shape.hxx>
46
47 #include <QDir>
48 #include <QMessageBox>
49
50 #include <iostream>
51 #include <sstream>
52 #include <string>
53
54 namespace XGUI_Tools {
55 //******************************************************************
56 QString dir(const QString& path, bool isAbs)
57 {
58   QDir aDir = QFileInfo(path).dir();
59   QString dirPath = isAbs ? aDir.absolutePath() : aDir.path();
60   if (dirPath == QString("."))
61     dirPath = QString();
62   return dirPath;
63 }
64
65 //******************************************************************
66 QString file(const QString& path, bool withExt)
67 {
68   QString fPath = path;
69   while (!fPath.isEmpty() && (fPath[fPath.length() - 1] == '\\' ||
70           fPath[fPath.length() - 1] == '/'))
71     fPath.remove(fPath.length() - 1, 1);
72
73   if (withExt)
74     return QFileInfo(fPath).fileName();
75   else
76     return QFileInfo(fPath).completeBaseName();
77 }
78
79 //******************************************************************
80 QString addSlash(const QString& path)
81 {
82   QString res = path;
83   if (!res.isEmpty() && res.at(res.length() - 1) != QChar('/')
84       && res.at(res.length() - 1) != QChar('\\'))
85     res += QDir::separator();
86   return res;
87 }
88
89 //******************************************************************
90 QString unionOfObjectNames(const QObjectPtrList& theObjects, const QString& theSeparator)
91 {
92   QStringList aObjectNames;
93   foreach (ObjectPtr aObj, theObjects) {
94     if (aObj->data()->isValid())
95       aObjectNames << QString::fromStdString(aObj->data()->name());
96   }
97   if (aObjectNames.count() == 0)
98     return QString();
99   if (aObjectNames.count() == 1)
100     return aObjectNames.first();
101   return aObjectNames.join(theSeparator);
102 }
103
104 //******************************************************************
105 bool isModelObject(FeaturePtr theFeature)
106 {
107   return theFeature && !theFeature->data();
108 }
109
110 //******************************************************************
111 std::string featureInfo(FeaturePtr theFeature)
112 {
113   std::ostringstream aStream;
114   if (theFeature)
115     aStream << theFeature.get() << " " << theFeature->getKind();
116   return QString(aStream.str().c_str()).toStdString();
117 }
118
119 //******************************************************************
120 /*FeaturePtr realFeature(const FeaturePtr theFeature)
121  {
122  if (theFeature->data()) {
123  return theFeature;
124  } else {
125  ObjectPtr aObject = std::dynamic_pointer_cast<ModelAPI_Object>(theFeature);
126  return aObject->featureRef();
127  }
128  }*/
129
130
131 //******************************************************************
132 bool canRemoveOrRename(QWidget* theParent, const std::set<FeaturePtr>& theFeatures)
133 {
134   bool aResult = true;
135   std::string aNotActivatedNames;
136   if (!ModelAPI_Tools::allDocumentsActivated(aNotActivatedNames)) {
137     bool aFoundPartSetObject = ModuleBase_Tools::hasModuleDocumentFeature(theFeatures);
138     if (aFoundPartSetObject) {
139       const char* aKeyStr = "Selected objects can be used in Part documents which are not loaded: "
140                             "%1. Whould you like to continue?";
141       QMessageBox::StandardButton aRes = QMessageBox::warning(theParent, QObject::tr("Warning"),
142                QObject::tr(aKeyStr).arg(aNotActivatedNames.c_str()),
143                QMessageBox::No | QMessageBox::Yes, QMessageBox::No);
144       aResult = aRes == QMessageBox::Yes;
145     }
146   }
147   return aResult;
148 }
149
150 //******************************************************************
151 bool canRename(const ObjectPtr& theObject, const QString& theName)
152 {
153   std::string aType = theObject->groupName();
154   if (aType == ModelAPI_ResultParameter::group()) {
155     double aValue;
156     ResultParameterPtr aParam;
157     if (ModelAPI_Tools::findVariable(theObject->document(),
158           FeaturePtr(), qPrintable(theName), aValue, aParam)) {
159       const char* aKeyStr = "Selected parameter can not be renamed to: %1. "
160                             "There is a parameter with the same name. Its value is: %2.";
161       QString aErrMsg(QObject::tr(aKeyStr).arg(theName).arg(aValue));
162       // We can not use here a dialog box for message -
163       // it will crash editing process in ObjectBrowser
164       Events_InfoMessage("XGUI_Tools", aErrMsg.toStdString()).send();
165       return false;
166     }
167   }
168   else {
169     DocumentPtr aDoc = theObject->document();
170     ObjectPtr aObj =
171       aDoc->objectByName(aType, theName.toStdString());
172
173     if (aObj.get() && theObject != aObj) {
174       QString aErrMsg(QObject::tr("Name %2 already exists in %1.").
175         arg(aType.c_str()).arg(theName));
176       // We can not use here a dialog box for message -
177       // it will crash editing process in ObjectBrowser
178       Events_InfoMessage("XGUI_Tools", aErrMsg.toStdString()).send();
179       return false;
180     }
181   }
182
183   return true;
184 }
185
186 //**************************************************************
187
188 XGUI_Workshop* workshop(ModuleBase_IWorkshop* theWorkshop)
189 {
190   XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(theWorkshop);
191   return aConnector ? aConnector->workshop() : 0;
192 }
193
194
195 //********************************************************************
196 QString generateName(const ModuleBase_ViewerPrsPtr& thePrs)
197 {
198   if (!thePrs.get() || !thePrs->object().get())
199     return "Undefined";
200
201   GeomShapePtr aContext;
202   ObjectPtr anObject = thePrs->object();
203   ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(anObject);
204   if (aResult.get())
205     aContext = aResult->shape();
206   else {
207     // TODO if there is this case
208   }
209
210   QString aName = anObject->data()->name().c_str();
211   if (aContext.get()) {
212     GeomShapePtr aSubShape(new GeomAPI_Shape());
213     TopoDS_Shape aShape = ModuleBase_Tools::getSelectedShape(thePrs);
214     aSubShape->setImpl(new TopoDS_Shape(aShape));
215     if (!aSubShape->isEqual(aContext)) {
216       QString aTypeName;
217       switch (aShape.ShapeType()) {
218       case TopAbs_COMPOUND:
219         aTypeName = "compound";
220         break;
221       case TopAbs_COMPSOLID:
222         aTypeName = "compsolid";
223         break;
224       case TopAbs_SOLID:
225         aTypeName = "solid";
226         break;
227       case TopAbs_SHELL:
228         aTypeName = "shell";
229         break;
230       case TopAbs_FACE:
231         aTypeName = "face";
232         break;
233       case TopAbs_WIRE:
234         aTypeName = "wire";
235         break;
236       case TopAbs_EDGE:
237         aTypeName = "edge";
238         break;
239       case TopAbs_VERTEX:
240         aTypeName = "vertex";
241         break;
242       case TopAbs_SHAPE:
243         aTypeName = "shape";
244         break;
245       }
246       int aId = GeomAlgoAPI_CompoundBuilder::id(aContext, aSubShape);
247       aName += QString("/%1_%2").arg(aTypeName).arg(aId);
248     }
249   }
250   return aName;
251 }
252
253 }