Salome HOME
Issue #1735: Names in deflection dialog box.
[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(), aLast = theColorMap.end();
89   bool isFound = false;
90   for (; anIt != aLast && !isFound; anIt++) {
91     std::vector<int> aValues = anIt->second;
92     isFound = aValues[0] == theValues[0] &&
93               aValues[1] == theValues[1] &&
94               aValues[2] == theValues[2];
95   }
96   return isFound;
97 }
98
99 std::vector<int> HSVtoRGB(int theH, int theS, int theV)
100 {
101   std::vector<int> aRGB;
102   if (theH < 0 || theH > 360 ||
103       theS < 0 || theS > 100 ||
104       theV < 0 || theV > 100)
105     return aRGB;
106
107   int aHi = (int)theH/60;
108
109   double aV = theV;
110   double aVmin = (100 - theS)*theV/100;
111
112   double anA = (theV - aVmin)* (theH % 60) / 60;
113
114   double aVinc = aVmin + anA;
115   double aVdec = theV - anA;
116
117   double aPercentToValue = 255./100;
118   int aV_int    = (int)(aV*aPercentToValue);
119   int aVinc_int = (int)(aVinc*aPercentToValue);
120   int aVmin_int = (int)(aVmin*aPercentToValue);
121   int aVdec_int = (int)(aVdec*aPercentToValue);
122
123   switch(aHi) {
124     case 0: appendValues(aRGB, aV_int,    aVinc_int, aVmin_int); break;
125     case 1: appendValues(aRGB, aVdec_int, aV_int,    aVmin_int); break;
126     case 2: appendValues(aRGB, aVmin_int, aV_int,    aVinc_int); break;
127     case 3: appendValues(aRGB, aVmin_int, aVdec_int, aV_int); break;
128     case 4: appendValues(aRGB, aVinc_int, aVmin_int, aV_int); break;
129     case 5: appendValues(aRGB, aV_int,    aVmin_int, aVdec_int); break;
130     default: break;
131   }
132   return aRGB;
133 }
134
135
136 void fillColorMap()
137 {
138   if (!myColorMap.empty())
139     return;
140
141   int i = 0;
142   for (int s = 100; s > 0; s = s - 50)
143   {
144     for (int v = 100; v >= 40; v = v - 20)
145     {
146       for (int h = 0; h < 359 ; h = h + 60)
147       {
148         std::vector<int> aColor = HSVtoRGB(h, s, v);
149         if (containsValues(myColorMap, aColor))
150           continue;
151         myColorMap[i] = aColor;
152         i++;
153       }
154     }
155   }
156 }
157
158 void findRandomColor(std::vector<int>& theValues)
159 {
160   theValues.clear();
161   if (myColorMap.empty()) {
162     fillColorMap();
163   }
164
165   size_t aSize = myColorMap.size();
166   int anIndex = rand() % aSize;
167   if (myColorMap.find(anIndex) != myColorMap.end()) {
168     theValues = myColorMap.at(anIndex);
169   }
170 }
171
172 std::vector<int> XGUI_ColorDialog::getRandomColor() const
173 {
174   std::vector<int> aValues;
175   if (isRandomColor()) {
176     findRandomColor(aValues);
177   }
178   return aValues;
179 }