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