All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
ColorMapWidget.cpp
Go to the documentation of this file.
1 /* Copyright (C) 2011-2012 National Institute For Space Research (INPE) - Brazil.
2 
3  This file is part of the TerraLib - a Framework for building GIS enabled applications.
4 
5  TerraLib is free software: you can redistribute it and/or modify
6  it under the terms of the GNU Lesser General Public License as published by
7  the Free Software Foundation, either version 3 of the License,
8  or (at your option) any later version.
9 
10  TerraLib is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU Lesser General Public License for more details.
14 
15  You should have received a copy of the GNU Lesser General Public License
16  along with TerraLib. See COPYING. If not, write to
17  TerraLib Team at <terralib-team@terralib.org>.
18  */
19 
20 /*!
21  \file terralib/qt/widgets/se/ColorMapWidget.cpp
22 
23  \brief A dialog used to build a Color Map element.
24 */
25 
26 // TerraLib
27 
28 #include "../../../color/ColorBar.h"
29 #include "../../../common/STLUtils.h"
30 #include "../../../datatype.h"
31 #include "../../../maptools/GroupingAlgorithms.h"
32 #include "../../../maptools/Utils.h"
33 #include "../../../raster.h"
34 #include "../../../raster/RasterSummary.h"
35 #include "../../../raster/RasterSummaryManager.h"
36 #include "../../../se/ColorMap.h"
37 #include "../../../se/Categorize.h"
38 #include "../../../se/Enums.h"
39 #include "../../../se/Interpolate.h"
40 #include "../../../se/InterpolationPoint.h"
41 #include "../../../se/ParameterValue.h"
42 #include "../../widgets/colorbar/ColorBar.h"
43 #include "ColorMapWidget.h"
44 #include "ui_ColorMapWidgetForm.h"
45 
46 // Qt
47 #include <QtGui/QPainter>
48 #include <QtGui/QValidator>
49 
50 // STL
51 #include <cassert>
52 
53 
54 te::qt::widgets::ColorMapWidget::ColorMapWidget(QWidget* parent, Qt::WindowFlags f)
55  : QWidget(parent, f),
56  m_ui(new Ui::ColorMapWidgetForm),
57  m_cm(0),
58  m_raster(0)
59 {
60  m_ui->setupUi(this);
61 
62  QGridLayout* l = new QGridLayout(m_ui->m_colorBarWidget);
63  l->setContentsMargins(0,0,0,0);
64  m_cbWidget = new te::qt::widgets::colorbar::ColorBar(m_ui->m_colorBarWidget);
65  l->addWidget(m_cbWidget);
66 
67  m_ui->m_minValueLineEdit->setValidator(new QDoubleValidator(this));
68  m_ui->m_maxValueLineEdit->setValidator(new QDoubleValidator(this));
69 
70  initialize();
71 
72  // Signals & slots
73  connect(m_cbWidget, SIGNAL(colorBarChanged()), this, SLOT(onApplyPushButtonClicked()));
74  connect(m_ui->m_bandComboBox, SIGNAL(activated(QString)), this, SLOT(onBandSelected(QString)));
75  connect(m_ui->m_applyPushButton, SIGNAL(clicked()), this, SLOT(onApplyPushButtonClicked()));
76 }
77 
79 {
80  delete m_cb;
81 
82  delete m_cm;
83 }
84 
86 {
87  assert(r);
88 
89  m_raster = r;
90 
91  int nBands = m_raster->getNumberOfBands();
92 
93  m_ui->m_bandComboBox->clear();
94 
95  for(int i = 0; i < nBands; ++i)
96  {
97  QString strBand;
98  strBand.setNum(i);
99 
100  m_ui->m_bandComboBox->addItem(strBand);
101  }
102 
103  if(nBands > 0)
104  onBandSelected(m_ui->m_bandComboBox->itemText(0));
105 }
106 
108 {
109  assert(cm);
110 
111  m_cm = cm->clone();
112 
113  updateUi();
114 }
115 
117 {
118  return m_cm->clone();
119 }
120 
122 {
123  if(m_ui->m_bandComboBox->count() != 0)
124  {
125  return m_ui->m_bandComboBox->currentText().toStdString();
126  }
127 
128  return "";
129 }
130 
132 {
133  m_cb = new te::color::ColorBar(te::color::RGBAColor(255, 0, 0, TE_OPAQUE), te::color::RGBAColor(0, 0, 0, TE_OPAQUE), 256);
134 
135  m_cbWidget->setHeight(20);
136  m_cbWidget->setColorBar(m_cb);
137  m_cbWidget->setScaleVisible(false);
138 
139  m_ui->m_transformComboBox->clear();
140 
141  m_ui->m_transformComboBox->addItem(tr("Categorize"), te::se::CATEGORIZE_TRANSFORMATION);
142  m_ui->m_transformComboBox->addItem(tr("Interpolate"), te::se::INTERPOLATE_TRANSFORMATION);
143  //m_ui->m_transformComboBox->addItem(tr("Recode"), te::se::RECODE_TRANSFORMATION);
144 }
145 
147 {
148  m_ui->m_tableWidget->setRowCount(0);
149 
150  if(!m_cm)
151  {
152  return;
153  }
154 
155  if(m_cm->getCategorize())
156  {
157  for(int i = 0; i < m_ui->m_transformComboBox->count(); ++i)
158  {
159  if(m_ui->m_transformComboBox->itemData(i).toInt() == te::se::CATEGORIZE_TRANSFORMATION)
160  m_ui->m_transformComboBox->setCurrentIndex(i);
161  }
162 
163  std::vector<te::se::ParameterValue*> t = m_cm->getCategorize()->getThresholds();
164  std::vector<te::se::ParameterValue*> tV = m_cm->getCategorize()->getThresholdValues();
165 
166  m_ui->m_slicesSpinBox->setValue(tV.size() - 2);
167 
168  m_ui->m_tableWidget->setRowCount(tV.size() - 2);
169 
170  for(size_t i = 1; i < tV.size() - 1; ++i)
171  {
172  QColor color;
173  std::string lowerLimit = "";
174  std::string upperLimit = "";
175 
176  if(i == 0)
177  {
178  lowerLimit = "...";
179  upperLimit = te::map::GetString(t[i]);
180  color.setNamedColor(te::map::GetString(tV[i]).c_str());
181  }
182  else if(i == tV.size() - 1)
183  {
184  lowerLimit = te::map::GetString(t[i - 1]);
185  upperLimit = "...";
186  color.setNamedColor(te::map::GetString(tV[i]).c_str());
187  }
188  else
189  {
190  lowerLimit = te::map::GetString(t[i - 1]);
191  upperLimit = te::map::GetString(t[i]);
192  color.setNamedColor(te::map::GetString(tV[i]).c_str());
193  }
194 
195  //color
196  QTableWidgetItem* item = new QTableWidgetItem();
197  item->setBackgroundColor(color);
198  item->setFlags(Qt::ItemIsSelectable|Qt::ItemIsEnabled);
199  m_ui->m_tableWidget->setItem(i - 1, 1, item);
200 
201  //value
202  std::string range = lowerLimit + " - " + upperLimit;
203  QTableWidgetItem* itemRange = new QTableWidgetItem();
204  itemRange->setText(range.c_str());
205  itemRange->setFlags(Qt::ItemIsSelectable|Qt::ItemIsEnabled);
206  m_ui->m_tableWidget->setItem(i - 1, 0, itemRange);
207 
208  //label
209  std::string rangeStr = lowerLimit + " to " + upperLimit;
210  QTableWidgetItem* itemRangeStr = new QTableWidgetItem();
211  itemRangeStr->setText(rangeStr.c_str());
212  itemRangeStr->setFlags(Qt::ItemIsSelectable|Qt::ItemIsEnabled);
213  m_ui->m_tableWidget->setItem(i - 1, 2, itemRangeStr);
214  }
215  }
216  else if(m_cm->getInterpolate())
217  {
218  for(int i = 0; i < m_ui->m_transformComboBox->count(); ++i)
219  {
220  if(m_ui->m_transformComboBox->itemData(i).toInt() == te::se::INTERPOLATE_TRANSFORMATION)
221  m_ui->m_transformComboBox->setCurrentIndex(i);
222  }
223 
224  std::vector<te::se::InterpolationPoint*> ip = m_cm->getInterpolate()->getInterpolationPoints();
225 
226  m_ui->m_slicesSpinBox->setValue(ip.size() - 1);
227 
228  m_ui->m_tableWidget->setRowCount(ip.size() - 1);
229 
230  for(size_t i = 0; i < ip.size() - 1; ++i)
231  {
232  QColor color;
233  QString valStrBegin;
234  QString valStrEnd;
235 
236  te::se::InterpolationPoint* ipItem = ip[i];
237 
238  color.setNamedColor(te::map::GetString(ipItem->getValue()).c_str());
239 
240  valStrBegin.setNum(ipItem->getData());
241  valStrEnd.setNum(ip[i+1]->getData());
242 
243  QString valStr;
244  valStr.append(valStrBegin);
245  valStr.append(" - ");
246  valStr.append(valStrEnd);
247 
248  //color
249  QTableWidgetItem* item = new QTableWidgetItem();
250  item->setBackgroundColor(color);
251  item->setFlags(Qt::ItemIsSelectable|Qt::ItemIsEnabled);
252  m_ui->m_tableWidget->setItem(i, 1, item);
253 
254  //value
255  QTableWidgetItem* itemRange = new QTableWidgetItem();
256  itemRange->setText(valStr);
257  itemRange->setFlags(Qt::ItemIsSelectable|Qt::ItemIsEnabled);
258  m_ui->m_tableWidget->setItem(i, 0, itemRange);
259 
260  //label
261  QTableWidgetItem* itemRangeStr = new QTableWidgetItem();
262  itemRangeStr->setText(valStr);
263  itemRangeStr->setFlags(Qt::ItemIsSelectable|Qt::ItemIsEnabled);
264  m_ui->m_tableWidget->setItem(i, 2, itemRangeStr);
265  }
266  }
267 
268  m_ui->m_tableWidget->resizeColumnToContents(0);
269  m_ui->m_tableWidget->resizeColumnToContents(1);
270 }
271 
273 {
274  int sliceValue = m_ui->m_slicesSpinBox->value();
275 
276  std::vector<te::color::RGBAColor> colorVec = m_cb->getSlices(sliceValue);
277 
278  std::vector<te::map::GroupingItem*> legVec;
279 
280  std::vector<double> vec;
281  vec.push_back(m_ui->m_minValueLineEdit->text().toDouble());
282  vec.push_back(m_ui->m_maxValueLineEdit->text().toDouble());
283 
284  te::map::GroupingByEqualSteps(vec.begin(), vec.end(), sliceValue, legVec, 1);
285 
287 
288  c->setFallbackValue("#000000");
289  c->setLookupValue(new te::se::ParameterValue("Rasterdata"));
290 
291  QColor cWhite(Qt::white);
292  std::string colorWhiteStr = cWhite.name().toLatin1().data();
293 
294  //added dummy color for values < than min values...
295  c->addValue(new te::se::ParameterValue(colorWhiteStr));
296 
297  for(size_t i = 0; i < colorVec.size(); ++i)
298  {
299  QColor color(colorVec[i].getRed(), colorVec[i].getGreen(), colorVec[i].getBlue(), colorVec[i].getAlpha());
300 
301  std::string rangeStr = legVec[i]->getLowerLimit();
302  std::string colorStr = color.name().toStdString();
303 
304  c->addThreshold(new te::se::ParameterValue(rangeStr));
305  c->addValue(new te::se::ParameterValue(colorStr));
306 
307  if(i == colorVec.size() - 1)
308  {
309  rangeStr = legVec[i]->getUpperLimit();
310  c->addThreshold(new te::se::ParameterValue(rangeStr));
311  }
312  }
313 
314  //added dummy color for values > than max values...
315  c->addValue(new te::se::ParameterValue(colorWhiteStr));
316 
317  c->setThresholdsBelongTo(te::se::Categorize::SUCCEEDING);
318 
319  if(m_cm)
320  {
321  m_cm->setCategorize(c);
322  m_cm->setInterpolate(0);
323  }
324 }
325 
327 {
328  int sliceValue = m_ui->m_slicesSpinBox->value();
329 
330  std::vector<te::color::RGBAColor> colorVec = m_cb->getSlices(sliceValue + 1);
331 
332  std::vector<te::map::GroupingItem*> legVec;
333 
334  std::vector<double> vec;
335  vec.push_back(m_ui->m_minValueLineEdit->text().toDouble());
336  vec.push_back(m_ui->m_maxValueLineEdit->text().toDouble());
337 
338  te::map::GroupingByEqualSteps(vec.begin(), vec.end(), sliceValue, legVec, 1);
339 
340 
341  te::se::Interpolate* interpolate = new te::se::Interpolate();
342 
343  interpolate->setFallbackValue("#000000");
344  interpolate->setLookupValue(new te::se::ParameterValue("Rasterdata"));
345  interpolate->setMethodType(te::se::Interpolate::COLOR);
346 
347 
348  for(size_t i = 0; i < colorVec.size(); ++i)
349  {
350  QColor color(colorVec[i].getRed(), colorVec[i].getGreen(), colorVec[i].getBlue(), colorVec[i].getAlpha());
351 
352  if(i == colorVec.size() - 1)
353  {
354  QString rangeStr = legVec[i - 1]->getUpperLimit().c_str();
355  std::string colorStr = color.name().toLatin1().data();
356 
358 
359  ip->setData(rangeStr.toDouble());
360  ip->setValue(new te::se::ParameterValue(colorStr));
361 
362  interpolate->add(ip);
363  }
364  else
365  {
366  QString rangeStr = legVec[i]->getLowerLimit().c_str();
367  std::string colorStr = color.name().toLatin1().data();
368 
370 
371  ip->setData(rangeStr.toDouble());
372  ip->setValue(new te::se::ParameterValue(colorStr));
373 
374  interpolate->add(ip);
375  }
376  }
377 
378  if(m_cm)
379  {
380  m_cm->setInterpolate(interpolate);
381  m_cm->setCategorize(0);
382  }
383 }
384 
386 {
387 
388 }
389 
390 
392 {
393  int index = m_ui->m_transformComboBox->currentIndex();
394 
395  int type = m_ui->m_transformComboBox->itemData(index).toInt();
396 
398  {
399  buildCategorizationMap();
400  }
401  else if(type == te::se::INTERPOLATE_TRANSFORMATION)
402  {
403  buildInterpolationMap();
404  }
405  else if(type == te::se::RECODE_TRANSFORMATION)
406  {
407  buildRecodingMap();
408  }
409 
410  emit applyPushButtonClicked();
411 
412  updateUi();
413 }
414 
416 {
417  if(value.isEmpty())
418  {
419  return;
420  }
421 
424  const std::complex<double>* cmin = rsMin->at(0).m_minVal;
425  const std::complex<double>* cmax = rsMax->at(0).m_maxVal;
426  double min = cmin->real();
427  double max = cmax->real();
428 
429  QString strMin;
430  strMin.setNum(min);
431  m_ui->m_minValueLineEdit->setText(strMin);
432 
433  QString strMax;
434  strMax.setNum(max);
435  m_ui->m_maxValueLineEdit->setText(strMax);
436 }
437 
boost::ptr_vector< BandSummary > RasterSummary
RasterSummary is just a typedef of a boost::ptr_vector.
Definition: RasterSummary.h:44
It models the concept of color bar.
Definition: ColorBar.h:49
void setData(const double &d)
te::se::ColorMap * getColorMap()
They are used to define a graph of points.
void setColorMap(te::se::ColorMap *cm)
ColorMapWidget(QWidget *parent=0, Qt::WindowFlags f=0)
Constructs a ShadedReliefWidget dialog which is a child of parent, with widget flags set to f...
The transformation of continuous values to distinct values (Categorize function). ...
Definition: Categorize.h:90
It QWidget implementation of color bar.
Definition: ColorBar.h:67
Calculate the max value.
Definition: Enums.h:41
ColorMap * clone() const
Definition: ColorMap.cpp:80
virtual std::size_t getNumberOfBands() const =0
Returns the number of bands (dimension of cells attribute values) in the raster.
void setRaster(te::rst::Raster *r)
The transformation of continuous values to a number of values (Interpolate function).
Definition: Interpolate.h:88
void setValue(ParameterValue *v)
te::qt::widgets::colorbar::ColorBar * m_cbWidget
QWT widget for color bar.
A ColorMap defines either the colors of a pallette-type raster source or the mapping of numeric pixel...
Definition: ColorMap.h:60
std::auto_ptr< Ui::ColorMapWidgetForm > m_ui
Dialog form.
void GroupingByEqualSteps(iterator begin, iterator end, int nSteps, std::vector< te::map::GroupingItem * > &legend, int precision=0, bool countElements=true)
It groups the values defined by a range of iterators using the equal steps algorithm.
void onBandSelected(QString value)
void updateUi()
Updates the widget form based on internal fill element.
A helper class for 32-bit RGBA (Red-Green-Blue-Alpha channel) color.
Definition: RGBAColor.h:57
TEMAPEXPORT std::string GetString(const te::se::ParameterValue *param)
Gets the parameter value as string.
Definition: Utils.cpp:132
ParameterValue * getValue() const
#define TE_OPAQUE
For an RGBA color this is the value of the alpha-channel for totally opaque.
Definition: Config.h:39
A widget used to build.
An abstract class for raster data strucutures.
Definition: Raster.h:70
void initialize()
Internal method to initialize the widget (e.g.: color, combos, icons, etc.)
static RasterSummaryManager & getInstance()
It returns a reference to the singleton instance.
The &quot;ParameterValueType&quot; uses WFS-Filter expressions to give values for SE graphic parameters...
Calculate the min value.
Definition: Enums.h:40