Salome HOME
Issue #1366: Remove Sub-Shapes feature added.
[modules/shaper.git] / src / ModuleBase / ModuleBase_Tools.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
2
3 // File:        ModuleBase_Tools.cpp
4 // Created:     11 July 2014
5 // Author:      Vitaly Smetannikov
6
7 #include "ModuleBase_Tools.h"
8
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>
15
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>
25
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>
33
34 #include <TopoDS_Iterator.hxx>
35
36 #include <GeomDataAPI_Point2D.h>
37 #include <Events_Error.h>
38
39 #include <Config_PropManager.h>
40
41 #include <QWidget>
42 #include <QLayout>
43 #include <QPainter>
44 #include <QBitmap>
45 #include <QDoubleSpinBox>
46 #include <QGraphicsDropShadowEffect>
47 #include <QColor>
48 #include <QApplication>
49 #include <QMessageBox>
50 #include <QAction>
51
52 #include <sstream>
53 #include <string>
54
55 const double tolerance = 1e-7;
56
57 //#define DEBUG_ACTIVATE_WINDOW
58 //#define DEBUG_SET_FOCUS
59
60 namespace ModuleBase_Tools {
61
62 //******************************************************************
63
64 //******************************************************************
65
66 void adjustMargins(QWidget* theWidget)
67 {
68   if(!theWidget)
69     return;
70   adjustMargins(theWidget->layout());
71 }
72
73 void adjustMargins(QLayout* theLayout)
74 {
75   if(!theLayout)
76     return;
77   theLayout->setContentsMargins(2, 5, 2, 5);
78   theLayout->setSpacing(4);
79 }
80
81 void zeroMargins(QWidget* theWidget)
82 {
83   if(!theWidget)
84     return;
85   zeroMargins(theWidget->layout());
86 }
87
88 void zeroMargins(QLayout* theLayout)
89 {
90   if(!theLayout)
91     return;
92   theLayout->setContentsMargins(0, 0, 0, 0);
93   theLayout->setSpacing(5);
94 }
95
96 void activateWindow(QWidget* theWidget, const QString& theInfo)
97 {
98   theWidget->activateWindow();
99
100 #ifdef DEBUG_ACTIVATE_WINDOW
101   qDebug(QString("activateWindow: %1").arg(theInfo).toStdString().c_str());
102 #endif
103 }
104
105 void setFocus(QWidget* theWidget, const QString& theInfo)
106 {
107   theWidget->setFocus();
108
109 #ifdef DEBUG_SET_FOCUS
110   qDebug(QString("setFocus: %1").arg(theInfo).toStdString().c_str());
111 #endif
112 }
113
114 void setShadowEffect(QWidget* theWidget, const bool isSetEffect)
115 {
116   if (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);
122   }
123   else {
124     QGraphicsEffect* anEffect = theWidget->graphicsEffect();
125     if(anEffect)
126     anEffect->deleteLater();
127     theWidget->setGraphicsEffect(NULL);
128   }
129 }
130
131 QPixmap composite(const QString& theAdditionalIcon, const QString& theIcon)
132 {
133   QImage anIcon = ModuleBase_IconFactory::loadImage(theIcon);
134   QImage anAditional(theAdditionalIcon);
135
136   if (anIcon.isNull())
137     return QPixmap();
138
139   int anAddWidth = anAditional.width();
140   int anAddHeight = anAditional.height();
141
142   int aWidth = anIcon.width();
143   int aHeight = anIcon.height();
144
145   int aStartWidthPos = aWidth - anAddWidth - 1;
146   int aStartHeightPos = aHeight - anAddHeight - 1;
147
148   for (int i = 0; i < anAddWidth && i + aStartWidthPos < aWidth; i++)
149   {
150     for (int j = 0; j < anAddHeight && j + aStartHeightPos < aHeight; j++)
151     {
152       if (qAlpha(anAditional.pixel(i, j)) > 0)
153         anIcon.setPixel(i + aStartWidthPos, j + aStartHeightPos, anAditional.pixel(i, j));
154     }
155   }
156   return QPixmap::fromImage(anIcon);
157 }
158
159 QPixmap lighter(const QString& theIcon, const int theLighterValue)
160 {
161   QImage anIcon = ModuleBase_IconFactory::loadImage(theIcon);
162   if (anIcon.isNull())
163     return QPixmap();
164
165   QImage aResult = ModuleBase_IconFactory::loadImage(theIcon);
166   for (int i = 0; i < anIcon.width(); i++)
167   {
168     for (int j = 0; j < anIcon.height(); j++)
169     {
170       QRgb anRgb = anIcon.pixel(i, j);
171       QColor aPixelColor(qRed(anRgb), qGreen(anRgb), qBlue(anRgb),
172                          qAlpha(aResult.pixel(i, j)));
173
174       QColor aLighterColor = aPixelColor.lighter(theLighterValue);
175       aResult.setPixel(i, j, qRgba(aLighterColor.red(), aLighterColor.green(),
176                                     aLighterColor.blue(), aLighterColor.alpha()));
177     }
178   }
179   return QPixmap::fromImage(aResult);
180 }
181
182 void setSpinText(ModuleBase_ParamSpinBox* theSpin, const QString& theText)
183 {
184   if (theSpin->text() == theText) 
185     return;
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);
191 }
192
193 void setSpinValue(QDoubleSpinBox* theSpin, double theValue)
194 {
195   if (fabs(theSpin->value() - theValue) < tolerance)
196     return;
197   bool isBlocked = theSpin->blockSignals(true);
198   theSpin->setValue(theValue);
199   theSpin->blockSignals(isBlocked);
200 }
201
202 void setSpinValue(ModuleBase_ParamSpinBox* theSpin, double theValue)
203 {
204   if (fabs(theSpin->value() - theValue) < tolerance)
205     return;
206   bool isBlocked = theSpin->blockSignals(true);
207   theSpin->setValue(theValue);
208   theSpin->blockSignals(isBlocked);
209 }
210
211 void setSpinText(ModuleBase_ParamIntSpinBox* theSpin, const QString& theText)
212 {
213   // In order to avoid extra text setting because it will
214   // reset cursor position in control
215   if (theSpin->text() == theText)
216     return;
217   bool isBlocked = theSpin->blockSignals(true);
218   theSpin->setText(theText);
219   theSpin->blockSignals(isBlocked);
220 }
221
222 void setSpinValue(ModuleBase_ParamIntSpinBox* theSpin, int theValue)
223 {
224   if (theSpin->value() == theValue)
225     return;
226   bool isBlocked = theSpin->blockSignals(true);
227   theSpin->setValue(theValue);
228   theSpin->blockSignals(isBlocked);
229 }
230
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)
235 {
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));
240   if (theReceiver)
241     QObject::connect(anAction, SIGNAL(triggered(bool)), theReceiver, theMember);
242
243   return anAction;
244 }
245
246 QString objectInfo(const ObjectPtr& theObj, const bool isUseAttributesInfo)
247 {
248   QString aFeatureStr = "feature";
249   if (!theObj.get())
250     return aFeatureStr;
251
252   std::ostringstream aPtrStr;
253   aPtrStr << "[" << theObj.get() << "]";
254
255   ResultPtr aRes = std::dynamic_pointer_cast<ModelAPI_Result>(theObj);
256   FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theObj);
257   if(aRes.get()) {
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]");
265
266     aFeature = ModelAPI_Feature::feature(aRes);
267   }
268   else
269     aFeatureStr.append(aPtrStr.str().c_str());
270
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()
275                                                                                        .c_str());
276     }
277     if (isUseAttributesInfo) {
278       std::list<AttributePtr> anAttrs = aFeature->data()->attributes("");
279       std::list<AttributePtr>::const_iterator anIt = anAttrs.begin(), aLast = anAttrs.end();
280       QStringList aValues;
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>(
287                                                                                          anAttr);
288           if (aPoint.get())
289             aValue = QString("(%1, %2)").arg(aPoint->x()).arg(aPoint->y());
290         }
291         else if (aType == ModelAPI_AttributeRefAttr::typeId()) {
292         }
293
294         aValues.push_back(QString("%1: %2").arg(anAttr->id().c_str()).arg(aValue).toStdString().c_str());
295       }
296       if (!aValues.empty())
297         aFeatureStr.append(QString(", attributes: %1").arg(aValues.join(", ").toStdString().c_str()));
298     }
299   }
300
301   return aFeatureStr;
302 }
303
304 typedef QMap<QString, TopAbs_ShapeEnum> ShapeTypes;
305 static ShapeTypes myShapeTypes;
306
307 TopAbs_ShapeEnum shapeType(const QString& theType)
308 {
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;
327   }
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!");
332   return TopAbs_SHAPE;
333 }
334
335 void checkObjects(const QObjectPtrList& theObjects, bool& hasResult, bool& hasFeature,
336                   bool& hasParameter, bool& hasCompositeOwner)
337 {
338   hasResult = false;
339   hasFeature = false;
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);
346
347     hasResult |= (aResult.get() != NULL);
348     hasFeature |= (aFeature.get() != NULL);
349     hasParameter |= (aConstruction.get() != NULL);
350     if (hasFeature) 
351       hasCompositeOwner |= (ModelAPI_Tools::compositeOwner(aFeature) != NULL);
352     if (hasFeature && hasResult  && hasParameter && hasCompositeOwner)
353       break;
354   }
355 }
356
357 void setDefaultDeviationCoefficient(const TopoDS_Shape& theShape,
358                                     const Handle(Prs3d_Drawer)& theDrawer)
359 {
360   if (theShape.IsNull())
361     return;
362   TopAbs_ShapeEnum aType = theShape.ShapeType();
363   if ((aType == TopAbs_EDGE) || (aType == TopAbs_WIRE)) 
364     theDrawer->SetDeviationCoefficient(1.e-4);
365 }
366
367 Quantity_Color color(const std::string& theSection,
368                      const std::string& theName,
369                      const std::string& theDefault)
370 {
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);
373 }
374
375 ObjectPtr getObject(const AttributePtr& theAttribute)
376 {
377   ObjectPtr anObject;
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();
383   }
384   if (anAttrType == ModelAPI_AttributeSelection::typeId()) {
385     AttributeSelectionPtr anAttr = std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(theAttribute);
386     if (anAttr != NULL)
387       anObject = anAttr->context();
388   }
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();
393   }
394   return anObject;
395 }
396
397 TopAbs_ShapeEnum getCompoundSubType(const TopoDS_Shape& theShape)
398 {
399   TopAbs_ShapeEnum aShapeType = theShape.ShapeType();
400
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;
409           break;
410         }
411         if (aShapeType == TopAbs_COMPOUND) {
412           aShapeType = aSubType;
413         } else if (aShapeType != aSubType) { // compound of shapes of different types
414           aShapeType = TopAbs_COMPOUND;
415           break;
416         }
417       }
418     }
419   }
420   return aShapeType;
421 }
422
423 void getParameters(QStringList& theParameters)
424 {
425   theParameters.clear();
426
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);
434   }
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());
443     }
444   }
445 }
446
447 std::string findGreedAttribute(ModuleBase_IWorkshop* theWorkshop, const FeaturePtr& theFeature)
448 {
449   std::string anAttributeId;
450
451   std::string aXmlCfg, aDescription;
452   theWorkshop->module()->getXMLRepresentation(theFeature->getKind(), aXmlCfg, aDescription);
453
454   ModuleBase_WidgetFactory aFactory(aXmlCfg, theWorkshop);
455   std::string anAttributeTitle;
456   aFactory.getGreedAttribute(anAttributeId);
457
458   return anAttributeId;
459 }
460
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)
465 {
466   bool aHasObject = false;
467   if (!theAttribute.get())
468     return aHasObject;
469
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);
477     //}
478   } else if (aType == ModelAPI_AttributeRefAttr::typeId()) {
479     AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
480
481     AttributePtr anAttribute = theWorkshop->module()->findAttribute(theObject, theShape);
482     if (anAttribute.get()) {
483       //aRefAttr->setAttr(anAttribute);
484     }
485     else {
486       ObjectPtr aObject = aRefAttr->object();
487       aHasObject = aObject && aObject->isSame(theObject);
488       //if (!(aObject && aObject->isSame(theObject))) {
489       //  aRefAttr->setObject(theObject);
490       //}
491     }
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);
498     }*/
499   }
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);
507   }
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);
513   }
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);
517
518     if (anAttribute.get()) {
519       aHasObject = aRefAttrListAttr->isInList(anAttribute);
520       //if (!theCheckIfAttributeHasObject || !aRefAttrListAttr->isInList(anAttribute))
521       //  aRefAttrListAttr->append(anAttribute);
522     }
523     else {
524       aHasObject = aRefAttrListAttr->isInList(theObject);
525       //if (!theCheckIfAttributeHasObject || !aRefAttrListAttr->isInList(theObject))
526       //  aRefAttrListAttr->append(theObject);
527     }
528   }
529   return aHasObject;
530 }
531
532 void setObject(const AttributePtr& theAttribute, const ObjectPtr& theObject,
533                const GeomShapePtr& theShape, ModuleBase_IWorkshop* theWorkshop,
534                const bool theTemporarily, const bool theCheckIfAttributeHasObject)
535 {
536   if (!theAttribute.get())
537     return;
538
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);
545     }
546   } else if (aType == ModelAPI_AttributeRefAttr::typeId()) {
547     AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
548
549     AttributePtr anAttribute = theWorkshop->module()->findAttribute(theObject, theShape);
550     if (anAttribute.get())
551       aRefAttr->setAttr(anAttribute);
552     else {
553       ObjectPtr aObject = aRefAttr->object();
554       if (!(aObject && aObject->isSame(theObject))) {
555         aRefAttr->setObject(theObject);
556       }
557     }
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);
564     }
565   }
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);
572   }
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);
577   }
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);
581
582     if (anAttribute.get()) {
583       if (!theCheckIfAttributeHasObject || !aRefAttrListAttr->isInList(anAttribute))
584         aRefAttrListAttr->append(anAttribute);
585     }
586     else {
587       if (!theCheckIfAttributeHasObject || !aRefAttrListAttr->isInList(theObject))
588         aRefAttrListAttr->append(theObject);
589     }
590   }
591 }
592
593 GeomShapePtr getShape(const AttributePtr& theAttribute, ModuleBase_IWorkshop* theWorkshop)
594 {
595   GeomShapePtr aShape;
596   if (!theAttribute.get())
597     return aShape;
598
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);
606     }
607   } else if (aType == ModelAPI_AttributeSelection::typeId()) {
608     AttributeSelectionPtr aSelectAttr = std::dynamic_pointer_cast<ModelAPI_AttributeSelection>
609                                                                                  (theAttribute);
610     aShape = aSelectAttr->value();
611   }
612   return aShape;
613 }
614
615 void flushUpdated(ObjectPtr theObject)
616 {
617   blockUpdateViewer(true);
618
619   Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_UPDATED));
620
621   blockUpdateViewer(false);
622 }
623
624 void blockUpdateViewer(const bool theValue)
625 {
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;
631   if (theValue) {
632     aMsg = std::shared_ptr<Events_Message>(
633         new Events_Message(Events_Loop::eventByName(EVENT_UPDATE_VIEWER_BLOCKED)));
634   }
635   else {
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)));
639   }
640   Events_Loop::loop()->send(aMsg);
641 }
642
643 QString wrapTextByWords(const QString& theValue, QWidget* theWidget,
644                                           int theMaxLineInPixels)
645 {
646   static QFontMetrics tfm(theWidget ? theWidget->font() : QApplication::font());
647   static qreal phi = 2.618;
648
649   QRect aBounds = tfm.boundingRect(theValue);
650   if(aBounds.width() <= theMaxLineInPixels)
651     return theValue;
652
653   qreal s = aBounds.width() * aBounds.height();
654   qreal aGoldWidth = sqrt(s*phi);
655
656   QStringList aWords = theValue.split(" ", QString::SkipEmptyParts);
657   QStringList aLines;
658   int n = aWords.count();
659   QString aLine;
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) {
667       // new line
668       aLines.append(aLine);
669       aLine = aWords[i];
670     }
671     else
672       aLine = aLineExt;
673   }
674
675   if(!aLine.isEmpty())
676     aLines.append(aLine);
677
678   QString aResult = aLines.join("\n");
679   return aResult;
680 }
681
682 //**************************************************************
683 void refsToFeatureInFeatureDocument(const ObjectPtr& theObject, std::set<FeaturePtr>& theRefFeatures)
684 {
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);
690   }
691 }
692
693
694 //**************************************************************
695 bool isSubOfComposite(const ObjectPtr& theObject)
696 {
697   bool isSub = false;
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);
704   }
705   return isSub;
706 }
707
708 //**************************************************************
709 bool isSubOfComposite(const ObjectPtr& theObject, const FeaturePtr& theFeature)
710 {
711   bool isSub = false;
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
717     if (!isSub) {
718       int aNbSubs = aComposite->numberOfSubs();
719       for (int aSub = 0; aSub < aNbSubs && !isSub; aSub++) {
720         isSub = isSubOfComposite(theObject, aComposite->subFeature(aSub));
721       }
722     }
723   }
724   return isSub;
725 }
726
727 //**************************************************************
728 bool isFeatureOfResult(const FeaturePtr& theFeature, const std::string& theGroupOfResult)
729 {
730   bool isResult = false;
731
732   if (!theFeature->data()->isValid())
733     return isResult;
734
735   ResultPtr aFirstResult = theFeature->firstResult();
736   if (!aFirstResult.get())
737     return isResult;
738
739   return aFirstResult->groupName() == theGroupOfResult;
740 }
741
742 //**************************************************************
743 bool askToDelete(const std::set<FeaturePtr> theFeatures,
744                  const std::map<FeaturePtr, std::set<FeaturePtr> >& theReferences,
745                  QWidget* theParent,
746                  std::set<FeaturePtr>& theReferencesToDelete)
747 {
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())
757       continue;
758
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);
767     }
768
769     if (isFeatureOfResult(aFeature, ModelAPI_ResultParameter::group())) {
770       aFeaturesRefsToParameter.insert(aRefFeatures.begin(), aRefFeatures.end());
771       aParameterFeatures.insert(aFeature);
772     }
773     else {
774       theReferencesToDelete.insert(aRefFeatures.begin(), aRefFeatures.end());
775     }
776   }
777
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());
788     }
789   }
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());
798     else
799       anOtherFeatureNames.append(aFeature->name().c_str());
800   }
801   aPartFeatureNames.sort();
802   anOtherFeatureNames.sort();
803
804   bool aCanReplaceParameters = !aFeaturesRefsToParameterOnly.empty();
805
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);
811
812   QString aText;
813   QString aSep = ", ";
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);
823   }
824
825   if (!aText.isEmpty()) {
826     aText += "Would you like to continue?";
827     aMessageBox.setText(aText);
828     aMessageBox.exec();
829     QMessageBox::ButtonRole aButtonRole = aMessageBox.buttonRole(aMessageBox.clickedButton());
830
831     if (aButtonRole == QMessageBox::NoRole)
832       return false;
833
834     if (aButtonRole == QMessageBox::ActionRole) {
835       foreach (FeaturePtr aObj, aParameterFeatures)
836         ModelAPI_ReplaceParameterMessage::send(aObj, 0);
837     }
838     else
839       theReferencesToDelete.insert(aFeaturesRefsToParameterOnly.begin(), aFeaturesRefsToParameterOnly.end());
840   }
841   return true;
842 }
843
844 //**************************************************************
845 void convertToFeatures(const QObjectPtrList& theObjects, std::set<FeaturePtr>& theFeatures)
846 {
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);
854     }
855     theFeatures.insert(aFeature);
856   }
857 }
858
859 } // namespace ModuleBase_Tools
860
861