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())
71 aObjectNames << QString::fromStdString(aObj->data()->name());
73 return aObjectNames.join(", ");
76 //******************************************************************
77 bool isModelObject(FeaturePtr theFeature)
79 return theFeature && !theFeature->data();
82 //******************************************************************
83 std::string featureInfo(FeaturePtr theFeature)
85 std::ostringstream aStream;
87 aStream << theFeature.get() << " " << theFeature->getKind();
88 return QString(aStream.str().c_str()).toStdString();
91 //******************************************************************
92 /*FeaturePtr realFeature(const FeaturePtr theFeature)
94 if (theFeature->data()) {
97 ObjectPtr aObject = std::dynamic_pointer_cast<ModelAPI_Object>(theFeature);
98 return aObject->featureRef();
102 //******************************************************************
103 bool canRemoveOrRename(QWidget* theParent, const QObjectPtrList& theObjects)
106 QString aNotActivatedNames;
107 if (!XGUI_Tools::allDocumentsActivated(aNotActivatedNames)) {
108 DocumentPtr aModuleDoc = ModelAPI_Session::get()->moduleDocument();
109 bool aFoundPartSetObject = false;
110 foreach (ObjectPtr aObj, theObjects) {
111 if (aObj->groupName() == ModelAPI_ResultPart::group())
113 aFoundPartSetObject = aObj->document() == aModuleDoc;
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),
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(), qPrintable(theName), aValue, aParam)) {
133 QString aErrMsg(QObject::tr("Selected parameter can not be renamed to: %1. \
134 There is a parameter with the same name. Its value is: %2.").arg(qPrintable(theName)).arg(aValue));
135 // We can not use here a dialog box for message - it will crash editing process in ObjectBrowser
136 Events_Error::send(aErrMsg.toStdString());
144 //******************************************************************
145 bool allDocumentsActivated(QString& theNotActivatedNames)
147 bool anAllPartActivated = true;
148 QStringList aRefNames;
150 DocumentPtr aRootDoc = ModelAPI_Session::get()->moduleDocument();
151 int aSize = aRootDoc->size(ModelAPI_ResultPart::group());
152 for (int i = 0; i < aSize; i++) {
153 ObjectPtr aObject = aRootDoc->object(ModelAPI_ResultPart::group(), i);
154 ResultPartPtr aPart = std::dynamic_pointer_cast<ModelAPI_ResultPart>(aObject);
155 if (!aPart->isActivated()) {
156 anAllPartActivated = false;
157 aRefNames.append(aObject->data()->name().c_str());
160 theNotActivatedNames = aRefNames.join(", ");
161 return anAllPartActivated;
164 //**************************************************************
166 XGUI_Workshop* workshop(ModuleBase_IWorkshop* theWorkshop)
168 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(theWorkshop);
169 return aConnector->workshop();