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