Salome HOME
Rebuild data tree in case of inconsistence with data model. Correct warning with...
[modules/shaper.git] / src / XGUI / XGUI_Tools.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D -->
2
3 #include "XGUI_Tools.h"
4
5 #include "XGUI_ModuleConnector.h"
6 #include "XGUI_Workshop.h"
7
8 #include "ModuleBase_IWorkshop.h"
9
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>
21
22 #include <GeomAPI_Shape.h>
23
24 #include <QDir>
25 #include <QMessageBox>
26
27 #include <iostream>
28 #include <sstream>
29
30 namespace XGUI_Tools {
31 //******************************************************************
32 QString dir(const QString& path, bool isAbs)
33 {
34   QDir aDir = QFileInfo(path).dir();
35   QString dirPath = isAbs ? aDir.absolutePath() : aDir.path();
36   if (dirPath == QString("."))
37     dirPath = QString();
38   return dirPath;
39 }
40
41 //******************************************************************
42 QString file(const QString& path, bool withExt)
43 {
44   QString fPath = path;
45   while (!fPath.isEmpty() && (fPath[fPath.length() - 1] == '\\' || fPath[fPath.length() - 1] == '/'))
46     fPath.remove(fPath.length() - 1, 1);
47
48   if (withExt)
49     return QFileInfo(fPath).fileName();
50   else
51     return QFileInfo(fPath).completeBaseName();
52 }
53
54 //******************************************************************
55 QString addSlash(const QString& path)
56 {
57   QString res = path;
58   if (!res.isEmpty() && res.at(res.length() - 1) != QChar('/')
59       && res.at(res.length() - 1) != QChar('\\'))
60     res += QDir::separator();
61   return res;
62 }
63
64 //******************************************************************
65 QString unionOfObjectNames(const QObjectPtrList& theObjects, const QString& theSeparator)
66 {
67   QStringList aObjectNames;
68   foreach (ObjectPtr aObj, theObjects) {
69     if (aObj->data()->isValid())
70       aObjectNames << QString::fromStdString(aObj->data()->name());
71   }
72   if (aObjectNames.count() == 0)
73     return QString();
74   if (aObjectNames.count() == 1)
75     return aObjectNames.first();
76   return aObjectNames.join(theSeparator);
77 }
78
79 //******************************************************************
80 bool isModelObject(FeaturePtr theFeature)
81 {
82   return theFeature && !theFeature->data();
83 }
84
85 //******************************************************************
86 std::string featureInfo(FeaturePtr theFeature)
87 {
88   std::ostringstream aStream;
89   if (theFeature)
90     aStream << theFeature.get() << " " << theFeature->getKind();
91   return QString(aStream.str().c_str()).toStdString();
92 }
93
94 //******************************************************************
95 /*FeaturePtr realFeature(const FeaturePtr theFeature)
96  {
97  if (theFeature->data()) {
98  return theFeature;
99  } else {
100  ObjectPtr aObject = std::dynamic_pointer_cast<ModelAPI_Object>(theFeature);
101  return aObject->featureRef();
102  }
103  }*/
104
105 //******************************************************************
106 bool canRemoveOrRename(QWidget* theParent, const QObjectPtrList& theObjects)
107 {
108   bool aResult = true;
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())
115         continue;
116       aFoundPartSetObject = aObj->document() == aModuleDoc;
117     }
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;
124     }
125   }
126   return aResult;
127 }
128
129 //******************************************************************
130 bool canRename(const ObjectPtr& theObject, const QString& theName)
131 {
132   if (std::dynamic_pointer_cast<ModelAPI_ResultParameter>(theObject).get()) {
133     double aValue;
134     ResultParameterPtr aParam;
135     if (ModelAPI_Tools::findVariable(theObject->document(), qPrintable(theName), aValue, aParam)) {
136       QString aErrMsg(QObject::tr("Selected parameter can not be renamed to: %1. \
137  There is a parameter with the same name. Its value is: %2.").arg(qPrintable(theName)).arg(aValue));
138       // We can not use here a dialog box for message - it will crash editing process in ObjectBrowser
139       Events_Error::send(aErrMsg.toStdString());
140       return false;
141     }
142   }
143
144   return true;
145 }
146
147 //******************************************************************
148 bool allDocumentsActivated(QString& theNotActivatedNames)
149 {
150   bool anAllPartActivated = true;
151   QStringList aRefNames;
152
153   DocumentPtr aRootDoc = ModelAPI_Session::get()->moduleDocument();
154   int aSize = aRootDoc->size(ModelAPI_ResultPart::group());
155   for (int i = 0; i < aSize; i++) {
156     ObjectPtr aObject = aRootDoc->object(ModelAPI_ResultPart::group(), i);
157     ResultPartPtr aPart = std::dynamic_pointer_cast<ModelAPI_ResultPart>(aObject);
158     if (!aPart->isActivated()) {
159       anAllPartActivated = false;
160       aRefNames.append(aObject->data()->name().c_str());
161     }
162   }
163   theNotActivatedNames = aRefNames.join(", ");
164   return anAllPartActivated;
165 }
166
167 //**************************************************************
168
169 XGUI_Workshop* workshop(ModuleBase_IWorkshop* theWorkshop)
170 {
171   XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(theWorkshop);
172   return aConnector->workshop();
173 }
174
175 }