1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
3 // File: ModuleBase_Tools.cpp
4 // Created: 11 July 2014
5 // Author: Vitaly Smetannikov
7 #include "ModuleBase_Tools.h"
8 #include <ModuleBase_ParamSpinBox.h>
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>
16 #include <GeomDataAPI_Point2D.h>
17 #include <Events_Error.h>
23 #include <QDoubleSpinBox>
27 namespace ModuleBase_Tools {
29 //******************************************************************
31 //******************************************************************
33 void adjustMargins(QWidget* theWidget)
37 adjustMargins(theWidget->layout());
40 void adjustMargins(QLayout* theLayout)
44 theLayout->setContentsMargins(2, 5, 2, 5);
45 theLayout->setSpacing(4);
48 void zeroMargins(QWidget* theWidget)
52 zeroMargins(theWidget->layout());
55 void zeroMargins(QLayout* theLayout)
59 theLayout->setContentsMargins(0, 0, 0, 0);
60 theLayout->setSpacing(5);
63 QPixmap composite(const QString& theAdditionalIcon, const QString& theIcon)
65 QImage anIcon(theIcon);
66 QImage anAditional(theAdditionalIcon);
71 int anAddWidth = anAditional.width();
72 int anAddHeight = anAditional.height();
74 int aWidth = anIcon.width();
75 int aHeight = anIcon.height();
77 int aStartWidthPos = aWidth - anAddWidth - 1;
78 int aStartHeightPos = aHeight - anAddHeight - 1;
80 for (int i = 0; i < anAddWidth && i + aStartWidthPos < aWidth; i++)
82 for (int j = 0; j < anAddHeight && j + aStartHeightPos < aHeight; j++)
84 if (qAlpha(anAditional.pixel(i, j)) > 0)
85 anIcon.setPixel(i + aStartWidthPos, j + aStartHeightPos, anAditional.pixel(i, j));
88 return QPixmap::fromImage(anIcon);
91 QPixmap lighter(const QString& theIcon, const int theLighterValue)
93 QImage anIcon(theIcon);
97 QImage aResult(theIcon);
98 for ( int i = 0; i < anIcon.width(); i++ )
100 for ( int j = 0; j < anIcon.height(); j++ )
102 QRgb anRgb = anIcon.pixel( i, j );
103 QColor aPixelColor(qRed(anRgb), qGreen(anRgb), qBlue(anRgb),
104 qAlpha( aResult.pixel( i, j ) ));
106 QColor aLighterColor = aPixelColor.lighter(theLighterValue);
107 aResult.setPixel(i, j, qRgba( aLighterColor.red(), aLighterColor.green(),
108 aLighterColor.blue(), aLighterColor.alpha() ) );
111 return QPixmap::fromImage(aResult);
114 void setSpinText(ModuleBase_ParamSpinBox* theSpin, const QString& theText)
116 bool isBlocked = theSpin->blockSignals(true);
117 theSpin->setText(theText);
118 theSpin->blockSignals(isBlocked);
121 void setSpinValue(QDoubleSpinBox* theSpin, double theValue)
123 bool isBlocked = theSpin->blockSignals(true);
124 theSpin->setValue(theValue);
125 theSpin->blockSignals(isBlocked);
128 void setSpinValue(ModuleBase_ParamSpinBox* theSpin, double theValue)
130 bool isBlocked = theSpin->blockSignals(true);
131 theSpin->setValue(theValue);
132 theSpin->blockSignals(isBlocked);
135 QString objectInfo(const ObjectPtr& theObj, const bool isUseAttributesInfo)
137 QString aFeatureStr = "feature";
141 std::ostringstream aPtrStr;
142 aPtrStr << "[" << theObj.get() << "]";
144 ResultPtr aRes = std::dynamic_pointer_cast<ModelAPI_Result>(theObj);
145 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theObj);
147 aFeatureStr.append(QString("(result%1)").arg(aPtrStr.str().c_str()).toStdString() .c_str());
148 if (aRes->isDisabled())
149 aFeatureStr.append("[disabled]");
150 if (aRes->isConcealed())
151 aFeatureStr.append("[concealed]");
153 aFeature = ModelAPI_Feature::feature(aRes);
156 aFeatureStr.append(aPtrStr.str().c_str());
158 if (aFeature.get()) {
159 aFeatureStr.append(QString(": %1").arg(aFeature->getKind().c_str()).toStdString().c_str());
160 if (aFeature->data()->isValid()) {
161 aFeatureStr.append(QString(", name=%1").arg(aFeature->data()->name().c_str()).toStdString()
164 if (isUseAttributesInfo) {
165 std::list<AttributePtr> anAttrs = aFeature->data()->attributes("");
166 std::list<AttributePtr>::const_iterator anIt = anAttrs.begin(), aLast = anAttrs.end();
168 for(; anIt != aLast; anIt++) {
169 AttributePtr anAttr = *anIt;
170 QString aValue = "not defined";
171 std::string aType = anAttr->attributeType();
172 if (aType == GeomDataAPI_Point2D::typeId()) {
173 std::shared_ptr<GeomDataAPI_Point2D> aPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
176 aValue = QString("(%1, %2)").arg(aPoint->x()).arg(aPoint->y());
178 else if (aType == ModelAPI_AttributeRefAttr::typeId()) {
181 aValues.push_back(QString("%1: %2").arg(anAttr->id().c_str()).arg(aValue).toStdString().c_str());
183 if (!aValues.empty())
184 aFeatureStr.append(QString(", attributes: %1").arg(aValues.join(", ").toStdString().c_str()));
191 typedef QMap<QString, TopAbs_ShapeEnum> ShapeTypes;
192 static ShapeTypes MyShapeTypes;
194 TopAbs_ShapeEnum shapeType(const QString& theType)
196 if (MyShapeTypes.count() == 0) {
197 MyShapeTypes["face"] = TopAbs_FACE;
198 MyShapeTypes["faces"] = TopAbs_FACE;
199 MyShapeTypes["vertex"] = TopAbs_VERTEX;
200 MyShapeTypes["vertices"] = TopAbs_VERTEX;
201 MyShapeTypes["wire"] = TopAbs_WIRE;
202 MyShapeTypes["edge"] = TopAbs_EDGE;
203 MyShapeTypes["edges"] = TopAbs_EDGE;
204 MyShapeTypes["shell"] = TopAbs_SHELL;
205 MyShapeTypes["solid"] = TopAbs_SOLID;
206 MyShapeTypes["solids"] = TopAbs_SOLID;
208 QString aType = theType.toLower();
209 if (MyShapeTypes.contains(aType))
210 return MyShapeTypes[aType];
211 Events_Error::send("Shape type defined in XML is not implemented!");
215 void checkObjects(const QObjectPtrList& theObjects, bool& hasResult, bool& hasFeature, bool& hasParameter)
219 hasParameter = false;
220 foreach(ObjectPtr aObj, theObjects) {
221 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aObj);
222 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(aObj);
223 ResultParameterPtr aConstruction = std::dynamic_pointer_cast<ModelAPI_ResultParameter>(aResult);
225 hasResult = (aResult.get() != NULL);
226 hasFeature = (aFeature.get() != NULL);
227 hasParameter = (aConstruction.get() != NULL);
228 if (hasFeature && hasResult && hasParameter)