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 canRename(const ObjectPtr& theObject, const QString& theName)
153 std::string aType = theObject->groupName();
154 if (aType == ModelAPI_ResultParameter::group()) {
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();
169 DocumentPtr aDoc = theObject->document();
171 aDoc->objectByName(aType, theName.toStdString());
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();
186 //**************************************************************
188 XGUI_Workshop* workshop(ModuleBase_IWorkshop* theWorkshop)
190 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(theWorkshop);
191 return aConnector ? aConnector->workshop() : 0;
195 //********************************************************************
196 QString generateName(const ModuleBase_ViewerPrsPtr& thePrs)
198 if (!thePrs.get() || !thePrs->object().get())
201 GeomShapePtr aContext;
202 ObjectPtr anObject = thePrs->object();
203 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(anObject);
205 aContext = aResult->shape();
207 // TODO if there is this case
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)) {
217 switch (aShape.ShapeType()) {
218 case TopAbs_COMPOUND:
219 aTypeName = "compound";
221 case TopAbs_COMPSOLID:
222 aTypeName = "compsolid";
240 aTypeName = "vertex";
246 int aId = GeomAlgoAPI_CompoundBuilder::id(aContext, aSubShape);
247 aName += QString("/%1_%2").arg(aTypeName).arg(aId);