Salome HOME
#1581 crash when create fillet
[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 #include <ModuleBase_ResultPrs.h>
16
17 #include <ModelAPI_Attribute.h>
18 #include <ModelAPI_AttributeRefAttr.h>
19 #include <ModelAPI_AttributeReference.h>
20 #include <ModelAPI_AttributeSelection.h>
21 #include <ModelAPI_AttributeSelectionList.h>
22 #include <ModelAPI_AttributeRefList.h>
23 #include <ModelAPI_AttributeRefAttrList.h>
24 #include <ModelAPI_ResultPart.h>
25 #include <ModelAPI_ResultConstruction.h>
26 #include <Events_Loop.h>
27
28 #include <ModelAPI_Data.h>
29 #include <ModelAPI_Result.h>
30 #include <ModelAPI_ResultCompSolid.h>
31 #include <ModelAPI_ResultParameter.h>
32 #include <ModelAPI_Tools.h>
33 #include <ModelAPI_Session.h>
34 #include <ModelAPI_Events.h>
35
36 #include <TopoDS_Iterator.hxx>
37
38 #include <GeomDataAPI_Point2D.h>
39 #include <Events_InfoMessage.h>
40 #include <GeomAPI_ShapeExplorer.h>
41
42 #include <Config_PropManager.h>
43 #include <Config_Translator.h>
44
45 #include <Prs3d_PointAspect.hxx>
46 #include <Graphic3d_AspectMarker3d.hxx>
47
48 #include <Image_AlienPixMap.hxx>
49
50 #include <QWidget>
51 #include <QLayout>
52 #include <QPainter>
53 #include <QBitmap>
54 #include <QDoubleSpinBox>
55 #include <QGraphicsDropShadowEffect>
56 #include <QColor>
57 #include <QApplication>
58 #include <QMessageBox>
59 #include <QAction>
60 #include <QTextCodec>
61
62 #include <sstream>
63 #include <string>
64
65 const double tolerance = 1e-7;
66 const double DEFAULT_DEVIATION_COEFFICIENT = 1.e-4;
67
68 //#define DEBUG_ACTIVATE_WINDOW
69 //#define DEBUG_SET_FOCUS
70 //#define DEBUG_SELECTION_INFO
71
72 #ifdef DEBUG_SELECTION_INFO
73 #include <ModuleBase_ISelection.h>
74 #include <ModuleBase_ViewerPrs.h>
75
76 #include <AIS_InteractiveContext.hxx>
77 #include <AIS_ListOfInteractive.hxx>
78 #include <AIS_ListIteratorOfListOfInteractive.hxx>
79 #include <AIS_Shape.hxx>
80 #include <TopoDS_Shape.hxx>
81 #include <AIS_LocalContext.hxx>
82 #include <NCollection_List.hxx>
83 #include <StdSelect_BRepOwner.hxx>
84 #include <TColStd_ListOfInteger.hxx>
85 #include <TColStd_ListIteratorOfListOfInteger.hxx>
86 #endif
87
88 #ifdef WIN32
89 # define FSEP "\\"
90 #else
91 # define FSEP "/"
92 #endif
93
94 namespace ModuleBase_Tools {
95
96 //******************************************************************
97
98 //******************************************************************
99
100 void adjustMargins(QWidget* theWidget)
101 {
102   if(!theWidget)
103     return;
104   adjustMargins(theWidget->layout());
105 }
106
107 void adjustMargins(QLayout* theLayout)
108 {
109   if(!theLayout)
110     return;
111   theLayout->setContentsMargins(2, 5, 2, 5);
112   theLayout->setSpacing(4);
113 }
114
115 void zeroMargins(QWidget* theWidget)
116 {
117   if(!theWidget)
118     return;
119   zeroMargins(theWidget->layout());
120 }
121
122 void zeroMargins(QLayout* theLayout)
123 {
124   if(!theLayout)
125     return;
126   theLayout->setContentsMargins(0, 0, 0, 0);
127   theLayout->setSpacing(5);
128 }
129
130 void activateWindow(QWidget* theWidget, const QString& theInfo)
131 {
132   theWidget->activateWindow();
133
134 #ifdef DEBUG_ACTIVATE_WINDOW
135   qDebug(QString("activateWindow: %1").arg(theInfo).toStdString().c_str());
136 #endif
137 }
138
139 void setFocus(QWidget* theWidget, const QString& theInfo)
140 {
141   theWidget->setFocus();
142
143 #ifdef DEBUG_SET_FOCUS
144   qDebug(QString("setFocus: %1").arg(theInfo).toStdString().c_str());
145 #endif
146 }
147
148 void setShadowEffect(QWidget* theWidget, const bool isSetEffect)
149 {
150   if (isSetEffect) {
151     QGraphicsDropShadowEffect* aGlowEffect = new QGraphicsDropShadowEffect();
152     aGlowEffect->setOffset(.0);
153     aGlowEffect->setBlurRadius(10.0);
154     aGlowEffect->setColor(QColor(0, 170, 255)); // Light-blue color, #00AAFF
155     theWidget->setGraphicsEffect(aGlowEffect);
156   }
157   else {
158     QGraphicsEffect* anEffect = theWidget->graphicsEffect();
159     if(anEffect)
160       anEffect->deleteLater();
161     theWidget->setGraphicsEffect(NULL);
162   }
163 }
164
165 QPixmap composite(const QString& theAdditionalIcon, const QString& theIcon)
166 {
167   QImage anIcon = ModuleBase_IconFactory::loadImage(theIcon);
168   QImage anAditional(theAdditionalIcon);
169
170   if (anIcon.isNull())
171     return QPixmap();
172
173   int anAddWidth = anAditional.width();
174   int anAddHeight = anAditional.height();
175
176   int aWidth = anIcon.width();
177   int aHeight = anIcon.height();
178
179   int aStartWidthPos = aWidth - anAddWidth - 1;
180   int aStartHeightPos = aHeight - anAddHeight - 1;
181
182   for (int i = 0; i < anAddWidth && i + aStartWidthPos < aWidth; i++)
183   {
184     for (int j = 0; j < anAddHeight && j + aStartHeightPos < aHeight; j++)
185     {
186       if (qAlpha(anAditional.pixel(i, j)) > 0)
187         anIcon.setPixel(i + aStartWidthPos, j + aStartHeightPos, anAditional.pixel(i, j));
188     }
189   }
190   return QPixmap::fromImage(anIcon);
191 }
192
193 QPixmap lighter(const QString& theIcon, const int theLighterValue)
194 {
195   QImage anIcon = ModuleBase_IconFactory::loadImage(theIcon);
196   if (anIcon.isNull())
197     return QPixmap();
198
199   QImage aResult = ModuleBase_IconFactory::loadImage(theIcon);
200   for (int i = 0; i < anIcon.width(); i++)
201   {
202     for (int j = 0; j < anIcon.height(); j++)
203     {
204       QRgb anRgb = anIcon.pixel(i, j);
205       QColor aPixelColor(qRed(anRgb), qGreen(anRgb), qBlue(anRgb),
206                          qAlpha(aResult.pixel(i, j)));
207
208       QColor aLighterColor = aPixelColor.lighter(theLighterValue);
209       aResult.setPixel(i, j, qRgba(aLighterColor.red(), aLighterColor.green(),
210                                     aLighterColor.blue(), aLighterColor.alpha()));
211     }
212   }
213   return QPixmap::fromImage(aResult);
214 }
215
216 void setSpinText(ModuleBase_ParamSpinBox* theSpin, const QString& theText)
217 {
218   if (theSpin->text() == theText) 
219     return;
220   // In order to avoid extra text setting because it will
221   // reset cursor position in control
222   bool isBlocked = theSpin->blockSignals(true);
223   theSpin->setText(theText);
224   theSpin->blockSignals(isBlocked);
225 }
226
227 void setSpinValue(QDoubleSpinBox* theSpin, double theValue)
228 {
229   if (fabs(theSpin->value() - theValue) < tolerance)
230     return;
231   bool isBlocked = theSpin->blockSignals(true);
232   theSpin->setValue(theValue);
233   theSpin->blockSignals(isBlocked);
234 }
235
236 void setSpinValue(ModuleBase_ParamSpinBox* theSpin, double theValue)
237 {
238   if (fabs(theSpin->value() - theValue) < tolerance)
239     return;
240   bool isBlocked = theSpin->blockSignals(true);
241   theSpin->setValue(theValue);
242   theSpin->blockSignals(isBlocked);
243 }
244
245 void setSpinText(ModuleBase_ParamIntSpinBox* theSpin, const QString& theText)
246 {
247   // In order to avoid extra text setting because it will
248   // reset cursor position in control
249   if (theSpin->text() == theText)
250     return;
251   bool isBlocked = theSpin->blockSignals(true);
252   theSpin->setText(theText);
253   theSpin->blockSignals(isBlocked);
254 }
255
256 void setSpinValue(ModuleBase_ParamIntSpinBox* theSpin, int theValue)
257 {
258   if (theSpin->value() == theValue)
259     return;
260   bool isBlocked = theSpin->blockSignals(true);
261   theSpin->setValue(theValue);
262   theSpin->blockSignals(isBlocked);
263 }
264
265 QAction* createAction(const QIcon& theIcon, const QString& theText,
266                       QObject* theParent, const QObject* theReceiver,
267                       const char* theMember, const QString& theToolTip,
268                       const QString& theStatusTip)
269 {
270   QAction* anAction = new QAction(theIcon, theText, theParent);
271   anAction->setToolTip(theToolTip.isEmpty() ? theText : theToolTip);
272   anAction->setStatusTip(!theStatusTip.isEmpty() ? theStatusTip :
273                                                    (!theToolTip.isEmpty() ? theToolTip : theText));
274   if (theReceiver)
275     QObject::connect(anAction, SIGNAL(triggered(bool)), theReceiver, theMember);
276
277   return anAction;
278 }
279
280 QString objectInfo(const ObjectPtr& theObj, const bool isUseAttributesInfo)
281 {
282   QString aFeatureStr = "feature";
283   if (!theObj.get())
284     return aFeatureStr;
285
286   std::ostringstream aPtrStr;
287   aPtrStr << "[" << theObj.get() << "]";
288
289   ResultPtr aRes = std::dynamic_pointer_cast<ModelAPI_Result>(theObj);
290   FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theObj);
291   if(aRes.get()) {
292     aFeatureStr.append(QString("(result%1)").arg(aPtrStr.str().c_str()).toStdString() .c_str());
293     if (aRes->isDisabled())
294       aFeatureStr.append("[disabled]");
295     if (aRes->isConcealed())
296       aFeatureStr.append("[concealed]");
297     if (ModelAPI_Tools::hasSubResults(aRes))
298       aFeatureStr.append("[hasSubResults]");
299
300     aFeature = ModelAPI_Feature::feature(aRes);
301   }
302   else
303     aFeatureStr.append(aPtrStr.str().c_str());
304
305   if (aFeature.get()) {
306     aFeatureStr.append(QString(": %1").arg(aFeature->getKind().c_str()).toStdString().c_str());
307     if (aFeature->data()->isValid()) {
308       aFeatureStr.append(QString(", name=%1").arg(theObj->data()->name().c_str()).toStdString()
309                                                                                        .c_str());
310     }
311     if (isUseAttributesInfo) {
312       std::list<AttributePtr> anAttrs = aFeature->data()->attributes("");
313       std::list<AttributePtr>::const_iterator anIt = anAttrs.begin(), aLast = anAttrs.end();
314       QStringList aValues;
315       for(; anIt != aLast; anIt++) {
316         AttributePtr anAttr = *anIt;
317         QString aValue = "not defined";
318         std::string aType = anAttr->attributeType();
319         if (aType == GeomDataAPI_Point2D::typeId()) {
320           std::shared_ptr<GeomDataAPI_Point2D> aPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
321                                                                                          anAttr);
322           if (aPoint.get())
323             aValue = QString("(%1, %2)").arg(aPoint->x()).arg(aPoint->y());
324         }
325         else if (aType == ModelAPI_AttributeRefAttr::typeId()) {
326         }
327
328         aValues.push_back(QString("%1: %2").arg(anAttr->id().c_str()).arg(aValue).toStdString().c_str());
329       }
330       if (!aValues.empty())
331         aFeatureStr.append(QString(", attributes: %1").arg(aValues.join(", ").toStdString().c_str()));
332     }
333   }
334
335   return aFeatureStr;
336 }
337
338 #ifdef DEBUG_SELECTION_INFO
339 QString getShapeTypeInfo(const int theType)
340 {
341   QString anInfo = "Undefined";
342   switch(theType) {
343     case TopAbs_COMPOUND:  anInfo = "compound"; break;
344     case TopAbs_COMPSOLID: anInfo = "compsolid"; break;
345     case TopAbs_SOLID:     anInfo = "solid"; break;
346     case TopAbs_SHELL:     anInfo = "shell"; break;
347     case TopAbs_FACE:      anInfo = "face"; break;
348     case TopAbs_WIRE:      anInfo = "wire"; break;
349     case TopAbs_EDGE:      anInfo = "edge"; break;
350     case TopAbs_VERTEX:    anInfo = "vertex"; break;
351     case TopAbs_SHAPE:     anInfo = "shape"; break;
352     case ModuleBase_ResultPrs::Sel_Result:
353                            anInfo = "result_shape"; break;
354     default: break;
355   }
356   return anInfo;
357 }
358
359 QString getModeInfo(const int theMode)
360 {
361   QString anInfo = "Undefined";
362   switch(theMode) {
363     case 0: anInfo = "SHAPE(0)"; break;
364     case 1: anInfo = "VERTEX(1)"; break;
365     case 2: anInfo = "EDGE(2)"; break;
366     case 3: anInfo = "WIRE(3)"; break;
367     case 4: anInfo = "FACE(4)"; break;
368     case 5: anInfo = "SHELL(5)"; break;
369     case 6: anInfo = "SOLID(6)"; break;
370     case 7: anInfo = "COMPSOLID(7)"; break;
371     case 8: anInfo = "COMPOUND(8)"; break;
372     case 100: anInfo = "Sel_Mode_First(100)"; break; //SketcherPrs_Tools
373     case 101: anInfo = "Sel_Constraint(101)"; break;
374     case 102: anInfo = "Sel_Dimension_All(102)"; break;
375     case 103: anInfo = "Sel_Dimension_Line(103)"; break;
376     case 104: anInfo = "Sel_Dimension_Text(104)"; break;
377     default: break;
378   }
379   return anInfo;
380 }
381
382 QString displayedInteractiveObjects(Handle(AIS_InteractiveContext)& theContext,
383                                     const bool theShapeInfoOnly = true)
384 {
385   if (theContext.IsNull())
386     return "";
387   AIS_ListOfInteractive aListOfIO;
388   theContext->DisplayedObjects(aListOfIO, false);
389   QStringList anObjects;
390   AIS_ListIteratorOfListOfInteractive aIt;
391   for (aIt.Initialize(aListOfIO); aIt.More(); aIt.Next()) {
392     Handle(AIS_InteractiveObject) anAISIO = aIt.Value();
393     Handle(AIS_Shape) aShapePrs = Handle(AIS_Shape)::DownCast(anAISIO);
394     if (theShapeInfoOnly && aShapePrs.IsNull())
395       continue;
396
397     QString anInfo = "IO";
398     std::ostringstream aPtrStr;
399     aPtrStr << "[" << anAISIO.Access() << "]";
400     anInfo += aPtrStr.str().c_str();
401
402     if (!aShapePrs.IsNull()) {
403       const TopoDS_Shape& aShape = aShapePrs->Shape();
404       if (aShape.IsNull()) {
405         //Events_Error::throwException("An empty AIS presentation");
406       }
407       else
408         anInfo += QString(", shape type: %1").arg(getShapeTypeInfo(aShape.ShapeType()));
409     }
410     TColStd_ListOfInteger aTColModes;
411     theContext->ActivatedModes(anAISIO, aTColModes);
412     TColStd_ListIteratorOfListOfInteger itr( aTColModes );
413     QIntList aModesActivatedForIO;
414     bool isDeactivated = false;
415     QStringList aModes;
416     for (; itr.More(); itr.Next() ) {
417       Standard_Integer aMode = itr.Value();
418       aModes.append(getModeInfo(aMode));
419       //int aShapeMode = (aMode > 8)? aMode : AIS_Shape::SelectionType(aMode);
420     }
421     if (aModes.size() > 0)
422       anInfo += QString(", activated modes: %1").arg(aModes.join(", "));
423
424     anObjects += anInfo;
425   }
426   QString aValue = QString("displayedIO[%1]").arg(anObjects.size());
427   if (anObjects.size() > 0)
428     aValue += QString(":\n  %1").arg(anObjects.join("\n  "));
429   return aValue;
430 }
431
432 QString activeOwners(Handle(AIS_InteractiveContext)& theContext, const bool theShapeInfoOnly = true)
433 {
434   if (theContext.IsNull())
435     return "";
436   Handle(AIS_LocalContext) aLContext = theContext->LocalContext();
437   TCollection_AsciiString aSelectionName = aLContext->SelectionName();
438   aLContext->UnhilightPicked(Standard_False);
439
440   NCollection_List<Handle(SelectBasics_EntityOwner)> anActiveOwners;
441   aLContext->MainSelector()->ActiveOwners(anActiveOwners);
442   NCollection_List<Handle(SelectBasics_EntityOwner)>::Iterator anOwnersIt (anActiveOwners);
443   Handle(SelectMgr_EntityOwner) anOwner;
444   QStringList anObjects;
445   for (; anOwnersIt.More(); anOwnersIt.Next()) {
446     anOwner = Handle(SelectMgr_EntityOwner)::DownCast (anOwnersIt.Value());
447     Handle(StdSelect_BRepOwner) BROwnr = Handle(StdSelect_BRepOwner)::DownCast(anOwner);
448     if (theShapeInfoOnly && BROwnr.IsNull())
449       continue;
450
451     QString anInfo = "Owner";
452     std::ostringstream aPtrStr;
453     aPtrStr << "[" << anOwner.Access() << "]";
454     anInfo += aPtrStr.str().c_str();
455
456     Handle(AIS_InteractiveObject) aAISObj = 
457           Handle(AIS_InteractiveObject)::DownCast(anOwner->Selectable());
458     if (!aAISObj.IsNull()) {
459       std::ostringstream aPtrStr;
460       aPtrStr << "[" << aAISObj.Access() << "]";
461       anInfo += QString(", selectable(IO): %1").arg(aPtrStr.str().c_str());
462     }
463
464     if (!BROwnr.IsNull() && BROwnr->HasShape()) {
465       const TopoDS_Shape& aShape = BROwnr->Shape();
466       if (aShape.IsNull()) {
467         //Events_Error::throwException("An empty AIS presentation");
468       }
469       else
470         anInfo += QString(", shape type: %1").arg(getShapeTypeInfo(aShape.ShapeType()));
471     }
472     anObjects += anInfo;
473   }
474   QString aValue = QString("activeOwners[%1]").arg(anObjects.size());
475   if (anObjects.size() > 0)
476     aValue += QString(":\n  %1").arg(anObjects.join("\n  "));
477   return aValue;
478 }
479
480 QString selectedOwners(Handle(AIS_InteractiveContext)& theContext, const bool theShapeInfoOnly = true)
481 {
482   QStringList anObjects;
483   if (theContext.IsNull())
484     return "";
485
486   QList<long> aSelectedIds; // Remember of selected address in order to avoid duplicates
487   for (theContext->InitSelected(); theContext->MoreSelected(); theContext->NextSelected()) {
488     Handle(SelectMgr_EntityOwner) anOwner = theContext->SelectedOwner();
489     Handle(StdSelect_BRepOwner) BROwnr = Handle(StdSelect_BRepOwner)::DownCast(anOwner);
490     if (theShapeInfoOnly && BROwnr.IsNull())
491       continue;
492
493     if (aSelectedIds.contains((long)anOwner.Access()))
494       continue;
495     aSelectedIds.append((long)anOwner.Access());
496
497     QString anInfo = "Owner";
498     std::ostringstream aPtrStr;
499     aPtrStr << "[" << anOwner.Access() << "]";
500     anInfo += aPtrStr.str().c_str();
501
502     if (!BROwnr.IsNull() && BROwnr->HasShape()) {
503       const TopoDS_Shape& aShape = BROwnr->Shape();
504       anInfo += QString(", shape type = %1").arg(getShapeTypeInfo(aShape.ShapeType()));
505     }
506     anObjects += anInfo;
507   }
508   QString aValue = QString("selectedOwners[%1]").arg(anObjects.size());
509   if (anObjects.size() > 0)
510     aValue += QString(":\n  %1").arg(anObjects.join("\n  "));
511   return aValue;
512 }
513 #endif
514
515 void selectionInfo(Handle(AIS_InteractiveContext)& theContext, const std::string& thePrefix)
516 {
517 #ifdef DEBUG_SELECTION_INFO
518   QString aValue = QString("\n\n\nDEBUG_SELECTION_INFO for '%1'\n%2\n%3\n%4")
519                                               .arg(thePrefix.c_str())
520                                               .arg(displayedInteractiveObjects(theContext))
521                                               .arg(activeOwners(theContext))
522                                               .arg(selectedOwners(theContext));
523   qDebug(aValue.toStdString().c_str());
524 #endif
525 }
526
527 typedef QMap<QString, int> ShapeTypes;
528 static ShapeTypes myShapeTypes;
529
530 int shapeType(const QString& theType)
531 {
532   if (myShapeTypes.count() == 0) {
533     myShapeTypes["compound"]   = TopAbs_COMPOUND;
534     myShapeTypes["compounds"]  = TopAbs_COMPOUND;
535     myShapeTypes["compsolid"]  = TopAbs_COMPSOLID;
536     myShapeTypes["compsolids"] = TopAbs_COMPSOLID;
537     myShapeTypes["solid"]      = TopAbs_SOLID;
538     myShapeTypes["solids"]     = TopAbs_SOLID;
539     myShapeTypes["shell"]      = TopAbs_SHELL;
540     myShapeTypes["shells"]     = TopAbs_SHELL;
541     myShapeTypes["face"]       = TopAbs_FACE;
542     myShapeTypes["faces"]      = TopAbs_FACE;
543     myShapeTypes["wire"]       = TopAbs_WIRE;
544     myShapeTypes["wires"]      = TopAbs_WIRE;
545     myShapeTypes["edge"]       = TopAbs_EDGE;
546     myShapeTypes["edges"]      = TopAbs_EDGE;
547     myShapeTypes["vertex"]     = TopAbs_VERTEX;
548     myShapeTypes["vertices"]   = TopAbs_VERTEX;
549     myShapeTypes["object"]     = ModuleBase_ResultPrs::Sel_Result;
550     myShapeTypes["objects"]    = ModuleBase_ResultPrs::Sel_Result;
551   }
552   QString aType = theType.toLower();
553   if(myShapeTypes.contains(aType))
554     return myShapeTypes[aType];
555   Events_InfoMessage("ModuleBase_Tools", "Shape type defined in XML is not implemented!").send();
556   return TopAbs_SHAPE;
557 }
558
559 void checkObjects(const QObjectPtrList& theObjects, bool& hasResult, bool& hasFeature,
560                   bool& hasParameter, bool& hasCompositeOwner)
561 {
562   hasResult = false;
563   hasFeature = false;
564   hasParameter = false;
565   hasCompositeOwner = false;
566   foreach(ObjectPtr aObj, theObjects) {
567     FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aObj);
568     ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(aObj);
569     ResultParameterPtr aConstruction = std::dynamic_pointer_cast<ModelAPI_ResultParameter>(aResult);
570
571     hasResult |= (aResult.get() != NULL);
572     hasFeature |= (aFeature.get() != NULL);
573     hasParameter |= (aConstruction.get() != NULL);
574     if (hasFeature) 
575       hasCompositeOwner |= (ModelAPI_Tools::compositeOwner(aFeature) != NULL);
576     if (hasFeature && hasResult  && hasParameter && hasCompositeOwner)
577       break;
578   }
579 }
580
581 bool setDefaultDeviationCoefficient(std::shared_ptr<GeomAPI_Shape> theGeomShape)
582 {
583   if (!theGeomShape.get())
584     return false;
585   // if the shape could not be exploded on faces, it contains only wires, edges, and vertices
586   // correction of deviation for them should not influence to the application performance
587   GeomAPI_ShapeExplorer anExp(theGeomShape, GeomAPI_Shape::FACE);
588   bool anEmpty = anExp.empty();
589   return !anExp.more();
590 }
591
592 void setDefaultDeviationCoefficient(const std::shared_ptr<ModelAPI_Result>& theResult,
593                                     const Handle(Prs3d_Drawer)& theDrawer)
594 {
595   if (!theResult.get())
596     return;
597   bool aUseDeviation = false;
598
599   std::string aResultGroup = theResult->groupName();
600   if (aResultGroup == ModelAPI_ResultConstruction::group())
601     aUseDeviation = true;
602   else if (aResultGroup == ModelAPI_ResultBody::group()) {
603     GeomShapePtr aGeomShape = theResult->shape();
604     if (aGeomShape.get())
605       aUseDeviation = setDefaultDeviationCoefficient(aGeomShape);
606   }
607   if (aUseDeviation)
608     theDrawer->SetDeviationCoefficient(DEFAULT_DEVIATION_COEFFICIENT);
609 }
610
611 void setDefaultDeviationCoefficient(const TopoDS_Shape& theShape,
612                                     const Handle(Prs3d_Drawer)& theDrawer)
613 {
614   if (theShape.IsNull())
615     return;
616
617   std::shared_ptr<GeomAPI_Shape> aGeomShape(new GeomAPI_Shape());
618   aGeomShape->setImpl(new TopoDS_Shape(theShape));
619   if (setDefaultDeviationCoefficient(aGeomShape))
620     theDrawer->SetDeviationCoefficient(DEFAULT_DEVIATION_COEFFICIENT);
621 }
622
623 Quantity_Color color(const std::string& theSection,
624                      const std::string& theName,
625                      const std::string& theDefault)
626 {
627   std::vector<int> aColor = Config_PropManager::color(theSection, theName, theDefault);
628   return Quantity_Color(aColor[0] / 255., aColor[1] / 255., aColor[2] / 255., Quantity_TOC_RGB);
629 }
630
631 ObjectPtr getObject(const AttributePtr& theAttribute)
632 {
633   ObjectPtr anObject;
634   std::string anAttrType = theAttribute->attributeType();
635   if (anAttrType == ModelAPI_AttributeRefAttr::typeId()) {
636     AttributeRefAttrPtr anAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
637     if (anAttr != NULL && anAttr->isObject())
638       anObject = anAttr->object();
639   }
640   if (anAttrType == ModelAPI_AttributeSelection::typeId()) {
641     AttributeSelectionPtr anAttr = std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(theAttribute);
642     if (anAttr != NULL)
643       anObject = anAttr->context();
644   }
645   if (anAttrType == ModelAPI_AttributeReference::typeId()) {
646     AttributeReferencePtr anAttr = std::dynamic_pointer_cast<ModelAPI_AttributeReference>(theAttribute);
647     if (anAttr.get() != NULL)
648       anObject = anAttr->value();
649   }
650   return anObject;
651 }
652
653 TopAbs_ShapeEnum getCompoundSubType(const TopoDS_Shape& theShape)
654 {
655   TopAbs_ShapeEnum aShapeType = theShape.ShapeType();
656
657   // for compounds check sub-shapes: it may be compound of needed type:
658   // Booleans may produce compounds of Solids
659   if (aShapeType == TopAbs_COMPOUND) {
660     for(TopoDS_Iterator aSubs(theShape); aSubs.More(); aSubs.Next()) {
661       if (!aSubs.Value().IsNull()) {
662         TopAbs_ShapeEnum aSubType = aSubs.Value().ShapeType();
663         if (aSubType == TopAbs_COMPOUND) { // compound of compound(s)
664           aShapeType = TopAbs_COMPOUND;
665           break;
666         }
667         if (aShapeType == TopAbs_COMPOUND) {
668           aShapeType = aSubType;
669         } else if (aShapeType != aSubType) { // compound of shapes of different types
670           aShapeType = TopAbs_COMPOUND;
671           break;
672         }
673       }
674     }
675   }
676   return aShapeType;
677 }
678
679 void getParameters(QStringList& theParameters)
680 {
681   theParameters.clear();
682
683   SessionPtr aSession = ModelAPI_Session::get();
684   std::list<DocumentPtr> aDocList;
685   DocumentPtr anActiveDocument = aSession->activeDocument();
686   DocumentPtr aRootDocument = aSession->moduleDocument();
687   aDocList.push_back(anActiveDocument);
688   if (anActiveDocument != aRootDocument) {
689     aDocList.push_back(aRootDocument);
690   }
691   std::string aGroupId = ModelAPI_ResultParameter::group();
692   for(std::list<DocumentPtr>::const_iterator it = aDocList.begin(); it != aDocList.end(); ++it) {
693     DocumentPtr aDocument = *it;
694     int aSize = aDocument->size(aGroupId);
695     for (int i = 0; i < aSize; i++) {
696       ObjectPtr anObject = aDocument->object(aGroupId, i);
697       std::string aParameterName = anObject->data()->name();
698       theParameters.append(aParameterName.c_str());
699     }
700   }
701 }
702
703 std::string findGreedAttribute(ModuleBase_IWorkshop* theWorkshop, const FeaturePtr& theFeature)
704 {
705   std::string anAttributeId;
706
707   std::string aXmlCfg, aDescription;
708   theWorkshop->module()->getXMLRepresentation(theFeature->getKind(), aXmlCfg, aDescription);
709
710   ModuleBase_WidgetFactory aFactory(aXmlCfg, theWorkshop);
711   std::string anAttributeTitle;
712   aFactory.getGreedAttribute(anAttributeId);
713
714   return anAttributeId;
715 }
716
717 bool hasObject(const AttributePtr& theAttribute, const ObjectPtr& theObject,
718                const std::shared_ptr<GeomAPI_Shape>& theShape,
719                ModuleBase_IWorkshop* theWorkshop,
720                const bool theTemporarily)
721 {
722   bool aHasObject = false;
723   if (!theAttribute.get())
724     return aHasObject;
725
726   std::string aType = theAttribute->attributeType();
727   if (aType == ModelAPI_AttributeReference::typeId()) {
728     AttributeReferencePtr aRef = std::dynamic_pointer_cast<ModelAPI_AttributeReference>(theAttribute);
729     ObjectPtr aObject = aRef->value();
730     aHasObject = aObject && aObject->isSame(theObject);
731     //if (!(aObject && aObject->isSame(theObject))) {
732     //  aRef->setValue(theObject);
733     //}
734   } else if (aType == ModelAPI_AttributeRefAttr::typeId()) {
735     AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
736
737     AttributePtr anAttribute = theWorkshop->module()->findAttribute(theObject, theShape);
738     if (anAttribute.get()) {
739       //aRefAttr->setAttr(anAttribute);
740     }
741     else {
742       ObjectPtr aObject = aRefAttr->object();
743       aHasObject = aObject && aObject->isSame(theObject);
744       //if (!(aObject && aObject->isSame(theObject))) {
745       //  aRefAttr->setObject(theObject);
746       //}
747     }
748   } else if (aType == ModelAPI_AttributeSelection::typeId()) {
749     /*AttributeSelectionPtr aSelectAttr =
750                              std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(theAttribute);
751     ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
752     if (aSelectAttr.get() != NULL) {
753       aSelectAttr->setValue(aResult, theShape, theTemporarily);
754     }*/
755   }
756   if (aType == ModelAPI_AttributeSelectionList::typeId()) {
757     AttributeSelectionListPtr aSelectionListAttr =
758                          std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(theAttribute);
759     ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
760     aHasObject = aSelectionListAttr->isInList(aResult, theShape, theTemporarily);
761     //if (!theCheckIfAttributeHasObject || !aSelectionListAttr->isInList(aResult, theShape, theTemporarily))
762     //  aSelectionListAttr->append(aResult, theShape, theTemporarily);
763   }
764   else if (aType == ModelAPI_AttributeRefList::typeId()) {
765     AttributeRefListPtr aRefListAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(theAttribute);
766     aHasObject = aRefListAttr->isInList(theObject);
767     //if (!theCheckIfAttributeHasObject || !aRefListAttr->isInList(theObject))
768     //  aRefListAttr->append(theObject);
769   }
770   else if (aType == ModelAPI_AttributeRefAttrList::typeId()) {
771     AttributeRefAttrListPtr aRefAttrListAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttrList>(theAttribute);
772     AttributePtr anAttribute = theWorkshop->module()->findAttribute(theObject, theShape);
773
774     if (anAttribute.get()) {
775       aHasObject = aRefAttrListAttr->isInList(anAttribute);
776       //if (!theCheckIfAttributeHasObject || !aRefAttrListAttr->isInList(anAttribute))
777       //  aRefAttrListAttr->append(anAttribute);
778     }
779     else {
780       aHasObject = aRefAttrListAttr->isInList(theObject);
781       //if (!theCheckIfAttributeHasObject || !aRefAttrListAttr->isInList(theObject))
782       //  aRefAttrListAttr->append(theObject);
783     }
784   }
785   return aHasObject;
786 }
787
788 bool setObject(const AttributePtr& theAttribute, const ObjectPtr& theObject,
789                const GeomShapePtr& theShape, ModuleBase_IWorkshop* theWorkshop,
790                const bool theTemporarily, const bool theCheckIfAttributeHasObject)
791 {
792   if (!theAttribute.get())
793     return false;
794
795   bool isDone = true;
796   std::string aType = theAttribute->attributeType();
797   if (aType == ModelAPI_AttributeReference::typeId()) {
798     AttributeReferencePtr aRef = std::dynamic_pointer_cast<ModelAPI_AttributeReference>(theAttribute);
799     ObjectPtr aObject = aRef->value();
800     if (!(aObject && aObject->isSame(theObject))) {
801       aRef->setValue(theObject);
802     }
803   } else if (aType == ModelAPI_AttributeRefAttr::typeId()) {
804     AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
805
806     AttributePtr anAttribute = theWorkshop->module()->findAttribute(theObject, theShape);
807     if (anAttribute.get())
808       aRefAttr->setAttr(anAttribute);
809     else {
810       ObjectPtr aObject = aRefAttr->object();
811       if (!(aObject && aObject->isSame(theObject))) {
812         aRefAttr->setObject(theObject);
813       }
814     }
815   } else if (aType == ModelAPI_AttributeSelection::typeId()) {
816     AttributeSelectionPtr aSelectAttr =
817                              std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(theAttribute);
818     ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
819     if (aSelectAttr.get() != NULL) {
820       aSelectAttr->setValue(aResult, theShape, theTemporarily);
821     }
822   }
823   if (aType == ModelAPI_AttributeSelectionList::typeId()) {
824     AttributeSelectionListPtr aSelectionListAttr =
825                          std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(theAttribute);
826     ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
827     if (!theCheckIfAttributeHasObject || !aSelectionListAttr->isInList(aResult, theShape, theTemporarily))
828       aSelectionListAttr->append(aResult, theShape, theTemporarily);
829   }
830   else if (aType == ModelAPI_AttributeRefList::typeId()) {
831     AttributeRefListPtr aRefListAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(theAttribute);
832     if (!theCheckIfAttributeHasObject || !aRefListAttr->isInList(theObject)) {
833       if (theObject.get())
834         aRefListAttr->append(theObject);
835       else
836         isDone = false;
837     }
838   }
839   else if (aType == ModelAPI_AttributeRefAttrList::typeId()) {
840     AttributeRefAttrListPtr aRefAttrListAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttrList>(theAttribute);
841     AttributePtr anAttribute = theWorkshop->module()->findAttribute(theObject, theShape);
842
843     if (anAttribute.get()) {
844       if (!theCheckIfAttributeHasObject || !aRefAttrListAttr->isInList(anAttribute))
845         aRefAttrListAttr->append(anAttribute);
846     }
847     else {
848       if (!theCheckIfAttributeHasObject || !aRefAttrListAttr->isInList(theObject)) {
849         if (theObject.get())
850           aRefAttrListAttr->append(theObject);
851         else
852           isDone = false;
853       }
854     }
855   }
856   return isDone;
857 }
858
859 GeomShapePtr getShape(const AttributePtr& theAttribute, ModuleBase_IWorkshop* theWorkshop)
860 {
861   GeomShapePtr aShape;
862   if (!theAttribute.get())
863     return aShape;
864
865   std::string aType = theAttribute->attributeType();
866   if (aType == ModelAPI_AttributeReference::typeId()) {
867   } else if (aType == ModelAPI_AttributeRefAttr::typeId()) {
868     AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
869     if (aRefAttr.get() && !aRefAttr->isObject()) {
870       AttributePtr anAttribute = aRefAttr->attr();
871       aShape = theWorkshop->module()->findShape(anAttribute);
872     }
873   } else if (aType == ModelAPI_AttributeSelection::typeId()) {
874     AttributeSelectionPtr aSelectAttr = std::dynamic_pointer_cast<ModelAPI_AttributeSelection>
875                                                                                  (theAttribute);
876     aShape = aSelectAttr->value();
877   }
878   else // Geom2D point processing
879     aShape = theWorkshop->module()->findShape(theAttribute);
880   return aShape;
881 }
882
883 void flushUpdated(ObjectPtr theObject)
884 {
885   blockUpdateViewer(true);
886
887   Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_UPDATED));
888
889   blockUpdateViewer(false);
890 }
891
892 void blockUpdateViewer(const bool theValue)
893 {
894   // the viewer update should be blocked in order to avoid the temporary feature content
895   // when the solver processes the feature, the redisplay message can be flushed
896   // what caused the display in the viewer preliminary states of object
897   // e.g. fillet feature, angle value change
898   std::shared_ptr<Events_Message> aMsg;
899   if (theValue) {
900     aMsg = std::shared_ptr<Events_Message>(
901         new Events_Message(Events_Loop::eventByName(EVENT_UPDATE_VIEWER_BLOCKED)));
902   }
903   else {
904     // the viewer update should be unblocked
905     aMsg = std::shared_ptr<Events_Message>(
906         new Events_Message(Events_Loop::eventByName(EVENT_UPDATE_VIEWER_UNBLOCKED)));
907   }
908   Events_Loop::loop()->send(aMsg);
909 }
910
911 QString wrapTextByWords(const QString& theValue, QWidget* theWidget,
912                                           int theMaxLineInPixels)
913 {
914   static QFontMetrics tfm(theWidget ? theWidget->font() : QApplication::font());
915   static qreal phi = 2.618;
916
917   QRect aBounds = tfm.boundingRect(theValue);
918   if(aBounds.width() <= theMaxLineInPixels)
919     return theValue;
920
921   qreal s = aBounds.width() * aBounds.height();
922   qreal aGoldWidth = sqrt(s*phi);
923
924   QStringList aWords = theValue.split(" ", QString::SkipEmptyParts);
925   QStringList aLines;
926   int n = aWords.count();
927   QString aLine;
928   for (int i = 0; i < n; i++) {
929     QString aLineExt = aLine + " " + aWords[i];
930     qreal anWidthNonExt = tfm.boundingRect(aLine).width();
931     qreal anWidthExt = tfm.boundingRect(aLineExt).width();
932     qreal aDeltaNonExt = fabs(anWidthNonExt-aGoldWidth);
933     qreal aDeltaExt    = fabs(anWidthExt-aGoldWidth);
934     if(aDeltaNonExt < aDeltaExt) {
935       // new line
936       aLines.append(aLine);
937       aLine = aWords[i];
938     }
939     else
940       aLine = aLineExt;
941   }
942
943   if(!aLine.isEmpty())
944     aLines.append(aLine);
945
946   QString aResult = aLines.join("\n");
947   return aResult;
948 }
949
950 //**************************************************************
951 void refsToFeatureInFeatureDocument(const ObjectPtr& theObject, std::set<FeaturePtr>& theRefFeatures)
952 {
953   FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
954   if (aFeature.get()) {
955     DocumentPtr aFeatureDoc = aFeature->document();
956     // 1. find references in the current document
957     aFeatureDoc->refsToFeature(aFeature, theRefFeatures, false);
958   }
959 }
960
961
962 //**************************************************************
963 /*bool isSubOfComposite(const ObjectPtr& theObject)
964 {
965   bool isSub = false;
966   std::set<FeaturePtr> aRefFeatures;
967   refsToFeatureInFeatureDocument(theObject, aRefFeatures);
968   std::set<FeaturePtr>::const_iterator anIt = aRefFeatures.begin(),
969                                        aLast = aRefFeatures.end();
970   for (; anIt != aLast && !isSub; anIt++) {
971     isSub = isSubOfComposite(theObject, *anIt);
972   }
973   return isSub;
974 }*/
975
976 //**************************************************************
977 /*bool isSubOfComposite(const ObjectPtr& theObject, const FeaturePtr& theFeature)
978 {
979   bool isSub = false;
980   CompositeFeaturePtr aComposite = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(theFeature);
981   if (aComposite.get()) {
982     isSub = aComposite->isSub(theObject);
983     // the recursive is possible, the parameters are sketch circle and extrusion cut. They are
984     // separated by composite sketch feature
985     if (!isSub) {
986       int aNbSubs = aComposite->numberOfSubs();
987       for (int aSub = 0; aSub < aNbSubs && !isSub; aSub++) {
988         isSub = isSubOfComposite(theObject, aComposite->subFeature(aSub));
989       }
990     }
991   }
992   return isSub;
993 }*/
994
995 //**************************************************************
996 ResultPtr firstResult(const ObjectPtr& theObject)
997 {
998   ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
999   if (!aResult.get()) {
1000     FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theObject);
1001     if (aFeature.get())
1002       aResult = aFeature->firstResult();
1003   }
1004   return aResult;
1005 }
1006
1007 //**************************************************************
1008 bool isFeatureOfResult(const FeaturePtr& theFeature, const std::string& theGroupOfResult)
1009 {
1010   bool isResult = false;
1011
1012   if (!theFeature->data()->isValid())
1013     return isResult;
1014
1015   ResultPtr aFirstResult = theFeature->firstResult();
1016   if (!aFirstResult.get())
1017     return isResult;
1018
1019   return aFirstResult->groupName() == theGroupOfResult;
1020 }
1021
1022 //**************************************************************
1023 bool hasModuleDocumentFeature(const std::set<FeaturePtr>& theFeatures)
1024 {
1025   bool aFoundModuleDocumentObject = false;
1026   DocumentPtr aModuleDoc = ModelAPI_Session::get()->moduleDocument();
1027
1028   std::set<FeaturePtr>::const_iterator anIt = theFeatures.begin(), aLast = theFeatures.end();
1029   for (; anIt != aLast && !aFoundModuleDocumentObject; anIt++) {
1030     FeaturePtr aFeature = *anIt;
1031     ResultPtr aResult = ModuleBase_Tools::firstResult(aFeature);
1032     if (aResult.get() && aResult->groupName() == ModelAPI_ResultPart::group())
1033       continue;
1034     aFoundModuleDocumentObject = aFeature->document() == aModuleDoc;
1035   }
1036
1037   return aFoundModuleDocumentObject;
1038 }
1039
1040 //**************************************************************
1041 bool askToDelete(const std::set<FeaturePtr> theFeatures,
1042                  const std::map<FeaturePtr, std::set<FeaturePtr> >& theReferences,
1043                  QWidget* theParent,
1044                  std::set<FeaturePtr>& theReferencesToDelete)
1045 {
1046   QString aNotActivatedDocWrn;
1047   std::string aNotActivatedNames;
1048   if (!ModelAPI_Tools::allDocumentsActivated(aNotActivatedNames)) {
1049     if (ModuleBase_Tools::hasModuleDocumentFeature(theFeatures))
1050       aNotActivatedDocWrn = QObject::tr("Selected objects can be used in Part documents which are not loaded:%1.\n")
1051                             .arg(aNotActivatedNames.c_str());
1052   }
1053   
1054   std::set<FeaturePtr> aFeaturesRefsTo;
1055   std::set<FeaturePtr> aFeaturesRefsToParameter;
1056   std::set<FeaturePtr> aParameterFeatures;
1057   QStringList aPartFeatureNames;
1058   std::set<FeaturePtr>::const_iterator anIt = theFeatures.begin(),
1059                                        aLast = theFeatures.end();
1060   // separate features to references to parameter features and references to others
1061   for (; anIt != aLast; anIt++) {
1062     FeaturePtr aFeature = *anIt;
1063     if (theReferences.find(aFeature) == theReferences.end())
1064       continue;
1065
1066     if (isFeatureOfResult(aFeature, ModelAPI_ResultPart::group()))
1067       aPartFeatureNames.append(aFeature->name().c_str());
1068
1069     std::set<FeaturePtr> aRefFeatures;
1070     std::set<FeaturePtr> aRefList = theReferences.at(aFeature);
1071     std::set<FeaturePtr>::const_iterator aRefIt = aRefList.begin(), aRefLast = aRefList.end();
1072     for (; aRefIt != aRefLast; aRefIt++) {
1073       FeaturePtr aRefFeature = *aRefIt;
1074       if (theFeatures.find(aRefFeature) == theFeatures.end() && // it is not selected
1075           aRefFeatures.find(aRefFeature) == aRefFeatures.end()) // it is not added
1076         aRefFeatures.insert(aRefFeature);
1077     }
1078
1079     if (isFeatureOfResult(aFeature, ModelAPI_ResultParameter::group())) {
1080       aFeaturesRefsToParameter.insert(aRefFeatures.begin(), aRefFeatures.end());
1081       aParameterFeatures.insert(aFeature);
1082     }
1083     else {
1084       theReferencesToDelete.insert(aRefFeatures.begin(), aRefFeatures.end());
1085     }
1086   }
1087
1088   std::set<FeaturePtr> aFeaturesRefsToParameterOnly;
1089   anIt = aFeaturesRefsToParameter.begin();
1090   aLast = aFeaturesRefsToParameter.end();
1091   // separate features to references to parameter features and references to others
1092   QStringList aParamFeatureNames;
1093   for (; anIt != aLast; anIt++) {
1094     FeaturePtr aFeature = *anIt;
1095     if (theReferencesToDelete.find(aFeature) == theReferencesToDelete.end()) {
1096       aFeaturesRefsToParameterOnly.insert(aFeature);
1097       aParamFeatureNames.append(aFeature->name().c_str());
1098     }
1099   }
1100   aParamFeatureNames.sort();
1101   QStringList anOtherFeatureNames;
1102   anIt = theReferencesToDelete.begin();
1103   aLast = theReferencesToDelete.end();
1104   for (; anIt != aLast; anIt++) {
1105     FeaturePtr aFeature = *anIt;
1106     if (isFeatureOfResult(aFeature, ModelAPI_ResultPart::group()))
1107       aPartFeatureNames.append(aFeature->name().c_str());
1108     else
1109       anOtherFeatureNames.append(aFeature->name().c_str());
1110   }
1111   aPartFeatureNames.sort();
1112   anOtherFeatureNames.sort();
1113
1114   bool aCanReplaceParameters = !aFeaturesRefsToParameterOnly.empty();
1115
1116   QMessageBox aMessageBox(theParent);
1117   aMessageBox.setWindowTitle(QObject::tr("Delete features"));
1118   aMessageBox.setIcon(QMessageBox::Warning);
1119   aMessageBox.setStandardButtons(QMessageBox::No | QMessageBox::Yes);
1120   aMessageBox.setDefaultButton(QMessageBox::No);
1121
1122   QString aText;
1123   QString aSep = ", ";
1124   if (!aPartFeatureNames.empty())
1125     aText += QString(QObject::tr("The following parts will be deleted: %1.\n")).arg(aPartFeatureNames.join(aSep));
1126   if (!aNotActivatedDocWrn.isEmpty())
1127     aText += aNotActivatedDocWrn;
1128   if (!anOtherFeatureNames.empty())
1129     aText += QString(QObject::tr("Selected features are used in the following features: %1.\nThese features will be deleted.\n"))
1130                      .arg(anOtherFeatureNames.join(aSep));
1131   if (!aParamFeatureNames.empty()) {
1132     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"))
1133                      .arg(aParamFeatureNames.join(aSep));
1134     QPushButton *aReplaceButton = aMessageBox.addButton(QObject::tr("Replace"), QMessageBox::ActionRole);
1135   }
1136
1137   if (!aText.isEmpty()) {
1138     aText += "Would you like to continue?";
1139     aMessageBox.setText(aText);
1140     aMessageBox.exec();
1141     QMessageBox::ButtonRole aButtonRole = aMessageBox.buttonRole(aMessageBox.clickedButton());
1142
1143     if (aButtonRole == QMessageBox::NoRole)
1144       return false;
1145
1146     if (aButtonRole == QMessageBox::ActionRole) {
1147       foreach (FeaturePtr aObj, aParameterFeatures)
1148         ModelAPI_ReplaceParameterMessage::send(aObj, 0);
1149     }
1150     else
1151       theReferencesToDelete.insert(aFeaturesRefsToParameterOnly.begin(), aFeaturesRefsToParameterOnly.end());
1152   }
1153   return true;
1154 }
1155
1156 //**************************************************************
1157 void convertToFeatures(const QObjectPtrList& theObjects, std::set<FeaturePtr>& theFeatures)
1158 {
1159   QObjectPtrList::const_iterator anIt = theObjects.begin(), aLast = theObjects.end();
1160   for(; anIt != aLast; anIt++) {
1161     ObjectPtr anObject = *anIt;
1162     FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(anObject);
1163     // for parameter result, use the corresponded reature to be removed
1164     if (!aFeature.get() && anObject->groupName() == ModelAPI_ResultParameter::group()) {
1165       aFeature = ModelAPI_Feature::feature(anObject);
1166     }
1167     theFeatures.insert(aFeature);
1168   }
1169 }
1170
1171 void translate(const std::string& theContext, std::string& theMessage)
1172 {
1173   if (!theMessage.empty()) {
1174     std::string aStr = Config_Translator::translate(theContext, theMessage);
1175     if (!aStr.empty()) {
1176       std::string aCodec = Config_Translator::codec(theContext);
1177       theMessage = QTextCodec::codecForName(aCodec.c_str())->toUnicode(aStr.c_str()).toStdString();
1178     }
1179   }
1180 }
1181
1182 void setPointBallHighlighting(AIS_Shape* theAIS)
1183 {
1184   static Handle(Image_AlienPixMap) aPixMap;
1185   if(aPixMap.IsNull()) {
1186     // Load icon for the presentation
1187     std::string aFile;
1188     char* anEnv = getenv("SHAPER_ROOT_DIR");
1189     if(anEnv) {
1190       aFile = std::string(anEnv) +
1191         FSEP + "share" + FSEP + "salome" + FSEP + "resources" + FSEP + "shaper";
1192     } else {
1193       anEnv = getenv("OPENPARTS_ROOT_DIR");
1194       if (anEnv)
1195         aFile = std::string(anEnv) + FSEP + "resources";
1196     }
1197
1198     aFile += FSEP;
1199     static const std::string aMarkerName = "marker_dot.png";
1200     aFile += aMarkerName;
1201     aPixMap = new Image_AlienPixMap();
1202     if(!aPixMap->Load(aFile.c_str())) {
1203       // The icon for constraint is not found
1204       static const std::string aMsg = "Error: Point market not found by path: \"" + aFile + "\". Falling back.";
1205       //Events_InfoMessage("ModuleBase_Tools::setPointBallHighlighting", aMsg).send();
1206     }
1207   }
1208
1209   Handle(Graphic3d_AspectMarker3d) anAspect;
1210   Handle(Prs3d_Drawer) aDrawer = theAIS->HilightAttributes();
1211   if(aDrawer->HasOwnPointAspect()) {
1212     Handle(Prs3d_PointAspect) aPntAspect = aDrawer->PointAspect();
1213     if(aPixMap->IsEmpty()) {
1214       anAspect = aPntAspect->Aspect();
1215       anAspect->SetType(Aspect_TOM_BALL);
1216     } else {
1217       if(aPixMap->Format() == Image_PixMap::ImgGray) {
1218         aPixMap->SetFormat (Image_PixMap::ImgAlpha);
1219       } else if(aPixMap->Format() == Image_PixMap::ImgGrayF) {
1220         aPixMap->SetFormat (Image_PixMap::ImgAlphaF);
1221       }
1222       anAspect = new Graphic3d_AspectMarker3d(aPixMap);
1223       aPntAspect->SetAspect(anAspect);
1224     }
1225     aDrawer->SetPointAspect(aPntAspect);
1226     theAIS->SetHilightAttributes(aDrawer);
1227   }
1228 }
1229
1230 } // namespace ModuleBase_Tools
1231
1232