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"
10 #include <TopoDS_Shape.hxx>
11 #include <ModelAPI_Object.h>
12 #include <ModelAPI_Result.h>
13 #include <ModelAPI_ResultParameter.h>
14 #include <ModelAPI_Feature.h>
15 #include <ModelAPI_Session.h>
16 #include <ModelAPI_Document.h>
17 #include <ModelAPI_ResultPart.h>
18 #include <ModelAPI_CompositeFeature.h>
19 #include <ModelAPI_Tools.h>
20 #include <Events_Error.h>
22 #include <GeomAPI_Shape.h>
25 #include <QMessageBox>
30 namespace XGUI_Tools {
31 //******************************************************************
32 QString dir(const QString& path, bool isAbs)
34 QDir aDir = QFileInfo(path).dir();
35 QString dirPath = isAbs ? aDir.absolutePath() : aDir.path();
36 if (dirPath == QString("."))
41 //******************************************************************
42 QString file(const QString& path, bool withExt)
45 while (!fPath.isEmpty() && (fPath[fPath.length() - 1] == '\\' || fPath[fPath.length() - 1] == '/'))
46 fPath.remove(fPath.length() - 1, 1);
49 return QFileInfo(fPath).fileName();
51 return QFileInfo(fPath).completeBaseName();
54 //******************************************************************
55 QString addSlash(const QString& path)
58 if (!res.isEmpty() && res.at(res.length() - 1) != QChar('/')
59 && res.at(res.length() - 1) != QChar('\\'))
60 res += QDir::separator();
64 //******************************************************************
65 QString unionOfObjectNames(const QObjectPtrList& theObjects, const QString& theSeparator)
67 QStringList aObjectNames;
68 foreach (ObjectPtr aObj, theObjects) {
69 if (aObj->data()->isValid())
70 aObjectNames << QString::fromStdString(aObj->data()->name());
72 if (aObjectNames.count() == 0)
74 if (aObjectNames.count() == 1)
75 return aObjectNames.first();
76 return aObjectNames.join(theSeparator);
79 //******************************************************************
80 bool isModelObject(FeaturePtr theFeature)
82 return theFeature && !theFeature->data();
85 //******************************************************************
86 std::string featureInfo(FeaturePtr theFeature)
88 std::ostringstream aStream;
90 aStream << theFeature.get() << " " << theFeature->getKind();
91 return QString(aStream.str().c_str()).toStdString();
94 //******************************************************************
95 /*FeaturePtr realFeature(const FeaturePtr theFeature)
97 if (theFeature->data()) {
100 ObjectPtr aObject = std::dynamic_pointer_cast<ModelAPI_Object>(theFeature);
101 return aObject->featureRef();
105 //******************************************************************
106 bool canRemoveOrRename(QWidget* theParent, const QObjectPtrList& theObjects)
109 QString aNotActivatedNames;
110 if (!XGUI_Tools::allDocumentsActivated(aNotActivatedNames)) {
111 DocumentPtr aModuleDoc = ModelAPI_Session::get()->moduleDocument();
112 bool aFoundPartSetObject = false;
113 foreach (ObjectPtr aObj, theObjects) {
114 if (aObj->groupName() == ModelAPI_ResultPart::group())
116 aFoundPartSetObject = aObj->document() == aModuleDoc;
118 if (aFoundPartSetObject) {
119 QMessageBox::StandardButton aRes = QMessageBox::warning(theParent, QObject::tr("Warning"),
120 QObject::tr("Selected objects can be used in Part documents which are not loaded: \
121 %1. Whould you like to continue?").arg(aNotActivatedNames),
122 QMessageBox::No | QMessageBox::Yes, QMessageBox::No);
123 aResult = aRes == QMessageBox::Yes;
129 //******************************************************************
130 bool canRename(const ObjectPtr& theObject, const QString& theName)
132 if (std::dynamic_pointer_cast<ModelAPI_ResultParameter>(theObject).get()) {
134 ResultParameterPtr aParam;
135 if (ModelAPI_Tools::findVariable(theObject->document(),
136 FeaturePtr(), qPrintable(theName), aValue, aParam)) {
137 QString aErrMsg(QObject::tr("Selected parameter can not be renamed to: %1. \
138 There is a parameter with the same name. Its value is: %2.").arg(qPrintable(theName)).arg(aValue));
139 // We can not use here a dialog box for message - it will crash editing process in ObjectBrowser
140 Events_Error::send(aErrMsg.toStdString());
148 //******************************************************************
149 bool allDocumentsActivated(QString& theNotActivatedNames)
151 bool anAllPartActivated = true;
152 QStringList aRefNames;
154 DocumentPtr aRootDoc = ModelAPI_Session::get()->moduleDocument();
155 int aSize = aRootDoc->size(ModelAPI_ResultPart::group());
156 for (int i = 0; i < aSize; i++) {
157 ObjectPtr aObject = aRootDoc->object(ModelAPI_ResultPart::group(), i);
158 ResultPartPtr aPart = std::dynamic_pointer_cast<ModelAPI_ResultPart>(aObject);
159 if (!aPart->isActivated()) {
160 anAllPartActivated = false;
161 aRefNames.append(aObject->data()->name().c_str());
164 theNotActivatedNames = aRefNames.join(", ");
165 return anAllPartActivated;
168 //**************************************************************
170 XGUI_Workshop* workshop(ModuleBase_IWorkshop* theWorkshop)
172 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(theWorkshop);
173 return aConnector->workshop();