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_Attribute.h>
11 #include <ModelAPI_AttributeRefAttr.h>
12 #include <ModelAPI_AttributeReference.h>
13 #include <ModelAPI_AttributeSelection.h>
14 #include <ModelAPI_Data.h>
15 #include <ModelAPI_Result.h>
16 #include <ModelAPI_ResultCompSolid.h>
17 #include <ModelAPI_ResultParameter.h>
18 #include <ModelAPI_Tools.h>
20 #include <TopoDS_Iterator.hxx>
22 #include <GeomDataAPI_Point2D.h>
23 #include <Events_Error.h>
25 #include <Config_PropManager.h>
31 #include <QDoubleSpinBox>
32 #include <QGraphicsDropShadowEffect>
37 const double tolerance = 1e-7;
39 //#define DEBUG_ACTIVATE_WINDOW
40 //#define DEBUG_SET_FOCUS
42 namespace ModuleBase_Tools {
44 //******************************************************************
46 //******************************************************************
48 void adjustMargins(QWidget* theWidget)
52 adjustMargins(theWidget->layout());
55 void adjustMargins(QLayout* theLayout)
59 theLayout->setContentsMargins(2, 5, 2, 5);
60 theLayout->setSpacing(4);
63 void zeroMargins(QWidget* theWidget)
67 zeroMargins(theWidget->layout());
70 void zeroMargins(QLayout* theLayout)
74 theLayout->setContentsMargins(0, 0, 0, 0);
75 theLayout->setSpacing(5);
78 void activateWindow(QWidget* theWidget, const QString& theInfo)
80 theWidget->activateWindow();
82 #ifdef DEBUG_ACTIVATE_WINDOW
83 qDebug(QString("activateWindow: %1").arg(theInfo).toStdString().c_str());
87 void setFocus(QWidget* theWidget, const QString& theInfo)
89 theWidget->setFocus();
91 #ifdef DEBUG_SET_FOCUS
92 qDebug(QString("setFocus: %1").arg(theInfo).toStdString().c_str());
96 void setShadowEffect(QWidget* theWidget, const bool isSetEffect)
99 QGraphicsDropShadowEffect* aGlowEffect = new QGraphicsDropShadowEffect();
100 aGlowEffect->setOffset(.0);
101 aGlowEffect->setBlurRadius(10.0);
102 aGlowEffect->setColor(QColor(0, 170, 255)); // Light-blue color, #00AAFF
103 theWidget->setGraphicsEffect(aGlowEffect);
106 QGraphicsEffect* anEffect = theWidget->graphicsEffect();
108 anEffect->deleteLater();
109 theWidget->setGraphicsEffect(NULL);
113 QPixmap composite(const QString& theAdditionalIcon, const QString& theIcon)
115 QImage anIcon(theIcon);
116 QImage anAditional(theAdditionalIcon);
121 int anAddWidth = anAditional.width();
122 int anAddHeight = anAditional.height();
124 int aWidth = anIcon.width();
125 int aHeight = anIcon.height();
127 int aStartWidthPos = aWidth - anAddWidth - 1;
128 int aStartHeightPos = aHeight - anAddHeight - 1;
130 for (int i = 0; i < anAddWidth && i + aStartWidthPos < aWidth; i++)
132 for (int j = 0; j < anAddHeight && j + aStartHeightPos < aHeight; j++)
134 if (qAlpha(anAditional.pixel(i, j)) > 0)
135 anIcon.setPixel(i + aStartWidthPos, j + aStartHeightPos, anAditional.pixel(i, j));
138 return QPixmap::fromImage(anIcon);
141 QPixmap lighter(const QString& theIcon, const int theLighterValue)
143 QImage anIcon(theIcon);
147 QImage aResult(theIcon);
148 for ( int i = 0; i < anIcon.width(); i++ )
150 for ( int j = 0; j < anIcon.height(); j++ )
152 QRgb anRgb = anIcon.pixel( i, j );
153 QColor aPixelColor(qRed(anRgb), qGreen(anRgb), qBlue(anRgb),
154 qAlpha( aResult.pixel( i, j ) ));
156 QColor aLighterColor = aPixelColor.lighter(theLighterValue);
157 aResult.setPixel(i, j, qRgba( aLighterColor.red(), aLighterColor.green(),
158 aLighterColor.blue(), aLighterColor.alpha() ) );
161 return QPixmap::fromImage(aResult);
164 void setSpinText(ModuleBase_ParamSpinBox* theSpin, const QString& theText)
166 if (theSpin->text() == theText)
168 // In order to avoid extra text setting because it will
169 // reset cursor position in control
170 bool isBlocked = theSpin->blockSignals(true);
171 theSpin->setText(theText);
172 theSpin->blockSignals(isBlocked);
175 void setSpinValue(QDoubleSpinBox* theSpin, double theValue)
177 if (fabs(theSpin->value() - theValue) < tolerance)
179 bool isBlocked = theSpin->blockSignals(true);
180 theSpin->setValue(theValue);
181 theSpin->blockSignals(isBlocked);
184 void setSpinValue(ModuleBase_ParamSpinBox* theSpin, double theValue)
186 if (fabs(theSpin->value() - theValue) < tolerance)
188 bool isBlocked = theSpin->blockSignals(true);
189 theSpin->setValue(theValue);
190 theSpin->blockSignals(isBlocked);
193 QString objectInfo(const ObjectPtr& theObj, const bool isUseAttributesInfo)
195 QString aFeatureStr = "feature";
199 std::ostringstream aPtrStr;
200 aPtrStr << "[" << theObj.get() << "]";
202 ResultPtr aRes = std::dynamic_pointer_cast<ModelAPI_Result>(theObj);
203 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theObj);
205 aFeatureStr.append(QString("(result%1)").arg(aPtrStr.str().c_str()).toStdString() .c_str());
206 if (aRes->isDisabled())
207 aFeatureStr.append("[disabled]");
208 if (aRes->isConcealed())
209 aFeatureStr.append("[concealed]");
210 if (ModelAPI_Tools::hasSubResults(aRes))
211 aFeatureStr.append("[hasSubResults]");
213 aFeature = ModelAPI_Feature::feature(aRes);
216 aFeatureStr.append(aPtrStr.str().c_str());
218 if (aFeature.get()) {
219 aFeatureStr.append(QString(": %1").arg(aFeature->getKind().c_str()).toStdString().c_str());
220 if (aFeature->data()->isValid()) {
221 aFeatureStr.append(QString(", name=%1").arg(aFeature->data()->name().c_str()).toStdString()
224 if (isUseAttributesInfo) {
225 std::list<AttributePtr> anAttrs = aFeature->data()->attributes("");
226 std::list<AttributePtr>::const_iterator anIt = anAttrs.begin(), aLast = anAttrs.end();
228 for(; anIt != aLast; anIt++) {
229 AttributePtr anAttr = *anIt;
230 QString aValue = "not defined";
231 std::string aType = anAttr->attributeType();
232 if (aType == GeomDataAPI_Point2D::typeId()) {
233 std::shared_ptr<GeomDataAPI_Point2D> aPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
236 aValue = QString("(%1, %2)").arg(aPoint->x()).arg(aPoint->y());
238 else if (aType == ModelAPI_AttributeRefAttr::typeId()) {
241 aValues.push_back(QString("%1: %2").arg(anAttr->id().c_str()).arg(aValue).toStdString().c_str());
243 if (!aValues.empty())
244 aFeatureStr.append(QString(", attributes: %1").arg(aValues.join(", ").toStdString().c_str()));
251 typedef QMap<QString, TopAbs_ShapeEnum> ShapeTypes;
252 static ShapeTypes MyShapeTypes;
254 TopAbs_ShapeEnum shapeType(const QString& theType)
256 if (MyShapeTypes.count() == 0) {
257 MyShapeTypes["face"] = TopAbs_FACE;
258 MyShapeTypes["faces"] = TopAbs_FACE;
259 MyShapeTypes["vertex"] = TopAbs_VERTEX;
260 MyShapeTypes["vertices"] = TopAbs_VERTEX;
261 MyShapeTypes["wire"] = TopAbs_WIRE;
262 MyShapeTypes["edge"] = TopAbs_EDGE;
263 MyShapeTypes["edges"] = TopAbs_EDGE;
264 MyShapeTypes["shell"] = TopAbs_SHELL;
265 MyShapeTypes["solid"] = TopAbs_SOLID;
266 MyShapeTypes["solids"] = TopAbs_SOLID;
267 MyShapeTypes["objects"] = TopAbs_SHAPE;
269 QString aType = theType.toLower();
270 if (MyShapeTypes.contains(aType))
271 return MyShapeTypes[aType];
272 Events_Error::send("Shape type defined in XML is not implemented!");
276 void checkObjects(const QObjectPtrList& theObjects, bool& hasResult, bool& hasFeature, bool& hasParameter, bool& hasSubFeature)
280 hasParameter = false;
281 hasSubFeature = false;
282 foreach(ObjectPtr aObj, theObjects) {
283 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aObj);
284 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(aObj);
285 ResultParameterPtr aConstruction = std::dynamic_pointer_cast<ModelAPI_ResultParameter>(aResult);
287 hasResult |= (aResult.get() != NULL);
288 hasFeature |= (aFeature.get() != NULL);
289 hasParameter |= (aConstruction.get() != NULL);
291 hasSubFeature |= (ModelAPI_Tools::compositeOwner(aFeature) != NULL);
292 if (hasFeature && hasResult && hasParameter && hasSubFeature)
297 void setDefaultDeviationCoefficient(const TopoDS_Shape& theShape,
298 const Handle(Prs3d_Drawer)& theDrawer)
300 if (theShape.IsNull())
302 TopAbs_ShapeEnum aType = theShape.ShapeType();
303 if ((aType == TopAbs_EDGE) || (aType == TopAbs_WIRE))
304 theDrawer->SetDeviationCoefficient(1.e-4);
307 Quantity_Color color(const std::string& theSection,
308 const std::string& theName,
309 const std::string& theDefault)
311 std::vector<int> aColor = Config_PropManager::color(theSection, theName, theDefault);
312 return Quantity_Color(aColor[0] / 255., aColor[1] / 255., aColor[2] / 255., Quantity_TOC_RGB);
315 ObjectPtr getObject(const AttributePtr& theAttribute)
318 std::string anAttrType = theAttribute->attributeType();
319 if (anAttrType == ModelAPI_AttributeRefAttr::typeId()) {
320 AttributeRefAttrPtr anAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
321 if (anAttr != NULL && anAttr->isObject())
322 anObject = anAttr->object();
324 if (anAttrType == ModelAPI_AttributeSelection::typeId()) {
325 AttributeSelectionPtr anAttr = std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(theAttribute);
327 anObject = anAttr->context();
329 if (anAttrType == ModelAPI_AttributeReference::typeId()) {
330 AttributeReferencePtr anAttr = std::dynamic_pointer_cast<ModelAPI_AttributeReference>(theAttribute);
331 if (anAttr.get() != NULL)
332 anObject = anAttr->value();
337 TopAbs_ShapeEnum getCompoundSubType(const TopoDS_Shape& theShape)
339 TopAbs_ShapeEnum aShapeType = theShape.ShapeType();
341 // for compounds check sub-shapes: it may be compound of needed type:
342 // Booleans may produce compounds of Solids
343 if (aShapeType == TopAbs_COMPOUND) {
344 for(TopoDS_Iterator aSubs(theShape); aSubs.More(); aSubs.Next()) {
345 if (!aSubs.Value().IsNull()) {
346 TopAbs_ShapeEnum aSubType = aSubs.Value().ShapeType();
347 if (aSubType == TopAbs_COMPOUND) { // compound of compound(s)
348 aShapeType = TopAbs_COMPOUND;
351 if (aShapeType == TopAbs_COMPOUND) {
352 aShapeType = aSubType;
353 } else if (aShapeType != aSubType) { // compound of shapes of different types
354 aShapeType = TopAbs_COMPOUND;
363 } // namespace ModuleBase_Tools