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_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_ResultCompSolid.h>
16 #include <ModelAPI_Tools.h>
18 #include <GeomDataAPI_Point2D.h>
19 #include <Events_Error.h>
21 #include <Config_PropManager.h>
27 #include <QDoubleSpinBox>
31 const double tolerance = 1e-7;
34 namespace ModuleBase_Tools {
36 //******************************************************************
38 //******************************************************************
40 void adjustMargins(QWidget* theWidget)
44 adjustMargins(theWidget->layout());
47 void adjustMargins(QLayout* theLayout)
51 theLayout->setContentsMargins(2, 5, 2, 5);
52 theLayout->setSpacing(4);
55 void zeroMargins(QWidget* theWidget)
59 zeroMargins(theWidget->layout());
62 void zeroMargins(QLayout* theLayout)
66 theLayout->setContentsMargins(0, 0, 0, 0);
67 theLayout->setSpacing(5);
70 QPixmap composite(const QString& theAdditionalIcon, const QString& theIcon)
72 QImage anIcon(theIcon);
73 QImage anAditional(theAdditionalIcon);
78 int anAddWidth = anAditional.width();
79 int anAddHeight = anAditional.height();
81 int aWidth = anIcon.width();
82 int aHeight = anIcon.height();
84 int aStartWidthPos = aWidth - anAddWidth - 1;
85 int aStartHeightPos = aHeight - anAddHeight - 1;
87 for (int i = 0; i < anAddWidth && i + aStartWidthPos < aWidth; i++)
89 for (int j = 0; j < anAddHeight && j + aStartHeightPos < aHeight; j++)
91 if (qAlpha(anAditional.pixel(i, j)) > 0)
92 anIcon.setPixel(i + aStartWidthPos, j + aStartHeightPos, anAditional.pixel(i, j));
95 return QPixmap::fromImage(anIcon);
98 QPixmap lighter(const QString& theIcon, const int theLighterValue)
100 QImage anIcon(theIcon);
104 QImage aResult(theIcon);
105 for ( int i = 0; i < anIcon.width(); i++ )
107 for ( int j = 0; j < anIcon.height(); j++ )
109 QRgb anRgb = anIcon.pixel( i, j );
110 QColor aPixelColor(qRed(anRgb), qGreen(anRgb), qBlue(anRgb),
111 qAlpha( aResult.pixel( i, j ) ));
113 QColor aLighterColor = aPixelColor.lighter(theLighterValue);
114 aResult.setPixel(i, j, qRgba( aLighterColor.red(), aLighterColor.green(),
115 aLighterColor.blue(), aLighterColor.alpha() ) );
118 return QPixmap::fromImage(aResult);
121 void setSpinText(ModuleBase_ParamSpinBox* theSpin, const QString& theText)
123 if (theSpin->text() == theText)
125 // In order to avoid extra text setting because it will
126 // reset cursor position in control
127 bool isBlocked = theSpin->blockSignals(true);
128 theSpin->setText(theText);
129 theSpin->blockSignals(isBlocked);
132 void setSpinValue(QDoubleSpinBox* theSpin, double theValue)
134 if (fabs(theSpin->value() - theValue) < tolerance)
136 bool isBlocked = theSpin->blockSignals(true);
137 theSpin->setValue(theValue);
138 theSpin->blockSignals(isBlocked);
141 void setSpinValue(ModuleBase_ParamSpinBox* theSpin, double theValue)
143 if (fabs(theSpin->value() - theValue) < tolerance)
145 bool isBlocked = theSpin->blockSignals(true);
146 theSpin->setValue(theValue);
147 theSpin->blockSignals(isBlocked);
150 QString objectInfo(const ObjectPtr& theObj, const bool isUseAttributesInfo)
152 QString aFeatureStr = "feature";
156 std::ostringstream aPtrStr;
157 aPtrStr << "[" << theObj.get() << "]";
159 ResultPtr aRes = std::dynamic_pointer_cast<ModelAPI_Result>(theObj);
160 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theObj);
162 aFeatureStr.append(QString("(result%1)").arg(aPtrStr.str().c_str()).toStdString() .c_str());
163 if (aRes->isDisabled())
164 aFeatureStr.append("[disabled]");
165 if (aRes->isConcealed())
166 aFeatureStr.append("[concealed]");
167 if (ModelAPI_Tools::hasSubResults(aRes))
168 aFeatureStr.append("[hasSubResults]");
170 aFeature = ModelAPI_Feature::feature(aRes);
173 aFeatureStr.append(aPtrStr.str().c_str());
175 if (aFeature.get()) {
176 aFeatureStr.append(QString(": %1").arg(aFeature->getKind().c_str()).toStdString().c_str());
177 if (aFeature->data()->isValid()) {
178 aFeatureStr.append(QString(", name=%1").arg(aFeature->data()->name().c_str()).toStdString()
181 if (isUseAttributesInfo) {
182 std::list<AttributePtr> anAttrs = aFeature->data()->attributes("");
183 std::list<AttributePtr>::const_iterator anIt = anAttrs.begin(), aLast = anAttrs.end();
185 for(; anIt != aLast; anIt++) {
186 AttributePtr anAttr = *anIt;
187 QString aValue = "not defined";
188 std::string aType = anAttr->attributeType();
189 if (aType == GeomDataAPI_Point2D::typeId()) {
190 std::shared_ptr<GeomDataAPI_Point2D> aPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
193 aValue = QString("(%1, %2)").arg(aPoint->x()).arg(aPoint->y());
195 else if (aType == ModelAPI_AttributeRefAttr::typeId()) {
198 aValues.push_back(QString("%1: %2").arg(anAttr->id().c_str()).arg(aValue).toStdString().c_str());
200 if (!aValues.empty())
201 aFeatureStr.append(QString(", attributes: %1").arg(aValues.join(", ").toStdString().c_str()));
208 typedef QMap<QString, TopAbs_ShapeEnum> ShapeTypes;
209 static ShapeTypes MyShapeTypes;
211 TopAbs_ShapeEnum shapeType(const QString& theType)
213 if (MyShapeTypes.count() == 0) {
214 MyShapeTypes["face"] = TopAbs_FACE;
215 MyShapeTypes["faces"] = TopAbs_FACE;
216 MyShapeTypes["vertex"] = TopAbs_VERTEX;
217 MyShapeTypes["vertices"] = TopAbs_VERTEX;
218 MyShapeTypes["wire"] = TopAbs_WIRE;
219 MyShapeTypes["edge"] = TopAbs_EDGE;
220 MyShapeTypes["edges"] = TopAbs_EDGE;
221 MyShapeTypes["shell"] = TopAbs_SHELL;
222 MyShapeTypes["solid"] = TopAbs_SOLID;
223 MyShapeTypes["solids"] = TopAbs_SOLID;
224 MyShapeTypes["objects"] = TopAbs_SHAPE;
226 QString aType = theType.toLower();
227 if (MyShapeTypes.contains(aType))
228 return MyShapeTypes[aType];
229 Events_Error::send("Shape type defined in XML is not implemented!");
233 void checkObjects(const QObjectPtrList& theObjects, bool& hasResult, bool& hasFeature, bool& hasParameter, bool& hasSubFeature)
237 hasParameter = false;
238 hasSubFeature = false;
239 foreach(ObjectPtr aObj, theObjects) {
240 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aObj);
241 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(aObj);
242 ResultParameterPtr aConstruction = std::dynamic_pointer_cast<ModelAPI_ResultParameter>(aResult);
244 hasResult = (aResult.get() != NULL);
245 hasFeature = (aFeature.get() != NULL);
246 hasParameter = (aConstruction.get() != NULL);
248 hasSubFeature = (ModelAPI_Tools::compositeOwner(aFeature) != NULL);
249 if (hasFeature && hasResult && hasParameter && hasSubFeature)
254 void setDefaultDeviationCoefficient(const TopoDS_Shape& theShape,
255 const Handle(Prs3d_Drawer)& theDrawer)
257 if (!theShape.IsNull() && theShape.ShapeType() == TopAbs_EDGE)
258 theDrawer->SetDeviationCoefficient(1.e-4);
261 Quantity_Color color(const std::string& theSection,
262 const std::string& theName,
263 const std::string& theDefault)
265 std::vector<int> aColor = Config_PropManager::color(theSection, theName, theDefault);
266 return Quantity_Color(aColor[0] / 255., aColor[1] / 255., aColor[2] / 255., Quantity_TOC_RGB);