Salome HOME
745497c8abd58c1650826c19e4faab3a8b3efa8c
[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 QString objectInfo(const ObjectPtr& theObj, const bool isUseAttributesInfo)
129 {
130   QString aFeatureStr = "feature";
131   if (!theObj.get())
132     return aFeatureStr;
133
134   std::ostringstream aPtrStr;
135   aPtrStr << "[" << theObj.get() << "]";
136
137   ResultPtr aRes = std::dynamic_pointer_cast<ModelAPI_Result>(theObj);
138   FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theObj);
139   if(aRes.get()) {
140     aFeatureStr.append(QString("(result%1)").arg(aPtrStr.str().c_str()).toStdString() .c_str());
141     if (aRes->isDisabled())
142       aFeatureStr.append("[disabled]");
143     if (aRes->isConcealed())
144       aFeatureStr.append("[concealed]");
145
146     aFeature = ModelAPI_Feature::feature(aRes);
147   }
148   else
149     aFeatureStr.append(aPtrStr.str().c_str());
150
151   if (aFeature.get()) {
152     aFeatureStr.append(QString(": %1").arg(aFeature->getKind().c_str()).toStdString().c_str());
153     if (aFeature->data().get() && aFeature->data()->isValid()) {
154       aFeatureStr.append(QString(", name=%1").arg(aFeature->data()->name().c_str()).toStdString()
155                                                                                        .c_str());
156     }
157     if (isUseAttributesInfo) {
158       std::list<AttributePtr> anAttrs = aFeature->data()->attributes("");
159       std::list<AttributePtr>::const_iterator anIt = anAttrs.begin(), aLast = anAttrs.end();
160       QStringList aValues;
161       for(; anIt != aLast; anIt++) {
162         AttributePtr anAttr = *anIt;
163         QString aValue = "not defined";
164         std::string aType = anAttr->attributeType();
165         if (aType == GeomDataAPI_Point2D::typeId()) {
166           std::shared_ptr<GeomDataAPI_Point2D> aPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
167                                                                                          anAttr);
168           if (aPoint.get())
169             aValue = QString("(%1, %2)").arg(aPoint->x()).arg(aPoint->y());
170         }
171         else if (aType == ModelAPI_AttributeRefAttr::typeId()) {
172         }
173
174         aValues.push_back(QString("%1: %2").arg(anAttr->id().c_str()).arg(aValue).toStdString().c_str());
175       }
176       if (!aValues.empty())
177         aFeatureStr.append(QString(", attributes: %1").arg(aValues.join(", ").toStdString().c_str()));
178     }
179   }
180
181   return aFeatureStr;
182 }
183
184 typedef QMap<QString, TopAbs_ShapeEnum> ShapeTypes;
185 static ShapeTypes MyShapeTypes;
186
187 TopAbs_ShapeEnum shapeType(const QString& theType)
188 {
189   if (MyShapeTypes.count() == 0) {
190     MyShapeTypes["face"] = TopAbs_FACE;
191     MyShapeTypes["faces"] = TopAbs_FACE;
192     MyShapeTypes["vertex"] = TopAbs_VERTEX;
193     MyShapeTypes["vertices"] = TopAbs_VERTEX;
194     MyShapeTypes["wire"] = TopAbs_WIRE;
195     MyShapeTypes["edge"] = TopAbs_EDGE;
196     MyShapeTypes["edges"] = TopAbs_EDGE;
197     MyShapeTypes["shell"] = TopAbs_SHELL;
198     MyShapeTypes["solid"] = TopAbs_SOLID;
199     MyShapeTypes["solids"] = TopAbs_SOLID;
200   }
201   QString aType = theType.toLower();
202   if (MyShapeTypes.contains(aType))
203     return MyShapeTypes[aType];
204   Events_Error::send("Shape type defined in XML is not implemented!");
205   return TopAbs_SHAPE;
206 }
207
208 void checkObjects(const QObjectPtrList& theObjects, bool& hasResult, bool& hasFeature, bool& hasParameter)
209 {
210   hasResult = false;
211   hasFeature = false;
212   hasParameter = false;
213   foreach(ObjectPtr aObj, theObjects) {
214     FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aObj);
215     ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(aObj);
216     ResultParameterPtr aConstruction = std::dynamic_pointer_cast<ModelAPI_ResultParameter>(aResult);
217
218     hasResult = (aResult.get() != NULL);
219     hasFeature = (aFeature.get() != NULL);
220     hasParameter = (aConstruction.get() != NULL);
221     if (hasFeature && hasResult  && hasParameter)
222       break;
223   }
224 }
225
226
227 }
228
229