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