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
12 #include <QWidget>
13 #include <QLayout>
14 #include <QPainter>
15 #include <QBitmap>
16 #include <QDoubleSpinBox>
17
18 namespace ModuleBase_Tools {
19
20 //******************************************************************
21
22 //******************************************************************
23
24 void adjustMargins(QWidget* theWidget)
25 {
26   if(!theWidget)
27     return;
28   adjustMargins(theWidget->layout());
29 }
30
31 void adjustMargins(QLayout* theLayout)
32 {
33   if(!theLayout)
34     return;
35   theLayout->setContentsMargins(2, 5, 2, 5);
36   theLayout->setSpacing(4);
37 }
38
39 void zeroMargins(QWidget* theWidget)
40 {
41   if(!theWidget)
42     return;
43   zeroMargins(theWidget->layout());
44 }
45
46 void zeroMargins(QLayout* theLayout)
47 {
48   if(!theLayout)
49     return;
50   theLayout->setContentsMargins(0, 0, 0, 0);
51   theLayout->setSpacing(5);
52 }
53
54 QPixmap composite(const QString& theAdditionalIcon, const QString& theIcon)
55 {
56   QImage anIcon(theIcon);
57   QImage anAditional(theAdditionalIcon);
58
59   if (anIcon.isNull())
60     return QPixmap();
61
62   int anAddWidth = anAditional.width();
63   int anAddHeight = anAditional.height();
64
65   int aWidth = anIcon.width();
66   int aHeight = anIcon.height();
67
68   int aStartWidthPos = aWidth - anAddWidth - 1;
69   int aStartHeightPos = aHeight - anAddHeight - 1;
70
71   for (int i = 0; i < anAddWidth && i + aStartWidthPos < aWidth; i++)
72   {
73     for (int j = 0; j < anAddHeight && j + aStartHeightPos < aHeight; j++)
74     {
75       if (qAlpha(anAditional.pixel(i, j)) > 0)
76         anIcon.setPixel(i + aStartWidthPos, j + aStartHeightPos, anAditional.pixel(i, j));
77     }
78   }
79   return QPixmap::fromImage(anIcon);
80 }
81
82 QPixmap lighter(const QString& theIcon, const int theLighterValue)
83 {
84   QImage anIcon(theIcon);
85   if (anIcon.isNull())
86     return QPixmap();
87
88   QImage aResult(theIcon);
89   for ( int i = 0; i < anIcon.width(); i++ )
90   {
91     for ( int j = 0; j < anIcon.height(); j++ )
92     {
93       QRgb anRgb = anIcon.pixel( i, j );
94       QColor aPixelColor(qRed(anRgb), qGreen(anRgb), qBlue(anRgb),
95                          qAlpha( aResult.pixel( i, j ) ));
96
97       QColor aLighterColor = aPixelColor.lighter(theLighterValue);
98       aResult.setPixel(i, j, qRgba( aLighterColor.red(), aLighterColor.green(),
99                                     aLighterColor.blue(), aLighterColor.alpha() ) );
100     }
101   }
102   return QPixmap::fromImage(aResult);
103 }
104
105 void setSpinValue(QDoubleSpinBox* theSpin, double theValue)
106 {
107   bool isBlocked = theSpin->blockSignals(true);
108   theSpin->setValue(theValue);
109   theSpin->blockSignals(isBlocked);
110 }
111
112 QString objectInfo(const ObjectPtr& theObj)
113 {
114   ResultPtr aRes = std::dynamic_pointer_cast<ModelAPI_Result>(theObj);
115   FeaturePtr aFeature;// = std::dynamic_pointer_cast<ModelAPI_Feature>(theObj);
116   QString aFeatureStr = "feature";
117   if(aRes.get()) {
118     aFeatureStr.append("(Result)");
119     //aFeature = ModelAPI_Feature::feature(aRes);
120   }
121   if (aFeature.get()) {
122     aFeatureStr.append(QString(": %1").arg(aFeature->getKind().c_str()).toStdString().c_str());
123     if (aFeature->data().get() && aFeature->data()->isValid())
124       aFeatureStr.append(QString("(name=%1)").arg(aFeature->data()->name().c_str()).toStdString().c_str());
125   }
126   return aFeatureStr;
127 }
128
129 }
130
131