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"
9 #include <ModelAPI_Result.h>
10 #include <ModelAPI_Data.h>
11 #include <ModelAPI_Attribute.h>
12 #include <ModelAPI_AttributeRefAttr.h>
14 #include <GeomDataAPI_Point2D.h>
15 #include <Events_Error.h>
21 #include <QDoubleSpinBox>
23 namespace ModuleBase_Tools {
25 //******************************************************************
27 //******************************************************************
29 void adjustMargins(QWidget* theWidget)
33 adjustMargins(theWidget->layout());
36 void adjustMargins(QLayout* theLayout)
40 theLayout->setContentsMargins(2, 5, 2, 5);
41 theLayout->setSpacing(4);
44 void zeroMargins(QWidget* theWidget)
48 zeroMargins(theWidget->layout());
51 void zeroMargins(QLayout* theLayout)
55 theLayout->setContentsMargins(0, 0, 0, 0);
56 theLayout->setSpacing(5);
59 QPixmap composite(const QString& theAdditionalIcon, const QString& theIcon)
61 QImage anIcon(theIcon);
62 QImage anAditional(theAdditionalIcon);
67 int anAddWidth = anAditional.width();
68 int anAddHeight = anAditional.height();
70 int aWidth = anIcon.width();
71 int aHeight = anIcon.height();
73 int aStartWidthPos = aWidth - anAddWidth - 1;
74 int aStartHeightPos = aHeight - anAddHeight - 1;
76 for (int i = 0; i < anAddWidth && i + aStartWidthPos < aWidth; i++)
78 for (int j = 0; j < anAddHeight && j + aStartHeightPos < aHeight; j++)
80 if (qAlpha(anAditional.pixel(i, j)) > 0)
81 anIcon.setPixel(i + aStartWidthPos, j + aStartHeightPos, anAditional.pixel(i, j));
84 return QPixmap::fromImage(anIcon);
87 QPixmap lighter(const QString& theIcon, const int theLighterValue)
89 QImage anIcon(theIcon);
93 QImage aResult(theIcon);
94 for ( int i = 0; i < anIcon.width(); i++ )
96 for ( int j = 0; j < anIcon.height(); j++ )
98 QRgb anRgb = anIcon.pixel( i, j );
99 QColor aPixelColor(qRed(anRgb), qGreen(anRgb), qBlue(anRgb),
100 qAlpha( aResult.pixel( i, j ) ));
102 QColor aLighterColor = aPixelColor.lighter(theLighterValue);
103 aResult.setPixel(i, j, qRgba( aLighterColor.red(), aLighterColor.green(),
104 aLighterColor.blue(), aLighterColor.alpha() ) );
107 return QPixmap::fromImage(aResult);
110 void setSpinValue(QDoubleSpinBox* theSpin, double theValue)
112 bool isBlocked = theSpin->blockSignals(true);
113 theSpin->setValue(theValue);
114 theSpin->blockSignals(isBlocked);
117 QString objectInfo(const ObjectPtr& theObj, const bool isUseAttributesInfo)
119 ResultPtr aRes = std::dynamic_pointer_cast<ModelAPI_Result>(theObj);
120 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theObj);
121 QString aFeatureStr = "feature";
123 aFeatureStr.append("(Result)");
124 aFeature = ModelAPI_Feature::feature(aRes);
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()
131 if (isUseAttributesInfo) {
132 std::list<AttributePtr> anAttrs = aFeature->data()->attributes("");
133 std::list<AttributePtr>::const_iterator anIt = anAttrs.begin(), aLast = anAttrs.end();
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>(
143 aValue = QString("(%1, %2)").arg(aPoint->x()).arg(aPoint->y());
145 else if (aType == ModelAPI_AttributeRefAttr::typeId()) {
148 aValues.push_back(QString("%1: %2").arg(anAttr->id().c_str()).arg(aValue).toStdString().c_str());
150 if (!aValues.empty())
151 aFeatureStr.append(QString(", attributes: %1").arg(aValues.join(", ").toStdString().c_str()));
158 typedef QMap<QString, TopAbs_ShapeEnum> ShapeTypes;
159 static ShapeTypes MyShapeTypes;
161 TopAbs_ShapeEnum shapeType(const QString& theType)
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;
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!");