Salome HOME
Abort Sketch by click on the button in the tool bar. Abort nested opened operations.
[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 <ModelAPI_Result.h>
10 #include <ModelAPI_Data.h>
11 #include <ModelAPI_Attribute.h>
12 #include <ModelAPI_AttributeRefAttr.h>
13
14 #include <GeomDataAPI_Point2D.h>
15 #include <Events_Error.h>
16
17 #include <QWidget>
18 #include <QLayout>
19 #include <QPainter>
20 #include <QBitmap>
21 #include <QDoubleSpinBox>
22
23 namespace ModuleBase_Tools {
24
25 //******************************************************************
26
27 //******************************************************************
28
29 void adjustMargins(QWidget* theWidget)
30 {
31   if(!theWidget)
32     return;
33   adjustMargins(theWidget->layout());
34 }
35
36 void adjustMargins(QLayout* theLayout)
37 {
38   if(!theLayout)
39     return;
40   theLayout->setContentsMargins(2, 5, 2, 5);
41   theLayout->setSpacing(4);
42 }
43
44 void zeroMargins(QWidget* theWidget)
45 {
46   if(!theWidget)
47     return;
48   zeroMargins(theWidget->layout());
49 }
50
51 void zeroMargins(QLayout* theLayout)
52 {
53   if(!theLayout)
54     return;
55   theLayout->setContentsMargins(0, 0, 0, 0);
56   theLayout->setSpacing(5);
57 }
58
59 QPixmap composite(const QString& theAdditionalIcon, const QString& theIcon)
60 {
61   QImage anIcon(theIcon);
62   QImage anAditional(theAdditionalIcon);
63
64   if (anIcon.isNull())
65     return QPixmap();
66
67   int anAddWidth = anAditional.width();
68   int anAddHeight = anAditional.height();
69
70   int aWidth = anIcon.width();
71   int aHeight = anIcon.height();
72
73   int aStartWidthPos = aWidth - anAddWidth - 1;
74   int aStartHeightPos = aHeight - anAddHeight - 1;
75
76   for (int i = 0; i < anAddWidth && i + aStartWidthPos < aWidth; i++)
77   {
78     for (int j = 0; j < anAddHeight && j + aStartHeightPos < aHeight; j++)
79     {
80       if (qAlpha(anAditional.pixel(i, j)) > 0)
81         anIcon.setPixel(i + aStartWidthPos, j + aStartHeightPos, anAditional.pixel(i, j));
82     }
83   }
84   return QPixmap::fromImage(anIcon);
85 }
86
87 QPixmap lighter(const QString& theIcon, const int theLighterValue)
88 {
89   QImage anIcon(theIcon);
90   if (anIcon.isNull())
91     return QPixmap();
92
93   QImage aResult(theIcon);
94   for ( int i = 0; i < anIcon.width(); i++ )
95   {
96     for ( int j = 0; j < anIcon.height(); j++ )
97     {
98       QRgb anRgb = anIcon.pixel( i, j );
99       QColor aPixelColor(qRed(anRgb), qGreen(anRgb), qBlue(anRgb),
100                          qAlpha( aResult.pixel( i, j ) ));
101
102       QColor aLighterColor = aPixelColor.lighter(theLighterValue);
103       aResult.setPixel(i, j, qRgba( aLighterColor.red(), aLighterColor.green(),
104                                     aLighterColor.blue(), aLighterColor.alpha() ) );
105     }
106   }
107   return QPixmap::fromImage(aResult);
108 }
109
110 void setSpinValue(QDoubleSpinBox* theSpin, double theValue)
111 {
112   bool isBlocked = theSpin->blockSignals(true);
113   theSpin->setValue(theValue);
114   theSpin->blockSignals(isBlocked);
115 }
116
117 QString objectInfo(const ObjectPtr& theObj, const bool isUseAttributesInfo)
118 {
119   ResultPtr aRes = std::dynamic_pointer_cast<ModelAPI_Result>(theObj);
120   FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theObj);
121   QString aFeatureStr = "feature";
122   if(aRes.get()) {
123     aFeatureStr.append("(Result)");
124     aFeature = ModelAPI_Feature::feature(aRes);
125   }
126   if (aFeature.get()) {
127     aFeatureStr.append(QString(": %1").arg(aFeature->getKind().c_str()).toStdString().c_str());
128     if (aFeature->data().get() && aFeature->data()->isValid())
129       aFeatureStr.append(QString(", name=%1").arg(aFeature->data()->name().c_str()).toStdString()
130                                                                                        .c_str());
131     if (isUseAttributesInfo) {
132       std::list<AttributePtr> anAttrs = aFeature->data()->attributes("");
133       std::list<AttributePtr>::const_iterator anIt = anAttrs.begin(), aLast = anAttrs.end();
134       QStringList aValues;
135       for(; anIt != aLast; anIt++) {
136         AttributePtr anAttr = *anIt;
137         QString aValue = "not defined";
138         std::string aType = anAttr->attributeType();
139         if (aType == GeomDataAPI_Point2D::typeId()) {
140           std::shared_ptr<GeomDataAPI_Point2D> aPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
141                                                                                          anAttr);
142           if (aPoint.get())
143             aValue = QString("(%1, %2)").arg(aPoint->x()).arg(aPoint->y());
144         }
145         else if (aType == ModelAPI_AttributeRefAttr::typeId()) {
146         }
147
148         aValues.push_back(QString("%1: %2").arg(anAttr->id().c_str()).arg(aValue).toStdString().c_str());
149       }
150       if (!aValues.empty())
151         aFeatureStr.append(QString(", attributes: %1").arg(aValues.join(", ").toStdString().c_str()));
152     }
153   }
154
155   return aFeatureStr;
156 }
157
158 typedef QMap<QString, TopAbs_ShapeEnum> ShapeTypes;
159 static ShapeTypes MyShapeTypes;
160
161 TopAbs_ShapeEnum shapeType(const QString& theType)
162 {
163   if (MyShapeTypes.count() == 0) {
164     MyShapeTypes["face"] = TopAbs_FACE;
165     MyShapeTypes["faces"] = TopAbs_FACE;
166     MyShapeTypes["vertex"] = TopAbs_VERTEX;
167     MyShapeTypes["vertices"] = TopAbs_VERTEX;
168     MyShapeTypes["wire"] = TopAbs_WIRE;
169     MyShapeTypes["edge"] = TopAbs_EDGE;
170     MyShapeTypes["edges"] = TopAbs_EDGE;
171     MyShapeTypes["shell"] = TopAbs_SHELL;
172     MyShapeTypes["solid"] = TopAbs_SOLID;
173     MyShapeTypes["solids"] = TopAbs_SOLID;
174   }
175   QString aType = theType.toLower();
176   if (MyShapeTypes.contains(aType))
177     return MyShapeTypes[aType];
178   Events_Error::send("Shape type defined in XML is not implemented!");
179   return TopAbs_SHAPE;
180 }
181
182
183 }
184
185