Salome HOME
Issue #1860: fix end lines with spaces
[modules/shaper.git] / src / XGUI / XGUI_ColorDialog.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
2
3 // File:        XGUI_ColorDialog.cpp
4 // Created:     27 Apr 2015
5 // Author:      Natalia ERMOLAEVA
6
7 #include <XGUI_ColorDialog.h>
8
9 #include <ModelAPI_Tools.h>
10
11 #include <QtxColorButton.h>
12
13 #include <QLabel>
14 #include <QButtonGroup>
15 #include <QGridLayout>
16 #include <QRadioButton>
17 #include <QDialogButtonBox>
18
19 XGUI_ColorDialog::XGUI_ColorDialog(QWidget* theParent)
20   : QDialog(theParent)
21 {
22   setWindowTitle(tr("Color"));
23   QGridLayout* aLay = new QGridLayout(this);
24
25   QRadioButton* aRandomChoiceBtn = new QRadioButton(this);
26   QRadioButton* aColorChoiceBtn = new QRadioButton(this);
27   aColorChoiceBtn->setChecked(true);
28   myButtonGroup = new QButtonGroup(this);
29   myButtonGroup->setExclusive(true);
30   myButtonGroup->addButton(aColorChoiceBtn, 0);
31   myButtonGroup->addButton(aRandomChoiceBtn, 1);
32
33   aLay->addWidget(aColorChoiceBtn, 0, 0);
34   aLay->addWidget(aRandomChoiceBtn, 1, 0);
35
36   myColorButton = new QtxColorButton(this);
37   myColorButton->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
38   aLay->addWidget(myColorButton, 0, 1);
39
40   QLabel* aRandomLabel = new QLabel(tr("Random"), this);
41   aLay->addWidget(aRandomLabel, 1, 1);
42
43   QDialogButtonBox* aButtons = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel,
44                                                     Qt::Horizontal, this);
45   connect(aButtons, SIGNAL(accepted()), this, SLOT(accept()));
46   connect(aButtons, SIGNAL(rejected()), this, SLOT(reject()));
47   aLay->addWidget(aButtons, 2, 0, 1, 2);
48 }
49
50 bool XGUI_ColorDialog::isRandomColor() const
51 {
52   int anId = myButtonGroup->checkedId();
53
54   return myButtonGroup->checkedId() == 1;
55 }
56
57 void XGUI_ColorDialog::setColor(const std::vector<int>& theValue)
58 {
59   if (theValue.size() != 3)
60     return;
61
62   myColorButton->setColor(QColor(theValue[0], theValue[1], theValue[2]));
63 }
64
65 std::vector<int> XGUI_ColorDialog::getColor() const
66 {
67   QColor aColorResult = myColorButton->color();
68
69   std::vector<int> aValues;
70   aValues.push_back(aColorResult.red());
71   aValues.push_back(aColorResult.green());
72   aValues.push_back(aColorResult.blue());
73
74   return aValues;
75 }
76 // contains global cash for integer index of the color -> RGB of this color
77 static std::map<int, std::vector<int> > myColorMap;
78
79 void appendValues(std::vector<int>& theRGB, const int theRed, const int theGreen, const int theBlue)
80 {
81   theRGB.push_back(theRed);
82   theRGB.push_back(theGreen);
83   theRGB.push_back(theBlue);
84 }
85
86 bool containsValues(std::map<int, std::vector<int> >& theColorMap, std::vector<int>& theValues)
87 {
88   std::map<int, std::vector<int> >::const_iterator anIt = theColorMap.begin(),
89                                                    aLast = theColorMap.end();
90   bool isFound = false;
91   for (; anIt != aLast && !isFound; anIt++) {
92     std::vector<int> aValues = anIt->second;
93     isFound = aValues[0] == theValues[0] &&
94               aValues[1] == theValues[1] &&
95               aValues[2] == theValues[2];
96   }
97   return isFound;
98 }
99
100 std::vector<int> HSVtoRGB(int theH, int theS, int theV)
101 {
102   std::vector<int> aRGB;
103   if (theH < 0 || theH > 360 ||
104       theS < 0 || theS > 100 ||
105       theV < 0 || theV > 100)
106     return aRGB;
107
108   int aHi = (int)theH/60;
109
110   double aV = theV;
111   double aVmin = (100 - theS)*theV/100;
112
113   double anA = (theV - aVmin)* (theH % 60) / 60;
114
115   double aVinc = aVmin + anA;
116   double aVdec = theV - anA;
117
118   double aPercentToValue = 255./100;
119   int aV_int    = (int)(aV*aPercentToValue);
120   int aVinc_int = (int)(aVinc*aPercentToValue);
121   int aVmin_int = (int)(aVmin*aPercentToValue);
122   int aVdec_int = (int)(aVdec*aPercentToValue);
123
124   switch(aHi) {
125     case 0: appendValues(aRGB, aV_int,    aVinc_int, aVmin_int); break;
126     case 1: appendValues(aRGB, aVdec_int, aV_int,    aVmin_int); break;
127     case 2: appendValues(aRGB, aVmin_int, aV_int,    aVinc_int); break;
128     case 3: appendValues(aRGB, aVmin_int, aVdec_int, aV_int); break;
129     case 4: appendValues(aRGB, aVinc_int, aVmin_int, aV_int); break;
130     case 5: appendValues(aRGB, aV_int,    aVmin_int, aVdec_int); break;
131     default: break;
132   }
133   return aRGB;
134 }
135
136
137 void fillColorMap()
138 {
139   if (!myColorMap.empty())
140     return;
141
142   int i = 0;
143   for (int s = 100; s > 0; s = s - 50)
144   {
145     for (int v = 100; v >= 40; v = v - 20)
146     {
147       for (int h = 0; h < 359 ; h = h + 60)
148       {
149         std::vector<int> aColor = HSVtoRGB(h, s, v);
150         if (containsValues(myColorMap, aColor))
151           continue;
152         myColorMap[i] = aColor;
153         i++;
154       }
155     }
156   }
157 }
158
159 void findRandomColor(std::vector<int>& theValues)
160 {
161   theValues.clear();
162   if (myColorMap.empty()) {
163     fillColorMap();
164   }
165
166   size_t aSize = myColorMap.size();
167   int anIndex = rand() % aSize;
168   if (myColorMap.find(anIndex) != myColorMap.end()) {
169     theValues = myColorMap.at(anIndex);
170   }
171 }
172
173 std::vector<int> XGUI_ColorDialog::getRandomColor() const
174 {
175   std::vector<int> aValues;
176   if (isRandomColor()) {
177     findRandomColor(aValues);
178   }
179   return aValues;
180 }