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