Salome HOME
Merge branch 'Dev_2.8.0'
[modules/shaper.git] / src / XGUI / XGUI_ColorDialog.cpp
1 // Copyright (C) 2014-2017  CEA/DEN, EDF R&D
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or
18 // email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
19 //
20
21 #include <XGUI_ColorDialog.h>
22
23 #include <ModelAPI_Tools.h>
24
25 #include <QtxColorButton.h>
26
27 #include <QLabel>
28 #include <QButtonGroup>
29 #include <QGridLayout>
30 #include <QRadioButton>
31 #include <QDialogButtonBox>
32
33 XGUI_ColorDialog::XGUI_ColorDialog(QWidget* theParent)
34   : QDialog(theParent)
35 {
36   setWindowTitle(tr("Color"));
37   QGridLayout* aLay = new QGridLayout(this);
38
39   QRadioButton* aRandomChoiceBtn = new QRadioButton(this);
40   QRadioButton* aColorChoiceBtn = new QRadioButton(this);
41   aColorChoiceBtn->setChecked(true);
42   myButtonGroup = new QButtonGroup(this);
43   myButtonGroup->setExclusive(true);
44   myButtonGroup->addButton(aColorChoiceBtn, 0);
45   myButtonGroup->addButton(aRandomChoiceBtn, 1);
46
47   aLay->addWidget(aColorChoiceBtn, 0, 0);
48   aLay->addWidget(aRandomChoiceBtn, 1, 0);
49
50   myColorButton = new QtxColorButton(this);
51   myColorButton->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
52   aLay->addWidget(myColorButton, 0, 1);
53
54   QLabel* aRandomLabel = new QLabel(tr("Random"), this);
55   aLay->addWidget(aRandomLabel, 1, 1);
56
57   QDialogButtonBox* aButtons = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel,
58                                                     Qt::Horizontal, this);
59   connect(aButtons, SIGNAL(accepted()), this, SLOT(accept()));
60   connect(aButtons, SIGNAL(rejected()), this, SLOT(reject()));
61   aLay->addWidget(aButtons, 2, 0, 1, 2);
62 }
63
64 bool XGUI_ColorDialog::isRandomColor() const
65 {
66   int anId = myButtonGroup->checkedId();
67
68   return myButtonGroup->checkedId() == 1;
69 }
70
71 void XGUI_ColorDialog::setColor(const std::vector<int>& theValue)
72 {
73   if (theValue.size() != 3)
74     return;
75
76   myColorButton->setColor(QColor(theValue[0], theValue[1], theValue[2]));
77 }
78
79 std::vector<int> XGUI_ColorDialog::getColor() const
80 {
81   QColor aColorResult = myColorButton->color();
82
83   std::vector<int> aValues;
84   aValues.push_back(aColorResult.red());
85   aValues.push_back(aColorResult.green());
86   aValues.push_back(aColorResult.blue());
87
88   return aValues;
89 }
90 // contains global cash for integer index of the color -> RGB of this color
91 static std::map<int, std::vector<int> > myColorMap;
92
93 void appendValues(std::vector<int>& theRGB, const int theRed, const int theGreen, const int theBlue)
94 {
95   theRGB.push_back(theRed);
96   theRGB.push_back(theGreen);
97   theRGB.push_back(theBlue);
98 }
99
100 bool containsValues(std::map<int, std::vector<int> >& theColorMap, std::vector<int>& theValues)
101 {
102   std::map<int, std::vector<int> >::const_iterator anIt = theColorMap.begin(),
103                                                    aLast = theColorMap.end();
104   bool isFound = false;
105   for (; anIt != aLast && !isFound; anIt++) {
106     std::vector<int> aValues = anIt->second;
107     isFound = aValues[0] == theValues[0] &&
108               aValues[1] == theValues[1] &&
109               aValues[2] == theValues[2];
110   }
111   return isFound;
112 }
113
114 std::vector<int> HSVtoRGB(int theH, int theS, int theV)
115 {
116   std::vector<int> aRGB;
117   if (theH < 0 || theH > 360 ||
118       theS < 0 || theS > 100 ||
119       theV < 0 || theV > 100)
120     return aRGB;
121
122   int aHi = (int)theH/60;
123
124   double aV = theV;
125   double aVmin = (100 - theS)*theV/100;
126
127   double anA = (theV - aVmin)* (theH % 60) / 60;
128
129   double aVinc = aVmin + anA;
130   double aVdec = theV - anA;
131
132   double aPercentToValue = 255./100;
133   int aV_int    = (int)(aV*aPercentToValue);
134   int aVinc_int = (int)(aVinc*aPercentToValue);
135   int aVmin_int = (int)(aVmin*aPercentToValue);
136   int aVdec_int = (int)(aVdec*aPercentToValue);
137
138   switch(aHi) {
139     case 0: appendValues(aRGB, aV_int,    aVinc_int, aVmin_int); break;
140     case 1: appendValues(aRGB, aVdec_int, aV_int,    aVmin_int); break;
141     case 2: appendValues(aRGB, aVmin_int, aV_int,    aVinc_int); break;
142     case 3: appendValues(aRGB, aVmin_int, aVdec_int, aV_int); break;
143     case 4: appendValues(aRGB, aVinc_int, aVmin_int, aV_int); break;
144     case 5: appendValues(aRGB, aV_int,    aVmin_int, aVdec_int); break;
145     default: break;
146   }
147   return aRGB;
148 }
149
150
151 void fillColorMap()
152 {
153   if (!myColorMap.empty())
154     return;
155
156   int i = 0;
157   for (int s = 100; s > 0; s = s - 50)
158   {
159     for (int v = 100; v >= 40; v = v - 20)
160     {
161       for (int h = 0; h < 359 ; h = h + 60)
162       {
163         std::vector<int> aColor = HSVtoRGB(h, s, v);
164         if (containsValues(myColorMap, aColor))
165           continue;
166         myColorMap[i] = aColor;
167         i++;
168       }
169     }
170   }
171 }
172
173 void findRandomColor(std::vector<int>& theValues)
174 {
175   theValues.clear();
176   if (myColorMap.empty()) {
177     fillColorMap();
178   }
179
180   size_t aSize = myColorMap.size();
181   int anIndex = rand() % aSize;
182   if (myColorMap.find(anIndex) != myColorMap.end()) {
183     theValues = myColorMap.at(anIndex);
184   }
185 }
186
187 std::vector<int> XGUI_ColorDialog::getRandomColor() const
188 {
189   std::vector<int> aValues;
190   if (isRandomColor()) {
191     findRandomColor(aValues);
192   }
193   return aValues;
194 }