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>
15 #include <GeomDataAPI_Point2D.h>
16 #include <Events_Error.h>
22 #include <QDoubleSpinBox>
24 namespace ModuleBase_Tools {
26 //******************************************************************
28 //******************************************************************
30 void adjustMargins(QWidget* theWidget)
34 adjustMargins(theWidget->layout());
37 void adjustMargins(QLayout* theLayout)
41 theLayout->setContentsMargins(2, 5, 2, 5);
42 theLayout->setSpacing(4);
45 void zeroMargins(QWidget* theWidget)
49 zeroMargins(theWidget->layout());
52 void zeroMargins(QLayout* theLayout)
56 theLayout->setContentsMargins(0, 0, 0, 0);
57 theLayout->setSpacing(5);
60 QPixmap composite(const QString& theAdditionalIcon, const QString& theIcon)
62 QImage anIcon(theIcon);
63 QImage anAditional(theAdditionalIcon);
68 int anAddWidth = anAditional.width();
69 int anAddHeight = anAditional.height();
71 int aWidth = anIcon.width();
72 int aHeight = anIcon.height();
74 int aStartWidthPos = aWidth - anAddWidth - 1;
75 int aStartHeightPos = aHeight - anAddHeight - 1;
77 for (int i = 0; i < anAddWidth && i + aStartWidthPos < aWidth; i++)
79 for (int j = 0; j < anAddHeight && j + aStartHeightPos < aHeight; j++)
81 if (qAlpha(anAditional.pixel(i, j)) > 0)
82 anIcon.setPixel(i + aStartWidthPos, j + aStartHeightPos, anAditional.pixel(i, j));
85 return QPixmap::fromImage(anIcon);
88 QPixmap lighter(const QString& theIcon, const int theLighterValue)
90 QImage anIcon(theIcon);
94 QImage aResult(theIcon);
95 for ( int i = 0; i < anIcon.width(); i++ )
97 for ( int j = 0; j < anIcon.height(); j++ )
99 QRgb anRgb = anIcon.pixel( i, j );
100 QColor aPixelColor(qRed(anRgb), qGreen(anRgb), qBlue(anRgb),
101 qAlpha( aResult.pixel( i, j ) ));
103 QColor aLighterColor = aPixelColor.lighter(theLighterValue);
104 aResult.setPixel(i, j, qRgba( aLighterColor.red(), aLighterColor.green(),
105 aLighterColor.blue(), aLighterColor.alpha() ) );
108 return QPixmap::fromImage(aResult);
111 void setSpinText(ModuleBase_ParamSpinBox* theSpin, const QString& theText)
113 bool isBlocked = theSpin->blockSignals(true);
114 theSpin->setText(theText);
115 theSpin->blockSignals(isBlocked);
118 void setSpinValue(QDoubleSpinBox* theSpin, double theValue)
120 bool isBlocked = theSpin->blockSignals(true);
121 theSpin->setValue(theValue);
122 theSpin->blockSignals(isBlocked);
125 QString objectInfo(const ObjectPtr& theObj, const bool isUseAttributesInfo)
127 ResultPtr aRes = std::dynamic_pointer_cast<ModelAPI_Result>(theObj);
128 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theObj);
129 QString aFeatureStr = "feature";
131 aFeatureStr.append("(Result)");
132 aFeature = ModelAPI_Feature::feature(aRes);
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()
139 if (isUseAttributesInfo) {
140 std::list<AttributePtr> anAttrs = aFeature->data()->attributes("");
141 std::list<AttributePtr>::const_iterator anIt = anAttrs.begin(), aLast = anAttrs.end();
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>(
151 aValue = QString("(%1, %2)").arg(aPoint->x()).arg(aPoint->y());
153 else if (aType == ModelAPI_AttributeRefAttr::typeId()) {
156 aValues.push_back(QString("%1: %2").arg(anAttr->id().c_str()).arg(aValue).toStdString().c_str());
158 if (!aValues.empty())
159 aFeatureStr.append(QString(", attributes: %1").arg(aValues.join(", ").toStdString().c_str()));
166 typedef QMap<QString, TopAbs_ShapeEnum> ShapeTypes;
167 static ShapeTypes MyShapeTypes;
169 TopAbs_ShapeEnum shapeType(const QString& theType)
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;
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!");