Salome HOME
Merge branch 'Dev_1.1.0' of newgeom:newgeom.git into Dev_1.1.0
[modules/shaper.git] / src / ModuleBase / ModuleBase_Tools.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
2
3 // File:        ModuleBase_Tools.cpp
4 // Created:     11 July 2014
5 // Author:      Vitaly Smetannikov
6
7 #include "ModuleBase_Tools.h"
8
9 #include <ModelAPI_Result.h>
10 #include <ModelAPI_Data.h>
11 #include <ModelAPI_Attribute.h>
12 #include <ModelAPI_AttributeRefAttr.h>
13
14 #include <GeomDataAPI_Point2D.h>
15
16 #include <QWidget>
17 #include <QLayout>
18 #include <QPainter>
19 #include <QBitmap>
20 #include <QDoubleSpinBox>
21
22 namespace ModuleBase_Tools {
23
24 //******************************************************************
25
26 //******************************************************************
27
28 void adjustMargins(QWidget* theWidget)
29 {
30   if(!theWidget)
31     return;
32   adjustMargins(theWidget->layout());
33 }
34
35 void adjustMargins(QLayout* theLayout)
36 {
37   if(!theLayout)
38     return;
39   theLayout->setContentsMargins(2, 5, 2, 5);
40   theLayout->setSpacing(4);
41 }
42
43 void zeroMargins(QWidget* theWidget)
44 {
45   if(!theWidget)
46     return;
47   zeroMargins(theWidget->layout());
48 }
49
50 void zeroMargins(QLayout* theLayout)
51 {
52   if(!theLayout)
53     return;
54   theLayout->setContentsMargins(0, 0, 0, 0);
55   theLayout->setSpacing(5);
56 }
57
58 QPixmap composite(const QString& theAdditionalIcon, const QString& theIcon)
59 {
60   QImage anIcon(theIcon);
61   QImage anAditional(theAdditionalIcon);
62
63   if (anIcon.isNull())
64     return QPixmap();
65
66   int anAddWidth = anAditional.width();
67   int anAddHeight = anAditional.height();
68
69   int aWidth = anIcon.width();
70   int aHeight = anIcon.height();
71
72   int aStartWidthPos = aWidth - anAddWidth - 1;
73   int aStartHeightPos = aHeight - anAddHeight - 1;
74
75   for (int i = 0; i < anAddWidth && i + aStartWidthPos < aWidth; i++)
76   {
77     for (int j = 0; j < anAddHeight && j + aStartHeightPos < aHeight; j++)
78     {
79       if (qAlpha(anAditional.pixel(i, j)) > 0)
80         anIcon.setPixel(i + aStartWidthPos, j + aStartHeightPos, anAditional.pixel(i, j));
81     }
82   }
83   return QPixmap::fromImage(anIcon);
84 }
85
86 QPixmap lighter(const QString& theIcon, const int theLighterValue)
87 {
88   QImage anIcon(theIcon);
89   if (anIcon.isNull())
90     return QPixmap();
91
92   QImage aResult(theIcon);
93   for ( int i = 0; i < anIcon.width(); i++ )
94   {
95     for ( int j = 0; j < anIcon.height(); j++ )
96     {
97       QRgb anRgb = anIcon.pixel( i, j );
98       QColor aPixelColor(qRed(anRgb), qGreen(anRgb), qBlue(anRgb),
99                          qAlpha( aResult.pixel( i, j ) ));
100
101       QColor aLighterColor = aPixelColor.lighter(theLighterValue);
102       aResult.setPixel(i, j, qRgba( aLighterColor.red(), aLighterColor.green(),
103                                     aLighterColor.blue(), aLighterColor.alpha() ) );
104     }
105   }
106   return QPixmap::fromImage(aResult);
107 }
108
109 void setSpinValue(QDoubleSpinBox* theSpin, double theValue)
110 {
111   bool isBlocked = theSpin->blockSignals(true);
112   theSpin->setValue(theValue);
113   theSpin->blockSignals(isBlocked);
114 }
115
116 QString objectInfo(const ObjectPtr& theObj, const bool isUseAttributesInfo)
117 {
118   ResultPtr aRes = std::dynamic_pointer_cast<ModelAPI_Result>(theObj);
119   FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theObj);
120   QString aFeatureStr = "feature";
121   if(aRes.get()) {
122     aFeatureStr.append("(Result)");
123     aFeature = ModelAPI_Feature::feature(aRes);
124   }
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()
129                                                                                        .c_str());
130     if (isUseAttributesInfo) {
131       std::list<AttributePtr> anAttrs = aFeature->data()->attributes("");
132       std::list<AttributePtr>::const_iterator anIt = anAttrs.begin(), aLast = anAttrs.end();
133       QStringList aValues;
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>(
140                                                                                          anAttr);
141           if (aPoint.get())
142             aValue = QString("(%1, %2)").arg(aPoint->x()).arg(aPoint->y());
143         }
144         else if (aType == ModelAPI_AttributeRefAttr::typeId()) {
145         }
146
147         aValues.push_back(QString("%1: %2").arg(anAttr->id().c_str()).arg(aValue).toStdString().c_str());
148       }
149       if (!aValues.empty())
150         aFeatureStr.append(QString(", attributes: %1").arg(aValues.join(", ").toStdString().c_str()));
151     }
152   }
153
154   return aFeatureStr;
155 }
156
157 }
158
159