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