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