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