1 // Copyright (C) 2014-2017 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
18 // email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
21 #include "XGUI_Tools.h"
23 #include "XGUI_ModuleConnector.h"
24 #include "XGUI_Workshop.h"
26 #include "ModuleBase_IWorkshop.h"
27 #include "ModuleBase_Tools.h"
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>
43 #include <GeomAPI_Shape.h>
44 #include <GeomAlgoAPI_CompoundBuilder.h>
46 #include <TopoDS_Shape.hxx>
49 #include <QMessageBox>
55 namespace XGUI_Tools {
56 //******************************************************************
57 QString dir(const QString& path, bool isAbs)
59 QDir aDir = QFileInfo(path).dir();
60 QString dirPath = isAbs ? aDir.absolutePath() : aDir.path();
61 if (dirPath == QString("."))
66 //******************************************************************
67 QString file(const QString& path, bool withExt)
70 while (!fPath.isEmpty() && (fPath[fPath.length() - 1] == '\\' ||
71 fPath[fPath.length() - 1] == '/'))
72 fPath.remove(fPath.length() - 1, 1);
75 return QFileInfo(fPath).fileName();
77 return QFileInfo(fPath).completeBaseName();
80 //******************************************************************
81 QString addSlash(const QString& path)
84 if (!res.isEmpty() && res.at(res.length() - 1) != QChar('/')
85 && res.at(res.length() - 1) != QChar('\\'))
86 res += QDir::separator();
90 //******************************************************************
91 QString unionOfObjectNames(const QObjectPtrList& theObjects, const QString& theSeparator)
93 QStringList aObjectNames;
94 foreach (ObjectPtr aObj, theObjects) {
95 if (aObj->data()->isValid())
96 aObjectNames << QString::fromStdString(aObj->data()->name());
98 if (aObjectNames.count() == 0)
100 if (aObjectNames.count() == 1)
101 return aObjectNames.first();
102 return aObjectNames.join(theSeparator);
105 //******************************************************************
106 bool isModelObject(FeaturePtr theFeature)
108 return theFeature && !theFeature->data();
111 //******************************************************************
112 std::string featureInfo(FeaturePtr theFeature)
114 std::ostringstream aStream;
116 aStream << theFeature.get() << " " << theFeature->getKind();
117 return QString(aStream.str().c_str()).toStdString();
120 //******************************************************************
121 /*FeaturePtr realFeature(const FeaturePtr theFeature)
123 if (theFeature->data()) {
126 ObjectPtr aObject = std::dynamic_pointer_cast<ModelAPI_Object>(theFeature);
127 return aObject->featureRef();
132 //******************************************************************
133 bool canRemoveOrRename(QWidget* theParent, const std::set<FeaturePtr>& theFeatures)
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;
151 //******************************************************************
152 bool canRename(const ObjectPtr& theObject, const QString& theName)
154 std::string aType = theObject->groupName();
155 if (aType == ModelAPI_ResultParameter::group()) {
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();
170 DocumentPtr aDoc = theObject->document();
172 aDoc->objectByName(aType, theName.toStdString());
175 QString aErrMsg(QObject::tr("%1 with name %2 already exists.").
176 arg(aType.c_str()).arg(theName));
177 // We can not use here a dialog box for message -
178 // it will crash editing process in ObjectBrowser
179 Events_InfoMessage("XGUI_Tools", aErrMsg.toStdString()).send();
187 //**************************************************************
189 XGUI_Workshop* workshop(ModuleBase_IWorkshop* theWorkshop)
191 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(theWorkshop);
192 return aConnector ? aConnector->workshop() : 0;
196 //********************************************************************
197 QString generateName(const ModuleBase_ViewerPrsPtr& thePrs)
199 if (!thePrs.get() || !thePrs->object().get())
202 GeomShapePtr aContext;
203 ObjectPtr anObject = thePrs->object();
204 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(anObject);
206 aContext = aResult->shape();
208 // TODO if there is this case
211 QString aName = anObject->data()->name().c_str();
212 if (aContext.get()) {
213 GeomShapePtr aSubShape(new GeomAPI_Shape());
214 TopoDS_Shape aShape = ModuleBase_Tools::getSelectedShape(thePrs);
215 aSubShape->setImpl(new TopoDS_Shape(aShape));
216 if (!aSubShape->isEqual(aContext)) {
218 switch (aShape.ShapeType()) {
219 case TopAbs_COMPOUND:
220 aTypeName = "compound";
222 case TopAbs_COMPSOLID:
223 aTypeName = "compsolid";
241 aTypeName = "vertex";
247 int aId = GeomAlgoAPI_CompoundBuilder::id(aContext, aSubShape);
248 aName += QString("/%1_%2").arg(aTypeName).arg(aId);