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