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 <ModuleBase_ParamIntSpinBox.h>
10 #include <ModuleBase_ParamSpinBox.h>
12 #include <ModelAPI_Attribute.h>
13 #include <ModelAPI_AttributeRefAttr.h>
14 #include <ModelAPI_AttributeReference.h>
15 #include <ModelAPI_AttributeSelection.h>
16 #include <ModelAPI_Data.h>
17 #include <ModelAPI_Result.h>
18 #include <ModelAPI_ResultCompSolid.h>
19 #include <ModelAPI_ResultParameter.h>
20 #include <ModelAPI_Tools.h>
22 #include <TopoDS_Iterator.hxx>
24 #include <GeomDataAPI_Point2D.h>
25 #include <Events_Error.h>
27 #include <Config_PropManager.h>
33 #include <QDoubleSpinBox>
34 #include <QGraphicsDropShadowEffect>
39 const double tolerance = 1e-7;
41 //#define DEBUG_ACTIVATE_WINDOW
42 //#define DEBUG_SET_FOCUS
44 namespace ModuleBase_Tools {
46 //******************************************************************
48 //******************************************************************
50 void adjustMargins(QWidget* theWidget)
54 adjustMargins(theWidget->layout());
57 void adjustMargins(QLayout* theLayout)
61 theLayout->setContentsMargins(2, 5, 2, 5);
62 theLayout->setSpacing(4);
65 void zeroMargins(QWidget* theWidget)
69 zeroMargins(theWidget->layout());
72 void zeroMargins(QLayout* theLayout)
76 theLayout->setContentsMargins(0, 0, 0, 0);
77 theLayout->setSpacing(5);
80 void activateWindow(QWidget* theWidget, const QString& theInfo)
82 theWidget->activateWindow();
84 #ifdef DEBUG_ACTIVATE_WINDOW
85 qDebug(QString("activateWindow: %1").arg(theInfo).toStdString().c_str());
89 void setFocus(QWidget* theWidget, const QString& theInfo)
91 theWidget->setFocus();
93 #ifdef DEBUG_SET_FOCUS
94 qDebug(QString("setFocus: %1").arg(theInfo).toStdString().c_str());
98 void setShadowEffect(QWidget* theWidget, const bool isSetEffect)
101 QGraphicsDropShadowEffect* aGlowEffect = new QGraphicsDropShadowEffect();
102 aGlowEffect->setOffset(.0);
103 aGlowEffect->setBlurRadius(10.0);
104 aGlowEffect->setColor(QColor(0, 170, 255)); // Light-blue color, #00AAFF
105 theWidget->setGraphicsEffect(aGlowEffect);
108 QGraphicsEffect* anEffect = theWidget->graphicsEffect();
110 anEffect->deleteLater();
111 theWidget->setGraphicsEffect(NULL);
115 QPixmap composite(const QString& theAdditionalIcon, const QString& theIcon)
117 QImage anIcon(theIcon);
118 QImage anAditional(theAdditionalIcon);
123 int anAddWidth = anAditional.width();
124 int anAddHeight = anAditional.height();
126 int aWidth = anIcon.width();
127 int aHeight = anIcon.height();
129 int aStartWidthPos = aWidth - anAddWidth - 1;
130 int aStartHeightPos = aHeight - anAddHeight - 1;
132 for (int i = 0; i < anAddWidth && i + aStartWidthPos < aWidth; i++)
134 for (int j = 0; j < anAddHeight && j + aStartHeightPos < aHeight; j++)
136 if (qAlpha(anAditional.pixel(i, j)) > 0)
137 anIcon.setPixel(i + aStartWidthPos, j + aStartHeightPos, anAditional.pixel(i, j));
140 return QPixmap::fromImage(anIcon);
143 QPixmap lighter(const QString& theIcon, const int theLighterValue)
145 QImage anIcon(theIcon);
149 QImage aResult(theIcon);
150 for ( int i = 0; i < anIcon.width(); i++ )
152 for ( int j = 0; j < anIcon.height(); j++ )
154 QRgb anRgb = anIcon.pixel( i, j );
155 QColor aPixelColor(qRed(anRgb), qGreen(anRgb), qBlue(anRgb),
156 qAlpha( aResult.pixel( i, j ) ));
158 QColor aLighterColor = aPixelColor.lighter(theLighterValue);
159 aResult.setPixel(i, j, qRgba( aLighterColor.red(), aLighterColor.green(),
160 aLighterColor.blue(), aLighterColor.alpha() ) );
163 return QPixmap::fromImage(aResult);
166 void setSpinText(ModuleBase_ParamSpinBox* theSpin, const QString& theText)
168 if (theSpin->text() == theText)
170 // In order to avoid extra text setting because it will
171 // reset cursor position in control
172 bool isBlocked = theSpin->blockSignals(true);
173 theSpin->setText(theText);
174 theSpin->blockSignals(isBlocked);
177 void setSpinValue(QDoubleSpinBox* theSpin, double theValue)
179 if (fabs(theSpin->value() - theValue) < tolerance)
181 bool isBlocked = theSpin->blockSignals(true);
182 theSpin->setValue(theValue);
183 theSpin->blockSignals(isBlocked);
186 void setSpinValue(ModuleBase_ParamSpinBox* theSpin, double theValue)
188 if (fabs(theSpin->value() - theValue) < tolerance)
190 bool isBlocked = theSpin->blockSignals(true);
191 theSpin->setValue(theValue);
192 theSpin->blockSignals(isBlocked);
195 void setSpinText(ModuleBase_ParamIntSpinBox* theSpin, const QString& theText)
197 // In order to avoid extra text setting because it will
198 // reset cursor position in control
199 if (theSpin->text() == theText)
201 bool isBlocked = theSpin->blockSignals(true);
202 theSpin->setText(theText);
203 theSpin->blockSignals(isBlocked);
206 void setSpinValue(ModuleBase_ParamIntSpinBox* theSpin, int theValue)
208 if (theSpin->value() == theValue)
210 bool isBlocked = theSpin->blockSignals(true);
211 theSpin->setValue(theValue);
212 theSpin->blockSignals(isBlocked);
215 QString objectInfo(const ObjectPtr& theObj, const bool isUseAttributesInfo)
217 QString aFeatureStr = "feature";
221 std::ostringstream aPtrStr;
222 aPtrStr << "[" << theObj.get() << "]";
224 ResultPtr aRes = std::dynamic_pointer_cast<ModelAPI_Result>(theObj);
225 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theObj);
227 aFeatureStr.append(QString("(result%1)").arg(aPtrStr.str().c_str()).toStdString() .c_str());
228 if (aRes->isDisabled())
229 aFeatureStr.append("[disabled]");
230 if (aRes->isConcealed())
231 aFeatureStr.append("[concealed]");
232 if (ModelAPI_Tools::hasSubResults(aRes))
233 aFeatureStr.append("[hasSubResults]");
235 aFeature = ModelAPI_Feature::feature(aRes);
238 aFeatureStr.append(aPtrStr.str().c_str());
240 if (aFeature.get()) {
241 aFeatureStr.append(QString(": %1").arg(aFeature->getKind().c_str()).toStdString().c_str());
242 if (aFeature->data()->isValid()) {
243 aFeatureStr.append(QString(", name=%1").arg(aFeature->data()->name().c_str()).toStdString()
246 if (isUseAttributesInfo) {
247 std::list<AttributePtr> anAttrs = aFeature->data()->attributes("");
248 std::list<AttributePtr>::const_iterator anIt = anAttrs.begin(), aLast = anAttrs.end();
250 for(; anIt != aLast; anIt++) {
251 AttributePtr anAttr = *anIt;
252 QString aValue = "not defined";
253 std::string aType = anAttr->attributeType();
254 if (aType == GeomDataAPI_Point2D::typeId()) {
255 std::shared_ptr<GeomDataAPI_Point2D> aPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
258 aValue = QString("(%1, %2)").arg(aPoint->x()).arg(aPoint->y());
260 else if (aType == ModelAPI_AttributeRefAttr::typeId()) {
263 aValues.push_back(QString("%1: %2").arg(anAttr->id().c_str()).arg(aValue).toStdString().c_str());
265 if (!aValues.empty())
266 aFeatureStr.append(QString(", attributes: %1").arg(aValues.join(", ").toStdString().c_str()));
273 typedef QMap<QString, TopAbs_ShapeEnum> ShapeTypes;
274 static ShapeTypes MyShapeTypes;
276 TopAbs_ShapeEnum shapeType(const QString& theType)
278 if (MyShapeTypes.count() == 0) {
279 MyShapeTypes["face"] = TopAbs_FACE;
280 MyShapeTypes["faces"] = TopAbs_FACE;
281 MyShapeTypes["vertex"] = TopAbs_VERTEX;
282 MyShapeTypes["vertices"] = TopAbs_VERTEX;
283 MyShapeTypes["wire"] = TopAbs_WIRE;
284 MyShapeTypes["edge"] = TopAbs_EDGE;
285 MyShapeTypes["edges"] = TopAbs_EDGE;
286 MyShapeTypes["shell"] = TopAbs_SHELL;
287 MyShapeTypes["solid"] = TopAbs_SOLID;
288 MyShapeTypes["solids"] = TopAbs_SOLID;
289 MyShapeTypes["objects"] = TopAbs_SHAPE;
291 QString aType = theType.toLower();
292 if (MyShapeTypes.contains(aType))
293 return MyShapeTypes[aType];
294 Events_Error::send("Shape type defined in XML is not implemented!");
298 void checkObjects(const QObjectPtrList& theObjects, bool& hasResult, bool& hasFeature, bool& hasParameter, bool& hasSubFeature)
302 hasParameter = false;
303 hasSubFeature = false;
304 foreach(ObjectPtr aObj, theObjects) {
305 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aObj);
306 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(aObj);
307 ResultParameterPtr aConstruction = std::dynamic_pointer_cast<ModelAPI_ResultParameter>(aResult);
309 hasResult |= (aResult.get() != NULL);
310 hasFeature |= (aFeature.get() != NULL);
311 hasParameter |= (aConstruction.get() != NULL);
313 hasSubFeature |= (ModelAPI_Tools::compositeOwner(aFeature) != NULL);
314 if (hasFeature && hasResult && hasParameter && hasSubFeature)
319 void setDefaultDeviationCoefficient(const TopoDS_Shape& theShape,
320 const Handle(Prs3d_Drawer)& theDrawer)
322 if (theShape.IsNull())
324 TopAbs_ShapeEnum aType = theShape.ShapeType();
325 if ((aType == TopAbs_EDGE) || (aType == TopAbs_WIRE))
326 theDrawer->SetDeviationCoefficient(1.e-4);
329 Quantity_Color color(const std::string& theSection,
330 const std::string& theName,
331 const std::string& theDefault)
333 std::vector<int> aColor = Config_PropManager::color(theSection, theName, theDefault);
334 return Quantity_Color(aColor[0] / 255., aColor[1] / 255., aColor[2] / 255., Quantity_TOC_RGB);
337 ObjectPtr getObject(const AttributePtr& theAttribute)
340 std::string anAttrType = theAttribute->attributeType();
341 if (anAttrType == ModelAPI_AttributeRefAttr::typeId()) {
342 AttributeRefAttrPtr anAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
343 if (anAttr != NULL && anAttr->isObject())
344 anObject = anAttr->object();
346 if (anAttrType == ModelAPI_AttributeSelection::typeId()) {
347 AttributeSelectionPtr anAttr = std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(theAttribute);
349 anObject = anAttr->context();
351 if (anAttrType == ModelAPI_AttributeReference::typeId()) {
352 AttributeReferencePtr anAttr = std::dynamic_pointer_cast<ModelAPI_AttributeReference>(theAttribute);
353 if (anAttr.get() != NULL)
354 anObject = anAttr->value();
359 TopAbs_ShapeEnum getCompoundSubType(const TopoDS_Shape& theShape)
361 TopAbs_ShapeEnum aShapeType = theShape.ShapeType();
363 // for compounds check sub-shapes: it may be compound of needed type:
364 // Booleans may produce compounds of Solids
365 if (aShapeType == TopAbs_COMPOUND) {
366 for(TopoDS_Iterator aSubs(theShape); aSubs.More(); aSubs.Next()) {
367 if (!aSubs.Value().IsNull()) {
368 TopAbs_ShapeEnum aSubType = aSubs.Value().ShapeType();
369 if (aSubType == TopAbs_COMPOUND) { // compound of compound(s)
370 aShapeType = TopAbs_COMPOUND;
373 if (aShapeType == TopAbs_COMPOUND) {
374 aShapeType = aSubType;
375 } else if (aShapeType != aSubType) { // compound of shapes of different types
376 aShapeType = TopAbs_COMPOUND;
385 } // namespace ModuleBase_Tools