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"
9 #include <ModuleBase_ParamIntSpinBox.h>
10 #include <ModuleBase_ParamSpinBox.h>
11 #include <ModuleBase_WidgetFactory.h>
12 #include <ModuleBase_IWorkshop.h>
13 #include <ModuleBase_IModule.h>
15 #include <ModelAPI_Attribute.h>
16 #include <ModelAPI_AttributeRefAttr.h>
17 #include <ModelAPI_AttributeReference.h>
18 #include <ModelAPI_AttributeSelection.h>
19 #include <ModelAPI_AttributeSelectionList.h>
20 #include <ModelAPI_AttributeRefList.h>
21 #include <ModelAPI_AttributeRefAttrList.h>
23 #include <ModelAPI_Data.h>
24 #include <ModelAPI_Result.h>
25 #include <ModelAPI_ResultCompSolid.h>
26 #include <ModelAPI_ResultParameter.h>
27 #include <ModelAPI_Tools.h>
28 #include <ModelAPI_Session.h>
30 #include <TopoDS_Iterator.hxx>
32 #include <GeomDataAPI_Point2D.h>
33 #include <Events_Error.h>
35 #include <Config_PropManager.h>
41 #include <QDoubleSpinBox>
42 #include <QGraphicsDropShadowEffect>
48 const double tolerance = 1e-7;
50 //#define DEBUG_ACTIVATE_WINDOW
51 //#define DEBUG_SET_FOCUS
53 namespace ModuleBase_Tools {
55 //******************************************************************
57 //******************************************************************
59 void adjustMargins(QWidget* theWidget)
63 adjustMargins(theWidget->layout());
66 void adjustMargins(QLayout* theLayout)
70 theLayout->setContentsMargins(2, 5, 2, 5);
71 theLayout->setSpacing(4);
74 void zeroMargins(QWidget* theWidget)
78 zeroMargins(theWidget->layout());
81 void zeroMargins(QLayout* theLayout)
85 theLayout->setContentsMargins(0, 0, 0, 0);
86 theLayout->setSpacing(5);
89 void activateWindow(QWidget* theWidget, const QString& theInfo)
91 theWidget->activateWindow();
93 #ifdef DEBUG_ACTIVATE_WINDOW
94 qDebug(QString("activateWindow: %1").arg(theInfo).toStdString().c_str());
98 void setFocus(QWidget* theWidget, const QString& theInfo)
100 theWidget->setFocus();
102 #ifdef DEBUG_SET_FOCUS
103 qDebug(QString("setFocus: %1").arg(theInfo).toStdString().c_str());
107 void setShadowEffect(QWidget* theWidget, const bool isSetEffect)
110 QGraphicsDropShadowEffect* aGlowEffect = new QGraphicsDropShadowEffect();
111 aGlowEffect->setOffset(.0);
112 aGlowEffect->setBlurRadius(10.0);
113 aGlowEffect->setColor(QColor(0, 170, 255)); // Light-blue color, #00AAFF
114 theWidget->setGraphicsEffect(aGlowEffect);
117 QGraphicsEffect* anEffect = theWidget->graphicsEffect();
119 anEffect->deleteLater();
120 theWidget->setGraphicsEffect(NULL);
124 QPixmap composite(const QString& theAdditionalIcon, const QString& theIcon)
126 QImage anIcon(theIcon);
127 QImage anAditional(theAdditionalIcon);
132 int anAddWidth = anAditional.width();
133 int anAddHeight = anAditional.height();
135 int aWidth = anIcon.width();
136 int aHeight = anIcon.height();
138 int aStartWidthPos = aWidth - anAddWidth - 1;
139 int aStartHeightPos = aHeight - anAddHeight - 1;
141 for (int i = 0; i < anAddWidth && i + aStartWidthPos < aWidth; i++)
143 for (int j = 0; j < anAddHeight && j + aStartHeightPos < aHeight; j++)
145 if (qAlpha(anAditional.pixel(i, j)) > 0)
146 anIcon.setPixel(i + aStartWidthPos, j + aStartHeightPos, anAditional.pixel(i, j));
149 return QPixmap::fromImage(anIcon);
152 QPixmap lighter(const QString& theIcon, const int theLighterValue)
154 QImage anIcon(theIcon);
158 QImage aResult(theIcon);
159 for ( int i = 0; i < anIcon.width(); i++ )
161 for ( int j = 0; j < anIcon.height(); j++ )
163 QRgb anRgb = anIcon.pixel( i, j );
164 QColor aPixelColor(qRed(anRgb), qGreen(anRgb), qBlue(anRgb),
165 qAlpha( aResult.pixel( i, j ) ));
167 QColor aLighterColor = aPixelColor.lighter(theLighterValue);
168 aResult.setPixel(i, j, qRgba( aLighterColor.red(), aLighterColor.green(),
169 aLighterColor.blue(), aLighterColor.alpha() ) );
172 return QPixmap::fromImage(aResult);
175 void setSpinText(ModuleBase_ParamSpinBox* theSpin, const QString& theText)
177 if (theSpin->text() == theText)
179 // In order to avoid extra text setting because it will
180 // reset cursor position in control
181 bool isBlocked = theSpin->blockSignals(true);
182 theSpin->setText(theText);
183 theSpin->blockSignals(isBlocked);
186 void setSpinValue(QDoubleSpinBox* theSpin, double theValue)
188 if (fabs(theSpin->value() - theValue) < tolerance)
190 bool isBlocked = theSpin->blockSignals(true);
191 theSpin->setValue(theValue);
192 theSpin->blockSignals(isBlocked);
195 void setSpinValue(ModuleBase_ParamSpinBox* theSpin, double theValue)
197 if (fabs(theSpin->value() - theValue) < tolerance)
199 bool isBlocked = theSpin->blockSignals(true);
200 theSpin->setValue(theValue);
201 theSpin->blockSignals(isBlocked);
204 void setSpinText(ModuleBase_ParamIntSpinBox* theSpin, const QString& theText)
206 // In order to avoid extra text setting because it will
207 // reset cursor position in control
208 if (theSpin->text() == theText)
210 bool isBlocked = theSpin->blockSignals(true);
211 theSpin->setText(theText);
212 theSpin->blockSignals(isBlocked);
215 void setSpinValue(ModuleBase_ParamIntSpinBox* theSpin, int theValue)
217 if (theSpin->value() == theValue)
219 bool isBlocked = theSpin->blockSignals(true);
220 theSpin->setValue(theValue);
221 theSpin->blockSignals(isBlocked);
224 QString objectInfo(const ObjectPtr& theObj, const bool isUseAttributesInfo)
226 QString aFeatureStr = "feature";
230 std::ostringstream aPtrStr;
231 aPtrStr << "[" << theObj.get() << "]";
233 ResultPtr aRes = std::dynamic_pointer_cast<ModelAPI_Result>(theObj);
234 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theObj);
236 aFeatureStr.append(QString("(result%1)").arg(aPtrStr.str().c_str()).toStdString() .c_str());
237 if (aRes->isDisabled())
238 aFeatureStr.append("[disabled]");
239 if (aRes->isConcealed())
240 aFeatureStr.append("[concealed]");
241 if (ModelAPI_Tools::hasSubResults(aRes))
242 aFeatureStr.append("[hasSubResults]");
244 aFeature = ModelAPI_Feature::feature(aRes);
247 aFeatureStr.append(aPtrStr.str().c_str());
249 if (aFeature.get()) {
250 aFeatureStr.append(QString(": %1").arg(aFeature->getKind().c_str()).toStdString().c_str());
251 if (aFeature->data()->isValid()) {
252 aFeatureStr.append(QString(", name=%1").arg(aFeature->data()->name().c_str()).toStdString()
255 if (isUseAttributesInfo) {
256 std::list<AttributePtr> anAttrs = aFeature->data()->attributes("");
257 std::list<AttributePtr>::const_iterator anIt = anAttrs.begin(), aLast = anAttrs.end();
259 for(; anIt != aLast; anIt++) {
260 AttributePtr anAttr = *anIt;
261 QString aValue = "not defined";
262 std::string aType = anAttr->attributeType();
263 if (aType == GeomDataAPI_Point2D::typeId()) {
264 std::shared_ptr<GeomDataAPI_Point2D> aPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
267 aValue = QString("(%1, %2)").arg(aPoint->x()).arg(aPoint->y());
269 else if (aType == ModelAPI_AttributeRefAttr::typeId()) {
272 aValues.push_back(QString("%1: %2").arg(anAttr->id().c_str()).arg(aValue).toStdString().c_str());
274 if (!aValues.empty())
275 aFeatureStr.append(QString(", attributes: %1").arg(aValues.join(", ").toStdString().c_str()));
282 typedef QMap<QString, TopAbs_ShapeEnum> ShapeTypes;
283 static ShapeTypes MyShapeTypes;
285 TopAbs_ShapeEnum shapeType(const QString& theType)
287 if (MyShapeTypes.count() == 0) {
288 MyShapeTypes["face"] = TopAbs_FACE;
289 MyShapeTypes["faces"] = TopAbs_FACE;
290 MyShapeTypes["vertex"] = TopAbs_VERTEX;
291 MyShapeTypes["vertices"] = TopAbs_VERTEX;
292 MyShapeTypes["wire"] = TopAbs_WIRE;
293 MyShapeTypes["edge"] = TopAbs_EDGE;
294 MyShapeTypes["edges"] = TopAbs_EDGE;
295 MyShapeTypes["shell"] = TopAbs_SHELL;
296 MyShapeTypes["solid"] = TopAbs_SOLID;
297 MyShapeTypes["solids"] = TopAbs_SOLID;
298 MyShapeTypes["objects"] = TopAbs_SHAPE;
300 QString aType = theType.toLower();
301 if (MyShapeTypes.contains(aType))
302 return MyShapeTypes[aType];
303 Events_Error::send("Shape type defined in XML is not implemented!");
307 void checkObjects(const QObjectPtrList& theObjects, bool& hasResult, bool& hasFeature,
308 bool& hasParameter, bool& hasCompositeOwner)
312 hasParameter = false;
313 hasCompositeOwner = false;
314 foreach(ObjectPtr aObj, theObjects) {
315 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aObj);
316 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(aObj);
317 ResultParameterPtr aConstruction = std::dynamic_pointer_cast<ModelAPI_ResultParameter>(aResult);
319 hasResult |= (aResult.get() != NULL);
320 hasFeature |= (aFeature.get() != NULL);
321 hasParameter |= (aConstruction.get() != NULL);
323 hasCompositeOwner |= (ModelAPI_Tools::compositeOwner(aFeature) != NULL);
324 if (hasFeature && hasResult && hasParameter && hasCompositeOwner)
329 void setDefaultDeviationCoefficient(const TopoDS_Shape& theShape,
330 const Handle(Prs3d_Drawer)& theDrawer)
332 if (theShape.IsNull())
334 TopAbs_ShapeEnum aType = theShape.ShapeType();
335 if ((aType == TopAbs_EDGE) || (aType == TopAbs_WIRE))
336 theDrawer->SetDeviationCoefficient(1.e-4);
339 Quantity_Color color(const std::string& theSection,
340 const std::string& theName,
341 const std::string& theDefault)
343 std::vector<int> aColor = Config_PropManager::color(theSection, theName, theDefault);
344 return Quantity_Color(aColor[0] / 255., aColor[1] / 255., aColor[2] / 255., Quantity_TOC_RGB);
347 ObjectPtr getObject(const AttributePtr& theAttribute)
350 std::string anAttrType = theAttribute->attributeType();
351 if (anAttrType == ModelAPI_AttributeRefAttr::typeId()) {
352 AttributeRefAttrPtr anAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
353 if (anAttr != NULL && anAttr->isObject())
354 anObject = anAttr->object();
356 if (anAttrType == ModelAPI_AttributeSelection::typeId()) {
357 AttributeSelectionPtr anAttr = std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(theAttribute);
359 anObject = anAttr->context();
361 if (anAttrType == ModelAPI_AttributeReference::typeId()) {
362 AttributeReferencePtr anAttr = std::dynamic_pointer_cast<ModelAPI_AttributeReference>(theAttribute);
363 if (anAttr.get() != NULL)
364 anObject = anAttr->value();
369 TopAbs_ShapeEnum getCompoundSubType(const TopoDS_Shape& theShape)
371 TopAbs_ShapeEnum aShapeType = theShape.ShapeType();
373 // for compounds check sub-shapes: it may be compound of needed type:
374 // Booleans may produce compounds of Solids
375 if (aShapeType == TopAbs_COMPOUND) {
376 for(TopoDS_Iterator aSubs(theShape); aSubs.More(); aSubs.Next()) {
377 if (!aSubs.Value().IsNull()) {
378 TopAbs_ShapeEnum aSubType = aSubs.Value().ShapeType();
379 if (aSubType == TopAbs_COMPOUND) { // compound of compound(s)
380 aShapeType = TopAbs_COMPOUND;
383 if (aShapeType == TopAbs_COMPOUND) {
384 aShapeType = aSubType;
385 } else if (aShapeType != aSubType) { // compound of shapes of different types
386 aShapeType = TopAbs_COMPOUND;
395 void getParameters(QStringList& theParameters)
397 theParameters.clear();
399 SessionPtr aSession = ModelAPI_Session::get();
400 std::list<DocumentPtr> aDocList;
401 DocumentPtr anActiveDocument = aSession->activeDocument();
402 DocumentPtr aRootDocument = aSession->moduleDocument();
403 aDocList.push_back(anActiveDocument);
404 if (anActiveDocument != aRootDocument) {
405 aDocList.push_back(aRootDocument);
407 std::string aGroupId = ModelAPI_ResultParameter::group();
408 for(std::list<DocumentPtr>::const_iterator it = aDocList.begin(); it != aDocList.end(); ++it) {
409 DocumentPtr aDocument = *it;
410 int aSize = aDocument->size(aGroupId);
411 for (int i = 0; i < aSize; i++) {
412 ObjectPtr anObject = aDocument->object(aGroupId, i);
413 std::string aParameterName = anObject->data()->name();
414 theParameters.append(aParameterName.c_str());
419 std::string findGreedAttribute(ModuleBase_IWorkshop* theWorkshop, const FeaturePtr& theFeature)
421 std::string anAttributeId;
423 std::string aXmlCfg, aDescription;
424 theWorkshop->module()->getXMLRepresentation(theFeature->getKind(), aXmlCfg, aDescription);
426 ModuleBase_WidgetFactory aFactory(aXmlCfg, theWorkshop);
427 std::string anAttributeTitle;
428 aFactory.getGreedAttribute(anAttributeId);
430 return anAttributeId;
433 void setObject(const AttributePtr& theAttribute, const ObjectPtr& theObject,
434 const GeomShapePtr& theShape, ModuleBase_IWorkshop* theWorkshop,
435 const bool theTemporarily)
437 if (!theAttribute.get())
440 std::string aType = theAttribute->attributeType();
441 if (aType == ModelAPI_AttributeReference::typeId()) {
442 AttributeReferencePtr aRef = std::dynamic_pointer_cast<ModelAPI_AttributeReference>(theAttribute);
443 ObjectPtr aObject = aRef->value();
444 if (!(aObject && aObject->isSame(theObject))) {
445 aRef->setValue(theObject);
447 } else if (aType == ModelAPI_AttributeRefAttr::typeId()) {
448 AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
450 AttributePtr anAttribute = theWorkshop->module()->findAttribute(theObject, theShape);
451 if (anAttribute.get())
452 aRefAttr->setAttr(anAttribute);
454 ObjectPtr aObject = aRefAttr->object();
455 if (!(aObject && aObject->isSame(theObject))) {
456 aRefAttr->setObject(theObject);
459 } else if (aType == ModelAPI_AttributeSelection::typeId()) {
460 AttributeSelectionPtr aSelectAttr =
461 std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(theAttribute);
462 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
463 if (aSelectAttr.get() != NULL) {
464 aSelectAttr->setValue(aResult, theShape, theTemporarily);
467 if (aType == ModelAPI_AttributeSelectionList::typeId()) {
468 AttributeSelectionListPtr aSelectionListAttr =
469 std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(theAttribute);
470 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
471 if (!aSelectionListAttr->isInList(aResult, theShape, theTemporarily))
472 aSelectionListAttr->append(aResult, theShape, theTemporarily);
474 else if (aType == ModelAPI_AttributeRefList::typeId()) {
475 AttributeRefListPtr aRefListAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(theAttribute);
476 if (!aRefListAttr->isInList(theObject))
477 aRefListAttr->append(theObject);
479 else if (aType == ModelAPI_AttributeRefAttrList::typeId()) {
480 AttributeRefAttrListPtr aRefAttrListAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttrList>(theAttribute);
481 AttributePtr anAttribute = theWorkshop->module()->findAttribute(theObject, theShape);
483 if (anAttribute.get()) {
484 if (!aRefAttrListAttr->isInList(anAttribute))
485 aRefAttrListAttr->append(anAttribute);
488 if (!aRefAttrListAttr->isInList(theObject))
489 aRefAttrListAttr->append(theObject);
494 GeomShapePtr getShape(const AttributePtr& theAttribute, ModuleBase_IWorkshop* theWorkshop)
497 if (!theAttribute.get())
500 std::string aType = theAttribute->attributeType();
501 if (aType == ModelAPI_AttributeReference::typeId()) {
502 } else if (aType == ModelAPI_AttributeRefAttr::typeId()) {
503 AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
504 if (aRefAttr.get() && !aRefAttr->isObject()) {
505 AttributePtr anAttribute = aRefAttr->attr();
506 aShape = theWorkshop->module()->findShape(anAttribute);
508 } else if (aType == ModelAPI_AttributeSelection::typeId()) {
509 AttributeSelectionPtr aSelectAttr = std::dynamic_pointer_cast<ModelAPI_AttributeSelection>
511 aShape = aSelectAttr->value();
516 } // namespace ModuleBase_Tools