Salome HOME
Issue #660 - Move Group is still not implemented -- Check refs only between the selec...
[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 #include <ModuleBase_ParamSpinBox.h>
9
10 #include <ModelAPI_Result.h>
11 #include <ModelAPI_Data.h>
12 #include <ModelAPI_Attribute.h>
13 #include <ModelAPI_AttributeRefAttr.h>
14 #include <ModelAPI_ResultParameter.h>
15
16 #include <GeomDataAPI_Point2D.h>
17 #include <Events_Error.h>
18
19 #include <QWidget>
20 #include <QLayout>
21 #include <QPainter>
22 #include <QBitmap>
23 #include <QDoubleSpinBox>
24
25 #include <sstream>
26
27 namespace ModuleBase_Tools {
28
29 //******************************************************************
30
31 //******************************************************************
32
33 void adjustMargins(QWidget* theWidget)
34 {
35   if(!theWidget)
36     return;
37   adjustMargins(theWidget->layout());
38 }
39
40 void adjustMargins(QLayout* theLayout)
41 {
42   if(!theLayout)
43     return;
44   theLayout->setContentsMargins(2, 5, 2, 5);
45   theLayout->setSpacing(4);
46 }
47
48 void zeroMargins(QWidget* theWidget)
49 {
50   if(!theWidget)
51     return;
52   zeroMargins(theWidget->layout());
53 }
54
55 void zeroMargins(QLayout* theLayout)
56 {
57   if(!theLayout)
58     return;
59   theLayout->setContentsMargins(0, 0, 0, 0);
60   theLayout->setSpacing(5);
61 }
62
63 QPixmap composite(const QString& theAdditionalIcon, const QString& theIcon)
64 {
65   QImage anIcon(theIcon);
66   QImage anAditional(theAdditionalIcon);
67
68   if (anIcon.isNull())
69     return QPixmap();
70
71   int anAddWidth = anAditional.width();
72   int anAddHeight = anAditional.height();
73
74   int aWidth = anIcon.width();
75   int aHeight = anIcon.height();
76
77   int aStartWidthPos = aWidth - anAddWidth - 1;
78   int aStartHeightPos = aHeight - anAddHeight - 1;
79
80   for (int i = 0; i < anAddWidth && i + aStartWidthPos < aWidth; i++)
81   {
82     for (int j = 0; j < anAddHeight && j + aStartHeightPos < aHeight; j++)
83     {
84       if (qAlpha(anAditional.pixel(i, j)) > 0)
85         anIcon.setPixel(i + aStartWidthPos, j + aStartHeightPos, anAditional.pixel(i, j));
86     }
87   }
88   return QPixmap::fromImage(anIcon);
89 }
90
91 QPixmap lighter(const QString& theIcon, const int theLighterValue)
92 {
93   QImage anIcon(theIcon);
94   if (anIcon.isNull())
95     return QPixmap();
96
97   QImage aResult(theIcon);
98   for ( int i = 0; i < anIcon.width(); i++ )
99   {
100     for ( int j = 0; j < anIcon.height(); j++ )
101     {
102       QRgb anRgb = anIcon.pixel( i, j );
103       QColor aPixelColor(qRed(anRgb), qGreen(anRgb), qBlue(anRgb),
104                          qAlpha( aResult.pixel( i, j ) ));
105
106       QColor aLighterColor = aPixelColor.lighter(theLighterValue);
107       aResult.setPixel(i, j, qRgba( aLighterColor.red(), aLighterColor.green(),
108                                     aLighterColor.blue(), aLighterColor.alpha() ) );
109     }
110   }
111   return QPixmap::fromImage(aResult);
112 }
113
114 void setSpinText(ModuleBase_ParamSpinBox* theSpin, const QString& theText)
115 {
116   if (theSpin->text() != theText) {
117     // In order to avoid extra text setting because it will
118     // reset cursor position in control
119     bool isBlocked = theSpin->blockSignals(true);
120     theSpin->setText(theText);
121     theSpin->blockSignals(isBlocked);
122   }
123 }
124
125 void setSpinValue(QDoubleSpinBox* theSpin, double theValue)
126 {
127   bool isBlocked = theSpin->blockSignals(true);
128   theSpin->setValue(theValue);
129   theSpin->blockSignals(isBlocked);
130 }
131
132 void setSpinValue(ModuleBase_ParamSpinBox* theSpin, double theValue)
133 {
134   bool isBlocked = theSpin->blockSignals(true);
135   theSpin->setValue(theValue);
136   theSpin->blockSignals(isBlocked);
137 }
138
139 QString objectInfo(const ObjectPtr& theObj, const bool isUseAttributesInfo)
140 {
141   QString aFeatureStr = "feature";
142   if (!theObj.get())
143     return aFeatureStr;
144
145   std::ostringstream aPtrStr;
146   aPtrStr << "[" << theObj.get() << "]";
147
148   ResultPtr aRes = std::dynamic_pointer_cast<ModelAPI_Result>(theObj);
149   FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theObj);
150   if(aRes.get()) {
151     aFeatureStr.append(QString("(result%1)").arg(aPtrStr.str().c_str()).toStdString() .c_str());
152     if (aRes->isDisabled())
153       aFeatureStr.append("[disabled]");
154     if (aRes->isConcealed())
155       aFeatureStr.append("[concealed]");
156
157     aFeature = ModelAPI_Feature::feature(aRes);
158   }
159   else
160     aFeatureStr.append(aPtrStr.str().c_str());
161
162   if (aFeature.get()) {
163     aFeatureStr.append(QString(": %1").arg(aFeature->getKind().c_str()).toStdString().c_str());
164     if (aFeature->data()->isValid()) {
165       aFeatureStr.append(QString(", name=%1").arg(aFeature->data()->name().c_str()).toStdString()
166                                                                                        .c_str());
167     }
168     if (isUseAttributesInfo) {
169       std::list<AttributePtr> anAttrs = aFeature->data()->attributes("");
170       std::list<AttributePtr>::const_iterator anIt = anAttrs.begin(), aLast = anAttrs.end();
171       QStringList aValues;
172       for(; anIt != aLast; anIt++) {
173         AttributePtr anAttr = *anIt;
174         QString aValue = "not defined";
175         std::string aType = anAttr->attributeType();
176         if (aType == GeomDataAPI_Point2D::typeId()) {
177           std::shared_ptr<GeomDataAPI_Point2D> aPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
178                                                                                          anAttr);
179           if (aPoint.get())
180             aValue = QString("(%1, %2)").arg(aPoint->x()).arg(aPoint->y());
181         }
182         else if (aType == ModelAPI_AttributeRefAttr::typeId()) {
183         }
184
185         aValues.push_back(QString("%1: %2").arg(anAttr->id().c_str()).arg(aValue).toStdString().c_str());
186       }
187       if (!aValues.empty())
188         aFeatureStr.append(QString(", attributes: %1").arg(aValues.join(", ").toStdString().c_str()));
189     }
190   }
191
192   return aFeatureStr;
193 }
194
195 typedef QMap<QString, TopAbs_ShapeEnum> ShapeTypes;
196 static ShapeTypes MyShapeTypes;
197
198 TopAbs_ShapeEnum shapeType(const QString& theType)
199 {
200   if (MyShapeTypes.count() == 0) {
201     MyShapeTypes["face"] = TopAbs_FACE;
202     MyShapeTypes["faces"] = TopAbs_FACE;
203     MyShapeTypes["vertex"] = TopAbs_VERTEX;
204     MyShapeTypes["vertices"] = TopAbs_VERTEX;
205     MyShapeTypes["wire"] = TopAbs_WIRE;
206     MyShapeTypes["edge"] = TopAbs_EDGE;
207     MyShapeTypes["edges"] = TopAbs_EDGE;
208     MyShapeTypes["shell"] = TopAbs_SHELL;
209     MyShapeTypes["solid"] = TopAbs_SOLID;
210     MyShapeTypes["solids"] = TopAbs_SOLID;
211   }
212   QString aType = theType.toLower();
213   if (MyShapeTypes.contains(aType))
214     return MyShapeTypes[aType];
215   Events_Error::send("Shape type defined in XML is not implemented!");
216   return TopAbs_SHAPE;
217 }
218
219 void checkObjects(const QObjectPtrList& theObjects, bool& hasResult, bool& hasFeature, bool& hasParameter)
220 {
221   hasResult = false;
222   hasFeature = false;
223   hasParameter = false;
224   foreach(ObjectPtr aObj, theObjects) {
225     FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aObj);
226     ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(aObj);
227     ResultParameterPtr aConstruction = std::dynamic_pointer_cast<ModelAPI_ResultParameter>(aResult);
228
229     hasResult = (aResult.get() != NULL);
230     hasFeature = (aFeature.get() != NULL);
231     hasParameter = (aConstruction.get() != NULL);
232     if (hasFeature && hasResult  && hasParameter)
233       break;
234   }
235 }
236
237
238 }
239
240