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>
14 #include <ModuleBase_IconFactory.h>
16 #include <ModelAPI_Attribute.h>
17 #include <ModelAPI_AttributeRefAttr.h>
18 #include <ModelAPI_AttributeReference.h>
19 #include <ModelAPI_AttributeSelection.h>
20 #include <ModelAPI_AttributeSelectionList.h>
21 #include <ModelAPI_AttributeRefList.h>
22 #include <ModelAPI_AttributeRefAttrList.h>
23 #include <Events_Loop.h>
25 #include <ModelAPI_Data.h>
26 #include <ModelAPI_Result.h>
27 #include <ModelAPI_ResultCompSolid.h>
28 #include <ModelAPI_ResultParameter.h>
29 #include <ModelAPI_Tools.h>
30 #include <ModelAPI_Session.h>
31 #include <ModelAPI_Events.h>
33 #include <TopoDS_Iterator.hxx>
35 #include <GeomDataAPI_Point2D.h>
36 #include <Events_Error.h>
38 #include <Config_PropManager.h>
44 #include <QDoubleSpinBox>
45 #include <QGraphicsDropShadowEffect>
47 #include <QApplication>
52 const double tolerance = 1e-7;
54 //#define DEBUG_ACTIVATE_WINDOW
55 //#define DEBUG_SET_FOCUS
57 namespace ModuleBase_Tools {
59 //******************************************************************
61 //******************************************************************
63 void adjustMargins(QWidget* theWidget)
67 adjustMargins(theWidget->layout());
70 void adjustMargins(QLayout* theLayout)
74 theLayout->setContentsMargins(2, 5, 2, 5);
75 theLayout->setSpacing(4);
78 void zeroMargins(QWidget* theWidget)
82 zeroMargins(theWidget->layout());
85 void zeroMargins(QLayout* theLayout)
89 theLayout->setContentsMargins(0, 0, 0, 0);
90 theLayout->setSpacing(5);
93 void activateWindow(QWidget* theWidget, const QString& theInfo)
95 theWidget->activateWindow();
97 #ifdef DEBUG_ACTIVATE_WINDOW
98 qDebug(QString("activateWindow: %1").arg(theInfo).toStdString().c_str());
102 void setFocus(QWidget* theWidget, const QString& theInfo)
104 theWidget->setFocus();
106 #ifdef DEBUG_SET_FOCUS
107 qDebug(QString("setFocus: %1").arg(theInfo).toStdString().c_str());
111 void setShadowEffect(QWidget* theWidget, const bool isSetEffect)
114 QGraphicsDropShadowEffect* aGlowEffect = new QGraphicsDropShadowEffect();
115 aGlowEffect->setOffset(.0);
116 aGlowEffect->setBlurRadius(10.0);
117 aGlowEffect->setColor(QColor(0, 170, 255)); // Light-blue color, #00AAFF
118 theWidget->setGraphicsEffect(aGlowEffect);
121 QGraphicsEffect* anEffect = theWidget->graphicsEffect();
123 anEffect->deleteLater();
124 theWidget->setGraphicsEffect(NULL);
128 QPixmap composite(const QString& theAdditionalIcon, const QString& theIcon)
130 QImage anIcon = ModuleBase_IconFactory::loadImage(theIcon);
131 QImage anAditional(theAdditionalIcon);
136 int anAddWidth = anAditional.width();
137 int anAddHeight = anAditional.height();
139 int aWidth = anIcon.width();
140 int aHeight = anIcon.height();
142 int aStartWidthPos = aWidth - anAddWidth - 1;
143 int aStartHeightPos = aHeight - anAddHeight - 1;
145 for (int i = 0; i < anAddWidth && i + aStartWidthPos < aWidth; i++)
147 for (int j = 0; j < anAddHeight && j + aStartHeightPos < aHeight; j++)
149 if (qAlpha(anAditional.pixel(i, j)) > 0)
150 anIcon.setPixel(i + aStartWidthPos, j + aStartHeightPos, anAditional.pixel(i, j));
153 return QPixmap::fromImage(anIcon);
156 QPixmap lighter(const QString& theIcon, const int theLighterValue)
158 QImage anIcon = ModuleBase_IconFactory::loadImage(theIcon);
162 QImage aResult = ModuleBase_IconFactory::loadImage(theIcon);
163 for (int i = 0; i < anIcon.width(); i++)
165 for (int j = 0; j < anIcon.height(); j++)
167 QRgb anRgb = anIcon.pixel(i, j);
168 QColor aPixelColor(qRed(anRgb), qGreen(anRgb), qBlue(anRgb),
169 qAlpha(aResult.pixel(i, j)));
171 QColor aLighterColor = aPixelColor.lighter(theLighterValue);
172 aResult.setPixel(i, j, qRgba(aLighterColor.red(), aLighterColor.green(),
173 aLighterColor.blue(), aLighterColor.alpha()));
176 return QPixmap::fromImage(aResult);
179 void setSpinText(ModuleBase_ParamSpinBox* theSpin, const QString& theText)
181 if (theSpin->text() == theText)
183 // In order to avoid extra text setting because it will
184 // reset cursor position in control
185 bool isBlocked = theSpin->blockSignals(true);
186 theSpin->setText(theText);
187 theSpin->blockSignals(isBlocked);
190 void setSpinValue(QDoubleSpinBox* theSpin, double theValue)
192 if (fabs(theSpin->value() - theValue) < tolerance)
194 bool isBlocked = theSpin->blockSignals(true);
195 theSpin->setValue(theValue);
196 theSpin->blockSignals(isBlocked);
199 void setSpinValue(ModuleBase_ParamSpinBox* theSpin, double theValue)
201 if (fabs(theSpin->value() - theValue) < tolerance)
203 bool isBlocked = theSpin->blockSignals(true);
204 theSpin->setValue(theValue);
205 theSpin->blockSignals(isBlocked);
208 void setSpinText(ModuleBase_ParamIntSpinBox* theSpin, const QString& theText)
210 // In order to avoid extra text setting because it will
211 // reset cursor position in control
212 if (theSpin->text() == theText)
214 bool isBlocked = theSpin->blockSignals(true);
215 theSpin->setText(theText);
216 theSpin->blockSignals(isBlocked);
219 void setSpinValue(ModuleBase_ParamIntSpinBox* theSpin, int theValue)
221 if (theSpin->value() == theValue)
223 bool isBlocked = theSpin->blockSignals(true);
224 theSpin->setValue(theValue);
225 theSpin->blockSignals(isBlocked);
228 QString objectInfo(const ObjectPtr& theObj, const bool isUseAttributesInfo)
230 QString aFeatureStr = "feature";
234 std::ostringstream aPtrStr;
235 aPtrStr << "[" << theObj.get() << "]";
237 ResultPtr aRes = std::dynamic_pointer_cast<ModelAPI_Result>(theObj);
238 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theObj);
240 aFeatureStr.append(QString("(result%1)").arg(aPtrStr.str().c_str()).toStdString() .c_str());
241 if (aRes->isDisabled())
242 aFeatureStr.append("[disabled]");
243 if (aRes->isConcealed())
244 aFeatureStr.append("[concealed]");
245 if (ModelAPI_Tools::hasSubResults(aRes))
246 aFeatureStr.append("[hasSubResults]");
248 aFeature = ModelAPI_Feature::feature(aRes);
251 aFeatureStr.append(aPtrStr.str().c_str());
253 if (aFeature.get()) {
254 aFeatureStr.append(QString(": %1").arg(aFeature->getKind().c_str()).toStdString().c_str());
255 if (aFeature->data()->isValid()) {
256 aFeatureStr.append(QString(", name=%1").arg(aFeature->data()->name().c_str()).toStdString()
259 if (isUseAttributesInfo) {
260 std::list<AttributePtr> anAttrs = aFeature->data()->attributes("");
261 std::list<AttributePtr>::const_iterator anIt = anAttrs.begin(), aLast = anAttrs.end();
263 for(; anIt != aLast; anIt++) {
264 AttributePtr anAttr = *anIt;
265 QString aValue = "not defined";
266 std::string aType = anAttr->attributeType();
267 if (aType == GeomDataAPI_Point2D::typeId()) {
268 std::shared_ptr<GeomDataAPI_Point2D> aPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
271 aValue = QString("(%1, %2)").arg(aPoint->x()).arg(aPoint->y());
273 else if (aType == ModelAPI_AttributeRefAttr::typeId()) {
276 aValues.push_back(QString("%1: %2").arg(anAttr->id().c_str()).arg(aValue).toStdString().c_str());
278 if (!aValues.empty())
279 aFeatureStr.append(QString(", attributes: %1").arg(aValues.join(", ").toStdString().c_str()));
286 typedef QMap<QString, TopAbs_ShapeEnum> ShapeTypes;
287 static ShapeTypes MyShapeTypes;
289 TopAbs_ShapeEnum shapeType(const QString& theType)
291 if (MyShapeTypes.count() == 0) {
292 MyShapeTypes["face"] = TopAbs_FACE;
293 MyShapeTypes["faces"] = TopAbs_FACE;
294 MyShapeTypes["vertex"] = TopAbs_VERTEX;
295 MyShapeTypes["vertices"] = TopAbs_VERTEX;
296 MyShapeTypes["wire"] = TopAbs_WIRE;
297 MyShapeTypes["edge"] = TopAbs_EDGE;
298 MyShapeTypes["edges"] = TopAbs_EDGE;
299 MyShapeTypes["shell"] = TopAbs_SHELL;
300 MyShapeTypes["solid"] = TopAbs_SOLID;
301 MyShapeTypes["solids"] = TopAbs_SOLID;
302 MyShapeTypes["objects"] = TopAbs_SHAPE;
304 QString aType = theType.toLower();
305 if (MyShapeTypes.contains(aType))
306 return MyShapeTypes[aType];
307 Events_Error::send("Shape type defined in XML is not implemented!");
311 void checkObjects(const QObjectPtrList& theObjects, bool& hasResult, bool& hasFeature,
312 bool& hasParameter, bool& hasCompositeOwner)
316 hasParameter = false;
317 hasCompositeOwner = false;
318 foreach(ObjectPtr aObj, theObjects) {
319 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aObj);
320 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(aObj);
321 ResultParameterPtr aConstruction = std::dynamic_pointer_cast<ModelAPI_ResultParameter>(aResult);
323 hasResult |= (aResult.get() != NULL);
324 hasFeature |= (aFeature.get() != NULL);
325 hasParameter |= (aConstruction.get() != NULL);
327 hasCompositeOwner |= (ModelAPI_Tools::compositeOwner(aFeature) != NULL);
328 if (hasFeature && hasResult && hasParameter && hasCompositeOwner)
333 void setDefaultDeviationCoefficient(const TopoDS_Shape& theShape,
334 const Handle(Prs3d_Drawer)& theDrawer)
336 if (theShape.IsNull())
338 TopAbs_ShapeEnum aType = theShape.ShapeType();
339 if ((aType == TopAbs_EDGE) || (aType == TopAbs_WIRE))
340 theDrawer->SetDeviationCoefficient(1.e-4);
343 Quantity_Color color(const std::string& theSection,
344 const std::string& theName,
345 const std::string& theDefault)
347 std::vector<int> aColor = Config_PropManager::color(theSection, theName, theDefault);
348 return Quantity_Color(aColor[0] / 255., aColor[1] / 255., aColor[2] / 255., Quantity_TOC_RGB);
351 ObjectPtr getObject(const AttributePtr& theAttribute)
354 std::string anAttrType = theAttribute->attributeType();
355 if (anAttrType == ModelAPI_AttributeRefAttr::typeId()) {
356 AttributeRefAttrPtr anAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
357 if (anAttr != NULL && anAttr->isObject())
358 anObject = anAttr->object();
360 if (anAttrType == ModelAPI_AttributeSelection::typeId()) {
361 AttributeSelectionPtr anAttr = std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(theAttribute);
363 anObject = anAttr->context();
365 if (anAttrType == ModelAPI_AttributeReference::typeId()) {
366 AttributeReferencePtr anAttr = std::dynamic_pointer_cast<ModelAPI_AttributeReference>(theAttribute);
367 if (anAttr.get() != NULL)
368 anObject = anAttr->value();
373 TopAbs_ShapeEnum getCompoundSubType(const TopoDS_Shape& theShape)
375 TopAbs_ShapeEnum aShapeType = theShape.ShapeType();
377 // for compounds check sub-shapes: it may be compound of needed type:
378 // Booleans may produce compounds of Solids
379 if (aShapeType == TopAbs_COMPOUND) {
380 for(TopoDS_Iterator aSubs(theShape); aSubs.More(); aSubs.Next()) {
381 if (!aSubs.Value().IsNull()) {
382 TopAbs_ShapeEnum aSubType = aSubs.Value().ShapeType();
383 if (aSubType == TopAbs_COMPOUND) { // compound of compound(s)
384 aShapeType = TopAbs_COMPOUND;
387 if (aShapeType == TopAbs_COMPOUND) {
388 aShapeType = aSubType;
389 } else if (aShapeType != aSubType) { // compound of shapes of different types
390 aShapeType = TopAbs_COMPOUND;
399 void getParameters(QStringList& theParameters)
401 theParameters.clear();
403 SessionPtr aSession = ModelAPI_Session::get();
404 std::list<DocumentPtr> aDocList;
405 DocumentPtr anActiveDocument = aSession->activeDocument();
406 DocumentPtr aRootDocument = aSession->moduleDocument();
407 aDocList.push_back(anActiveDocument);
408 if (anActiveDocument != aRootDocument) {
409 aDocList.push_back(aRootDocument);
411 std::string aGroupId = ModelAPI_ResultParameter::group();
412 for(std::list<DocumentPtr>::const_iterator it = aDocList.begin(); it != aDocList.end(); ++it) {
413 DocumentPtr aDocument = *it;
414 int aSize = aDocument->size(aGroupId);
415 for (int i = 0; i < aSize; i++) {
416 ObjectPtr anObject = aDocument->object(aGroupId, i);
417 std::string aParameterName = anObject->data()->name();
418 theParameters.append(aParameterName.c_str());
423 std::string findGreedAttribute(ModuleBase_IWorkshop* theWorkshop, const FeaturePtr& theFeature)
425 std::string anAttributeId;
427 std::string aXmlCfg, aDescription;
428 theWorkshop->module()->getXMLRepresentation(theFeature->getKind(), aXmlCfg, aDescription);
430 ModuleBase_WidgetFactory aFactory(aXmlCfg, theWorkshop);
431 std::string anAttributeTitle;
432 aFactory.getGreedAttribute(anAttributeId);
434 return anAttributeId;
437 void setObject(const AttributePtr& theAttribute, const ObjectPtr& theObject,
438 const GeomShapePtr& theShape, ModuleBase_IWorkshop* theWorkshop,
439 const bool theTemporarily)
441 if (!theAttribute.get())
444 std::string aType = theAttribute->attributeType();
445 if (aType == ModelAPI_AttributeReference::typeId()) {
446 AttributeReferencePtr aRef = std::dynamic_pointer_cast<ModelAPI_AttributeReference>(theAttribute);
447 ObjectPtr aObject = aRef->value();
448 if (!(aObject && aObject->isSame(theObject))) {
449 aRef->setValue(theObject);
451 } else if (aType == ModelAPI_AttributeRefAttr::typeId()) {
452 AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
454 AttributePtr anAttribute = theWorkshop->module()->findAttribute(theObject, theShape);
455 if (anAttribute.get())
456 aRefAttr->setAttr(anAttribute);
458 ObjectPtr aObject = aRefAttr->object();
459 if (!(aObject && aObject->isSame(theObject))) {
460 aRefAttr->setObject(theObject);
463 } else if (aType == ModelAPI_AttributeSelection::typeId()) {
464 AttributeSelectionPtr aSelectAttr =
465 std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(theAttribute);
466 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
467 if (aSelectAttr.get() != NULL) {
468 aSelectAttr->setValue(aResult, theShape, theTemporarily);
471 if (aType == ModelAPI_AttributeSelectionList::typeId()) {
472 AttributeSelectionListPtr aSelectionListAttr =
473 std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(theAttribute);
474 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
475 if (!aSelectionListAttr->isInList(aResult, theShape, theTemporarily))
476 aSelectionListAttr->append(aResult, theShape, theTemporarily);
478 else if (aType == ModelAPI_AttributeRefList::typeId()) {
479 AttributeRefListPtr aRefListAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(theAttribute);
480 if (!aRefListAttr->isInList(theObject))
481 aRefListAttr->append(theObject);
483 else if (aType == ModelAPI_AttributeRefAttrList::typeId()) {
484 AttributeRefAttrListPtr aRefAttrListAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttrList>(theAttribute);
485 AttributePtr anAttribute = theWorkshop->module()->findAttribute(theObject, theShape);
487 if (anAttribute.get()) {
488 if (!aRefAttrListAttr->isInList(anAttribute))
489 aRefAttrListAttr->append(anAttribute);
492 if (!aRefAttrListAttr->isInList(theObject))
493 aRefAttrListAttr->append(theObject);
498 GeomShapePtr getShape(const AttributePtr& theAttribute, ModuleBase_IWorkshop* theWorkshop)
501 if (!theAttribute.get())
504 std::string aType = theAttribute->attributeType();
505 if (aType == ModelAPI_AttributeReference::typeId()) {
506 } else if (aType == ModelAPI_AttributeRefAttr::typeId()) {
507 AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
508 if (aRefAttr.get() && !aRefAttr->isObject()) {
509 AttributePtr anAttribute = aRefAttr->attr();
510 aShape = theWorkshop->module()->findShape(anAttribute);
512 } else if (aType == ModelAPI_AttributeSelection::typeId()) {
513 AttributeSelectionPtr aSelectAttr = std::dynamic_pointer_cast<ModelAPI_AttributeSelection>
515 aShape = aSelectAttr->value();
520 void flushUpdated(ObjectPtr theObject)
522 blockUpdateViewer(true);
524 Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_UPDATED));
526 blockUpdateViewer(false);
529 void blockUpdateViewer(const bool theValue)
531 // the viewer update should be blocked in order to avoid the temporary feature content
532 // when the solver processes the feature, the redisplay message can be flushed
533 // what caused the display in the viewer preliminary states of object
534 // e.g. fillet feature, angle value change
535 std::shared_ptr<Events_Message> aMsg;
537 aMsg = std::shared_ptr<Events_Message>(
538 new Events_Message(Events_Loop::eventByName(EVENT_UPDATE_VIEWER_BLOCKED)));
541 // the viewer update should be unblocked
542 aMsg = std::shared_ptr<Events_Message>(
543 new Events_Message(Events_Loop::eventByName(EVENT_UPDATE_VIEWER_UNBLOCKED)));
545 Events_Loop::loop()->send(aMsg);
548 QString wrapTextByWords(const QString& theValue, QWidget* theWidget,
549 int theMaxLineInPixels)
551 static QFontMetrics tfm(theWidget ? theWidget->font() : QApplication::font());
552 static qreal phi = 2.618;
554 QRect aBounds = tfm.boundingRect(theValue);
555 if(aBounds.width() <= theMaxLineInPixels)
558 qreal s = aBounds.width() * aBounds.height();
559 qreal aGoldWidth = sqrt(s*phi);
561 QStringList aWords = theValue.split(" ", QString::SkipEmptyParts);
563 int n = aWords.count();
565 for (int i = 0; i < n; i++) {
566 QString aLineExt = aLine + " " + aWords[i];
567 qreal anWidthNonExt = tfm.boundingRect(aLine).width();
568 qreal anWidthExt = tfm.boundingRect(aLineExt).width();
569 qreal aDeltaNonExt = fabs(anWidthNonExt-aGoldWidth);
570 qreal aDeltaExt = fabs(anWidthExt-aGoldWidth);
571 if(aDeltaNonExt < aDeltaExt) {
573 aLines.append(aLine);
581 aLines.append(aLine);
583 QString aResult = aLines.join("\n");
587 } // namespace ModuleBase_Tools