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_InfoMessage.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] == '\\' ||
48 fPath[fPath.length() - 1] == '/'))
49 fPath.remove(fPath.length() - 1, 1);
52 return QFileInfo(fPath).fileName();
54 return QFileInfo(fPath).completeBaseName();
57 //******************************************************************
58 QString addSlash(const QString& path)
61 if (!res.isEmpty() && res.at(res.length() - 1) != QChar('/')
62 && res.at(res.length() - 1) != QChar('\\'))
63 res += QDir::separator();
67 //******************************************************************
68 QString unionOfObjectNames(const QObjectPtrList& theObjects, const QString& theSeparator)
70 QStringList aObjectNames;
71 foreach (ObjectPtr aObj, theObjects) {
72 if (aObj->data()->isValid())
73 aObjectNames << QString::fromStdString(aObj->data()->name());
75 if (aObjectNames.count() == 0)
77 if (aObjectNames.count() == 1)
78 return aObjectNames.first();
79 return aObjectNames.join(theSeparator);
82 //******************************************************************
83 bool isModelObject(FeaturePtr theFeature)
85 return theFeature && !theFeature->data();
88 //******************************************************************
89 std::string featureInfo(FeaturePtr theFeature)
91 std::ostringstream aStream;
93 aStream << theFeature.get() << " " << theFeature->getKind();
94 return QString(aStream.str().c_str()).toStdString();
97 //******************************************************************
98 /*FeaturePtr realFeature(const FeaturePtr theFeature)
100 if (theFeature->data()) {
103 ObjectPtr aObject = std::dynamic_pointer_cast<ModelAPI_Object>(theFeature);
104 return aObject->featureRef();
109 //******************************************************************
110 bool canRemoveOrRename(QWidget* theParent, const std::set<FeaturePtr>& theFeatures)
113 std::string aNotActivatedNames;
114 if (!ModelAPI_Tools::allDocumentsActivated(aNotActivatedNames)) {
115 bool aFoundPartSetObject = ModuleBase_Tools::hasModuleDocumentFeature(theFeatures);
116 if (aFoundPartSetObject) {
117 const char* aKeyStr = "Selected objects can be used in Part documents which are not loaded: "
118 "%1. Whould you like to continue?";
119 QMessageBox::StandardButton aRes = QMessageBox::warning(theParent, QObject::tr("Warning"),
120 QObject::tr(aKeyStr).arg(aNotActivatedNames.c_str()),
121 QMessageBox::No | QMessageBox::Yes, QMessageBox::No);
122 aResult = aRes == QMessageBox::Yes;
128 //******************************************************************
129 bool canRename(const ObjectPtr& theObject, const QString& theName)
131 if (std::dynamic_pointer_cast<ModelAPI_ResultParameter>(theObject).get()) {
133 ResultParameterPtr aParam;
134 if (ModelAPI_Tools::findVariable(theObject->document(),
135 FeaturePtr(), qPrintable(theName), aValue, aParam)) {
136 const char* aKeyStr = "Selected parameter can not be renamed to: %1. "
137 "There is a parameter with the same name. Its value is: %2.";
138 QString aErrMsg(QObject::tr(aKeyStr).arg(qPrintable(theName)).arg(aValue));
139 // We can not use here a dialog box for message -
140 // it will crash editing process in ObjectBrowser
141 Events_InfoMessage("XGUI_Tools", aErrMsg.toStdString()).send();
149 //**************************************************************
151 XGUI_Workshop* workshop(ModuleBase_IWorkshop* theWorkshop)
153 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(theWorkshop);
155 return aConnector->workshop();