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 <ModelAPI_ResultPart.h>
24 #include <Events_Loop.h>
26 #include <ModelAPI_Data.h>
27 #include <ModelAPI_Result.h>
28 #include <ModelAPI_ResultCompSolid.h>
29 #include <ModelAPI_ResultParameter.h>
30 #include <ModelAPI_Tools.h>
31 #include <ModelAPI_Session.h>
32 #include <ModelAPI_Events.h>
34 #include <TopoDS_Iterator.hxx>
36 #include <GeomDataAPI_Point2D.h>
37 #include <Events_Error.h>
39 #include <Config_PropManager.h>
45 #include <QDoubleSpinBox>
46 #include <QGraphicsDropShadowEffect>
48 #include <QApplication>
49 #include <QMessageBox>
55 const double tolerance = 1e-7;
57 //#define DEBUG_ACTIVATE_WINDOW
58 //#define DEBUG_SET_FOCUS
60 namespace ModuleBase_Tools {
62 //******************************************************************
64 //******************************************************************
66 void adjustMargins(QWidget* theWidget)
70 adjustMargins(theWidget->layout());
73 void adjustMargins(QLayout* theLayout)
77 theLayout->setContentsMargins(2, 5, 2, 5);
78 theLayout->setSpacing(4);
81 void zeroMargins(QWidget* theWidget)
85 zeroMargins(theWidget->layout());
88 void zeroMargins(QLayout* theLayout)
92 theLayout->setContentsMargins(0, 0, 0, 0);
93 theLayout->setSpacing(5);
96 void activateWindow(QWidget* theWidget, const QString& theInfo)
98 theWidget->activateWindow();
100 #ifdef DEBUG_ACTIVATE_WINDOW
101 qDebug(QString("activateWindow: %1").arg(theInfo).toStdString().c_str());
105 void setFocus(QWidget* theWidget, const QString& theInfo)
107 theWidget->setFocus();
109 #ifdef DEBUG_SET_FOCUS
110 qDebug(QString("setFocus: %1").arg(theInfo).toStdString().c_str());
114 void setShadowEffect(QWidget* theWidget, const bool isSetEffect)
117 QGraphicsDropShadowEffect* aGlowEffect = new QGraphicsDropShadowEffect();
118 aGlowEffect->setOffset(.0);
119 aGlowEffect->setBlurRadius(10.0);
120 aGlowEffect->setColor(QColor(0, 170, 255)); // Light-blue color, #00AAFF
121 theWidget->setGraphicsEffect(aGlowEffect);
124 QGraphicsEffect* anEffect = theWidget->graphicsEffect();
126 anEffect->deleteLater();
127 theWidget->setGraphicsEffect(NULL);
131 QPixmap composite(const QString& theAdditionalIcon, const QString& theIcon)
133 QImage anIcon = ModuleBase_IconFactory::loadImage(theIcon);
134 QImage anAditional(theAdditionalIcon);
139 int anAddWidth = anAditional.width();
140 int anAddHeight = anAditional.height();
142 int aWidth = anIcon.width();
143 int aHeight = anIcon.height();
145 int aStartWidthPos = aWidth - anAddWidth - 1;
146 int aStartHeightPos = aHeight - anAddHeight - 1;
148 for (int i = 0; i < anAddWidth && i + aStartWidthPos < aWidth; i++)
150 for (int j = 0; j < anAddHeight && j + aStartHeightPos < aHeight; j++)
152 if (qAlpha(anAditional.pixel(i, j)) > 0)
153 anIcon.setPixel(i + aStartWidthPos, j + aStartHeightPos, anAditional.pixel(i, j));
156 return QPixmap::fromImage(anIcon);
159 QPixmap lighter(const QString& theIcon, const int theLighterValue)
161 QImage anIcon = ModuleBase_IconFactory::loadImage(theIcon);
165 QImage aResult = ModuleBase_IconFactory::loadImage(theIcon);
166 for (int i = 0; i < anIcon.width(); i++)
168 for (int j = 0; j < anIcon.height(); j++)
170 QRgb anRgb = anIcon.pixel(i, j);
171 QColor aPixelColor(qRed(anRgb), qGreen(anRgb), qBlue(anRgb),
172 qAlpha(aResult.pixel(i, j)));
174 QColor aLighterColor = aPixelColor.lighter(theLighterValue);
175 aResult.setPixel(i, j, qRgba(aLighterColor.red(), aLighterColor.green(),
176 aLighterColor.blue(), aLighterColor.alpha()));
179 return QPixmap::fromImage(aResult);
182 void setSpinText(ModuleBase_ParamSpinBox* theSpin, const QString& theText)
184 if (theSpin->text() == theText)
186 // In order to avoid extra text setting because it will
187 // reset cursor position in control
188 bool isBlocked = theSpin->blockSignals(true);
189 theSpin->setText(theText);
190 theSpin->blockSignals(isBlocked);
193 void setSpinValue(QDoubleSpinBox* theSpin, double theValue)
195 if (fabs(theSpin->value() - theValue) < tolerance)
197 bool isBlocked = theSpin->blockSignals(true);
198 theSpin->setValue(theValue);
199 theSpin->blockSignals(isBlocked);
202 void setSpinValue(ModuleBase_ParamSpinBox* theSpin, double theValue)
204 if (fabs(theSpin->value() - theValue) < tolerance)
206 bool isBlocked = theSpin->blockSignals(true);
207 theSpin->setValue(theValue);
208 theSpin->blockSignals(isBlocked);
211 void setSpinText(ModuleBase_ParamIntSpinBox* theSpin, const QString& theText)
213 // In order to avoid extra text setting because it will
214 // reset cursor position in control
215 if (theSpin->text() == theText)
217 bool isBlocked = theSpin->blockSignals(true);
218 theSpin->setText(theText);
219 theSpin->blockSignals(isBlocked);
222 void setSpinValue(ModuleBase_ParamIntSpinBox* theSpin, int theValue)
224 if (theSpin->value() == theValue)
226 bool isBlocked = theSpin->blockSignals(true);
227 theSpin->setValue(theValue);
228 theSpin->blockSignals(isBlocked);
231 QAction* createAction(const QIcon& theIcon, const QString& theText,
232 QObject* theParent, const QObject* theReceiver,
233 const char* theMember, const QString& theToolTip,
234 const QString& theStatusTip)
236 QAction* anAction = new QAction(theIcon, theText, theParent);
237 anAction->setToolTip(theToolTip.isEmpty() ? theText : theToolTip);
238 anAction->setStatusTip(!theStatusTip.isEmpty() ? theStatusTip :
239 (!theToolTip.isEmpty() ? theToolTip : theText));
241 QObject::connect(anAction, SIGNAL(triggered(bool)), theReceiver, theMember);
246 QString objectInfo(const ObjectPtr& theObj, const bool isUseAttributesInfo)
248 QString aFeatureStr = "feature";
252 std::ostringstream aPtrStr;
253 aPtrStr << "[" << theObj.get() << "]";
255 ResultPtr aRes = std::dynamic_pointer_cast<ModelAPI_Result>(theObj);
256 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theObj);
258 aFeatureStr.append(QString("(result%1)").arg(aPtrStr.str().c_str()).toStdString() .c_str());
259 if (aRes->isDisabled())
260 aFeatureStr.append("[disabled]");
261 if (aRes->isConcealed())
262 aFeatureStr.append("[concealed]");
263 if (ModelAPI_Tools::hasSubResults(aRes))
264 aFeatureStr.append("[hasSubResults]");
266 aFeature = ModelAPI_Feature::feature(aRes);
269 aFeatureStr.append(aPtrStr.str().c_str());
271 if (aFeature.get()) {
272 aFeatureStr.append(QString(": %1").arg(aFeature->getKind().c_str()).toStdString().c_str());
273 if (aFeature->data()->isValid()) {
274 aFeatureStr.append(QString(", name=%1").arg(aFeature->data()->name().c_str()).toStdString()
277 if (isUseAttributesInfo) {
278 std::list<AttributePtr> anAttrs = aFeature->data()->attributes("");
279 std::list<AttributePtr>::const_iterator anIt = anAttrs.begin(), aLast = anAttrs.end();
281 for(; anIt != aLast; anIt++) {
282 AttributePtr anAttr = *anIt;
283 QString aValue = "not defined";
284 std::string aType = anAttr->attributeType();
285 if (aType == GeomDataAPI_Point2D::typeId()) {
286 std::shared_ptr<GeomDataAPI_Point2D> aPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
289 aValue = QString("(%1, %2)").arg(aPoint->x()).arg(aPoint->y());
291 else if (aType == ModelAPI_AttributeRefAttr::typeId()) {
294 aValues.push_back(QString("%1: %2").arg(anAttr->id().c_str()).arg(aValue).toStdString().c_str());
296 if (!aValues.empty())
297 aFeatureStr.append(QString(", attributes: %1").arg(aValues.join(", ").toStdString().c_str()));
304 typedef QMap<QString, TopAbs_ShapeEnum> ShapeTypes;
305 static ShapeTypes myShapeTypes;
307 TopAbs_ShapeEnum shapeType(const QString& theType)
309 if (myShapeTypes.count() == 0) {
310 myShapeTypes["compound"] = TopAbs_COMPOUND;
311 myShapeTypes["compounds"] = TopAbs_COMPOUND;
312 myShapeTypes["compsolid"] = TopAbs_COMPSOLID;
313 myShapeTypes["compsolids"] = TopAbs_COMPSOLID;
314 myShapeTypes["solid"] = TopAbs_SOLID;
315 myShapeTypes["solids"] = TopAbs_SOLID;
316 myShapeTypes["shell"] = TopAbs_SHELL;
317 myShapeTypes["shells"] = TopAbs_SHELL;
318 myShapeTypes["face"] = TopAbs_FACE;
319 myShapeTypes["faces"] = TopAbs_FACE;
320 myShapeTypes["wire"] = TopAbs_WIRE;
321 myShapeTypes["wires"] = TopAbs_WIRE;
322 myShapeTypes["edge"] = TopAbs_EDGE;
323 myShapeTypes["edges"] = TopAbs_EDGE;
324 myShapeTypes["vertex"] = TopAbs_VERTEX;
325 myShapeTypes["vertices"] = TopAbs_VERTEX;
326 myShapeTypes["objects"] = TopAbs_SHAPE;
328 QString aType = theType.toLower();
329 if(myShapeTypes.contains(aType))
330 return myShapeTypes[aType];
331 Events_Error::send("Shape type defined in XML is not implemented!");
335 void checkObjects(const QObjectPtrList& theObjects, bool& hasResult, bool& hasFeature,
336 bool& hasParameter, bool& hasCompositeOwner)
340 hasParameter = false;
341 hasCompositeOwner = false;
342 foreach(ObjectPtr aObj, theObjects) {
343 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aObj);
344 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(aObj);
345 ResultParameterPtr aConstruction = std::dynamic_pointer_cast<ModelAPI_ResultParameter>(aResult);
347 hasResult |= (aResult.get() != NULL);
348 hasFeature |= (aFeature.get() != NULL);
349 hasParameter |= (aConstruction.get() != NULL);
351 hasCompositeOwner |= (ModelAPI_Tools::compositeOwner(aFeature) != NULL);
352 if (hasFeature && hasResult && hasParameter && hasCompositeOwner)
357 void setDefaultDeviationCoefficient(const TopoDS_Shape& theShape,
358 const Handle(Prs3d_Drawer)& theDrawer)
360 if (theShape.IsNull())
362 TopAbs_ShapeEnum aType = theShape.ShapeType();
363 if ((aType == TopAbs_EDGE) || (aType == TopAbs_WIRE))
364 theDrawer->SetDeviationCoefficient(1.e-4);
367 Quantity_Color color(const std::string& theSection,
368 const std::string& theName,
369 const std::string& theDefault)
371 std::vector<int> aColor = Config_PropManager::color(theSection, theName, theDefault);
372 return Quantity_Color(aColor[0] / 255., aColor[1] / 255., aColor[2] / 255., Quantity_TOC_RGB);
375 ObjectPtr getObject(const AttributePtr& theAttribute)
378 std::string anAttrType = theAttribute->attributeType();
379 if (anAttrType == ModelAPI_AttributeRefAttr::typeId()) {
380 AttributeRefAttrPtr anAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
381 if (anAttr != NULL && anAttr->isObject())
382 anObject = anAttr->object();
384 if (anAttrType == ModelAPI_AttributeSelection::typeId()) {
385 AttributeSelectionPtr anAttr = std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(theAttribute);
387 anObject = anAttr->context();
389 if (anAttrType == ModelAPI_AttributeReference::typeId()) {
390 AttributeReferencePtr anAttr = std::dynamic_pointer_cast<ModelAPI_AttributeReference>(theAttribute);
391 if (anAttr.get() != NULL)
392 anObject = anAttr->value();
397 TopAbs_ShapeEnum getCompoundSubType(const TopoDS_Shape& theShape)
399 TopAbs_ShapeEnum aShapeType = theShape.ShapeType();
401 // for compounds check sub-shapes: it may be compound of needed type:
402 // Booleans may produce compounds of Solids
403 if (aShapeType == TopAbs_COMPOUND) {
404 for(TopoDS_Iterator aSubs(theShape); aSubs.More(); aSubs.Next()) {
405 if (!aSubs.Value().IsNull()) {
406 TopAbs_ShapeEnum aSubType = aSubs.Value().ShapeType();
407 if (aSubType == TopAbs_COMPOUND) { // compound of compound(s)
408 aShapeType = TopAbs_COMPOUND;
411 if (aShapeType == TopAbs_COMPOUND) {
412 aShapeType = aSubType;
413 } else if (aShapeType != aSubType) { // compound of shapes of different types
414 aShapeType = TopAbs_COMPOUND;
423 void getParameters(QStringList& theParameters)
425 theParameters.clear();
427 SessionPtr aSession = ModelAPI_Session::get();
428 std::list<DocumentPtr> aDocList;
429 DocumentPtr anActiveDocument = aSession->activeDocument();
430 DocumentPtr aRootDocument = aSession->moduleDocument();
431 aDocList.push_back(anActiveDocument);
432 if (anActiveDocument != aRootDocument) {
433 aDocList.push_back(aRootDocument);
435 std::string aGroupId = ModelAPI_ResultParameter::group();
436 for(std::list<DocumentPtr>::const_iterator it = aDocList.begin(); it != aDocList.end(); ++it) {
437 DocumentPtr aDocument = *it;
438 int aSize = aDocument->size(aGroupId);
439 for (int i = 0; i < aSize; i++) {
440 ObjectPtr anObject = aDocument->object(aGroupId, i);
441 std::string aParameterName = anObject->data()->name();
442 theParameters.append(aParameterName.c_str());
447 std::string findGreedAttribute(ModuleBase_IWorkshop* theWorkshop, const FeaturePtr& theFeature)
449 std::string anAttributeId;
451 std::string aXmlCfg, aDescription;
452 theWorkshop->module()->getXMLRepresentation(theFeature->getKind(), aXmlCfg, aDescription);
454 ModuleBase_WidgetFactory aFactory(aXmlCfg, theWorkshop);
455 std::string anAttributeTitle;
456 aFactory.getGreedAttribute(anAttributeId);
458 return anAttributeId;
461 bool hasObject(const AttributePtr& theAttribute, const ObjectPtr& theObject,
462 const std::shared_ptr<GeomAPI_Shape>& theShape,
463 ModuleBase_IWorkshop* theWorkshop,
464 const bool theTemporarily)
466 bool aHasObject = false;
467 if (!theAttribute.get())
470 std::string aType = theAttribute->attributeType();
471 if (aType == ModelAPI_AttributeReference::typeId()) {
472 AttributeReferencePtr aRef = std::dynamic_pointer_cast<ModelAPI_AttributeReference>(theAttribute);
473 ObjectPtr aObject = aRef->value();
474 aHasObject = aObject && aObject->isSame(theObject);
475 //if (!(aObject && aObject->isSame(theObject))) {
476 // aRef->setValue(theObject);
478 } else if (aType == ModelAPI_AttributeRefAttr::typeId()) {
479 AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
481 AttributePtr anAttribute = theWorkshop->module()->findAttribute(theObject, theShape);
482 if (anAttribute.get()) {
483 //aRefAttr->setAttr(anAttribute);
486 ObjectPtr aObject = aRefAttr->object();
487 aHasObject = aObject && aObject->isSame(theObject);
488 //if (!(aObject && aObject->isSame(theObject))) {
489 // aRefAttr->setObject(theObject);
492 } else if (aType == ModelAPI_AttributeSelection::typeId()) {
493 /*AttributeSelectionPtr aSelectAttr =
494 std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(theAttribute);
495 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
496 if (aSelectAttr.get() != NULL) {
497 aSelectAttr->setValue(aResult, theShape, theTemporarily);
500 if (aType == ModelAPI_AttributeSelectionList::typeId()) {
501 AttributeSelectionListPtr aSelectionListAttr =
502 std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(theAttribute);
503 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
504 aHasObject = aSelectionListAttr->isInList(aResult, theShape, theTemporarily);
505 //if (!theCheckIfAttributeHasObject || !aSelectionListAttr->isInList(aResult, theShape, theTemporarily))
506 // aSelectionListAttr->append(aResult, theShape, theTemporarily);
508 else if (aType == ModelAPI_AttributeRefList::typeId()) {
509 AttributeRefListPtr aRefListAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(theAttribute);
510 aHasObject = aRefListAttr->isInList(theObject);
511 //if (!theCheckIfAttributeHasObject || !aRefListAttr->isInList(theObject))
512 // aRefListAttr->append(theObject);
514 else if (aType == ModelAPI_AttributeRefAttrList::typeId()) {
515 AttributeRefAttrListPtr aRefAttrListAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttrList>(theAttribute);
516 AttributePtr anAttribute = theWorkshop->module()->findAttribute(theObject, theShape);
518 if (anAttribute.get()) {
519 aHasObject = aRefAttrListAttr->isInList(anAttribute);
520 //if (!theCheckIfAttributeHasObject || !aRefAttrListAttr->isInList(anAttribute))
521 // aRefAttrListAttr->append(anAttribute);
524 aHasObject = aRefAttrListAttr->isInList(theObject);
525 //if (!theCheckIfAttributeHasObject || !aRefAttrListAttr->isInList(theObject))
526 // aRefAttrListAttr->append(theObject);
532 void setObject(const AttributePtr& theAttribute, const ObjectPtr& theObject,
533 const GeomShapePtr& theShape, ModuleBase_IWorkshop* theWorkshop,
534 const bool theTemporarily, const bool theCheckIfAttributeHasObject)
536 if (!theAttribute.get())
539 std::string aType = theAttribute->attributeType();
540 if (aType == ModelAPI_AttributeReference::typeId()) {
541 AttributeReferencePtr aRef = std::dynamic_pointer_cast<ModelAPI_AttributeReference>(theAttribute);
542 ObjectPtr aObject = aRef->value();
543 if (!(aObject && aObject->isSame(theObject))) {
544 aRef->setValue(theObject);
546 } else if (aType == ModelAPI_AttributeRefAttr::typeId()) {
547 AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
549 AttributePtr anAttribute = theWorkshop->module()->findAttribute(theObject, theShape);
550 if (anAttribute.get())
551 aRefAttr->setAttr(anAttribute);
553 ObjectPtr aObject = aRefAttr->object();
554 if (!(aObject && aObject->isSame(theObject))) {
555 aRefAttr->setObject(theObject);
558 } else if (aType == ModelAPI_AttributeSelection::typeId()) {
559 AttributeSelectionPtr aSelectAttr =
560 std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(theAttribute);
561 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
562 if (aSelectAttr.get() != NULL) {
563 aSelectAttr->setValue(aResult, theShape, theTemporarily);
566 if (aType == ModelAPI_AttributeSelectionList::typeId()) {
567 AttributeSelectionListPtr aSelectionListAttr =
568 std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(theAttribute);
569 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
570 if (!theCheckIfAttributeHasObject || !aSelectionListAttr->isInList(aResult, theShape, theTemporarily))
571 aSelectionListAttr->append(aResult, theShape, theTemporarily);
573 else if (aType == ModelAPI_AttributeRefList::typeId()) {
574 AttributeRefListPtr aRefListAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(theAttribute);
575 if (!theCheckIfAttributeHasObject || !aRefListAttr->isInList(theObject))
576 aRefListAttr->append(theObject);
578 else if (aType == ModelAPI_AttributeRefAttrList::typeId()) {
579 AttributeRefAttrListPtr aRefAttrListAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttrList>(theAttribute);
580 AttributePtr anAttribute = theWorkshop->module()->findAttribute(theObject, theShape);
582 if (anAttribute.get()) {
583 if (!theCheckIfAttributeHasObject || !aRefAttrListAttr->isInList(anAttribute))
584 aRefAttrListAttr->append(anAttribute);
587 if (!theCheckIfAttributeHasObject || !aRefAttrListAttr->isInList(theObject))
588 aRefAttrListAttr->append(theObject);
593 GeomShapePtr getShape(const AttributePtr& theAttribute, ModuleBase_IWorkshop* theWorkshop)
596 if (!theAttribute.get())
599 std::string aType = theAttribute->attributeType();
600 if (aType == ModelAPI_AttributeReference::typeId()) {
601 } else if (aType == ModelAPI_AttributeRefAttr::typeId()) {
602 AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
603 if (aRefAttr.get() && !aRefAttr->isObject()) {
604 AttributePtr anAttribute = aRefAttr->attr();
605 aShape = theWorkshop->module()->findShape(anAttribute);
607 } else if (aType == ModelAPI_AttributeSelection::typeId()) {
608 AttributeSelectionPtr aSelectAttr = std::dynamic_pointer_cast<ModelAPI_AttributeSelection>
610 aShape = aSelectAttr->value();
615 void flushUpdated(ObjectPtr theObject)
617 blockUpdateViewer(true);
619 Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_UPDATED));
621 blockUpdateViewer(false);
624 void blockUpdateViewer(const bool theValue)
626 // the viewer update should be blocked in order to avoid the temporary feature content
627 // when the solver processes the feature, the redisplay message can be flushed
628 // what caused the display in the viewer preliminary states of object
629 // e.g. fillet feature, angle value change
630 std::shared_ptr<Events_Message> aMsg;
632 aMsg = std::shared_ptr<Events_Message>(
633 new Events_Message(Events_Loop::eventByName(EVENT_UPDATE_VIEWER_BLOCKED)));
636 // the viewer update should be unblocked
637 aMsg = std::shared_ptr<Events_Message>(
638 new Events_Message(Events_Loop::eventByName(EVENT_UPDATE_VIEWER_UNBLOCKED)));
640 Events_Loop::loop()->send(aMsg);
643 QString wrapTextByWords(const QString& theValue, QWidget* theWidget,
644 int theMaxLineInPixels)
646 static QFontMetrics tfm(theWidget ? theWidget->font() : QApplication::font());
647 static qreal phi = 2.618;
649 QRect aBounds = tfm.boundingRect(theValue);
650 if(aBounds.width() <= theMaxLineInPixels)
653 qreal s = aBounds.width() * aBounds.height();
654 qreal aGoldWidth = sqrt(s*phi);
656 QStringList aWords = theValue.split(" ", QString::SkipEmptyParts);
658 int n = aWords.count();
660 for (int i = 0; i < n; i++) {
661 QString aLineExt = aLine + " " + aWords[i];
662 qreal anWidthNonExt = tfm.boundingRect(aLine).width();
663 qreal anWidthExt = tfm.boundingRect(aLineExt).width();
664 qreal aDeltaNonExt = fabs(anWidthNonExt-aGoldWidth);
665 qreal aDeltaExt = fabs(anWidthExt-aGoldWidth);
666 if(aDeltaNonExt < aDeltaExt) {
668 aLines.append(aLine);
676 aLines.append(aLine);
678 QString aResult = aLines.join("\n");
682 //**************************************************************
683 void refsToFeatureInFeatureDocument(const ObjectPtr& theObject, std::set<FeaturePtr>& theRefFeatures)
685 FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
686 if (aFeature.get()) {
687 DocumentPtr aFeatureDoc = aFeature->document();
688 // 1. find references in the current document
689 aFeatureDoc->refsToFeature(aFeature, theRefFeatures, false);
694 //**************************************************************
695 bool isSubOfComposite(const ObjectPtr& theObject)
698 std::set<FeaturePtr> aRefFeatures;
699 refsToFeatureInFeatureDocument(theObject, aRefFeatures);
700 std::set<FeaturePtr>::const_iterator anIt = aRefFeatures.begin(),
701 aLast = aRefFeatures.end();
702 for (; anIt != aLast && !isSub; anIt++) {
703 isSub = isSubOfComposite(theObject, *anIt);
708 //**************************************************************
709 bool isSubOfComposite(const ObjectPtr& theObject, const FeaturePtr& theFeature)
712 CompositeFeaturePtr aComposite = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(theFeature);
713 if (aComposite.get()) {
714 isSub = aComposite->isSub(theObject);
715 // the recursive is possible, the parameters are sketch circle and extrusion cut. They are
716 // separated by composite sketch feature
718 int aNbSubs = aComposite->numberOfSubs();
719 for (int aSub = 0; aSub < aNbSubs && !isSub; aSub++) {
720 isSub = isSubOfComposite(theObject, aComposite->subFeature(aSub));
727 //**************************************************************
728 bool isFeatureOfResult(const FeaturePtr& theFeature, const std::string& theGroupOfResult)
730 bool isResult = false;
732 if (!theFeature->data()->isValid())
735 ResultPtr aFirstResult = theFeature->firstResult();
736 if (!aFirstResult.get())
739 return aFirstResult->groupName() == theGroupOfResult;
742 //**************************************************************
743 bool askToDelete(const std::set<FeaturePtr> theFeatures,
744 const std::map<FeaturePtr, std::set<FeaturePtr> >& theReferences,
746 std::set<FeaturePtr>& theReferencesToDelete)
748 std::set<FeaturePtr> aFeaturesRefsTo;
749 std::set<FeaturePtr> aFeaturesRefsToParameter;
750 std::set<FeaturePtr> aParameterFeatures;
751 std::set<FeaturePtr>::const_iterator anIt = theFeatures.begin(),
752 aLast = theFeatures.end();
753 // separate features to references to parameter features and references to others
754 for (; anIt != aLast; anIt++) {
755 FeaturePtr aFeature = *anIt;
756 if (theReferences.find(aFeature) == theReferences.end())
759 std::set<FeaturePtr> aRefFeatures;
760 std::set<FeaturePtr> aRefList = theReferences.at(aFeature);
761 std::set<FeaturePtr>::const_iterator aRefIt = aRefList.begin(), aRefLast = aRefList.end();
762 for (; aRefIt != aRefLast; aRefIt++) {
763 FeaturePtr aRefFeature = *aRefIt;
764 if (theFeatures.find(aRefFeature) == theFeatures.end() && // it is not selected
765 aRefFeatures.find(aRefFeature) == aRefFeatures.end()) // it is not added
766 aRefFeatures.insert(aRefFeature);
769 if (isFeatureOfResult(aFeature, ModelAPI_ResultParameter::group())) {
770 aFeaturesRefsToParameter.insert(aRefFeatures.begin(), aRefFeatures.end());
771 aParameterFeatures.insert(aFeature);
774 theReferencesToDelete.insert(aRefFeatures.begin(), aRefFeatures.end());
778 std::set<FeaturePtr> aFeaturesRefsToParameterOnly;
779 anIt = aFeaturesRefsToParameter.begin();
780 aLast = aFeaturesRefsToParameter.end();
781 // separate features to references to parameter features and references to others
782 QStringList aParamFeatureNames;
783 for (; anIt != aLast; anIt++) {
784 FeaturePtr aFeature = *anIt;
785 if (theReferencesToDelete.find(aFeature) == theReferencesToDelete.end()) {
786 aFeaturesRefsToParameterOnly.insert(aFeature);
787 aParamFeatureNames.append(aFeature->name().c_str());
790 aParamFeatureNames.sort();
791 QStringList aPartFeatureNames, anOtherFeatureNames;
792 anIt = theReferencesToDelete.begin();
793 aLast = theReferencesToDelete.end();
794 for (; anIt != aLast; anIt++) {
795 FeaturePtr aFeature = *anIt;
796 if (isFeatureOfResult(aFeature, ModelAPI_ResultPart::group()))
797 aPartFeatureNames.append(aFeature->name().c_str());
799 anOtherFeatureNames.append(aFeature->name().c_str());
801 aPartFeatureNames.sort();
802 anOtherFeatureNames.sort();
804 bool aCanReplaceParameters = !aFeaturesRefsToParameterOnly.empty();
806 QMessageBox aMessageBox(theParent);
807 aMessageBox.setWindowTitle(QObject::tr("Delete features"));
808 aMessageBox.setIcon(QMessageBox::Warning);
809 aMessageBox.setStandardButtons(QMessageBox::No | QMessageBox::Yes);
810 aMessageBox.setDefaultButton(QMessageBox::No);
814 if (!aPartFeatureNames.empty())
815 aText += QString(QObject::tr("The following parts will be deleted: %1.\n")).arg(aPartFeatureNames.join(aSep));
816 if (!anOtherFeatureNames.empty())
817 aText += QString(QObject::tr("Selected features are used in the following features: %1.\nThese features will be deleted.\n"))
818 .arg(anOtherFeatureNames.join(aSep));
819 if (!aParamFeatureNames.empty()) {
820 aText += QString(QObject::tr("Selected parameters are used in the following features: %1.\nThese features will be deleted.\nOr parameters could be replaced by their values.\n"))
821 .arg(aParamFeatureNames.join(aSep));
822 QPushButton *aReplaceButton = aMessageBox.addButton(QObject::tr("Replace"), QMessageBox::ActionRole);
825 if (!aText.isEmpty()) {
826 aText += "Would you like to continue?";
827 aMessageBox.setText(aText);
829 QMessageBox::ButtonRole aButtonRole = aMessageBox.buttonRole(aMessageBox.clickedButton());
831 if (aButtonRole == QMessageBox::NoRole)
834 if (aButtonRole == QMessageBox::ActionRole) {
835 foreach (FeaturePtr aObj, aParameterFeatures)
836 ModelAPI_ReplaceParameterMessage::send(aObj, 0);
839 theReferencesToDelete.insert(aFeaturesRefsToParameterOnly.begin(), aFeaturesRefsToParameterOnly.end());
844 //**************************************************************
845 void convertToFeatures(const QObjectPtrList& theObjects, std::set<FeaturePtr>& theFeatures)
847 QObjectPtrList::const_iterator anIt = theObjects.begin(), aLast = theObjects.end();
848 for(; anIt != aLast; anIt++) {
849 ObjectPtr anObject = *anIt;
850 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(anObject);
851 // for parameter result, use the corresponded reature to be removed
852 if (!aFeature.get() && anObject->groupName() == ModelAPI_ResultParameter::group()) {
853 aFeature = ModelAPI_Feature::feature(anObject);
855 theFeatures.insert(aFeature);
859 } // namespace ModuleBase_Tools