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 namespace ModuleBase_Tools {
31 //******************************************************************
33 //******************************************************************
35 void adjustMargins(QWidget* theWidget)
39 adjustMargins(theWidget->layout());
42 void adjustMargins(QLayout* theLayout)
46 theLayout->setContentsMargins(2, 5, 2, 5);
47 theLayout->setSpacing(4);
50 void zeroMargins(QWidget* theWidget)
54 zeroMargins(theWidget->layout());
57 void zeroMargins(QLayout* theLayout)
61 theLayout->setContentsMargins(0, 0, 0, 0);
62 theLayout->setSpacing(5);
65 QPixmap composite(const QString& theAdditionalIcon, const QString& theIcon)
67 QImage anIcon(theIcon);
68 QImage anAditional(theAdditionalIcon);
73 int anAddWidth = anAditional.width();
74 int anAddHeight = anAditional.height();
76 int aWidth = anIcon.width();
77 int aHeight = anIcon.height();
79 int aStartWidthPos = aWidth - anAddWidth - 1;
80 int aStartHeightPos = aHeight - anAddHeight - 1;
82 for (int i = 0; i < anAddWidth && i + aStartWidthPos < aWidth; i++)
84 for (int j = 0; j < anAddHeight && j + aStartHeightPos < aHeight; j++)
86 if (qAlpha(anAditional.pixel(i, j)) > 0)
87 anIcon.setPixel(i + aStartWidthPos, j + aStartHeightPos, anAditional.pixel(i, j));
90 return QPixmap::fromImage(anIcon);
93 QPixmap lighter(const QString& theIcon, const int theLighterValue)
95 QImage anIcon(theIcon);
99 QImage aResult(theIcon);
100 for ( int i = 0; i < anIcon.width(); i++ )
102 for ( int j = 0; j < anIcon.height(); j++ )
104 QRgb anRgb = anIcon.pixel( i, j );
105 QColor aPixelColor(qRed(anRgb), qGreen(anRgb), qBlue(anRgb),
106 qAlpha( aResult.pixel( i, j ) ));
108 QColor aLighterColor = aPixelColor.lighter(theLighterValue);
109 aResult.setPixel(i, j, qRgba( aLighterColor.red(), aLighterColor.green(),
110 aLighterColor.blue(), aLighterColor.alpha() ) );
113 return QPixmap::fromImage(aResult);
116 void setSpinText(ModuleBase_ParamSpinBox* theSpin, const QString& theText)
118 if (theSpin->text() == theText)
120 // In order to avoid extra text setting because it will
121 // reset cursor position in control
122 bool isBlocked = theSpin->blockSignals(true);
123 theSpin->setText(theText);
124 theSpin->blockSignals(isBlocked);
127 void setSpinValue(QDoubleSpinBox* theSpin, double theValue)
129 if (theSpin->value() == theValue)
131 bool isBlocked = theSpin->blockSignals(true);
132 theSpin->setValue(theValue);
133 theSpin->blockSignals(isBlocked);
136 void setSpinValue(ModuleBase_ParamSpinBox* theSpin, double theValue)
138 //if (theSpin->value() == theValue)
140 bool isBlocked = theSpin->blockSignals(true);
141 theSpin->setValue(theValue);
142 theSpin->blockSignals(isBlocked);
145 QString objectInfo(const ObjectPtr& theObj, const bool isUseAttributesInfo)
147 QString aFeatureStr = "feature";
151 std::ostringstream aPtrStr;
152 aPtrStr << "[" << theObj.get() << "]";
154 ResultPtr aRes = std::dynamic_pointer_cast<ModelAPI_Result>(theObj);
155 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theObj);
157 aFeatureStr.append(QString("(result%1)").arg(aPtrStr.str().c_str()).toStdString() .c_str());
158 if (aRes->isDisabled())
159 aFeatureStr.append("[disabled]");
160 if (aRes->isConcealed())
161 aFeatureStr.append("[concealed]");
162 if (ModelAPI_Tools::hasSubResults(aRes))
163 aFeatureStr.append("[hasSubResults]");
165 aFeature = ModelAPI_Feature::feature(aRes);
168 aFeatureStr.append(aPtrStr.str().c_str());
170 if (aFeature.get()) {
171 aFeatureStr.append(QString(": %1").arg(aFeature->getKind().c_str()).toStdString().c_str());
172 if (aFeature->data()->isValid()) {
173 aFeatureStr.append(QString(", name=%1").arg(aFeature->data()->name().c_str()).toStdString()
176 if (isUseAttributesInfo) {
177 std::list<AttributePtr> anAttrs = aFeature->data()->attributes("");
178 std::list<AttributePtr>::const_iterator anIt = anAttrs.begin(), aLast = anAttrs.end();
180 for(; anIt != aLast; anIt++) {
181 AttributePtr anAttr = *anIt;
182 QString aValue = "not defined";
183 std::string aType = anAttr->attributeType();
184 if (aType == GeomDataAPI_Point2D::typeId()) {
185 std::shared_ptr<GeomDataAPI_Point2D> aPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
188 aValue = QString("(%1, %2)").arg(aPoint->x()).arg(aPoint->y());
190 else if (aType == ModelAPI_AttributeRefAttr::typeId()) {
193 aValues.push_back(QString("%1: %2").arg(anAttr->id().c_str()).arg(aValue).toStdString().c_str());
195 if (!aValues.empty())
196 aFeatureStr.append(QString(", attributes: %1").arg(aValues.join(", ").toStdString().c_str()));
203 typedef QMap<QString, TopAbs_ShapeEnum> ShapeTypes;
204 static ShapeTypes MyShapeTypes;
206 TopAbs_ShapeEnum shapeType(const QString& theType)
208 if (MyShapeTypes.count() == 0) {
209 MyShapeTypes["face"] = TopAbs_FACE;
210 MyShapeTypes["faces"] = TopAbs_FACE;
211 MyShapeTypes["vertex"] = TopAbs_VERTEX;
212 MyShapeTypes["vertices"] = TopAbs_VERTEX;
213 MyShapeTypes["wire"] = TopAbs_WIRE;
214 MyShapeTypes["edge"] = TopAbs_EDGE;
215 MyShapeTypes["edges"] = TopAbs_EDGE;
216 MyShapeTypes["shell"] = TopAbs_SHELL;
217 MyShapeTypes["solid"] = TopAbs_SOLID;
218 MyShapeTypes["solids"] = TopAbs_SOLID;
219 MyShapeTypes["objects"] = TopAbs_SHAPE;
221 QString aType = theType.toLower();
222 if (MyShapeTypes.contains(aType))
223 return MyShapeTypes[aType];
224 Events_Error::send("Shape type defined in XML is not implemented!");
228 void checkObjects(const QObjectPtrList& theObjects, bool& hasResult, bool& hasFeature, bool& hasParameter, bool& hasSubFeature)
232 hasParameter = false;
233 hasSubFeature = false;
234 foreach(ObjectPtr aObj, theObjects) {
235 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aObj);
236 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(aObj);
237 ResultParameterPtr aConstruction = std::dynamic_pointer_cast<ModelAPI_ResultParameter>(aResult);
239 hasResult = (aResult.get() != NULL);
240 hasFeature = (aFeature.get() != NULL);
241 hasParameter = (aConstruction.get() != NULL);
243 hasSubFeature = (ModelAPI_Tools::compositeOwner(aFeature) != NULL);
244 if (hasFeature && hasResult && hasParameter && hasSubFeature)
249 void setDefaultDeviationCoefficient(const TopoDS_Shape& theShape,
250 const Handle(Prs3d_Drawer)& theDrawer)
252 if (!theShape.IsNull() && theShape.ShapeType() == TopAbs_EDGE)
253 theDrawer->SetDeviationCoefficient(1.e-4);