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>
25 #include <QDoubleSpinBox>
29 const double tolerance = 1e-7;
32 namespace ModuleBase_Tools {
34 //******************************************************************
36 //******************************************************************
38 void adjustMargins(QWidget* theWidget)
42 adjustMargins(theWidget->layout());
45 void adjustMargins(QLayout* theLayout)
49 theLayout->setContentsMargins(2, 5, 2, 5);
50 theLayout->setSpacing(4);
53 void zeroMargins(QWidget* theWidget)
57 zeroMargins(theWidget->layout());
60 void zeroMargins(QLayout* theLayout)
64 theLayout->setContentsMargins(0, 0, 0, 0);
65 theLayout->setSpacing(5);
68 QPixmap composite(const QString& theAdditionalIcon, const QString& theIcon)
70 QImage anIcon(theIcon);
71 QImage anAditional(theAdditionalIcon);
76 int anAddWidth = anAditional.width();
77 int anAddHeight = anAditional.height();
79 int aWidth = anIcon.width();
80 int aHeight = anIcon.height();
82 int aStartWidthPos = aWidth - anAddWidth - 1;
83 int aStartHeightPos = aHeight - anAddHeight - 1;
85 for (int i = 0; i < anAddWidth && i + aStartWidthPos < aWidth; i++)
87 for (int j = 0; j < anAddHeight && j + aStartHeightPos < aHeight; j++)
89 if (qAlpha(anAditional.pixel(i, j)) > 0)
90 anIcon.setPixel(i + aStartWidthPos, j + aStartHeightPos, anAditional.pixel(i, j));
93 return QPixmap::fromImage(anIcon);
96 QPixmap lighter(const QString& theIcon, const int theLighterValue)
98 QImage anIcon(theIcon);
102 QImage aResult(theIcon);
103 for ( int i = 0; i < anIcon.width(); i++ )
105 for ( int j = 0; j < anIcon.height(); j++ )
107 QRgb anRgb = anIcon.pixel( i, j );
108 QColor aPixelColor(qRed(anRgb), qGreen(anRgb), qBlue(anRgb),
109 qAlpha( aResult.pixel( i, j ) ));
111 QColor aLighterColor = aPixelColor.lighter(theLighterValue);
112 aResult.setPixel(i, j, qRgba( aLighterColor.red(), aLighterColor.green(),
113 aLighterColor.blue(), aLighterColor.alpha() ) );
116 return QPixmap::fromImage(aResult);
119 void setSpinText(ModuleBase_ParamSpinBox* theSpin, const QString& theText)
121 if (theSpin->text() == theText)
123 // In order to avoid extra text setting because it will
124 // reset cursor position in control
125 bool isBlocked = theSpin->blockSignals(true);
126 theSpin->setText(theText);
127 theSpin->blockSignals(isBlocked);
130 void setSpinValue(QDoubleSpinBox* theSpin, double theValue)
132 if (fabs(theSpin->value() - theValue) < tolerance)
134 bool isBlocked = theSpin->blockSignals(true);
135 theSpin->setValue(theValue);
136 theSpin->blockSignals(isBlocked);
139 void setSpinValue(ModuleBase_ParamSpinBox* theSpin, double theValue)
141 if (fabs(theSpin->value() - theValue) < tolerance)
143 bool isBlocked = theSpin->blockSignals(true);
144 theSpin->setValue(theValue);
145 theSpin->blockSignals(isBlocked);
148 QString objectInfo(const ObjectPtr& theObj, const bool isUseAttributesInfo)
150 QString aFeatureStr = "feature";
154 std::ostringstream aPtrStr;
155 aPtrStr << "[" << theObj.get() << "]";
157 ResultPtr aRes = std::dynamic_pointer_cast<ModelAPI_Result>(theObj);
158 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theObj);
160 aFeatureStr.append(QString("(result%1)").arg(aPtrStr.str().c_str()).toStdString() .c_str());
161 if (aRes->isDisabled())
162 aFeatureStr.append("[disabled]");
163 if (aRes->isConcealed())
164 aFeatureStr.append("[concealed]");
165 if (ModelAPI_Tools::hasSubResults(aRes))
166 aFeatureStr.append("[hasSubResults]");
168 aFeature = ModelAPI_Feature::feature(aRes);
171 aFeatureStr.append(aPtrStr.str().c_str());
173 if (aFeature.get()) {
174 aFeatureStr.append(QString(": %1").arg(aFeature->getKind().c_str()).toStdString().c_str());
175 if (aFeature->data()->isValid()) {
176 aFeatureStr.append(QString(", name=%1").arg(aFeature->data()->name().c_str()).toStdString()
179 if (isUseAttributesInfo) {
180 std::list<AttributePtr> anAttrs = aFeature->data()->attributes("");
181 std::list<AttributePtr>::const_iterator anIt = anAttrs.begin(), aLast = anAttrs.end();
183 for(; anIt != aLast; anIt++) {
184 AttributePtr anAttr = *anIt;
185 QString aValue = "not defined";
186 std::string aType = anAttr->attributeType();
187 if (aType == GeomDataAPI_Point2D::typeId()) {
188 std::shared_ptr<GeomDataAPI_Point2D> aPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
191 aValue = QString("(%1, %2)").arg(aPoint->x()).arg(aPoint->y());
193 else if (aType == ModelAPI_AttributeRefAttr::typeId()) {
196 aValues.push_back(QString("%1: %2").arg(anAttr->id().c_str()).arg(aValue).toStdString().c_str());
198 if (!aValues.empty())
199 aFeatureStr.append(QString(", attributes: %1").arg(aValues.join(", ").toStdString().c_str()));
206 typedef QMap<QString, TopAbs_ShapeEnum> ShapeTypes;
207 static ShapeTypes MyShapeTypes;
209 TopAbs_ShapeEnum shapeType(const QString& theType)
211 if (MyShapeTypes.count() == 0) {
212 MyShapeTypes["face"] = TopAbs_FACE;
213 MyShapeTypes["faces"] = TopAbs_FACE;
214 MyShapeTypes["vertex"] = TopAbs_VERTEX;
215 MyShapeTypes["vertices"] = TopAbs_VERTEX;
216 MyShapeTypes["wire"] = TopAbs_WIRE;
217 MyShapeTypes["edge"] = TopAbs_EDGE;
218 MyShapeTypes["edges"] = TopAbs_EDGE;
219 MyShapeTypes["shell"] = TopAbs_SHELL;
220 MyShapeTypes["solid"] = TopAbs_SOLID;
221 MyShapeTypes["solids"] = TopAbs_SOLID;
222 MyShapeTypes["objects"] = TopAbs_SHAPE;
224 QString aType = theType.toLower();
225 if (MyShapeTypes.contains(aType))
226 return MyShapeTypes[aType];
227 Events_Error::send("Shape type defined in XML is not implemented!");
231 void checkObjects(const QObjectPtrList& theObjects, bool& hasResult, bool& hasFeature, bool& hasParameter, bool& hasSubFeature)
235 hasParameter = false;
236 hasSubFeature = false;
237 foreach(ObjectPtr aObj, theObjects) {
238 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aObj);
239 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(aObj);
240 ResultParameterPtr aConstruction = std::dynamic_pointer_cast<ModelAPI_ResultParameter>(aResult);
242 hasResult = (aResult.get() != NULL);
243 hasFeature = (aFeature.get() != NULL);
244 hasParameter = (aConstruction.get() != NULL);
246 hasSubFeature = (ModelAPI_Tools::compositeOwner(aFeature) != NULL);
247 if (hasFeature && hasResult && hasParameter && hasSubFeature)
252 void setDefaultDeviationCoefficient(const TopoDS_Shape& theShape,
253 const Handle(Prs3d_Drawer)& theDrawer)
255 if (!theShape.IsNull() && theShape.ShapeType() == TopAbs_EDGE)
256 theDrawer->SetDeviationCoefficient(1.e-4);