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