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