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