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