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 <ModelAPI_Result.h>
10 #include <ModelAPI_Data.h>
11 #include <ModelAPI_Attribute.h>
12 #include <ModelAPI_AttributeRefAttr.h>
14 #include <GeomDataAPI_Point2D.h>
20 #include <QDoubleSpinBox>
22 namespace ModuleBase_Tools {
24 //******************************************************************
26 //******************************************************************
28 void adjustMargins(QWidget* theWidget)
32 adjustMargins(theWidget->layout());
35 void adjustMargins(QLayout* theLayout)
39 theLayout->setContentsMargins(2, 5, 2, 5);
40 theLayout->setSpacing(4);
43 void zeroMargins(QWidget* theWidget)
47 zeroMargins(theWidget->layout());
50 void zeroMargins(QLayout* theLayout)
54 theLayout->setContentsMargins(0, 0, 0, 0);
55 theLayout->setSpacing(5);
58 QPixmap composite(const QString& theAdditionalIcon, const QString& theIcon)
60 QImage anIcon(theIcon);
61 QImage anAditional(theAdditionalIcon);
66 int anAddWidth = anAditional.width();
67 int anAddHeight = anAditional.height();
69 int aWidth = anIcon.width();
70 int aHeight = anIcon.height();
72 int aStartWidthPos = aWidth - anAddWidth - 1;
73 int aStartHeightPos = aHeight - anAddHeight - 1;
75 for (int i = 0; i < anAddWidth && i + aStartWidthPos < aWidth; i++)
77 for (int j = 0; j < anAddHeight && j + aStartHeightPos < aHeight; j++)
79 if (qAlpha(anAditional.pixel(i, j)) > 0)
80 anIcon.setPixel(i + aStartWidthPos, j + aStartHeightPos, anAditional.pixel(i, j));
83 return QPixmap::fromImage(anIcon);
86 QPixmap lighter(const QString& theIcon, const int theLighterValue)
88 QImage anIcon(theIcon);
92 QImage aResult(theIcon);
93 for ( int i = 0; i < anIcon.width(); i++ )
95 for ( int j = 0; j < anIcon.height(); j++ )
97 QRgb anRgb = anIcon.pixel( i, j );
98 QColor aPixelColor(qRed(anRgb), qGreen(anRgb), qBlue(anRgb),
99 qAlpha( aResult.pixel( i, j ) ));
101 QColor aLighterColor = aPixelColor.lighter(theLighterValue);
102 aResult.setPixel(i, j, qRgba( aLighterColor.red(), aLighterColor.green(),
103 aLighterColor.blue(), aLighterColor.alpha() ) );
106 return QPixmap::fromImage(aResult);
109 void setSpinValue(QDoubleSpinBox* theSpin, double theValue)
111 bool isBlocked = theSpin->blockSignals(true);
112 theSpin->setValue(theValue);
113 theSpin->blockSignals(isBlocked);
116 QString objectInfo(const ObjectPtr& theObj, const bool isUseAttributesInfo)
118 ResultPtr aRes = std::dynamic_pointer_cast<ModelAPI_Result>(theObj);
119 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theObj);
120 QString aFeatureStr = "feature";
122 aFeatureStr.append("(Result)");
123 aFeature = ModelAPI_Feature::feature(aRes);
125 if (aFeature.get()) {
126 aFeatureStr.append(QString(": %1").arg(aFeature->getKind().c_str()).toStdString().c_str());
127 if (aFeature->data().get() && aFeature->data()->isValid())
128 aFeatureStr.append(QString(", name=%1").arg(aFeature->data()->name().c_str()).toStdString()
130 if (isUseAttributesInfo) {
131 std::list<AttributePtr> anAttrs = aFeature->data()->attributes("");
132 std::list<AttributePtr>::const_iterator anIt = anAttrs.begin(), aLast = anAttrs.end();
134 for(; anIt != aLast; anIt++) {
135 AttributePtr anAttr = *anIt;
136 QString aValue = "not defined";
137 std::string aType = anAttr->attributeType();
138 if (aType == GeomDataAPI_Point2D::typeId()) {
139 std::shared_ptr<GeomDataAPI_Point2D> aPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
142 aValue = QString("(%1, %2)").arg(aPoint->x()).arg(aPoint->y());
144 else if (aType == ModelAPI_AttributeRefAttr::typeId()) {
147 aValues.push_back(QString("%1: %2").arg(anAttr->id().c_str()).arg(aValue).toStdString().c_str());
149 if (!aValues.empty())
150 aFeatureStr.append(QString(", attributes: %1").arg(aValues.join(", ").toStdString().c_str()));