1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
3 // File: ModuleBase_Tools.cpp
4 // Created: 11 July 2014
5 // Author: Vitaly Smetannikov
7 #include "ModuleBase_Tools.h"
8 #include <ModuleBase_ParamSpinBox.h>
10 #include <ModelAPI_Attribute.h>
11 #include <ModelAPI_AttributeRefAttr.h>
12 #include <ModelAPI_AttributeReference.h>
13 #include <ModelAPI_AttributeSelection.h>
14 #include <ModelAPI_Data.h>
15 #include <ModelAPI_Result.h>
16 #include <ModelAPI_ResultCompSolid.h>
17 #include <ModelAPI_ResultParameter.h>
18 #include <ModelAPI_Tools.h>
20 #include <TopoDS_Iterator.hxx>
22 #include <GeomDataAPI_Point2D.h>
23 #include <Events_Error.h>
25 #include <Config_PropManager.h>
31 #include <QDoubleSpinBox>
35 const double tolerance = 1e-7;
38 namespace ModuleBase_Tools {
40 //******************************************************************
42 //******************************************************************
44 void adjustMargins(QWidget* theWidget)
48 adjustMargins(theWidget->layout());
51 void adjustMargins(QLayout* theLayout)
55 theLayout->setContentsMargins(2, 5, 2, 5);
56 theLayout->setSpacing(4);
59 void zeroMargins(QWidget* theWidget)
63 zeroMargins(theWidget->layout());
66 void zeroMargins(QLayout* theLayout)
70 theLayout->setContentsMargins(0, 0, 0, 0);
71 theLayout->setSpacing(5);
74 QPixmap composite(const QString& theAdditionalIcon, const QString& theIcon)
76 QImage anIcon(theIcon);
77 QImage anAditional(theAdditionalIcon);
82 int anAddWidth = anAditional.width();
83 int anAddHeight = anAditional.height();
85 int aWidth = anIcon.width();
86 int aHeight = anIcon.height();
88 int aStartWidthPos = aWidth - anAddWidth - 1;
89 int aStartHeightPos = aHeight - anAddHeight - 1;
91 for (int i = 0; i < anAddWidth && i + aStartWidthPos < aWidth; i++)
93 for (int j = 0; j < anAddHeight && j + aStartHeightPos < aHeight; j++)
95 if (qAlpha(anAditional.pixel(i, j)) > 0)
96 anIcon.setPixel(i + aStartWidthPos, j + aStartHeightPos, anAditional.pixel(i, j));
99 return QPixmap::fromImage(anIcon);
102 QPixmap lighter(const QString& theIcon, const int theLighterValue)
104 QImage anIcon(theIcon);
108 QImage aResult(theIcon);
109 for ( int i = 0; i < anIcon.width(); i++ )
111 for ( int j = 0; j < anIcon.height(); j++ )
113 QRgb anRgb = anIcon.pixel( i, j );
114 QColor aPixelColor(qRed(anRgb), qGreen(anRgb), qBlue(anRgb),
115 qAlpha( aResult.pixel( i, j ) ));
117 QColor aLighterColor = aPixelColor.lighter(theLighterValue);
118 aResult.setPixel(i, j, qRgba( aLighterColor.red(), aLighterColor.green(),
119 aLighterColor.blue(), aLighterColor.alpha() ) );
122 return QPixmap::fromImage(aResult);
125 void setSpinText(ModuleBase_ParamSpinBox* theSpin, const QString& theText)
127 if (theSpin->text() == theText)
129 // In order to avoid extra text setting because it will
130 // reset cursor position in control
131 bool isBlocked = theSpin->blockSignals(true);
132 theSpin->setText(theText);
133 theSpin->blockSignals(isBlocked);
136 void setSpinValue(QDoubleSpinBox* theSpin, double theValue)
138 if (fabs(theSpin->value() - theValue) < tolerance)
140 bool isBlocked = theSpin->blockSignals(true);
141 theSpin->setValue(theValue);
142 theSpin->blockSignals(isBlocked);
145 void setSpinValue(ModuleBase_ParamSpinBox* theSpin, double theValue)
147 if (fabs(theSpin->value() - theValue) < tolerance)
149 bool isBlocked = theSpin->blockSignals(true);
150 theSpin->setValue(theValue);
151 theSpin->blockSignals(isBlocked);
154 QString objectInfo(const ObjectPtr& theObj, const bool isUseAttributesInfo)
156 QString aFeatureStr = "feature";
160 std::ostringstream aPtrStr;
161 aPtrStr << "[" << theObj.get() << "]";
163 ResultPtr aRes = std::dynamic_pointer_cast<ModelAPI_Result>(theObj);
164 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theObj);
166 aFeatureStr.append(QString("(result%1)").arg(aPtrStr.str().c_str()).toStdString() .c_str());
167 if (aRes->isDisabled())
168 aFeatureStr.append("[disabled]");
169 if (aRes->isConcealed())
170 aFeatureStr.append("[concealed]");
171 if (ModelAPI_Tools::hasSubResults(aRes))
172 aFeatureStr.append("[hasSubResults]");
174 aFeature = ModelAPI_Feature::feature(aRes);
177 aFeatureStr.append(aPtrStr.str().c_str());
179 if (aFeature.get()) {
180 aFeatureStr.append(QString(": %1").arg(aFeature->getKind().c_str()).toStdString().c_str());
181 if (aFeature->data()->isValid()) {
182 aFeatureStr.append(QString(", name=%1").arg(aFeature->data()->name().c_str()).toStdString()
185 if (isUseAttributesInfo) {
186 std::list<AttributePtr> anAttrs = aFeature->data()->attributes("");
187 std::list<AttributePtr>::const_iterator anIt = anAttrs.begin(), aLast = anAttrs.end();
189 for(; anIt != aLast; anIt++) {
190 AttributePtr anAttr = *anIt;
191 QString aValue = "not defined";
192 std::string aType = anAttr->attributeType();
193 if (aType == GeomDataAPI_Point2D::typeId()) {
194 std::shared_ptr<GeomDataAPI_Point2D> aPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
197 aValue = QString("(%1, %2)").arg(aPoint->x()).arg(aPoint->y());
199 else if (aType == ModelAPI_AttributeRefAttr::typeId()) {
202 aValues.push_back(QString("%1: %2").arg(anAttr->id().c_str()).arg(aValue).toStdString().c_str());
204 if (!aValues.empty())
205 aFeatureStr.append(QString(", attributes: %1").arg(aValues.join(", ").toStdString().c_str()));
212 typedef QMap<QString, TopAbs_ShapeEnum> ShapeTypes;
213 static ShapeTypes MyShapeTypes;
215 TopAbs_ShapeEnum shapeType(const QString& theType)
217 if (MyShapeTypes.count() == 0) {
218 MyShapeTypes["face"] = TopAbs_FACE;
219 MyShapeTypes["faces"] = TopAbs_FACE;
220 MyShapeTypes["vertex"] = TopAbs_VERTEX;
221 MyShapeTypes["vertices"] = TopAbs_VERTEX;
222 MyShapeTypes["wire"] = TopAbs_WIRE;
223 MyShapeTypes["edge"] = TopAbs_EDGE;
224 MyShapeTypes["edges"] = TopAbs_EDGE;
225 MyShapeTypes["shell"] = TopAbs_SHELL;
226 MyShapeTypes["solid"] = TopAbs_SOLID;
227 MyShapeTypes["solids"] = TopAbs_SOLID;
228 MyShapeTypes["objects"] = TopAbs_SHAPE;
230 QString aType = theType.toLower();
231 if (MyShapeTypes.contains(aType))
232 return MyShapeTypes[aType];
233 Events_Error::send("Shape type defined in XML is not implemented!");
237 void checkObjects(const QObjectPtrList& theObjects, bool& hasResult, bool& hasFeature, bool& hasParameter, bool& hasSubFeature)
241 hasParameter = false;
242 hasSubFeature = false;
243 foreach(ObjectPtr aObj, theObjects) {
244 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aObj);
245 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(aObj);
246 ResultParameterPtr aConstruction = std::dynamic_pointer_cast<ModelAPI_ResultParameter>(aResult);
248 hasResult |= (aResult.get() != NULL);
249 hasFeature |= (aFeature.get() != NULL);
250 hasParameter |= (aConstruction.get() != NULL);
252 hasSubFeature |= (ModelAPI_Tools::compositeOwner(aFeature) != NULL);
253 if (hasFeature && hasResult && hasParameter && hasSubFeature)
258 void setDefaultDeviationCoefficient(const TopoDS_Shape& theShape,
259 const Handle(Prs3d_Drawer)& theDrawer)
261 if (!theShape.IsNull() && theShape.ShapeType() == TopAbs_EDGE)
262 theDrawer->SetDeviationCoefficient(1.e-4);
265 Quantity_Color color(const std::string& theSection,
266 const std::string& theName,
267 const std::string& theDefault)
269 std::vector<int> aColor = Config_PropManager::color(theSection, theName, theDefault);
270 return Quantity_Color(aColor[0] / 255., aColor[1] / 255., aColor[2] / 255., Quantity_TOC_RGB);
273 ObjectPtr getObject(const AttributePtr& theAttribute)
276 std::string anAttrType = theAttribute->attributeType();
277 if (anAttrType == ModelAPI_AttributeRefAttr::typeId()) {
278 AttributeRefAttrPtr anAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
279 if (anAttr != NULL && anAttr->isObject())
280 anObject = anAttr->object();
282 if (anAttrType == ModelAPI_AttributeSelection::typeId()) {
283 AttributeSelectionPtr anAttr = std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(theAttribute);
285 anObject = anAttr->context();
287 if (anAttrType == ModelAPI_AttributeReference::typeId()) {
288 AttributeReferencePtr anAttr = std::dynamic_pointer_cast<ModelAPI_AttributeReference>(theAttribute);
289 if (anAttr.get() != NULL)
290 anObject = anAttr->value();
295 TopAbs_ShapeEnum getCompoundSubType(const TopoDS_Shape& theShape)
297 TopAbs_ShapeEnum aShapeType = theShape.ShapeType();
299 // for compounds check sub-shapes: it may be compound of needed type:
300 // Booleans may produce compounds of Solids
301 if (aShapeType == TopAbs_COMPOUND) {
302 for(TopoDS_Iterator aSubs(theShape); aSubs.More(); aSubs.Next()) {
303 if (!aSubs.Value().IsNull()) {
304 TopAbs_ShapeEnum aSubType = aSubs.Value().ShapeType();
305 if (aSubType == TopAbs_COMPOUND) { // compound of compound(s)
306 aShapeType = TopAbs_COMPOUND;
309 if (aShapeType == TopAbs_COMPOUND) {
310 aShapeType = aSubType;
311 } else if (aShapeType != aSubType) { // compound of shapes of different types
312 aShapeType = TopAbs_COMPOUND;
321 } // namespace ModuleBase_Tools