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["face"] = TopAbs_FACE;
311 MyShapeTypes["faces"] = TopAbs_FACE;
312 MyShapeTypes["vertex"] = TopAbs_VERTEX;
313 MyShapeTypes["vertices"] = TopAbs_VERTEX;
314 MyShapeTypes["wire"] = TopAbs_WIRE;
315 MyShapeTypes["edge"] = TopAbs_EDGE;
316 MyShapeTypes["edges"] = TopAbs_EDGE;
317 MyShapeTypes["shell"] = TopAbs_SHELL;
318 MyShapeTypes["solid"] = TopAbs_SOLID;
319 MyShapeTypes["solids"] = TopAbs_SOLID;
320 MyShapeTypes["objects"] = TopAbs_SHAPE;
322 QString aType = theType.toLower();
323 if (MyShapeTypes.contains(aType))
324 return MyShapeTypes[aType];
325 Events_Error::send("Shape type defined in XML is not implemented!");
329 void checkObjects(const QObjectPtrList& theObjects, bool& hasResult, bool& hasFeature,
330 bool& hasParameter, bool& hasCompositeOwner)
334 hasParameter = false;
335 hasCompositeOwner = false;
336 foreach(ObjectPtr aObj, theObjects) {
337 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aObj);
338 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(aObj);
339 ResultParameterPtr aConstruction = std::dynamic_pointer_cast<ModelAPI_ResultParameter>(aResult);
341 hasResult |= (aResult.get() != NULL);
342 hasFeature |= (aFeature.get() != NULL);
343 hasParameter |= (aConstruction.get() != NULL);
345 hasCompositeOwner |= (ModelAPI_Tools::compositeOwner(aFeature) != NULL);
346 if (hasFeature && hasResult && hasParameter && hasCompositeOwner)
351 void setDefaultDeviationCoefficient(const TopoDS_Shape& theShape,
352 const Handle(Prs3d_Drawer)& theDrawer)
354 if (theShape.IsNull())
356 TopAbs_ShapeEnum aType = theShape.ShapeType();
357 if ((aType == TopAbs_EDGE) || (aType == TopAbs_WIRE))
358 theDrawer->SetDeviationCoefficient(1.e-4);
361 Quantity_Color color(const std::string& theSection,
362 const std::string& theName,
363 const std::string& theDefault)
365 std::vector<int> aColor = Config_PropManager::color(theSection, theName, theDefault);
366 return Quantity_Color(aColor[0] / 255., aColor[1] / 255., aColor[2] / 255., Quantity_TOC_RGB);
369 ObjectPtr getObject(const AttributePtr& theAttribute)
372 std::string anAttrType = theAttribute->attributeType();
373 if (anAttrType == ModelAPI_AttributeRefAttr::typeId()) {
374 AttributeRefAttrPtr anAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
375 if (anAttr != NULL && anAttr->isObject())
376 anObject = anAttr->object();
378 if (anAttrType == ModelAPI_AttributeSelection::typeId()) {
379 AttributeSelectionPtr anAttr = std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(theAttribute);
381 anObject = anAttr->context();
383 if (anAttrType == ModelAPI_AttributeReference::typeId()) {
384 AttributeReferencePtr anAttr = std::dynamic_pointer_cast<ModelAPI_AttributeReference>(theAttribute);
385 if (anAttr.get() != NULL)
386 anObject = anAttr->value();
391 TopAbs_ShapeEnum getCompoundSubType(const TopoDS_Shape& theShape)
393 TopAbs_ShapeEnum aShapeType = theShape.ShapeType();
395 // for compounds check sub-shapes: it may be compound of needed type:
396 // Booleans may produce compounds of Solids
397 if (aShapeType == TopAbs_COMPOUND) {
398 for(TopoDS_Iterator aSubs(theShape); aSubs.More(); aSubs.Next()) {
399 if (!aSubs.Value().IsNull()) {
400 TopAbs_ShapeEnum aSubType = aSubs.Value().ShapeType();
401 if (aSubType == TopAbs_COMPOUND) { // compound of compound(s)
402 aShapeType = TopAbs_COMPOUND;
405 if (aShapeType == TopAbs_COMPOUND) {
406 aShapeType = aSubType;
407 } else if (aShapeType != aSubType) { // compound of shapes of different types
408 aShapeType = TopAbs_COMPOUND;
417 void getParameters(QStringList& theParameters)
419 theParameters.clear();
421 SessionPtr aSession = ModelAPI_Session::get();
422 std::list<DocumentPtr> aDocList;
423 DocumentPtr anActiveDocument = aSession->activeDocument();
424 DocumentPtr aRootDocument = aSession->moduleDocument();
425 aDocList.push_back(anActiveDocument);
426 if (anActiveDocument != aRootDocument) {
427 aDocList.push_back(aRootDocument);
429 std::string aGroupId = ModelAPI_ResultParameter::group();
430 for(std::list<DocumentPtr>::const_iterator it = aDocList.begin(); it != aDocList.end(); ++it) {
431 DocumentPtr aDocument = *it;
432 int aSize = aDocument->size(aGroupId);
433 for (int i = 0; i < aSize; i++) {
434 ObjectPtr anObject = aDocument->object(aGroupId, i);
435 std::string aParameterName = anObject->data()->name();
436 theParameters.append(aParameterName.c_str());
441 std::string findGreedAttribute(ModuleBase_IWorkshop* theWorkshop, const FeaturePtr& theFeature)
443 std::string anAttributeId;
445 std::string aXmlCfg, aDescription;
446 theWorkshop->module()->getXMLRepresentation(theFeature->getKind(), aXmlCfg, aDescription);
448 ModuleBase_WidgetFactory aFactory(aXmlCfg, theWorkshop);
449 std::string anAttributeTitle;
450 aFactory.getGreedAttribute(anAttributeId);
452 return anAttributeId;
455 bool hasObject(const AttributePtr& theAttribute, const ObjectPtr& theObject,
456 const std::shared_ptr<GeomAPI_Shape>& theShape,
457 ModuleBase_IWorkshop* theWorkshop,
458 const bool theTemporarily)
460 bool aHasObject = false;
461 if (!theAttribute.get())
464 std::string aType = theAttribute->attributeType();
465 if (aType == ModelAPI_AttributeReference::typeId()) {
466 AttributeReferencePtr aRef = std::dynamic_pointer_cast<ModelAPI_AttributeReference>(theAttribute);
467 ObjectPtr aObject = aRef->value();
468 aHasObject = aObject && aObject->isSame(theObject);
469 //if (!(aObject && aObject->isSame(theObject))) {
470 // aRef->setValue(theObject);
472 } else if (aType == ModelAPI_AttributeRefAttr::typeId()) {
473 AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
475 AttributePtr anAttribute = theWorkshop->module()->findAttribute(theObject, theShape);
476 if (anAttribute.get()) {
477 //aRefAttr->setAttr(anAttribute);
480 ObjectPtr aObject = aRefAttr->object();
481 aHasObject = aObject && aObject->isSame(theObject);
482 //if (!(aObject && aObject->isSame(theObject))) {
483 // aRefAttr->setObject(theObject);
486 } else if (aType == ModelAPI_AttributeSelection::typeId()) {
487 /*AttributeSelectionPtr aSelectAttr =
488 std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(theAttribute);
489 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
490 if (aSelectAttr.get() != NULL) {
491 aSelectAttr->setValue(aResult, theShape, theTemporarily);
494 if (aType == ModelAPI_AttributeSelectionList::typeId()) {
495 AttributeSelectionListPtr aSelectionListAttr =
496 std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(theAttribute);
497 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
498 aHasObject = aSelectionListAttr->isInList(aResult, theShape, theTemporarily);
499 //if (!theCheckIfAttributeHasObject || !aSelectionListAttr->isInList(aResult, theShape, theTemporarily))
500 // aSelectionListAttr->append(aResult, theShape, theTemporarily);
502 else if (aType == ModelAPI_AttributeRefList::typeId()) {
503 AttributeRefListPtr aRefListAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(theAttribute);
504 aHasObject = aRefListAttr->isInList(theObject);
505 //if (!theCheckIfAttributeHasObject || !aRefListAttr->isInList(theObject))
506 // aRefListAttr->append(theObject);
508 else if (aType == ModelAPI_AttributeRefAttrList::typeId()) {
509 AttributeRefAttrListPtr aRefAttrListAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttrList>(theAttribute);
510 AttributePtr anAttribute = theWorkshop->module()->findAttribute(theObject, theShape);
512 if (anAttribute.get()) {
513 aHasObject = aRefAttrListAttr->isInList(anAttribute);
514 //if (!theCheckIfAttributeHasObject || !aRefAttrListAttr->isInList(anAttribute))
515 // aRefAttrListAttr->append(anAttribute);
518 aHasObject = aRefAttrListAttr->isInList(theObject);
519 //if (!theCheckIfAttributeHasObject || !aRefAttrListAttr->isInList(theObject))
520 // aRefAttrListAttr->append(theObject);
526 void setObject(const AttributePtr& theAttribute, const ObjectPtr& theObject,
527 const GeomShapePtr& theShape, ModuleBase_IWorkshop* theWorkshop,
528 const bool theTemporarily, const bool theCheckIfAttributeHasObject)
530 if (!theAttribute.get())
533 std::string aType = theAttribute->attributeType();
534 if (aType == ModelAPI_AttributeReference::typeId()) {
535 AttributeReferencePtr aRef = std::dynamic_pointer_cast<ModelAPI_AttributeReference>(theAttribute);
536 ObjectPtr aObject = aRef->value();
537 if (!(aObject && aObject->isSame(theObject))) {
538 aRef->setValue(theObject);
540 } else if (aType == ModelAPI_AttributeRefAttr::typeId()) {
541 AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
543 AttributePtr anAttribute = theWorkshop->module()->findAttribute(theObject, theShape);
544 if (anAttribute.get())
545 aRefAttr->setAttr(anAttribute);
547 ObjectPtr aObject = aRefAttr->object();
548 if (!(aObject && aObject->isSame(theObject))) {
549 aRefAttr->setObject(theObject);
552 } else if (aType == ModelAPI_AttributeSelection::typeId()) {
553 AttributeSelectionPtr aSelectAttr =
554 std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(theAttribute);
555 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
556 if (aSelectAttr.get() != NULL) {
557 aSelectAttr->setValue(aResult, theShape, theTemporarily);
560 if (aType == ModelAPI_AttributeSelectionList::typeId()) {
561 AttributeSelectionListPtr aSelectionListAttr =
562 std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(theAttribute);
563 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
564 if (!theCheckIfAttributeHasObject || !aSelectionListAttr->isInList(aResult, theShape, theTemporarily))
565 aSelectionListAttr->append(aResult, theShape, theTemporarily);
567 else if (aType == ModelAPI_AttributeRefList::typeId()) {
568 AttributeRefListPtr aRefListAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(theAttribute);
569 if (!theCheckIfAttributeHasObject || !aRefListAttr->isInList(theObject))
570 aRefListAttr->append(theObject);
572 else if (aType == ModelAPI_AttributeRefAttrList::typeId()) {
573 AttributeRefAttrListPtr aRefAttrListAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttrList>(theAttribute);
574 AttributePtr anAttribute = theWorkshop->module()->findAttribute(theObject, theShape);
576 if (anAttribute.get()) {
577 if (!theCheckIfAttributeHasObject || !aRefAttrListAttr->isInList(anAttribute))
578 aRefAttrListAttr->append(anAttribute);
581 if (!theCheckIfAttributeHasObject || !aRefAttrListAttr->isInList(theObject))
582 aRefAttrListAttr->append(theObject);
587 GeomShapePtr getShape(const AttributePtr& theAttribute, ModuleBase_IWorkshop* theWorkshop)
590 if (!theAttribute.get())
593 std::string aType = theAttribute->attributeType();
594 if (aType == ModelAPI_AttributeReference::typeId()) {
595 } else if (aType == ModelAPI_AttributeRefAttr::typeId()) {
596 AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
597 if (aRefAttr.get() && !aRefAttr->isObject()) {
598 AttributePtr anAttribute = aRefAttr->attr();
599 aShape = theWorkshop->module()->findShape(anAttribute);
601 } else if (aType == ModelAPI_AttributeSelection::typeId()) {
602 AttributeSelectionPtr aSelectAttr = std::dynamic_pointer_cast<ModelAPI_AttributeSelection>
604 aShape = aSelectAttr->value();
609 void flushUpdated(ObjectPtr theObject)
611 blockUpdateViewer(true);
613 Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_UPDATED));
615 blockUpdateViewer(false);
618 void blockUpdateViewer(const bool theValue)
620 // the viewer update should be blocked in order to avoid the temporary feature content
621 // when the solver processes the feature, the redisplay message can be flushed
622 // what caused the display in the viewer preliminary states of object
623 // e.g. fillet feature, angle value change
624 std::shared_ptr<Events_Message> aMsg;
626 aMsg = std::shared_ptr<Events_Message>(
627 new Events_Message(Events_Loop::eventByName(EVENT_UPDATE_VIEWER_BLOCKED)));
630 // the viewer update should be unblocked
631 aMsg = std::shared_ptr<Events_Message>(
632 new Events_Message(Events_Loop::eventByName(EVENT_UPDATE_VIEWER_UNBLOCKED)));
634 Events_Loop::loop()->send(aMsg);
637 QString wrapTextByWords(const QString& theValue, QWidget* theWidget,
638 int theMaxLineInPixels)
640 static QFontMetrics tfm(theWidget ? theWidget->font() : QApplication::font());
641 static qreal phi = 2.618;
643 QRect aBounds = tfm.boundingRect(theValue);
644 if(aBounds.width() <= theMaxLineInPixels)
647 qreal s = aBounds.width() * aBounds.height();
648 qreal aGoldWidth = sqrt(s*phi);
650 QStringList aWords = theValue.split(" ", QString::SkipEmptyParts);
652 int n = aWords.count();
654 for (int i = 0; i < n; i++) {
655 QString aLineExt = aLine + " " + aWords[i];
656 qreal anWidthNonExt = tfm.boundingRect(aLine).width();
657 qreal anWidthExt = tfm.boundingRect(aLineExt).width();
658 qreal aDeltaNonExt = fabs(anWidthNonExt-aGoldWidth);
659 qreal aDeltaExt = fabs(anWidthExt-aGoldWidth);
660 if(aDeltaNonExt < aDeltaExt) {
662 aLines.append(aLine);
670 aLines.append(aLine);
672 QString aResult = aLines.join("\n");
676 //**************************************************************
677 void refsToFeatureInFeatureDocument(const ObjectPtr& theObject, std::set<FeaturePtr>& theRefFeatures)
679 FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
680 if (aFeature.get()) {
681 DocumentPtr aFeatureDoc = aFeature->document();
682 // 1. find references in the current document
683 aFeatureDoc->refsToFeature(aFeature, theRefFeatures, false);
688 //**************************************************************
689 bool isSubOfComposite(const ObjectPtr& theObject)
692 std::set<FeaturePtr> aRefFeatures;
693 refsToFeatureInFeatureDocument(theObject, aRefFeatures);
694 std::set<FeaturePtr>::const_iterator anIt = aRefFeatures.begin(),
695 aLast = aRefFeatures.end();
696 for (; anIt != aLast && !isSub; anIt++) {
697 isSub = isSubOfComposite(theObject, *anIt);
702 //**************************************************************
703 bool isSubOfComposite(const ObjectPtr& theObject, const FeaturePtr& theFeature)
706 CompositeFeaturePtr aComposite = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(theFeature);
707 if (aComposite.get()) {
708 isSub = aComposite->isSub(theObject);
709 // the recursive is possible, the parameters are sketch circle and extrusion cut. They are
710 // separated by composite sketch feature
712 int aNbSubs = aComposite->numberOfSubs();
713 for (int aSub = 0; aSub < aNbSubs && !isSub; aSub++) {
714 isSub = isSubOfComposite(theObject, aComposite->subFeature(aSub));
721 //**************************************************************
722 bool isFeatureOfResult(const FeaturePtr& theFeature, const std::string& theGroupOfResult)
724 bool isResult = false;
726 if (!theFeature->data()->isValid())
729 ResultPtr aFirstResult = theFeature->firstResult();
730 if (!aFirstResult.get())
733 return aFirstResult->groupName() == theGroupOfResult;
736 //**************************************************************
737 bool askToDelete(const std::set<FeaturePtr> theFeatures,
738 const std::map<FeaturePtr, std::set<FeaturePtr> >& theReferences,
740 std::set<FeaturePtr>& theReferencesToDelete)
742 std::set<FeaturePtr> aFeaturesRefsTo;
743 std::set<FeaturePtr> aFeaturesRefsToParameter;
744 std::set<FeaturePtr> aParameterFeatures;
745 std::set<FeaturePtr>::const_iterator anIt = theFeatures.begin(),
746 aLast = theFeatures.end();
747 // separate features to references to parameter features and references to others
748 for (; anIt != aLast; anIt++) {
749 FeaturePtr aFeature = *anIt;
750 if (theReferences.find(aFeature) == theReferences.end())
753 std::set<FeaturePtr> aRefFeatures;
754 std::set<FeaturePtr> aRefList = theReferences.at(aFeature);
755 std::set<FeaturePtr>::const_iterator aRefIt = aRefList.begin(), aRefLast = aRefList.end();
756 for (; aRefIt != aRefLast; aRefIt++) {
757 FeaturePtr aRefFeature = *aRefIt;
758 if (theFeatures.find(aRefFeature) == theFeatures.end() && // it is not selected
759 aRefFeatures.find(aRefFeature) == aRefFeatures.end()) // it is not added
760 aRefFeatures.insert(aRefFeature);
763 if (isFeatureOfResult(aFeature, ModelAPI_ResultParameter::group())) {
764 aFeaturesRefsToParameter.insert(aRefFeatures.begin(), aRefFeatures.end());
765 aParameterFeatures.insert(aFeature);
768 theReferencesToDelete.insert(aRefFeatures.begin(), aRefFeatures.end());
772 std::set<FeaturePtr> aFeaturesRefsToParameterOnly;
773 anIt = aFeaturesRefsToParameter.begin();
774 aLast = aFeaturesRefsToParameter.end();
775 // separate features to references to parameter features and references to others
776 QStringList aParamFeatureNames;
777 for (; anIt != aLast; anIt++) {
778 FeaturePtr aFeature = *anIt;
779 if (theReferencesToDelete.find(aFeature) == theReferencesToDelete.end()) {
780 aFeaturesRefsToParameterOnly.insert(aFeature);
781 aParamFeatureNames.append(aFeature->name().c_str());
784 aParamFeatureNames.sort();
785 QStringList aPartFeatureNames, anOtherFeatureNames;
786 anIt = theReferencesToDelete.begin();
787 aLast = theReferencesToDelete.end();
788 for (; anIt != aLast; anIt++) {
789 FeaturePtr aFeature = *anIt;
790 if (isFeatureOfResult(aFeature, ModelAPI_ResultPart::group()))
791 aPartFeatureNames.append(aFeature->name().c_str());
793 anOtherFeatureNames.append(aFeature->name().c_str());
795 aPartFeatureNames.sort();
796 anOtherFeatureNames.sort();
798 bool aCanReplaceParameters = !aFeaturesRefsToParameterOnly.empty();
800 QMessageBox aMessageBox(theParent);
801 aMessageBox.setWindowTitle(QObject::tr("Delete features"));
802 aMessageBox.setIcon(QMessageBox::Warning);
803 aMessageBox.setStandardButtons(QMessageBox::No | QMessageBox::Yes);
804 aMessageBox.setDefaultButton(QMessageBox::No);
808 if (!aPartFeatureNames.empty())
809 aText += QString(QObject::tr("The following parts will be deleted: %1.\n")).arg(aPartFeatureNames.join(aSep));
810 if (!anOtherFeatureNames.empty())
811 aText += QString(QObject::tr("Selected features are used in the following features: %1.\nThese features will be deleted.\n"))
812 .arg(anOtherFeatureNames.join(aSep));
813 if (!aParamFeatureNames.empty()) {
814 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"))
815 .arg(aParamFeatureNames.join(aSep));
816 QPushButton *aReplaceButton = aMessageBox.addButton(QObject::tr("Replace"), QMessageBox::ActionRole);
819 if (!aText.isEmpty()) {
820 aText += "Would you like to continue?";
821 aMessageBox.setText(aText);
823 QMessageBox::ButtonRole aButtonRole = aMessageBox.buttonRole(aMessageBox.clickedButton());
825 if (aButtonRole == QMessageBox::NoRole)
828 if (aButtonRole == QMessageBox::ActionRole) {
829 foreach (FeaturePtr aObj, aParameterFeatures)
830 ModelAPI_ReplaceParameterMessage::send(aObj, 0);
833 theReferencesToDelete.insert(aFeaturesRefsToParameterOnly.begin(), aFeaturesRefsToParameterOnly.end());
838 //**************************************************************
839 void convertToFeatures(const QObjectPtrList& theObjects, std::set<FeaturePtr>& theFeatures)
841 QObjectPtrList::const_iterator anIt = theObjects.begin(), aLast = theObjects.end();
842 for(; anIt != aLast; anIt++) {
843 ObjectPtr anObject = *anIt;
844 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(anObject);
845 // for parameter result, use the corresponded reature to be removed
846 if (!aFeature.get() && anObject->groupName() == ModelAPI_ResultParameter::group()) {
847 aFeature = ModelAPI_Feature::feature(anObject);
849 theFeatures.insert(aFeature);
853 } // namespace ModuleBase_Tools