Salome HOME
843c4cd510dea4f2ac622984b2688208fa22dfa7
[modules/shaper.git] / src / ModuleBase / ModuleBase_Tools.cpp
1 // File:        ModuleBase_Tools.cpp
2 // Created:     11 July 2014
3 // Author:      Vitaly Smetannikov
4
5 #include "ModuleBase_Tools.h"
6 #include <QWidget>
7 #include <QLayout>
8 #include <QPainter>
9 #include <QBitmap>
10
11 namespace ModuleBase_Tools {
12
13 //******************************************************************
14
15 //******************************************************************
16
17 void adjustMargins(QWidget* theWidget)
18 {
19   if(!theWidget)
20     return;
21   adjustMargins(theWidget->layout());
22 }
23
24 void adjustMargins(QLayout* theLayout)
25 {
26   if(!theLayout)
27     return;
28   theLayout->setContentsMargins(2, 5, 5, 2);
29   theLayout->setSpacing(4);
30 }
31
32 void zeroMargins(QWidget* theWidget)
33 {
34   if(!theWidget)
35     return;
36   zeroMargins(theWidget->layout());
37 }
38
39 void zeroMargins(QLayout* theLayout)
40 {
41   if(!theLayout)
42     return;
43   theLayout->setContentsMargins(0, 0, 0, 0);
44   theLayout->setSpacing(5);
45 }
46
47 QPixmap composite(const QString& theAdditionalIcon, const QString& theIcon)
48 {
49   QImage anIcon(theIcon);
50   QImage anAditional(theAdditionalIcon);
51
52   if (anIcon.isNull())
53     return QPixmap();
54
55   int anAddWidth = anAditional.width();
56   int anAddHeight = anAditional.height();
57
58   int aWidth = anIcon.width();
59   int aHeight = anIcon.height();
60
61   int aStartWidthPos = aWidth - anAddWidth - 1;
62   int aStartHeightPos = aHeight - anAddHeight - 1;
63
64   for (int i = 0; i < anAddWidth && i + aStartWidthPos < aWidth; i++)
65   {
66     for (int j = 0; j < anAddHeight && j + aStartHeightPos < aHeight; j++)
67     {
68       if (qAlpha(anAditional.pixel(i, j)) > 0)
69         anIcon.setPixel(i + aStartWidthPos, j + aStartHeightPos, anAditional.pixel(i, j));
70     }
71   }
72   return QPixmap::fromImage(anIcon);
73 }
74
75 QPixmap lighter(const QString& theIcon, const int theLighterValue)
76 {
77   QImage anIcon(theIcon);
78   if (anIcon.isNull())
79     return QPixmap();
80
81   QImage aResult(theIcon);
82   for ( int i = 0; i < anIcon.width(); i++ )
83   {
84     for ( int j = 0; j < anIcon.height(); j++ )
85     {
86       QRgb anRgb = anIcon.pixel( i, j );
87       QColor aPixelColor(qRed(anRgb), qGreen(anRgb), qBlue(anRgb),
88                          qAlpha( aResult.pixel( i, j ) ));
89
90       QColor aLighterColor = aPixelColor.lighter(theLighterValue);
91       aResult.setPixel(i, j, qRgba( aLighterColor.red(), aLighterColor.green(),
92                                     aLighterColor.blue(), aLighterColor.alpha() ) );
93     }
94   }
95   return QPixmap::fromImage(aResult);
96 }
97
98 }
99
100