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>
11 #include <ModuleBase_WidgetFactory.h>
12 #include <ModuleBase_IWorkshop.h>
13 #include <ModuleBase_IModule.h>
15 #include <ModelAPI_Attribute.h>
16 #include <ModelAPI_AttributeRefAttr.h>
17 #include <ModelAPI_AttributeReference.h>
18 #include <ModelAPI_AttributeSelection.h>
19 #include <ModelAPI_Data.h>
20 #include <ModelAPI_Result.h>
21 #include <ModelAPI_ResultCompSolid.h>
22 #include <ModelAPI_ResultParameter.h>
23 #include <ModelAPI_Tools.h>
24 #include <ModelAPI_Session.h>
26 #include <TopoDS_Iterator.hxx>
28 #include <GeomDataAPI_Point2D.h>
29 #include <Events_Error.h>
31 #include <Config_PropManager.h>
37 #include <QDoubleSpinBox>
38 #include <QGraphicsDropShadowEffect>
43 const double tolerance = 1e-7;
45 //#define DEBUG_ACTIVATE_WINDOW
46 //#define DEBUG_SET_FOCUS
48 namespace ModuleBase_Tools {
50 //******************************************************************
52 //******************************************************************
54 void adjustMargins(QWidget* theWidget)
58 adjustMargins(theWidget->layout());
61 void adjustMargins(QLayout* theLayout)
65 theLayout->setContentsMargins(2, 5, 2, 5);
66 theLayout->setSpacing(4);
69 void zeroMargins(QWidget* theWidget)
73 zeroMargins(theWidget->layout());
76 void zeroMargins(QLayout* theLayout)
80 theLayout->setContentsMargins(0, 0, 0, 0);
81 theLayout->setSpacing(5);
84 void activateWindow(QWidget* theWidget, const QString& theInfo)
86 theWidget->activateWindow();
88 #ifdef DEBUG_ACTIVATE_WINDOW
89 qDebug(QString("activateWindow: %1").arg(theInfo).toStdString().c_str());
93 void setFocus(QWidget* theWidget, const QString& theInfo)
95 theWidget->setFocus();
97 #ifdef DEBUG_SET_FOCUS
98 qDebug(QString("setFocus: %1").arg(theInfo).toStdString().c_str());
102 void setShadowEffect(QWidget* theWidget, const bool isSetEffect)
105 QGraphicsDropShadowEffect* aGlowEffect = new QGraphicsDropShadowEffect();
106 aGlowEffect->setOffset(.0);
107 aGlowEffect->setBlurRadius(10.0);
108 aGlowEffect->setColor(QColor(0, 170, 255)); // Light-blue color, #00AAFF
109 theWidget->setGraphicsEffect(aGlowEffect);
112 QGraphicsEffect* anEffect = theWidget->graphicsEffect();
114 anEffect->deleteLater();
115 theWidget->setGraphicsEffect(NULL);
119 QPixmap composite(const QString& theAdditionalIcon, const QString& theIcon)
121 QImage anIcon(theIcon);
122 QImage anAditional(theAdditionalIcon);
127 int anAddWidth = anAditional.width();
128 int anAddHeight = anAditional.height();
130 int aWidth = anIcon.width();
131 int aHeight = anIcon.height();
133 int aStartWidthPos = aWidth - anAddWidth - 1;
134 int aStartHeightPos = aHeight - anAddHeight - 1;
136 for (int i = 0; i < anAddWidth && i + aStartWidthPos < aWidth; i++)
138 for (int j = 0; j < anAddHeight && j + aStartHeightPos < aHeight; j++)
140 if (qAlpha(anAditional.pixel(i, j)) > 0)
141 anIcon.setPixel(i + aStartWidthPos, j + aStartHeightPos, anAditional.pixel(i, j));
144 return QPixmap::fromImage(anIcon);
147 QPixmap lighter(const QString& theIcon, const int theLighterValue)
149 QImage anIcon(theIcon);
153 QImage aResult(theIcon);
154 for ( int i = 0; i < anIcon.width(); i++ )
156 for ( int j = 0; j < anIcon.height(); j++ )
158 QRgb anRgb = anIcon.pixel( i, j );
159 QColor aPixelColor(qRed(anRgb), qGreen(anRgb), qBlue(anRgb),
160 qAlpha( aResult.pixel( i, j ) ));
162 QColor aLighterColor = aPixelColor.lighter(theLighterValue);
163 aResult.setPixel(i, j, qRgba( aLighterColor.red(), aLighterColor.green(),
164 aLighterColor.blue(), aLighterColor.alpha() ) );
167 return QPixmap::fromImage(aResult);
170 void setSpinText(ModuleBase_ParamSpinBox* theSpin, const QString& theText)
172 if (theSpin->text() == theText)
174 // In order to avoid extra text setting because it will
175 // reset cursor position in control
176 bool isBlocked = theSpin->blockSignals(true);
177 theSpin->setText(theText);
178 theSpin->blockSignals(isBlocked);
181 void setSpinValue(QDoubleSpinBox* theSpin, double theValue)
183 if (fabs(theSpin->value() - theValue) < tolerance)
185 bool isBlocked = theSpin->blockSignals(true);
186 theSpin->setValue(theValue);
187 theSpin->blockSignals(isBlocked);
190 void setSpinValue(ModuleBase_ParamSpinBox* theSpin, double theValue)
192 if (fabs(theSpin->value() - theValue) < tolerance)
194 bool isBlocked = theSpin->blockSignals(true);
195 theSpin->setValue(theValue);
196 theSpin->blockSignals(isBlocked);
199 void setSpinText(ModuleBase_ParamIntSpinBox* theSpin, const QString& theText)
201 // In order to avoid extra text setting because it will
202 // reset cursor position in control
203 if (theSpin->text() == theText)
205 bool isBlocked = theSpin->blockSignals(true);
206 theSpin->setText(theText);
207 theSpin->blockSignals(isBlocked);
210 void setSpinValue(ModuleBase_ParamIntSpinBox* theSpin, int theValue)
212 if (theSpin->value() == theValue)
214 bool isBlocked = theSpin->blockSignals(true);
215 theSpin->setValue(theValue);
216 theSpin->blockSignals(isBlocked);
219 QString objectInfo(const ObjectPtr& theObj, const bool isUseAttributesInfo)
221 QString aFeatureStr = "feature";
225 std::ostringstream aPtrStr;
226 aPtrStr << "[" << theObj.get() << "]";
228 ResultPtr aRes = std::dynamic_pointer_cast<ModelAPI_Result>(theObj);
229 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theObj);
231 aFeatureStr.append(QString("(result%1)").arg(aPtrStr.str().c_str()).toStdString() .c_str());
232 if (aRes->isDisabled())
233 aFeatureStr.append("[disabled]");
234 if (aRes->isConcealed())
235 aFeatureStr.append("[concealed]");
236 if (ModelAPI_Tools::hasSubResults(aRes))
237 aFeatureStr.append("[hasSubResults]");
239 aFeature = ModelAPI_Feature::feature(aRes);
242 aFeatureStr.append(aPtrStr.str().c_str());
244 if (aFeature.get()) {
245 aFeatureStr.append(QString(": %1").arg(aFeature->getKind().c_str()).toStdString().c_str());
246 if (aFeature->data()->isValid()) {
247 aFeatureStr.append(QString(", name=%1").arg(aFeature->data()->name().c_str()).toStdString()
250 if (isUseAttributesInfo) {
251 std::list<AttributePtr> anAttrs = aFeature->data()->attributes("");
252 std::list<AttributePtr>::const_iterator anIt = anAttrs.begin(), aLast = anAttrs.end();
254 for(; anIt != aLast; anIt++) {
255 AttributePtr anAttr = *anIt;
256 QString aValue = "not defined";
257 std::string aType = anAttr->attributeType();
258 if (aType == GeomDataAPI_Point2D::typeId()) {
259 std::shared_ptr<GeomDataAPI_Point2D> aPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
262 aValue = QString("(%1, %2)").arg(aPoint->x()).arg(aPoint->y());
264 else if (aType == ModelAPI_AttributeRefAttr::typeId()) {
267 aValues.push_back(QString("%1: %2").arg(anAttr->id().c_str()).arg(aValue).toStdString().c_str());
269 if (!aValues.empty())
270 aFeatureStr.append(QString(", attributes: %1").arg(aValues.join(", ").toStdString().c_str()));
277 typedef QMap<QString, TopAbs_ShapeEnum> ShapeTypes;
278 static ShapeTypes MyShapeTypes;
280 TopAbs_ShapeEnum shapeType(const QString& theType)
282 if (MyShapeTypes.count() == 0) {
283 MyShapeTypes["face"] = TopAbs_FACE;
284 MyShapeTypes["faces"] = TopAbs_FACE;
285 MyShapeTypes["vertex"] = TopAbs_VERTEX;
286 MyShapeTypes["vertices"] = TopAbs_VERTEX;
287 MyShapeTypes["wire"] = TopAbs_WIRE;
288 MyShapeTypes["edge"] = TopAbs_EDGE;
289 MyShapeTypes["edges"] = TopAbs_EDGE;
290 MyShapeTypes["shell"] = TopAbs_SHELL;
291 MyShapeTypes["solid"] = TopAbs_SOLID;
292 MyShapeTypes["solids"] = TopAbs_SOLID;
293 MyShapeTypes["objects"] = TopAbs_SHAPE;
295 QString aType = theType.toLower();
296 if (MyShapeTypes.contains(aType))
297 return MyShapeTypes[aType];
298 Events_Error::send("Shape type defined in XML is not implemented!");
302 void checkObjects(const QObjectPtrList& theObjects, bool& hasResult, bool& hasFeature, bool& hasParameter, bool& hasSubFeature)
306 hasParameter = false;
307 hasSubFeature = false;
308 foreach(ObjectPtr aObj, theObjects) {
309 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aObj);
310 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(aObj);
311 ResultParameterPtr aConstruction = std::dynamic_pointer_cast<ModelAPI_ResultParameter>(aResult);
313 hasResult |= (aResult.get() != NULL);
314 hasFeature |= (aFeature.get() != NULL);
315 hasParameter |= (aConstruction.get() != NULL);
317 hasSubFeature |= (ModelAPI_Tools::compositeOwner(aFeature) != NULL);
318 if (hasFeature && hasResult && hasParameter && hasSubFeature)
323 void setDefaultDeviationCoefficient(const TopoDS_Shape& theShape,
324 const Handle(Prs3d_Drawer)& theDrawer)
326 if (theShape.IsNull())
328 TopAbs_ShapeEnum aType = theShape.ShapeType();
329 if ((aType == TopAbs_EDGE) || (aType == TopAbs_WIRE))
330 theDrawer->SetDeviationCoefficient(1.e-4);
333 Quantity_Color color(const std::string& theSection,
334 const std::string& theName,
335 const std::string& theDefault)
337 std::vector<int> aColor = Config_PropManager::color(theSection, theName, theDefault);
338 return Quantity_Color(aColor[0] / 255., aColor[1] / 255., aColor[2] / 255., Quantity_TOC_RGB);
341 ObjectPtr getObject(const AttributePtr& theAttribute)
344 std::string anAttrType = theAttribute->attributeType();
345 if (anAttrType == ModelAPI_AttributeRefAttr::typeId()) {
346 AttributeRefAttrPtr anAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
347 if (anAttr != NULL && anAttr->isObject())
348 anObject = anAttr->object();
350 if (anAttrType == ModelAPI_AttributeSelection::typeId()) {
351 AttributeSelectionPtr anAttr = std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(theAttribute);
353 anObject = anAttr->context();
355 if (anAttrType == ModelAPI_AttributeReference::typeId()) {
356 AttributeReferencePtr anAttr = std::dynamic_pointer_cast<ModelAPI_AttributeReference>(theAttribute);
357 if (anAttr.get() != NULL)
358 anObject = anAttr->value();
363 TopAbs_ShapeEnum getCompoundSubType(const TopoDS_Shape& theShape)
365 TopAbs_ShapeEnum aShapeType = theShape.ShapeType();
367 // for compounds check sub-shapes: it may be compound of needed type:
368 // Booleans may produce compounds of Solids
369 if (aShapeType == TopAbs_COMPOUND) {
370 for(TopoDS_Iterator aSubs(theShape); aSubs.More(); aSubs.Next()) {
371 if (!aSubs.Value().IsNull()) {
372 TopAbs_ShapeEnum aSubType = aSubs.Value().ShapeType();
373 if (aSubType == TopAbs_COMPOUND) { // compound of compound(s)
374 aShapeType = TopAbs_COMPOUND;
377 if (aShapeType == TopAbs_COMPOUND) {
378 aShapeType = aSubType;
379 } else if (aShapeType != aSubType) { // compound of shapes of different types
380 aShapeType = TopAbs_COMPOUND;
389 void getParameters(QStringList& theParameters)
391 theParameters.clear();
393 SessionPtr aSession = ModelAPI_Session::get();
394 std::list<DocumentPtr> aDocList;
395 DocumentPtr anActiveDocument = aSession->activeDocument();
396 DocumentPtr aRootDocument = aSession->moduleDocument();
397 aDocList.push_back(anActiveDocument);
398 if (anActiveDocument != aRootDocument) {
399 aDocList.push_back(aRootDocument);
401 std::string aGroupId = ModelAPI_ResultParameter::group();
402 for(std::list<DocumentPtr>::const_iterator it = aDocList.begin(); it != aDocList.end(); ++it) {
403 DocumentPtr aDocument = *it;
404 int aSize = aDocument->size(aGroupId);
405 for (int i = 0; i < aSize; i++) {
406 ObjectPtr anObject = aDocument->object(aGroupId, i);
407 std::string aParameterName = anObject->data()->name();
408 theParameters.append(aParameterName.c_str());
413 std::string findGreedAttribute(ModuleBase_IWorkshop* theWorkshop, const FeaturePtr& theFeature)
415 std::string anAttributeId;
417 std::string aXmlCfg, aDescription;
418 theWorkshop->module()->getXMLRepresentation(theFeature->getKind(), aXmlCfg, aDescription);
420 ModuleBase_WidgetFactory aFactory(aXmlCfg, theWorkshop);
421 std::string anAttributeTitle;
422 aFactory.getGreedAttribute(anAttributeId);
424 return anAttributeId;
427 } // namespace ModuleBase_Tools