Salome HOME
Issue #805 In the sketch presentation, show both the parameter and its value: minor...
[modules/shaper.git] / src / ModuleBase / ModuleBase_Tools.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
2
3 // File:        ModuleBase_Tools.cpp
4 // Created:     11 July 2014
5 // Author:      Vitaly Smetannikov
6
7 #include "ModuleBase_Tools.h"
8
9 #include <ModuleBase_ParamIntSpinBox.h>
10 #include <ModuleBase_ParamSpinBox.h>
11 #include <ModuleBase_WidgetFactory.h>
12 #include <ModuleBase_IWorkshop.h>
13 #include <ModuleBase_IModule.h>
14
15 #include <ModelAPI_Attribute.h>
16 #include <ModelAPI_AttributeRefAttr.h>
17 #include <ModelAPI_AttributeReference.h>
18 #include <ModelAPI_AttributeSelection.h>
19 #include <ModelAPI_Data.h>
20 #include <ModelAPI_Result.h>
21 #include <ModelAPI_ResultCompSolid.h>
22 #include <ModelAPI_ResultParameter.h>
23 #include <ModelAPI_Tools.h>
24 #include <ModelAPI_Session.h>
25
26 #include <TopoDS_Iterator.hxx>
27
28 #include <GeomDataAPI_Point2D.h>
29 #include <Events_Error.h>
30
31 #include <Config_PropManager.h>
32
33 #include <QWidget>
34 #include <QLayout>
35 #include <QPainter>
36 #include <QBitmap>
37 #include <QDoubleSpinBox>
38 #include <QGraphicsDropShadowEffect>
39 #include <QColor>
40
41 #include <sstream>
42
43 const double tolerance = 1e-7;
44
45 //#define DEBUG_ACTIVATE_WINDOW
46 //#define DEBUG_SET_FOCUS
47
48 namespace ModuleBase_Tools {
49
50 //******************************************************************
51
52 //******************************************************************
53
54 void adjustMargins(QWidget* theWidget)
55 {
56   if(!theWidget)
57     return;
58   adjustMargins(theWidget->layout());
59 }
60
61 void adjustMargins(QLayout* theLayout)
62 {
63   if(!theLayout)
64     return;
65   theLayout->setContentsMargins(2, 5, 2, 5);
66   theLayout->setSpacing(4);
67 }
68
69 void zeroMargins(QWidget* theWidget)
70 {
71   if(!theWidget)
72     return;
73   zeroMargins(theWidget->layout());
74 }
75
76 void zeroMargins(QLayout* theLayout)
77 {
78   if(!theLayout)
79     return;
80   theLayout->setContentsMargins(0, 0, 0, 0);
81   theLayout->setSpacing(5);
82 }
83
84 void activateWindow(QWidget* theWidget, const QString& theInfo)
85 {
86   theWidget->activateWindow();
87
88 #ifdef DEBUG_ACTIVATE_WINDOW
89   qDebug(QString("activateWindow: %1").arg(theInfo).toStdString().c_str());
90 #endif
91 }
92
93 void setFocus(QWidget* theWidget, const QString& theInfo)
94 {
95   theWidget->setFocus();
96
97 #ifdef DEBUG_SET_FOCUS
98   qDebug(QString("setFocus: %1").arg(theInfo).toStdString().c_str());
99 #endif
100 }
101
102 void setShadowEffect(QWidget* theWidget, const bool isSetEffect)
103 {
104   if (isSetEffect) {
105     QGraphicsDropShadowEffect* aGlowEffect = new QGraphicsDropShadowEffect();
106     aGlowEffect->setOffset(.0);
107     aGlowEffect->setBlurRadius(10.0);
108     aGlowEffect->setColor(QColor(0, 170, 255)); // Light-blue color, #00AAFF
109     theWidget->setGraphicsEffect(aGlowEffect);
110   }
111   else {
112     QGraphicsEffect* anEffect = theWidget->graphicsEffect();
113     if(anEffect)
114     anEffect->deleteLater();
115     theWidget->setGraphicsEffect(NULL);
116   }
117 }
118
119 QPixmap composite(const QString& theAdditionalIcon, const QString& theIcon)
120 {
121   QImage anIcon(theIcon);
122   QImage anAditional(theAdditionalIcon);
123
124   if (anIcon.isNull())
125     return QPixmap();
126
127   int anAddWidth = anAditional.width();
128   int anAddHeight = anAditional.height();
129
130   int aWidth = anIcon.width();
131   int aHeight = anIcon.height();
132
133   int aStartWidthPos = aWidth - anAddWidth - 1;
134   int aStartHeightPos = aHeight - anAddHeight - 1;
135
136   for (int i = 0; i < anAddWidth && i + aStartWidthPos < aWidth; i++)
137   {
138     for (int j = 0; j < anAddHeight && j + aStartHeightPos < aHeight; j++)
139     {
140       if (qAlpha(anAditional.pixel(i, j)) > 0)
141         anIcon.setPixel(i + aStartWidthPos, j + aStartHeightPos, anAditional.pixel(i, j));
142     }
143   }
144   return QPixmap::fromImage(anIcon);
145 }
146
147 QPixmap lighter(const QString& theIcon, const int theLighterValue)
148 {
149   QImage anIcon(theIcon);
150   if (anIcon.isNull())
151     return QPixmap();
152
153   QImage aResult(theIcon);
154   for ( int i = 0; i < anIcon.width(); i++ )
155   {
156     for ( int j = 0; j < anIcon.height(); j++ )
157     {
158       QRgb anRgb = anIcon.pixel( i, j );
159       QColor aPixelColor(qRed(anRgb), qGreen(anRgb), qBlue(anRgb),
160                          qAlpha( aResult.pixel( i, j ) ));
161
162       QColor aLighterColor = aPixelColor.lighter(theLighterValue);
163       aResult.setPixel(i, j, qRgba( aLighterColor.red(), aLighterColor.green(),
164                                     aLighterColor.blue(), aLighterColor.alpha() ) );
165     }
166   }
167   return QPixmap::fromImage(aResult);
168 }
169
170 void setSpinText(ModuleBase_ParamSpinBox* theSpin, const QString& theText)
171 {
172   if (theSpin->text() == theText) 
173     return;
174   // In order to avoid extra text setting because it will
175   // reset cursor position in control
176   bool isBlocked = theSpin->blockSignals(true);
177   theSpin->setText(theText);
178   theSpin->blockSignals(isBlocked);
179 }
180
181 void setSpinValue(QDoubleSpinBox* theSpin, double theValue)
182 {
183   if (fabs(theSpin->value() - theValue) < tolerance)
184     return;
185   bool isBlocked = theSpin->blockSignals(true);
186   theSpin->setValue(theValue);
187   theSpin->blockSignals(isBlocked);
188 }
189
190 void setSpinValue(ModuleBase_ParamSpinBox* theSpin, double theValue)
191 {
192   if (fabs(theSpin->value() - theValue) < tolerance)
193     return;
194   bool isBlocked = theSpin->blockSignals(true);
195   theSpin->setValue(theValue);
196   theSpin->blockSignals(isBlocked);
197 }
198
199 void setSpinText(ModuleBase_ParamIntSpinBox* theSpin, const QString& theText)
200 {
201   // In order to avoid extra text setting because it will
202   // reset cursor position in control
203   if (theSpin->text() == theText)
204     return;
205   bool isBlocked = theSpin->blockSignals(true);
206   theSpin->setText(theText);
207   theSpin->blockSignals(isBlocked);
208 }
209
210 void setSpinValue(ModuleBase_ParamIntSpinBox* theSpin, int theValue)
211 {
212   if (theSpin->value() == theValue)
213     return;
214   bool isBlocked = theSpin->blockSignals(true);
215   theSpin->setValue(theValue);
216   theSpin->blockSignals(isBlocked);
217 }
218
219 QString objectInfo(const ObjectPtr& theObj, const bool isUseAttributesInfo)
220 {
221   QString aFeatureStr = "feature";
222   if (!theObj.get())
223     return aFeatureStr;
224
225   std::ostringstream aPtrStr;
226   aPtrStr << "[" << theObj.get() << "]";
227
228   ResultPtr aRes = std::dynamic_pointer_cast<ModelAPI_Result>(theObj);
229   FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theObj);
230   if(aRes.get()) {
231     aFeatureStr.append(QString("(result%1)").arg(aPtrStr.str().c_str()).toStdString() .c_str());
232     if (aRes->isDisabled())
233       aFeatureStr.append("[disabled]");
234     if (aRes->isConcealed())
235       aFeatureStr.append("[concealed]");
236     if (ModelAPI_Tools::hasSubResults(aRes))
237       aFeatureStr.append("[hasSubResults]");
238
239     aFeature = ModelAPI_Feature::feature(aRes);
240   }
241   else
242     aFeatureStr.append(aPtrStr.str().c_str());
243
244   if (aFeature.get()) {
245     aFeatureStr.append(QString(": %1").arg(aFeature->getKind().c_str()).toStdString().c_str());
246     if (aFeature->data()->isValid()) {
247       aFeatureStr.append(QString(", name=%1").arg(aFeature->data()->name().c_str()).toStdString()
248                                                                                        .c_str());
249     }
250     if (isUseAttributesInfo) {
251       std::list<AttributePtr> anAttrs = aFeature->data()->attributes("");
252       std::list<AttributePtr>::const_iterator anIt = anAttrs.begin(), aLast = anAttrs.end();
253       QStringList aValues;
254       for(; anIt != aLast; anIt++) {
255         AttributePtr anAttr = *anIt;
256         QString aValue = "not defined";
257         std::string aType = anAttr->attributeType();
258         if (aType == GeomDataAPI_Point2D::typeId()) {
259           std::shared_ptr<GeomDataAPI_Point2D> aPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
260                                                                                          anAttr);
261           if (aPoint.get())
262             aValue = QString("(%1, %2)").arg(aPoint->x()).arg(aPoint->y());
263         }
264         else if (aType == ModelAPI_AttributeRefAttr::typeId()) {
265         }
266
267         aValues.push_back(QString("%1: %2").arg(anAttr->id().c_str()).arg(aValue).toStdString().c_str());
268       }
269       if (!aValues.empty())
270         aFeatureStr.append(QString(", attributes: %1").arg(aValues.join(", ").toStdString().c_str()));
271     }
272   }
273
274   return aFeatureStr;
275 }
276
277 typedef QMap<QString, TopAbs_ShapeEnum> ShapeTypes;
278 static ShapeTypes MyShapeTypes;
279
280 TopAbs_ShapeEnum shapeType(const QString& theType)
281 {
282   if (MyShapeTypes.count() == 0) {
283     MyShapeTypes["face"] = TopAbs_FACE;
284     MyShapeTypes["faces"] = TopAbs_FACE;
285     MyShapeTypes["vertex"] = TopAbs_VERTEX;
286     MyShapeTypes["vertices"] = TopAbs_VERTEX;
287     MyShapeTypes["wire"] = TopAbs_WIRE;
288     MyShapeTypes["edge"] = TopAbs_EDGE;
289     MyShapeTypes["edges"] = TopAbs_EDGE;
290     MyShapeTypes["shell"] = TopAbs_SHELL;
291     MyShapeTypes["solid"] = TopAbs_SOLID;
292     MyShapeTypes["solids"] = TopAbs_SOLID;
293     MyShapeTypes["objects"] = TopAbs_SHAPE;
294   }
295   QString aType = theType.toLower();
296   if (MyShapeTypes.contains(aType))
297     return MyShapeTypes[aType];
298   Events_Error::send("Shape type defined in XML is not implemented!");
299   return TopAbs_SHAPE;
300 }
301
302 void checkObjects(const QObjectPtrList& theObjects, bool& hasResult, bool& hasFeature, bool& hasParameter, bool& hasSubFeature)
303 {
304   hasResult = false;
305   hasFeature = false;
306   hasParameter = false;
307   hasSubFeature = false;
308   foreach(ObjectPtr aObj, theObjects) {
309     FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aObj);
310     ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(aObj);
311     ResultParameterPtr aConstruction = std::dynamic_pointer_cast<ModelAPI_ResultParameter>(aResult);
312
313     hasResult |= (aResult.get() != NULL);
314     hasFeature |= (aFeature.get() != NULL);
315     hasParameter |= (aConstruction.get() != NULL);
316     if (hasFeature) 
317       hasSubFeature |= (ModelAPI_Tools::compositeOwner(aFeature) != NULL);
318     if (hasFeature && hasResult  && hasParameter && hasSubFeature)
319       break;
320   }
321 }
322
323 void setDefaultDeviationCoefficient(const TopoDS_Shape& theShape,
324                                     const Handle(Prs3d_Drawer)& theDrawer)
325 {
326   if (theShape.IsNull())
327     return;
328   TopAbs_ShapeEnum aType = theShape.ShapeType();
329   if ((aType == TopAbs_EDGE) || (aType == TopAbs_WIRE)) 
330     theDrawer->SetDeviationCoefficient(1.e-4);
331 }
332
333 Quantity_Color color(const std::string& theSection,
334                      const std::string& theName,
335                      const std::string& theDefault)
336 {
337   std::vector<int> aColor = Config_PropManager::color(theSection, theName, theDefault);
338   return Quantity_Color(aColor[0] / 255., aColor[1] / 255., aColor[2] / 255., Quantity_TOC_RGB);
339 }
340
341 ObjectPtr getObject(const AttributePtr& theAttribute)
342 {
343   ObjectPtr anObject;
344   std::string anAttrType = theAttribute->attributeType();
345   if (anAttrType == ModelAPI_AttributeRefAttr::typeId()) {
346     AttributeRefAttrPtr anAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
347     if (anAttr != NULL && anAttr->isObject())
348       anObject = anAttr->object();
349   }
350   if (anAttrType == ModelAPI_AttributeSelection::typeId()) {
351     AttributeSelectionPtr anAttr = std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(theAttribute);
352     if (anAttr != NULL)
353       anObject = anAttr->context();
354   }
355   if (anAttrType == ModelAPI_AttributeReference::typeId()) {
356     AttributeReferencePtr anAttr = std::dynamic_pointer_cast<ModelAPI_AttributeReference>(theAttribute);
357     if (anAttr.get() != NULL)
358       anObject = anAttr->value();
359   }
360   return anObject;
361 }
362
363 TopAbs_ShapeEnum getCompoundSubType(const TopoDS_Shape& theShape)
364 {
365   TopAbs_ShapeEnum aShapeType = theShape.ShapeType();
366
367   // for compounds check sub-shapes: it may be compound of needed type:
368   // Booleans may produce compounds of Solids
369   if (aShapeType == TopAbs_COMPOUND) {
370     for(TopoDS_Iterator aSubs(theShape); aSubs.More(); aSubs.Next()) {
371       if (!aSubs.Value().IsNull()) {
372         TopAbs_ShapeEnum aSubType = aSubs.Value().ShapeType();
373         if (aSubType == TopAbs_COMPOUND) { // compound of compound(s)
374           aShapeType = TopAbs_COMPOUND;
375           break;
376         }
377         if (aShapeType == TopAbs_COMPOUND) {
378           aShapeType = aSubType;
379         } else if (aShapeType != aSubType) { // compound of shapes of different types
380           aShapeType = TopAbs_COMPOUND;
381           break;
382         }
383       }
384     }
385   }
386   return aShapeType;
387 }
388
389 void getParameters(QStringList& theParameters)
390 {
391   theParameters.clear();
392
393   SessionPtr aSession = ModelAPI_Session::get();
394   std::list<DocumentPtr> aDocList;
395   DocumentPtr anActiveDocument = aSession->activeDocument();
396   DocumentPtr aRootDocument = aSession->moduleDocument();
397   aDocList.push_back(anActiveDocument);
398   if (anActiveDocument != aRootDocument) {
399     aDocList.push_back(aRootDocument);
400   }
401   std::string aGroupId = ModelAPI_ResultParameter::group();
402   for(std::list<DocumentPtr>::const_iterator it = aDocList.begin(); it != aDocList.end(); ++it) {
403     DocumentPtr aDocument = *it;
404     int aSize = aDocument->size(aGroupId);
405     for (int i = 0; i < aSize; i++) {
406       ObjectPtr anObject = aDocument->object(aGroupId, i);
407       std::string aParameterName = anObject->data()->name();
408       theParameters.append(aParameterName.c_str());
409     }
410   }
411 }
412
413 std::string findGreedAttribute(ModuleBase_IWorkshop* theWorkshop, const FeaturePtr& theFeature)
414 {
415   std::string anAttributeId;
416
417   std::string aXmlCfg, aDescription;
418   theWorkshop->module()->getXMLRepresentation(theFeature->getKind(), aXmlCfg, aDescription);
419
420   ModuleBase_WidgetFactory aFactory(aXmlCfg, theWorkshop);
421   std::string anAttributeTitle;
422   aFactory.getGreedAttribute(anAttributeId);
423
424   return anAttributeId;
425 }
426
427 } // namespace ModuleBase_Tools
428
429