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>
25 namespace ModuleBase_Tools {
27 //******************************************************************
29 //******************************************************************
31 void adjustMargins(QWidget* theWidget)
35 adjustMargins(theWidget->layout());
38 void adjustMargins(QLayout* theLayout)
42 theLayout->setContentsMargins(2, 5, 2, 5);
43 theLayout->setSpacing(4);
46 void zeroMargins(QWidget* theWidget)
50 zeroMargins(theWidget->layout());
53 void zeroMargins(QLayout* theLayout)
57 theLayout->setContentsMargins(0, 0, 0, 0);
58 theLayout->setSpacing(5);
61 QPixmap composite(const QString& theAdditionalIcon, const QString& theIcon)
63 QImage anIcon(theIcon);
64 QImage anAditional(theAdditionalIcon);
69 int anAddWidth = anAditional.width();
70 int anAddHeight = anAditional.height();
72 int aWidth = anIcon.width();
73 int aHeight = anIcon.height();
75 int aStartWidthPos = aWidth - anAddWidth - 1;
76 int aStartHeightPos = aHeight - anAddHeight - 1;
78 for (int i = 0; i < anAddWidth && i + aStartWidthPos < aWidth; i++)
80 for (int j = 0; j < anAddHeight && j + aStartHeightPos < aHeight; j++)
82 if (qAlpha(anAditional.pixel(i, j)) > 0)
83 anIcon.setPixel(i + aStartWidthPos, j + aStartHeightPos, anAditional.pixel(i, j));
86 return QPixmap::fromImage(anIcon);
89 QPixmap lighter(const QString& theIcon, const int theLighterValue)
91 QImage anIcon(theIcon);
95 QImage aResult(theIcon);
96 for ( int i = 0; i < anIcon.width(); i++ )
98 for ( int j = 0; j < anIcon.height(); j++ )
100 QRgb anRgb = anIcon.pixel( i, j );
101 QColor aPixelColor(qRed(anRgb), qGreen(anRgb), qBlue(anRgb),
102 qAlpha( aResult.pixel( i, j ) ));
104 QColor aLighterColor = aPixelColor.lighter(theLighterValue);
105 aResult.setPixel(i, j, qRgba( aLighterColor.red(), aLighterColor.green(),
106 aLighterColor.blue(), aLighterColor.alpha() ) );
109 return QPixmap::fromImage(aResult);
112 void setSpinText(ModuleBase_ParamSpinBox* theSpin, const QString& theText)
114 bool isBlocked = theSpin->blockSignals(true);
115 theSpin->setText(theText);
116 theSpin->blockSignals(isBlocked);
119 void setSpinValue(QDoubleSpinBox* theSpin, double theValue)
121 bool isBlocked = theSpin->blockSignals(true);
122 theSpin->setValue(theValue);
123 theSpin->blockSignals(isBlocked);
126 QString objectInfo(const ObjectPtr& theObj, const bool isUseAttributesInfo)
128 ResultPtr aRes = std::dynamic_pointer_cast<ModelAPI_Result>(theObj);
129 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theObj);
130 QString aFeatureStr = "feature";
132 aFeatureStr.append("(Result)");
133 aFeature = ModelAPI_Feature::feature(aRes);
135 if (aFeature.get()) {
136 aFeatureStr.append(QString(": %1").arg(aFeature->getKind().c_str()).toStdString().c_str());
137 if (aFeature->data().get() && aFeature->data()->isValid())
138 aFeatureStr.append(QString(", name=%1").arg(aFeature->data()->name().c_str()).toStdString()
140 if (isUseAttributesInfo) {
141 std::list<AttributePtr> anAttrs = aFeature->data()->attributes("");
142 std::list<AttributePtr>::const_iterator anIt = anAttrs.begin(), aLast = anAttrs.end();
144 for(; anIt != aLast; anIt++) {
145 AttributePtr anAttr = *anIt;
146 QString aValue = "not defined";
147 std::string aType = anAttr->attributeType();
148 if (aType == GeomDataAPI_Point2D::typeId()) {
149 std::shared_ptr<GeomDataAPI_Point2D> aPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
152 aValue = QString("(%1, %2)").arg(aPoint->x()).arg(aPoint->y());
154 else if (aType == ModelAPI_AttributeRefAttr::typeId()) {
157 aValues.push_back(QString("%1: %2").arg(anAttr->id().c_str()).arg(aValue).toStdString().c_str());
159 if (!aValues.empty())
160 aFeatureStr.append(QString(", attributes: %1").arg(aValues.join(", ").toStdString().c_str()));
167 typedef QMap<QString, TopAbs_ShapeEnum> ShapeTypes;
168 static ShapeTypes MyShapeTypes;
170 TopAbs_ShapeEnum shapeType(const QString& theType)
172 if (MyShapeTypes.count() == 0) {
173 MyShapeTypes["face"] = TopAbs_FACE;
174 MyShapeTypes["faces"] = TopAbs_FACE;
175 MyShapeTypes["vertex"] = TopAbs_VERTEX;
176 MyShapeTypes["vertices"] = TopAbs_VERTEX;
177 MyShapeTypes["wire"] = TopAbs_WIRE;
178 MyShapeTypes["edge"] = TopAbs_EDGE;
179 MyShapeTypes["edges"] = TopAbs_EDGE;
180 MyShapeTypes["shell"] = TopAbs_SHELL;
181 MyShapeTypes["solid"] = TopAbs_SOLID;
182 MyShapeTypes["solids"] = TopAbs_SOLID;
184 QString aType = theType.toLower();
185 if (MyShapeTypes.contains(aType))
186 return MyShapeTypes[aType];
187 Events_Error::send("Shape type defined in XML is not implemented!");
191 void checkObjects(const QObjectPtrList& theObjects, bool& hasResult, bool& hasFeature, bool& hasParameter)
195 hasParameter = false;
196 foreach(ObjectPtr aObj, theObjects) {
197 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aObj);
198 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(aObj);
199 ResultParameterPtr aConstruction = std::dynamic_pointer_cast<ModelAPI_ResultParameter>(aResult);
201 hasResult = (aResult.get() != NULL);
202 hasFeature = (aFeature.get() != NULL);
203 hasParameter = (aConstruction.get() != NULL);
204 if (hasFeature && hasResult && hasParameter)