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