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>
31 namespace XGUI_Tools {
32 //******************************************************************
33 QString dir(const QString& path, bool isAbs)
35 QDir aDir = QFileInfo(path).dir();
36 QString dirPath = isAbs ? aDir.absolutePath() : aDir.path();
37 if (dirPath == QString("."))
42 //******************************************************************
43 QString file(const QString& path, bool withExt)
46 while (!fPath.isEmpty() && (fPath[fPath.length() - 1] == '\\' || fPath[fPath.length() - 1] == '/'))
47 fPath.remove(fPath.length() - 1, 1);
50 return QFileInfo(fPath).fileName();
52 return QFileInfo(fPath).completeBaseName();
55 //******************************************************************
56 QString addSlash(const QString& path)
59 if (!res.isEmpty() && res.at(res.length() - 1) != QChar('/')
60 && res.at(res.length() - 1) != QChar('\\'))
61 res += QDir::separator();
65 //******************************************************************
66 QString unionOfObjectNames(const QObjectPtrList& theObjects, const QString& theSeparator)
68 QStringList aObjectNames;
69 foreach (ObjectPtr aObj, theObjects) {
70 if (aObj->data()->isValid())
71 aObjectNames << QString::fromStdString(aObj->data()->name());
73 if (aObjectNames.count() == 0)
75 if (aObjectNames.count() == 1)
76 return aObjectNames.first();
77 return aObjectNames.join(theSeparator);
80 //******************************************************************
81 bool isModelObject(FeaturePtr theFeature)
83 return theFeature && !theFeature->data();
86 //******************************************************************
87 std::string featureInfo(FeaturePtr theFeature)
89 std::ostringstream aStream;
91 aStream << theFeature.get() << " " << theFeature->getKind();
92 return QString(aStream.str().c_str()).toStdString();
95 //******************************************************************
96 /*FeaturePtr realFeature(const FeaturePtr theFeature)
98 if (theFeature->data()) {
101 ObjectPtr aObject = std::dynamic_pointer_cast<ModelAPI_Object>(theFeature);
102 return aObject->featureRef();
106 //******************************************************************
107 bool canRemoveOrRename(QWidget* theParent, const QObjectPtrList& theObjects)
110 std::string aNotActivatedNames;
111 if (!ModelAPI_Tools::allDocumentsActivated(aNotActivatedNames)) {
112 DocumentPtr aModuleDoc = ModelAPI_Session::get()->moduleDocument();
113 bool aFoundPartSetObject = false;
114 foreach (ObjectPtr aObj, theObjects) {
115 if (aObj->groupName() == ModelAPI_ResultPart::group())
117 aFoundPartSetObject = aObj->document() == aModuleDoc;
119 if (aFoundPartSetObject) {
120 QMessageBox::StandardButton aRes = QMessageBox::warning(theParent, QObject::tr("Warning"),
121 QObject::tr("Selected objects can be used in Part documents which are not loaded: \
122 %1. Whould you like to continue?").arg(aNotActivatedNames.c_str()),
123 QMessageBox::No | QMessageBox::Yes, QMessageBox::No);
124 aResult = aRes == QMessageBox::Yes;
130 //******************************************************************
131 bool canRename(const ObjectPtr& theObject, const QString& theName)
133 if (std::dynamic_pointer_cast<ModelAPI_ResultParameter>(theObject).get()) {
135 ResultParameterPtr aParam;
136 if (ModelAPI_Tools::findVariable(theObject->document(),
137 FeaturePtr(), qPrintable(theName), aValue, aParam)) {
138 QString aErrMsg(QObject::tr("Selected parameter can not be renamed to: %1. \
139 There is a parameter with the same name. Its value is: %2.").arg(qPrintable(theName)).arg(aValue));
140 // We can not use here a dialog box for message - it will crash editing process in ObjectBrowser
141 Events_Error::send(aErrMsg.toStdString());
149 //**************************************************************
151 XGUI_Workshop* workshop(ModuleBase_IWorkshop* theWorkshop)
153 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(theWorkshop);
154 return aConnector->workshop();