Salome HOME
Issue #353 constraint on 2 segments from not acive sketches
[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 #include <QDoubleSpinBox>
13
14 namespace ModuleBase_Tools {
15
16 //******************************************************************
17
18 //******************************************************************
19
20 void adjustMargins(QWidget* theWidget)
21 {
22   if(!theWidget)
23     return;
24   adjustMargins(theWidget->layout());
25 }
26
27 void adjustMargins(QLayout* theLayout)
28 {
29   if(!theLayout)
30     return;
31   theLayout->setContentsMargins(2, 5, 5, 2);
32   theLayout->setSpacing(4);
33 }
34
35 void zeroMargins(QWidget* theWidget)
36 {
37   if(!theWidget)
38     return;
39   zeroMargins(theWidget->layout());
40 }
41
42 void zeroMargins(QLayout* theLayout)
43 {
44   if(!theLayout)
45     return;
46   theLayout->setContentsMargins(0, 0, 0, 0);
47   theLayout->setSpacing(5);
48 }
49
50 QPixmap composite(const QString& theAdditionalIcon, const QString& theIcon)
51 {
52   QImage anIcon(theIcon);
53   QImage anAditional(theAdditionalIcon);
54
55   if (anIcon.isNull())
56     return QPixmap();
57
58   int anAddWidth = anAditional.width();
59   int anAddHeight = anAditional.height();
60
61   int aWidth = anIcon.width();
62   int aHeight = anIcon.height();
63
64   int aStartWidthPos = aWidth - anAddWidth - 1;
65   int aStartHeightPos = aHeight - anAddHeight - 1;
66
67   for (int i = 0; i < anAddWidth && i + aStartWidthPos < aWidth; i++)
68   {
69     for (int j = 0; j < anAddHeight && j + aStartHeightPos < aHeight; j++)
70     {
71       if (qAlpha(anAditional.pixel(i, j)) > 0)
72         anIcon.setPixel(i + aStartWidthPos, j + aStartHeightPos, anAditional.pixel(i, j));
73     }
74   }
75   return QPixmap::fromImage(anIcon);
76 }
77
78 QPixmap lighter(const QString& theIcon, const int theLighterValue)
79 {
80   QImage anIcon(theIcon);
81   if (anIcon.isNull())
82     return QPixmap();
83
84   QImage aResult(theIcon);
85   for ( int i = 0; i < anIcon.width(); i++ )
86   {
87     for ( int j = 0; j < anIcon.height(); j++ )
88     {
89       QRgb anRgb = anIcon.pixel( i, j );
90       QColor aPixelColor(qRed(anRgb), qGreen(anRgb), qBlue(anRgb),
91                          qAlpha( aResult.pixel( i, j ) ));
92
93       QColor aLighterColor = aPixelColor.lighter(theLighterValue);
94       aResult.setPixel(i, j, qRgba( aLighterColor.red(), aLighterColor.green(),
95                                     aLighterColor.blue(), aLighterColor.alpha() ) );
96     }
97   }
98   return QPixmap::fromImage(aResult);
99 }
100
101 void setSpinValue(QDoubleSpinBox* theSpin, double theValue)
102 {
103   bool isBlocked = theSpin->blockSignals(true);
104   theSpin->setValue(theValue);
105   theSpin->blockSignals(isBlocked);
106 }
107
108 }
109
110