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;
37 //#define DEBUG_ACTIVATE_WINDOW
38 //#define DEBUG_SET_FOCUS
40 namespace ModuleBase_Tools {
42 //******************************************************************
44 //******************************************************************
46 void adjustMargins(QWidget* theWidget)
50 adjustMargins(theWidget->layout());
53 void adjustMargins(QLayout* theLayout)
57 theLayout->setContentsMargins(2, 5, 2, 5);
58 theLayout->setSpacing(4);
61 void zeroMargins(QWidget* theWidget)
65 zeroMargins(theWidget->layout());
68 void zeroMargins(QLayout* theLayout)
72 theLayout->setContentsMargins(0, 0, 0, 0);
73 theLayout->setSpacing(5);
76 void activateWindow(QWidget* theWidget, const QString& theInfo)
78 theWidget->activateWindow();
80 #ifdef DEBUG_ACTIVATE_WINDOW
81 qDebug(QString("activateWindow: %1").arg(theInfo).toStdString().c_str());
85 void setFocus(QWidget* theWidget, const QString& theInfo)
87 theWidget->setFocus();
89 #ifdef DEBUG_SET_FOCUS
90 qDebug(QString("setFocus: %1").arg(theInfo).toStdString().c_str());
94 QPixmap composite(const QString& theAdditionalIcon, const QString& theIcon)
96 QImage anIcon(theIcon);
97 QImage anAditional(theAdditionalIcon);
102 int anAddWidth = anAditional.width();
103 int anAddHeight = anAditional.height();
105 int aWidth = anIcon.width();
106 int aHeight = anIcon.height();
108 int aStartWidthPos = aWidth - anAddWidth - 1;
109 int aStartHeightPos = aHeight - anAddHeight - 1;
111 for (int i = 0; i < anAddWidth && i + aStartWidthPos < aWidth; i++)
113 for (int j = 0; j < anAddHeight && j + aStartHeightPos < aHeight; j++)
115 if (qAlpha(anAditional.pixel(i, j)) > 0)
116 anIcon.setPixel(i + aStartWidthPos, j + aStartHeightPos, anAditional.pixel(i, j));
119 return QPixmap::fromImage(anIcon);
122 QPixmap lighter(const QString& theIcon, const int theLighterValue)
124 QImage anIcon(theIcon);
128 QImage aResult(theIcon);
129 for ( int i = 0; i < anIcon.width(); i++ )
131 for ( int j = 0; j < anIcon.height(); j++ )
133 QRgb anRgb = anIcon.pixel( i, j );
134 QColor aPixelColor(qRed(anRgb), qGreen(anRgb), qBlue(anRgb),
135 qAlpha( aResult.pixel( i, j ) ));
137 QColor aLighterColor = aPixelColor.lighter(theLighterValue);
138 aResult.setPixel(i, j, qRgba( aLighterColor.red(), aLighterColor.green(),
139 aLighterColor.blue(), aLighterColor.alpha() ) );
142 return QPixmap::fromImage(aResult);
145 void setSpinText(ModuleBase_ParamSpinBox* theSpin, const QString& theText)
147 if (theSpin->text() == theText)
149 // In order to avoid extra text setting because it will
150 // reset cursor position in control
151 bool isBlocked = theSpin->blockSignals(true);
152 theSpin->setText(theText);
153 theSpin->blockSignals(isBlocked);
156 void setSpinValue(QDoubleSpinBox* theSpin, double theValue)
158 if (fabs(theSpin->value() - theValue) < tolerance)
160 bool isBlocked = theSpin->blockSignals(true);
161 theSpin->setValue(theValue);
162 theSpin->blockSignals(isBlocked);
165 void setSpinValue(ModuleBase_ParamSpinBox* theSpin, double theValue)
167 if (fabs(theSpin->value() - theValue) < tolerance)
169 bool isBlocked = theSpin->blockSignals(true);
170 theSpin->setValue(theValue);
171 theSpin->blockSignals(isBlocked);
174 QString objectInfo(const ObjectPtr& theObj, const bool isUseAttributesInfo)
176 QString aFeatureStr = "feature";
180 std::ostringstream aPtrStr;
181 aPtrStr << "[" << theObj.get() << "]";
183 ResultPtr aRes = std::dynamic_pointer_cast<ModelAPI_Result>(theObj);
184 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theObj);
186 aFeatureStr.append(QString("(result%1)").arg(aPtrStr.str().c_str()).toStdString() .c_str());
187 if (aRes->isDisabled())
188 aFeatureStr.append("[disabled]");
189 if (aRes->isConcealed())
190 aFeatureStr.append("[concealed]");
191 if (ModelAPI_Tools::hasSubResults(aRes))
192 aFeatureStr.append("[hasSubResults]");
194 aFeature = ModelAPI_Feature::feature(aRes);
197 aFeatureStr.append(aPtrStr.str().c_str());
199 if (aFeature.get()) {
200 aFeatureStr.append(QString(": %1").arg(aFeature->getKind().c_str()).toStdString().c_str());
201 if (aFeature->data()->isValid()) {
202 aFeatureStr.append(QString(", name=%1").arg(aFeature->data()->name().c_str()).toStdString()
205 if (isUseAttributesInfo) {
206 std::list<AttributePtr> anAttrs = aFeature->data()->attributes("");
207 std::list<AttributePtr>::const_iterator anIt = anAttrs.begin(), aLast = anAttrs.end();
209 for(; anIt != aLast; anIt++) {
210 AttributePtr anAttr = *anIt;
211 QString aValue = "not defined";
212 std::string aType = anAttr->attributeType();
213 if (aType == GeomDataAPI_Point2D::typeId()) {
214 std::shared_ptr<GeomDataAPI_Point2D> aPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
217 aValue = QString("(%1, %2)").arg(aPoint->x()).arg(aPoint->y());
219 else if (aType == ModelAPI_AttributeRefAttr::typeId()) {
222 aValues.push_back(QString("%1: %2").arg(anAttr->id().c_str()).arg(aValue).toStdString().c_str());
224 if (!aValues.empty())
225 aFeatureStr.append(QString(", attributes: %1").arg(aValues.join(", ").toStdString().c_str()));
232 typedef QMap<QString, TopAbs_ShapeEnum> ShapeTypes;
233 static ShapeTypes MyShapeTypes;
235 TopAbs_ShapeEnum shapeType(const QString& theType)
237 if (MyShapeTypes.count() == 0) {
238 MyShapeTypes["face"] = TopAbs_FACE;
239 MyShapeTypes["faces"] = TopAbs_FACE;
240 MyShapeTypes["vertex"] = TopAbs_VERTEX;
241 MyShapeTypes["vertices"] = TopAbs_VERTEX;
242 MyShapeTypes["wire"] = TopAbs_WIRE;
243 MyShapeTypes["edge"] = TopAbs_EDGE;
244 MyShapeTypes["edges"] = TopAbs_EDGE;
245 MyShapeTypes["shell"] = TopAbs_SHELL;
246 MyShapeTypes["solid"] = TopAbs_SOLID;
247 MyShapeTypes["solids"] = TopAbs_SOLID;
248 MyShapeTypes["objects"] = TopAbs_SHAPE;
250 QString aType = theType.toLower();
251 if (MyShapeTypes.contains(aType))
252 return MyShapeTypes[aType];
253 Events_Error::send("Shape type defined in XML is not implemented!");
257 void checkObjects(const QObjectPtrList& theObjects, bool& hasResult, bool& hasFeature, bool& hasParameter, bool& hasSubFeature)
261 hasParameter = false;
262 hasSubFeature = false;
263 foreach(ObjectPtr aObj, theObjects) {
264 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aObj);
265 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(aObj);
266 ResultParameterPtr aConstruction = std::dynamic_pointer_cast<ModelAPI_ResultParameter>(aResult);
268 hasResult |= (aResult.get() != NULL);
269 hasFeature |= (aFeature.get() != NULL);
270 hasParameter |= (aConstruction.get() != NULL);
272 hasSubFeature |= (ModelAPI_Tools::compositeOwner(aFeature) != NULL);
273 if (hasFeature && hasResult && hasParameter && hasSubFeature)
278 void setDefaultDeviationCoefficient(const TopoDS_Shape& theShape,
279 const Handle(Prs3d_Drawer)& theDrawer)
281 if (theShape.IsNull())
283 TopAbs_ShapeEnum aType = theShape.ShapeType();
284 if ((aType == TopAbs_EDGE) || (aType == TopAbs_WIRE))
285 theDrawer->SetDeviationCoefficient(1.e-4);
288 Quantity_Color color(const std::string& theSection,
289 const std::string& theName,
290 const std::string& theDefault)
292 std::vector<int> aColor = Config_PropManager::color(theSection, theName, theDefault);
293 return Quantity_Color(aColor[0] / 255., aColor[1] / 255., aColor[2] / 255., Quantity_TOC_RGB);
296 ObjectPtr getObject(const AttributePtr& theAttribute)
299 std::string anAttrType = theAttribute->attributeType();
300 if (anAttrType == ModelAPI_AttributeRefAttr::typeId()) {
301 AttributeRefAttrPtr anAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
302 if (anAttr != NULL && anAttr->isObject())
303 anObject = anAttr->object();
305 if (anAttrType == ModelAPI_AttributeSelection::typeId()) {
306 AttributeSelectionPtr anAttr = std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(theAttribute);
308 anObject = anAttr->context();
310 if (anAttrType == ModelAPI_AttributeReference::typeId()) {
311 AttributeReferencePtr anAttr = std::dynamic_pointer_cast<ModelAPI_AttributeReference>(theAttribute);
312 if (anAttr.get() != NULL)
313 anObject = anAttr->value();
318 TopAbs_ShapeEnum getCompoundSubType(const TopoDS_Shape& theShape)
320 TopAbs_ShapeEnum aShapeType = theShape.ShapeType();
322 // for compounds check sub-shapes: it may be compound of needed type:
323 // Booleans may produce compounds of Solids
324 if (aShapeType == TopAbs_COMPOUND) {
325 for(TopoDS_Iterator aSubs(theShape); aSubs.More(); aSubs.Next()) {
326 if (!aSubs.Value().IsNull()) {
327 TopAbs_ShapeEnum aSubType = aSubs.Value().ShapeType();
328 if (aSubType == TopAbs_COMPOUND) { // compound of compound(s)
329 aShapeType = TopAbs_COMPOUND;
332 if (aShapeType == TopAbs_COMPOUND) {
333 aShapeType = aSubType;
334 } else if (aShapeType != aSubType) { // compound of shapes of different types
335 aShapeType = TopAbs_COMPOUND;
344 } // namespace ModuleBase_Tools