1 // Copyright (C) 2014-2019 CEA/DEN, EDF R&D
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.
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.
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
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 #include "XGUI_Tools.h"
22 #include "XGUI_ModuleConnector.h"
23 #include "XGUI_Workshop.h"
25 #include "ModuleBase_IWorkshop.h"
26 #include "ModuleBase_Tools.h"
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>
42 #include <GeomAPI_Shape.h>
43 #include <GeomAlgoAPI_CompoundBuilder.h>
45 #include <TopoDS_Shape.hxx>
48 #include <QMessageBox>
54 namespace XGUI_Tools {
55 //******************************************************************
56 QString dir(const QString& path, bool isAbs)
58 QDir aDir = QFileInfo(path).dir();
59 QString dirPath = isAbs ? aDir.absolutePath() : aDir.path();
60 if (dirPath == QString("."))
65 //******************************************************************
66 QString file(const QString& path, bool withExt)
69 while (!fPath.isEmpty() && (fPath[fPath.length() - 1] == '\\' ||
70 fPath[fPath.length() - 1] == '/'))
71 fPath.remove(fPath.length() - 1, 1);
74 return QFileInfo(fPath).fileName();
76 return QFileInfo(fPath).completeBaseName();
79 //******************************************************************
80 QString addSlash(const QString& path)
83 if (!res.isEmpty() && res.at(res.length() - 1) != QChar('/')
84 && res.at(res.length() - 1) != QChar('\\'))
85 res += QDir::separator();
89 //******************************************************************
90 QString unionOfObjectNames(const QObjectPtrList& theObjects, const QString& theSeparator)
92 QStringList aObjectNames;
93 foreach (ObjectPtr aObj, theObjects) {
94 if (aObj->data()->isValid())
95 aObjectNames << QString::fromStdString(aObj->data()->name());
97 if (aObjectNames.count() == 0)
99 if (aObjectNames.count() == 1)
100 return aObjectNames.first();
101 return aObjectNames.join(theSeparator);
104 //******************************************************************
105 bool isModelObject(FeaturePtr theFeature)
107 return theFeature && !theFeature->data();
110 //******************************************************************
111 std::string featureInfo(FeaturePtr theFeature)
113 std::ostringstream aStream;
115 aStream << theFeature.get() << " " << theFeature->getKind();
116 return QString(aStream.str().c_str()).toStdString();
119 //******************************************************************
120 /*FeaturePtr realFeature(const FeaturePtr theFeature)
122 if (theFeature->data()) {
125 ObjectPtr aObject = std::dynamic_pointer_cast<ModelAPI_Object>(theFeature);
126 return aObject->featureRef();
131 //******************************************************************
132 bool canRemoveOrRename(QWidget* theParent, const std::set<FeaturePtr>& theFeatures)
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;
150 //******************************************************************
151 bool isAscii(const QString& theStr)
154 for (int i = 0; i < theStr.size(); i++) {
155 aCh = theStr[i].toLatin1();
162 //******************************************************************
163 bool canRename(const ObjectPtr& theObject, const QString& theName)
165 std::string aType = theObject->groupName();
166 if (aType == ModelAPI_ResultParameter::group()) {
167 // For parameters names only ASCII symbols have to be used
168 if (!isAscii(theName))
172 ResultParameterPtr aParam;
173 if (ModelAPI_Tools::findVariable(theObject->document(),
174 FeaturePtr(), qPrintable(theName), aValue, aParam)) {
175 const char* aKeyStr = "Selected parameter can not be renamed to: %1. "
176 "There is a parameter with the same name. Its value is: %2.";
177 QString aErrMsg(QObject::tr(aKeyStr).arg(theName).arg(aValue));
178 // We can not use here a dialog box for message -
179 // it will crash editing process in ObjectBrowser
180 Events_InfoMessage("XGUI_Tools", aErrMsg.toStdString()).send();
185 DocumentPtr aDoc = theObject->document();
187 aDoc->objectByName(aType, theName.toStdString());
189 if (aObj.get() && theObject != aObj) {
190 QString aErrMsg(QObject::tr("Name %2 already exists in %1.").
191 arg(aType.c_str()).arg(theName));
192 // We can not use here a dialog box for message -
193 // it will crash editing process in ObjectBrowser
194 Events_InfoMessage("XGUI_Tools", aErrMsg.toStdString()).send();
202 //**************************************************************
204 XGUI_Workshop* workshop(ModuleBase_IWorkshop* theWorkshop)
206 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(theWorkshop);
207 return aConnector ? aConnector->workshop() : 0;
211 //********************************************************************
212 QString generateName(const ModuleBase_ViewerPrsPtr& thePrs)
214 if (!thePrs.get() || !thePrs->object().get())
217 GeomShapePtr aContext;
218 ObjectPtr anObject = thePrs->object();
219 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(anObject);
221 aContext = aResult->shape();
223 // TODO if there is this case
226 QString aName = anObject->data()->name().c_str();
227 if (aContext.get()) {
228 GeomShapePtr aSubShape(new GeomAPI_Shape());
229 TopoDS_Shape aShape = ModuleBase_Tools::getSelectedShape(thePrs);
230 aSubShape->setImpl(new TopoDS_Shape(aShape));
231 if (!aSubShape->isEqual(aContext)) {
233 switch (aShape.ShapeType()) {
234 case TopAbs_COMPOUND:
235 aTypeName = "compound";
237 case TopAbs_COMPSOLID:
238 aTypeName = "compsolid";
256 aTypeName = "vertex";
262 int aId = GeomAlgoAPI_CompoundBuilder::id(aContext, aSubShape);
263 aName += QString("/%1_%2").arg(aTypeName).arg(aId);