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