All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
ColorBar.cpp
Go to the documentation of this file.
1 #include "ColorBar.h"
2 
3 // TerraLib
4 #include "../../../color.h"
5 #include "../../../color/ColorBar.h"
6 #include "../../../color/ColorTransform.h"
7 #include "../utils/ColorPickerToolButton.h"
8 #include "../Utils.h"
9 
10 // STL
11 #include <iostream>
12 
13 // QT
14 #include <QAction>
15 #include <QColorDialog>
16 #include <QEvent>
17 #include <QMenu>
18 #include <QPainter>
19 #include <QWidgetAction>
20 
21 // QWT
22 #include <qwt_color_map.h>
23 #include <qwt_interval.h>
24 #include <qwt_scale_engine.h>
25 
26 using namespace std;
27 
28 te::qt::widgets::colorbar::ColorBar::ColorBar(QWidget* parent) : QwtScaleWidget(QwtScaleDraw::BottomScale, parent),
29  m_colorBar(0),
30  m_currentPinPos(0),
31  m_colorBarMenu(0),
32  m_pinMenu(0),
33  m_colorBarPicker(0),
34  m_pinPicker(0),
35  m_addPinAction(0),
36  m_editPinAction(0),
37  m_removePinAction(0)
38 {
39  this->setColorBarEnabled(true);
40 
41  m_interval.setInterval(0., 1.);
42 
43  setHeight(20);
44 
45  m_initialXPos = -1;
46 
47  // sets mouse config
48  setClickPrecision(0.0035);
49  setMouseTracking(true);
50 
52 
53  // Color pickers
56 
57  // Set the actions for the color bar menu
58  m_colorBarMenu = new QMenu(this);
59  m_addPinAction = new QWidgetAction(m_colorBarMenu);
60  m_addPinAction->setDefaultWidget(m_colorBarPicker);
61  m_equalStepAction = new QAction(QObject::tr("Equal Step"), m_colorBarMenu);
62  m_equalStepAction->setStatusTip(QObject::tr("Equal Step"));
63  connect(m_equalStepAction, SIGNAL(triggered()), this, SLOT(equalStep()));
64  connect(m_colorBarPicker, SIGNAL(colorChanged(const QColor&)), this, SLOT(addPin()));
65  m_colorBarMenu->addAction(m_addPinAction);
67 
68  // Set the actions for the color bar pin menu
69  m_pinMenu = new QMenu(this);
70  m_editPinAction = new QWidgetAction(m_pinMenu);
71  m_editPinAction->setStatusTip(QObject::tr("Edit"));
72  m_editPinAction->setDefaultWidget(m_pinPicker);
73  m_removePinAction = new QAction(QObject::tr("Remove"), m_pinMenu);
74  m_removePinAction->setStatusTip(QObject::tr("Remove"));
75  connect(m_pinPicker, SIGNAL(colorChanged(const QColor&)), this, SLOT(editPin()));
76  connect(m_removePinAction, SIGNAL(triggered()), this, SLOT(removePin()));
77  m_pinMenu->addAction(m_editPinAction);
78  m_pinMenu->addAction(m_removePinAction);
79 }
80 
82 {
83  delete m_colorBar;
84 }
85 
87 {
88  this->scaleDraw()->enableComponent(QwtScaleDraw::Backbone, flag);
89  this->scaleDraw()->enableComponent(QwtScaleDraw::Ticks, flag);
90  this->scaleDraw()->enableComponent(QwtScaleDraw::Labels, flag);
91 }
92 
94 {
95  m_height = value;
96 
97  this->setColorBarWidth(m_height);
98 }
99 
101 {
102  m_interval.setMinValue(min);
103  m_interval.setMaxValue(max);
104 
105  setScaleEngine();
106 }
107 
109 {
110  delete m_colorBar;
111 
112  m_colorBar = cb;
113 
114  buildColorBar();
115 }
116 
118 {
119  return new te::color::ColorBar(*m_colorBar);
120 }
121 
123 {
124  QwtLinearScaleEngine se;
125  this->setScaleDiv(se.divideScale(m_interval.minValue(), m_interval.maxValue(), 8, 5));
126 }
127 
129 {
130  m_clickPrecision = precision;
131 }
132 
134 {
135  if(m_colorBar && m_colorBar->getColorMap().size() >= 2)
136  {
137 
138  std::map<double, te::color::RGBAColor> map = m_colorBar->getColorMap();
139 
140  //QwtScaleWidget will delete this pointer automatically
141  QwtLinearColorMap* colormap = new QwtLinearColorMap(
142  QColor(map[0.].getRed(), map[0.].getGreen(), map[0.].getBlue(), map[0.].getAlpha()),
143  QColor(map[1.].getRed(), map[1.].getGreen(), map[1.].getBlue(), map[1.].getAlpha()));
144 
145  std::map<double, te::color::RGBAColor>::const_iterator it = m_colorBar->getColorMap().begin();
146 
147  while(it != m_colorBar->getColorMap().end())
148  {
149  double pos = it->first;
150 
151  if(pos != 0. && pos != 1.)
152  {
153  QColor c(it->second.getRed(), it->second.getGreen(), it->second.getBlue());
154 
155  colormap->addColorStop(pos, c);
156  }
157 
158  ++it;
159  }
160 
161  this->setColorMap(m_interval, colormap);
162 
163  emit colorBarChanged();
164  }
165 }
166 
168 {
169  if(m_currentPinPos != 0 && m_currentPinPos != 1)
170  m_colorBar->remove(m_currentPinPos);
171 
172  buildColorBar();
173  this->repaint();
174 }
175 
177 {
178  QColor c = m_pinPicker->getColor();
179  te::color::RGBAColor rgbaC(c.red(), c.green(), c.blue(), c.alpha());
180 
181  m_colorBar->changeColor(m_currentPinPos, rgbaC);
182 
183  buildColorBar();
184  this->repaint();
185 }
186 
188 {
189  QColor c = m_colorBarPicker->getColor();
190  te::color::RGBAColor rgbaC(c.red(), c.green(), c.blue(), c.alpha());
191 
192  m_colorBar->addColor(rgbaC, m_currentPinPos);
193 
194  m_colorBarMenu->hide();
195 
196  buildColorBar();
197  this->repaint();
198 }
199 
201 {
202  std::map<double, te::color::RGBAColor>::const_iterator it = m_colorBar->getColorMap().begin();
203 
204  std::map<double, double> newpos = std::map<double, double>();
205 
206  int numPins = m_colorBar->getColorMap().size();
207 
208  int increment = (int)this->width() / (numPins-1);
209 
210  int pinCount = 0;
211  while(it != m_colorBar->getColorMap().end())
212  {
213  if(it->first != 0 && it->first != 1)
214  {
215  pinCount == 0 ? pinCount = 2:true;
216  newpos[it->first] = convert2toolbarPos((pinCount-1)*increment);
217  ++pinCount;
218  }
219  ++it;
220  }
221 
222  std::map<double, double>::const_iterator it2 = newpos.begin();
223  while(it2 != newpos.end())
224  {
225  m_colorBar->move(it2->first, it2->second);
226  ++it2;
227  }
228 
229  buildColorBar();
230  this->repaint();
231 }
232 
234 {
235  QwtScaleWidget::paintEvent(e);
236 
237  if(!m_colorBar)
238  return;
239 
240  QPainter p(this);
241  p.setPen(QPen(Qt::black));
242 
243  std::map<double, te::color::RGBAColor>::const_iterator it = m_colorBar->getColorMap().begin();
244 
245  while(it != m_colorBar->getColorMap().end())
246  {
247  double pos = it->first * this->width();
248 
249  int r = it->second.getRed();
250  int g = it->second.getGreen();
251  int b = it->second.getBlue();
252  int a = it->second.getAlpha();
253 
254  p.drawLine(QPoint(pos, 0 + 2), QPoint(pos, m_height + 2));
255  p.setBrush(QBrush(QColor(r, g, b, a)));
256  p.drawEllipse(QPoint(pos, 0 + 2), 2, 2);
257 
258  ++it;
259  }
260 }
261 
263 {
264  if(!m_colorBar)
265  return;
266 
267  double pos = convert2toolbarPos(e->x());
268  double pinPos = getPin(e->x());
269 
270  if(e->button() == Qt::RightButton)
271  {
272  //Pin menu or colorbar menu
273  if(pinPos != -1)
274  {
275  m_currentPinPos = pinPos;
276  te::color::RGBAColor rgbaC = m_colorBar->getColorMap().at(m_currentPinPos);
277  int red = rgbaC.getRed();
278  int green = rgbaC.getGreen();
279  int blue = rgbaC.getBlue();
280  QColor c(red, green, blue);
281  m_pinPicker->setColor(c);
282  m_pinMenu->exec(QCursor::pos());
283  }
284  else
285  {
286  m_currentPinPos = pos;
287  m_colorBarMenu->exec(QCursor::pos());
288  }
289  }
290 
291  //gets the initial position to move the pin
292  if (e->button() == Qt::LeftButton)
293  if(pinPos != -1)
294  m_initialXPos = pinPos;
295 
296 }
297 
299 {
300  //finalize moving stop from colobar
301  if (e->button() == Qt::LeftButton)
302  {
303  m_initialXPos = -1;
304  }
305 }
306 
308 {
309  double currentPos = convert2toolbarPos(e->x());
310 
311  if(getPin(e->x()) != -1)
312  setCursor(Qt::SplitHCursor);
313  else
314  setCursor(Qt::PointingHandCursor);
315 
316  if(m_initialXPos > -1)
317  {
318  m_colorBar->move(m_initialXPos, currentPos);
319 
320  m_initialXPos = currentPos;
321 
322  buildColorBar();
323  this->repaint();
324  }
325 
326 }
327 
329 {
330  double pin = getPin(e->x());
331 
332  if(pin != -1)
333  {
334  int hh, ss, ll, a;
335  int h, s, l, d = 1;
336 
337  te::color::RGBAColor color1 = m_colorBar->getColorMap().at(pin);
338  te::color::RGBAColor color2 = m_colorBar->getColorMap().at(pin);
339 
341  c.getHsl(&hh, &ss, &ll, &a);
342 
343  te::color::ColorTransform qc(color2);
344 
345  d = 8;
346 
347  if(e->delta() < 0)
348  d *= -1;
349 
350  qc.getHsl(&h, &s, &l, &a);
351 
352  l += d;
353 
354  if(l > 245)
355  l = 245;
356  if(l < 10)
357  l = 10;
358 
359  qc.setHsl(hh, ss, l, a);
360 
361  te::color::RGBAColor cor(qc.getRed(), qc.getGreen(), qc.getBlue(), qc.getAlpha());
362  m_colorBar->changeColor(pin, cor);
363 
364  buildColorBar();
365  this->repaint();
366  }
367 
368 }
369 
371 {
372  double width = this->width();
373  double currentPos = (double) pos / width;
374 
375  std::map<double, te::color::RGBAColor>::const_iterator it = m_colorBar->getColorMap().begin();
376 
377  bool isPin = false;
378 
379  while(it != m_colorBar->getColorMap().end())
380  {
381  double colorPos = it->first;
382 
383  if ( colorPos > (currentPos - m_clickPrecision) && colorPos < (currentPos + m_clickPrecision))
384  {
385  currentPos = colorPos;
386  isPin = true;
387  break;
388  }
389  ++it;
390  }
391 
392  if(isPin)
393  return currentPos;
394  else
395  return -1;
396 }
397 
399 {
400  double width = this->width();
401  double currentPos = (double) pos / width;
402 
403  return currentPos;
404 }
void getHsl(int *h, int *s, int *l, int *a=0) const
It gets the h, s, l components of this color.
void getRgba(int *r, int *g, int *b, int *a=0) const
It gets the color value.
Definition: RGBAColor.h:315
QMenu * m_pinMenu
Popup menu for Pin on color bar.
Definition: ColorBar.h:270
int getRed() const
It returns the red component color value (a value from 0 to 255).
Definition: RGBAColor.h:295
virtual void mousePressEvent(QMouseEvent *e)
It waits a pressEvent from mouse to do something.
Definition: ColorBar.cpp:262
void setScaleVisible(bool flag)
Sets scale of color bar visibility.
Definition: ColorBar.cpp:86
QWidgetAction * m_addPinAction
Add Pin Action.
Definition: ColorBar.h:276
void setClickPrecision(double precision)
It sets the precision of mouse click.
Definition: ColorBar.cpp:128
QAction * m_removePinAction
Remove Pin Action.
Definition: ColorBar.h:280
void setHeight(int value)
Sets the height of colobar.
Definition: ColorBar.cpp:93
int getBlue() const
It returns the blue component color value (a value from 0 to 255).
Definition: RGBAColor.h:305
int getGreen() const
It returns the green component color value (a value from 0 to 255).
Definition: RGBAColor.h:300
virtual void mouseReleaseEvent(QMouseEvent *e)
It waits a releaseEvent from mouse to do something.
Definition: ColorBar.cpp:298
te::qt::widgets::ColorPickerToolButton * m_colorBarPicker
Color picker for colorbar menu.
Definition: ColorBar.h:272
QwtInterval m_interval
Color vector.
Definition: ColorBar.h:258
virtual void mouseMoveEvent(QMouseEvent *e)
It waits a moveEvent from mouse to do something.
Definition: ColorBar.cpp:307
int getRed() const
It returns the red component.
int getAlpha() const
It returns the alpha component.
void buildColorBar()
It build and paint the colorbar.
Definition: ColorBar.cpp:133
void setColorBar(te::color::ColorBar *cb)
It sets the color bar.
Definition: ColorBar.cpp:108
double getPin(int pos)
It returns the pin based on mouse click and the click precision.
Definition: ColorBar.cpp:370
virtual void paintEvent(QPaintEvent *e)
Inherit of QEvent, receive events to paint e update widgets.
Definition: ColorBar.cpp:233
Custom tool button used to pick a color.
QMenu * m_colorBarMenu
Popup menu for Pin on color bar.
Definition: ColorBar.h:268
double convert2toolbarPos(int pos)
It convert a mouse position to a toolbar position.
Definition: ColorBar.cpp:398
void addPin()
It add a colorbar pin.
Definition: ColorBar.cpp:187
virtual void wheelEvent(QWheelEvent *e)
It waits the wheel from mouse to do something.
Definition: ColorBar.cpp:328
void editPin()
It edit a colorbar pin.
Definition: ColorBar.cpp:176
void setHsl(int h, int s, int l, int a=255)
It sets the h, s, l components of this color.
void removePin()
It remove a colorbar pin.
Definition: ColorBar.cpp:167
te::qt::widgets::ColorPickerToolButton * m_pinPicker
Color picker for pin menu.
Definition: ColorBar.h:274
double m_initialXPos
Position initial to move color.
Definition: ColorBar.h:262
The concept of color bar.
QAction * m_equalStepAction
Equal Step Pin Action.
Definition: ColorBar.h:282
A helper class for 32-bit RGBA (Red-Green-Blue-Alpha channel) color.
Definition: RGBAColor.h:57
te::color::ColorBar * getColorBar()
It returns the colorbar.
Definition: ColorBar.cpp:117
void equalStep()
It changes the pins positions for an equal distribution.
Definition: ColorBar.cpp:200
It models the concept of color bar.
Definition: ColorBar.h:49
A class to transform RGBA color to HSV, HSL and CMYK.
int getBlue() const
It returns the blue component.
void setScaleEngine()
Sets the transformation and the scale of colorbar values.
Definition: ColorBar.cpp:122
ColorBar(QWidget *parent=0)
It initializes a new ColorBar.
Definition: ColorBar.cpp:28
QWidgetAction * m_editPinAction
Edit Pin Action.
Definition: ColorBar.h:278
void setInterval(double min, double max)
It sets the min e max value of colorbar.
Definition: ColorBar.cpp:100
int getGreen() const
It returns the green component.