Salome HOME
d2009ac41fc63f31006a51439d9916eb51e71362
[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_Result.h>
11 #include <ModelAPI_Data.h>
12 #include <ModelAPI_Attribute.h>
13 #include <ModelAPI_AttributeRefAttr.h>
14 #include <ModelAPI_ResultParameter.h>
15 #include <ModelAPI_ResultCompSolid.h>
16 #include <ModelAPI_Tools.h>
17
18 #include <GeomDataAPI_Point2D.h>
19 #include <Events_Error.h>
20
21 #include <Config_PropManager.h>
22
23 #include <QWidget>
24 #include <QLayout>
25 #include <QPainter>
26 #include <QBitmap>
27 #include <QDoubleSpinBox>
28
29 #include <sstream>
30
31 const double tolerance = 1e-7;
32
33
34 namespace ModuleBase_Tools {
35
36 //******************************************************************
37
38 //******************************************************************
39
40 void adjustMargins(QWidget* theWidget)
41 {
42   if(!theWidget)
43     return;
44   adjustMargins(theWidget->layout());
45 }
46
47 void adjustMargins(QLayout* theLayout)
48 {
49   if(!theLayout)
50     return;
51   theLayout->setContentsMargins(2, 5, 2, 5);
52   theLayout->setSpacing(4);
53 }
54
55 void zeroMargins(QWidget* theWidget)
56 {
57   if(!theWidget)
58     return;
59   zeroMargins(theWidget->layout());
60 }
61
62 void zeroMargins(QLayout* theLayout)
63 {
64   if(!theLayout)
65     return;
66   theLayout->setContentsMargins(0, 0, 0, 0);
67   theLayout->setSpacing(5);
68 }
69
70 QPixmap composite(const QString& theAdditionalIcon, const QString& theIcon)
71 {
72   QImage anIcon(theIcon);
73   QImage anAditional(theAdditionalIcon);
74
75   if (anIcon.isNull())
76     return QPixmap();
77
78   int anAddWidth = anAditional.width();
79   int anAddHeight = anAditional.height();
80
81   int aWidth = anIcon.width();
82   int aHeight = anIcon.height();
83
84   int aStartWidthPos = aWidth - anAddWidth - 1;
85   int aStartHeightPos = aHeight - anAddHeight - 1;
86
87   for (int i = 0; i < anAddWidth && i + aStartWidthPos < aWidth; i++)
88   {
89     for (int j = 0; j < anAddHeight && j + aStartHeightPos < aHeight; j++)
90     {
91       if (qAlpha(anAditional.pixel(i, j)) > 0)
92         anIcon.setPixel(i + aStartWidthPos, j + aStartHeightPos, anAditional.pixel(i, j));
93     }
94   }
95   return QPixmap::fromImage(anIcon);
96 }
97
98 QPixmap lighter(const QString& theIcon, const int theLighterValue)
99 {
100   QImage anIcon(theIcon);
101   if (anIcon.isNull())
102     return QPixmap();
103
104   QImage aResult(theIcon);
105   for ( int i = 0; i < anIcon.width(); i++ )
106   {
107     for ( int j = 0; j < anIcon.height(); j++ )
108     {
109       QRgb anRgb = anIcon.pixel( i, j );
110       QColor aPixelColor(qRed(anRgb), qGreen(anRgb), qBlue(anRgb),
111                          qAlpha( aResult.pixel( i, j ) ));
112
113       QColor aLighterColor = aPixelColor.lighter(theLighterValue);
114       aResult.setPixel(i, j, qRgba( aLighterColor.red(), aLighterColor.green(),
115                                     aLighterColor.blue(), aLighterColor.alpha() ) );
116     }
117   }
118   return QPixmap::fromImage(aResult);
119 }
120
121 void setSpinText(ModuleBase_ParamSpinBox* theSpin, const QString& theText)
122 {
123   if (theSpin->text() == theText) 
124     return;
125   // In order to avoid extra text setting because it will
126   // reset cursor position in control
127   bool isBlocked = theSpin->blockSignals(true);
128   theSpin->setText(theText);
129   theSpin->blockSignals(isBlocked);
130 }
131
132 void setSpinValue(QDoubleSpinBox* theSpin, double theValue)
133 {
134   if (fabs(theSpin->value() - theValue) < tolerance)
135     return;
136   bool isBlocked = theSpin->blockSignals(true);
137   theSpin->setValue(theValue);
138   theSpin->blockSignals(isBlocked);
139 }
140
141 void setSpinValue(ModuleBase_ParamSpinBox* theSpin, double theValue)
142 {
143   if (fabs(theSpin->value() - theValue) < tolerance)
144     return;
145   bool isBlocked = theSpin->blockSignals(true);
146   theSpin->setValue(theValue);
147   theSpin->blockSignals(isBlocked);
148 }
149
150 QString objectInfo(const ObjectPtr& theObj, const bool isUseAttributesInfo)
151 {
152   QString aFeatureStr = "feature";
153   if (!theObj.get())
154     return aFeatureStr;
155
156   std::ostringstream aPtrStr;
157   aPtrStr << "[" << theObj.get() << "]";
158
159   ResultPtr aRes = std::dynamic_pointer_cast<ModelAPI_Result>(theObj);
160   FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theObj);
161   if(aRes.get()) {
162     aFeatureStr.append(QString("(result%1)").arg(aPtrStr.str().c_str()).toStdString() .c_str());
163     if (aRes->isDisabled())
164       aFeatureStr.append("[disabled]");
165     if (aRes->isConcealed())
166       aFeatureStr.append("[concealed]");
167     if (ModelAPI_Tools::hasSubResults(aRes))
168       aFeatureStr.append("[hasSubResults]");
169
170     aFeature = ModelAPI_Feature::feature(aRes);
171   }
172   else
173     aFeatureStr.append(aPtrStr.str().c_str());
174
175   if (aFeature.get()) {
176     aFeatureStr.append(QString(": %1").arg(aFeature->getKind().c_str()).toStdString().c_str());
177     if (aFeature->data()->isValid()) {
178       aFeatureStr.append(QString(", name=%1").arg(aFeature->data()->name().c_str()).toStdString()
179                                                                                        .c_str());
180     }
181     if (isUseAttributesInfo) {
182       std::list<AttributePtr> anAttrs = aFeature->data()->attributes("");
183       std::list<AttributePtr>::const_iterator anIt = anAttrs.begin(), aLast = anAttrs.end();
184       QStringList aValues;
185       for(; anIt != aLast; anIt++) {
186         AttributePtr anAttr = *anIt;
187         QString aValue = "not defined";
188         std::string aType = anAttr->attributeType();
189         if (aType == GeomDataAPI_Point2D::typeId()) {
190           std::shared_ptr<GeomDataAPI_Point2D> aPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
191                                                                                          anAttr);
192           if (aPoint.get())
193             aValue = QString("(%1, %2)").arg(aPoint->x()).arg(aPoint->y());
194         }
195         else if (aType == ModelAPI_AttributeRefAttr::typeId()) {
196         }
197
198         aValues.push_back(QString("%1: %2").arg(anAttr->id().c_str()).arg(aValue).toStdString().c_str());
199       }
200       if (!aValues.empty())
201         aFeatureStr.append(QString(", attributes: %1").arg(aValues.join(", ").toStdString().c_str()));
202     }
203   }
204
205   return aFeatureStr;
206 }
207
208 typedef QMap<QString, TopAbs_ShapeEnum> ShapeTypes;
209 static ShapeTypes MyShapeTypes;
210
211 TopAbs_ShapeEnum shapeType(const QString& theType)
212 {
213   if (MyShapeTypes.count() == 0) {
214     MyShapeTypes["face"] = TopAbs_FACE;
215     MyShapeTypes["faces"] = TopAbs_FACE;
216     MyShapeTypes["vertex"] = TopAbs_VERTEX;
217     MyShapeTypes["vertices"] = TopAbs_VERTEX;
218     MyShapeTypes["wire"] = TopAbs_WIRE;
219     MyShapeTypes["edge"] = TopAbs_EDGE;
220     MyShapeTypes["edges"] = TopAbs_EDGE;
221     MyShapeTypes["shell"] = TopAbs_SHELL;
222     MyShapeTypes["solid"] = TopAbs_SOLID;
223     MyShapeTypes["solids"] = TopAbs_SOLID;
224     MyShapeTypes["objects"] = TopAbs_SHAPE;
225   }
226   QString aType = theType.toLower();
227   if (MyShapeTypes.contains(aType))
228     return MyShapeTypes[aType];
229   Events_Error::send("Shape type defined in XML is not implemented!");
230   return TopAbs_SHAPE;
231 }
232
233 void checkObjects(const QObjectPtrList& theObjects, bool& hasResult, bool& hasFeature, bool& hasParameter, bool& hasSubFeature)
234 {
235   hasResult = false;
236   hasFeature = false;
237   hasParameter = false;
238   hasSubFeature = false;
239   foreach(ObjectPtr aObj, theObjects) {
240     FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aObj);
241     ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(aObj);
242     ResultParameterPtr aConstruction = std::dynamic_pointer_cast<ModelAPI_ResultParameter>(aResult);
243
244     hasResult = (aResult.get() != NULL);
245     hasFeature = (aFeature.get() != NULL);
246     hasParameter = (aConstruction.get() != NULL);
247     if (hasFeature) 
248       hasSubFeature = (ModelAPI_Tools::compositeOwner(aFeature) != NULL);
249     if (hasFeature && hasResult  && hasParameter && hasSubFeature)
250       break;
251   }
252 }
253
254 void setDefaultDeviationCoefficient(const TopoDS_Shape& theShape,
255                                     const Handle(Prs3d_Drawer)& theDrawer)
256 {
257   if (!theShape.IsNull() && theShape.ShapeType() == TopAbs_EDGE)
258     theDrawer->SetDeviationCoefficient(1.e-4);
259 }
260
261 Quantity_Color color(const std::string& theSection,
262                      const std::string& theName,
263                      const std::string& theDefault)
264 {
265   std::vector<int> aColor = Config_PropManager::color(theSection, theName, theDefault);
266   return Quantity_Color(aColor[0] / 255., aColor[1] / 255., aColor[2] / 255., Quantity_TOC_RGB);
267 }
268
269 }
270
271