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>
15 #include <ModelAPI_Tools.h>
17 #include <GeomDataAPI_Point2D.h>
18 #include <Events_Error.h>
24 #include <QDoubleSpinBox>
28 namespace ModuleBase_Tools {
30 //******************************************************************
32 //******************************************************************
34 void adjustMargins(QWidget* theWidget)
38 adjustMargins(theWidget->layout());
41 void adjustMargins(QLayout* theLayout)
45 theLayout->setContentsMargins(2, 5, 2, 5);
46 theLayout->setSpacing(4);
49 void zeroMargins(QWidget* theWidget)
53 zeroMargins(theWidget->layout());
56 void zeroMargins(QLayout* theLayout)
60 theLayout->setContentsMargins(0, 0, 0, 0);
61 theLayout->setSpacing(5);
64 QPixmap composite(const QString& theAdditionalIcon, const QString& theIcon)
66 QImage anIcon(theIcon);
67 QImage anAditional(theAdditionalIcon);
72 int anAddWidth = anAditional.width();
73 int anAddHeight = anAditional.height();
75 int aWidth = anIcon.width();
76 int aHeight = anIcon.height();
78 int aStartWidthPos = aWidth - anAddWidth - 1;
79 int aStartHeightPos = aHeight - anAddHeight - 1;
81 for (int i = 0; i < anAddWidth && i + aStartWidthPos < aWidth; i++)
83 for (int j = 0; j < anAddHeight && j + aStartHeightPos < aHeight; j++)
85 if (qAlpha(anAditional.pixel(i, j)) > 0)
86 anIcon.setPixel(i + aStartWidthPos, j + aStartHeightPos, anAditional.pixel(i, j));
89 return QPixmap::fromImage(anIcon);
92 QPixmap lighter(const QString& theIcon, const int theLighterValue)
94 QImage anIcon(theIcon);
98 QImage aResult(theIcon);
99 for ( int i = 0; i < anIcon.width(); i++ )
101 for ( int j = 0; j < anIcon.height(); j++ )
103 QRgb anRgb = anIcon.pixel( i, j );
104 QColor aPixelColor(qRed(anRgb), qGreen(anRgb), qBlue(anRgb),
105 qAlpha( aResult.pixel( i, j ) ));
107 QColor aLighterColor = aPixelColor.lighter(theLighterValue);
108 aResult.setPixel(i, j, qRgba( aLighterColor.red(), aLighterColor.green(),
109 aLighterColor.blue(), aLighterColor.alpha() ) );
112 return QPixmap::fromImage(aResult);
115 void setSpinText(ModuleBase_ParamSpinBox* theSpin, const QString& theText)
117 if (theSpin->text() != theText) {
118 // In order to avoid extra text setting because it will
119 // reset cursor position in control
120 bool isBlocked = theSpin->blockSignals(true);
121 theSpin->setText(theText);
122 theSpin->blockSignals(isBlocked);
126 void setSpinValue(QDoubleSpinBox* theSpin, double theValue)
128 bool isBlocked = theSpin->blockSignals(true);
129 theSpin->setValue(theValue);
130 theSpin->blockSignals(isBlocked);
133 void setSpinValue(ModuleBase_ParamSpinBox* theSpin, double theValue)
135 bool isBlocked = theSpin->blockSignals(true);
136 theSpin->setValue(theValue);
137 theSpin->blockSignals(isBlocked);
140 QString objectInfo(const ObjectPtr& theObj, const bool isUseAttributesInfo)
142 QString aFeatureStr = "feature";
146 std::ostringstream aPtrStr;
147 aPtrStr << "[" << theObj.get() << "]";
149 ResultPtr aRes = std::dynamic_pointer_cast<ModelAPI_Result>(theObj);
150 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theObj);
152 aFeatureStr.append(QString("(result%1)").arg(aPtrStr.str().c_str()).toStdString() .c_str());
153 if (aRes->isDisabled())
154 aFeatureStr.append("[disabled]");
155 if (aRes->isConcealed())
156 aFeatureStr.append("[concealed]");
158 aFeature = ModelAPI_Feature::feature(aRes);
161 aFeatureStr.append(aPtrStr.str().c_str());
163 if (aFeature.get()) {
164 aFeatureStr.append(QString(": %1").arg(aFeature->getKind().c_str()).toStdString().c_str());
165 if (aFeature->data()->isValid()) {
166 aFeatureStr.append(QString(", name=%1").arg(aFeature->data()->name().c_str()).toStdString()
169 if (isUseAttributesInfo) {
170 std::list<AttributePtr> anAttrs = aFeature->data()->attributes("");
171 std::list<AttributePtr>::const_iterator anIt = anAttrs.begin(), aLast = anAttrs.end();
173 for(; anIt != aLast; anIt++) {
174 AttributePtr anAttr = *anIt;
175 QString aValue = "not defined";
176 std::string aType = anAttr->attributeType();
177 if (aType == GeomDataAPI_Point2D::typeId()) {
178 std::shared_ptr<GeomDataAPI_Point2D> aPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
181 aValue = QString("(%1, %2)").arg(aPoint->x()).arg(aPoint->y());
183 else if (aType == ModelAPI_AttributeRefAttr::typeId()) {
186 aValues.push_back(QString("%1: %2").arg(anAttr->id().c_str()).arg(aValue).toStdString().c_str());
188 if (!aValues.empty())
189 aFeatureStr.append(QString(", attributes: %1").arg(aValues.join(", ").toStdString().c_str()));
196 typedef QMap<QString, TopAbs_ShapeEnum> ShapeTypes;
197 static ShapeTypes MyShapeTypes;
199 TopAbs_ShapeEnum shapeType(const QString& theType)
201 if (MyShapeTypes.count() == 0) {
202 MyShapeTypes["face"] = TopAbs_FACE;
203 MyShapeTypes["faces"] = TopAbs_FACE;
204 MyShapeTypes["vertex"] = TopAbs_VERTEX;
205 MyShapeTypes["vertices"] = TopAbs_VERTEX;
206 MyShapeTypes["wire"] = TopAbs_WIRE;
207 MyShapeTypes["edge"] = TopAbs_EDGE;
208 MyShapeTypes["edges"] = TopAbs_EDGE;
209 MyShapeTypes["shell"] = TopAbs_SHELL;
210 MyShapeTypes["solid"] = TopAbs_SOLID;
211 MyShapeTypes["solids"] = TopAbs_SOLID;
213 QString aType = theType.toLower();
214 if (MyShapeTypes.contains(aType))
215 return MyShapeTypes[aType];
216 Events_Error::send("Shape type defined in XML is not implemented!");
220 void checkObjects(const QObjectPtrList& theObjects, bool& hasResult, bool& hasFeature, bool& hasParameter, bool& hasSubFeature)
224 hasParameter = false;
225 hasSubFeature = false;
226 foreach(ObjectPtr aObj, theObjects) {
227 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aObj);
228 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(aObj);
229 ResultParameterPtr aConstruction = std::dynamic_pointer_cast<ModelAPI_ResultParameter>(aResult);
231 hasResult = (aResult.get() != NULL);
232 hasFeature = (aFeature.get() != NULL);
233 hasParameter = (aConstruction.get() != NULL);
235 hasSubFeature = (ModelAPI_Tools::compositeOwner(aFeature) != NULL);
236 if (hasFeature && hasResult && hasParameter && hasSubFeature)