1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D -->
3 #include "XGUI_Tools.h"
5 #include "XGUI_ModuleConnector.h"
6 #include "XGUI_Workshop.h"
8 #include "ModuleBase_IWorkshop.h"
9 #include "ModuleBase_Tools.h"
11 #include <TopoDS_Shape.hxx>
12 #include <ModelAPI_Object.h>
13 #include <ModelAPI_Result.h>
14 #include <ModelAPI_ResultParameter.h>
15 #include <ModelAPI_Feature.h>
16 #include <ModelAPI_Session.h>
17 #include <ModelAPI_Document.h>
18 #include <ModelAPI_ResultPart.h>
19 #include <ModelAPI_CompositeFeature.h>
20 #include <ModelAPI_Tools.h>
21 #include <Events_Error.h>
23 #include <GeomAPI_Shape.h>
26 #include <QMessageBox>
32 namespace XGUI_Tools {
33 //******************************************************************
34 QString dir(const QString& path, bool isAbs)
36 QDir aDir = QFileInfo(path).dir();
37 QString dirPath = isAbs ? aDir.absolutePath() : aDir.path();
38 if (dirPath == QString("."))
43 //******************************************************************
44 QString file(const QString& path, bool withExt)
47 while (!fPath.isEmpty() && (fPath[fPath.length() - 1] == '\\' || fPath[fPath.length() - 1] == '/'))
48 fPath.remove(fPath.length() - 1, 1);
51 return QFileInfo(fPath).fileName();
53 return QFileInfo(fPath).completeBaseName();
56 //******************************************************************
57 QString addSlash(const QString& path)
60 if (!res.isEmpty() && res.at(res.length() - 1) != QChar('/')
61 && res.at(res.length() - 1) != QChar('\\'))
62 res += QDir::separator();
66 //******************************************************************
67 QString unionOfObjectNames(const QObjectPtrList& theObjects, const QString& theSeparator)
69 QStringList aObjectNames;
70 foreach (ObjectPtr aObj, theObjects) {
71 if (aObj->data()->isValid())
72 aObjectNames << QString::fromStdString(aObj->data()->name());
74 if (aObjectNames.count() == 0)
76 if (aObjectNames.count() == 1)
77 return aObjectNames.first();
78 return aObjectNames.join(theSeparator);
81 //******************************************************************
82 bool isModelObject(FeaturePtr theFeature)
84 return theFeature && !theFeature->data();
87 //******************************************************************
88 std::string featureInfo(FeaturePtr theFeature)
90 std::ostringstream aStream;
92 aStream << theFeature.get() << " " << theFeature->getKind();
93 return QString(aStream.str().c_str()).toStdString();
96 //******************************************************************
97 /*FeaturePtr realFeature(const FeaturePtr theFeature)
99 if (theFeature->data()) {
102 ObjectPtr aObject = std::dynamic_pointer_cast<ModelAPI_Object>(theFeature);
103 return aObject->featureRef();
108 //******************************************************************
109 bool canRemoveOrRename(QWidget* theParent, const std::set<FeaturePtr>& theFeatures)
112 std::string aNotActivatedNames;
113 if (!ModelAPI_Tools::allDocumentsActivated(aNotActivatedNames)) {
114 bool aFoundPartSetObject = ModuleBase_Tools::hasModuleDocumentFeature(theFeatures);
115 if (aFoundPartSetObject) {
116 QMessageBox::StandardButton aRes = QMessageBox::warning(theParent, QObject::tr("Warning"),
117 QObject::tr("Selected objects can be used in Part documents which are not loaded: \
118 %1. Whould you like to continue?").arg(aNotActivatedNames.c_str()),
119 QMessageBox::No | QMessageBox::Yes, QMessageBox::No);
120 aResult = aRes == QMessageBox::Yes;
126 //******************************************************************
127 bool canRename(const ObjectPtr& theObject, const QString& theName)
129 if (std::dynamic_pointer_cast<ModelAPI_ResultParameter>(theObject).get()) {
131 ResultParameterPtr aParam;
132 if (ModelAPI_Tools::findVariable(theObject->document(),
133 FeaturePtr(), qPrintable(theName), aValue, aParam)) {
134 QString aErrMsg(QObject::tr("Selected parameter can not be renamed to: %1. \
135 There is a parameter with the same name. Its value is: %2.").arg(qPrintable(theName)).arg(aValue));
136 // We can not use here a dialog box for message - it will crash editing process in ObjectBrowser
137 Events_Error::send(aErrMsg.toStdString());
145 //**************************************************************
147 XGUI_Workshop* workshop(ModuleBase_IWorkshop* theWorkshop)
149 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(theWorkshop);
150 return aConnector->workshop();