SlicingColorMapWidget.cpp
Go to the documentation of this file.
1 /* Copyright (C) 2008 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/SlicingColorMapWidget.cpp
22 
23 \brief A widget used to adjust the color map with the minimal required parameters.
24 */
25 
26 // TerraLib
27 #include "../../../color/ColorBar.h"
28 #include "../../../common/STLUtils.h"
29 #include "../../../dataaccess/dataset/DataSet.h"
30 #include "../../../dataaccess/dataset/DataSetType.h"
31 #include "../../../dataaccess/utils/Utils.h"
32 #include "../../../datatype.h"
33 #include "../../../maptools/DataSetLayer.h"
34 #include "../../../maptools/GroupingAlgorithms.h"
35 #include "../../../maptools/RasterLayer.h"
36 #include "../../../fe/Literal.h"
37 #include "../../../fe/Utils.h"
38 #include "../../../raster.h"
39 #include "../../../raster/RasterSummary.h"
40 #include "../../../raster/RasterSummaryManager.h"
41 #include "../../../se/ColorMap.h"
42 #include "../../../se/Categorize.h"
43 #include "../../../se/MapItem.h"
44 #include "../../../se/ParameterValue.h"
45 #include "../../../se/RasterSymbolizer.h"
46 #include "../../../se/Rule.h"
47 #include "../../../se/Utils.h"
48 #include "../../widgets/colorbar/ColorBar.h"
49 #include "../../widgets/colorbar/ColorCatalogWidget.h"
50 #include "SlicingColorMapWidget.h"
51 #include "ui_SlicingColorMapForm.h"
52 
53 // Qt
54 #include <QColorDialog>
55 #include <QMessageBox>
56 #include <QPainter>
57 #include <QTableWidget>
58 #include <QValidator>
59 
60 // STL
61 #include <cassert>
62 
64  : QWidget(parent, f),
65  m_ui(new Ui::SlicingColorMapForm),
66  m_cm(nullptr),
67  m_cb(nullptr),
68  m_raster(nullptr)
69 {
70  m_ui->setupUi(this);
71 
72  QGridLayout* l = new QGridLayout(m_ui->m_colorBarWidget);
73  l->setContentsMargins(0, 0, 0, 0);
74  m_colorBar = new te::qt::widgets::ColorCatalogWidget(m_ui->m_colorBarWidget);
75  l->addWidget(m_colorBar);
76 
77  m_ui->m_minValueLineEdit->setValidator(new QDoubleValidator(this));
78  m_ui->m_maxValueLineEdit->setValidator(new QDoubleValidator(this));
79 
82 
83  // Signals & slots
84  connect(m_colorBar, SIGNAL(colorBarChanged()), this, SLOT(onApplyPushButtonClicked()));
85  connect(m_ui->m_applyPushButton, SIGNAL(clicked()), this, SLOT(onApplyPushButtonClicked()));
86  connect(m_ui->m_tableWidget, SIGNAL(itemDoubleClicked(QTableWidgetItem*)), this, SLOT(onTableWidgetItemDoubleClicked(QTableWidgetItem*)));
87 }
88 
90 {
91  delete m_cb;
92 
93  delete m_cm;
94 }
95 
97 {
98  assert(r);
99 
100  m_raster = r;
101 
102  int nBands = static_cast<int>(m_raster->getNumberOfBands());
103  if (nBands > 0)
104  onBandSelected(0);
105 }
106 
108 {
109  assert(cm);
110 
111  m_cm = cm->clone();
112 
113  updateUi(true);
114 }
115 
117 {
119 
120  c->setFallbackValue("#000000");
121  c->setLookupValue(new te::se::ParameterValue("Rasterdata"));
122 
123  for (int i = 0; i < m_ui->m_tableWidget->rowCount(); ++i)
124  {
125  QColor color = QColor::fromRgb(m_ui->m_tableWidget->item(i, 0)->icon().pixmap(24, 24).toImage().pixel(1, 1));
126 
127  std::string rangeStr = m_ui->m_tableWidget->item(i, 1)->text().toUtf8().data();
128  rangeStr += " ";
129  rangeStr += m_ui->m_tableWidget->item(i, 2)->text().toUtf8().data();
130  std::string colorStr = color.name().toUtf8().data();
131 
132  c->addThreshold(new te::se::ParameterValue(rangeStr));
133  c->addValue(new te::se::ParameterValue(colorStr));
134  }
135 
136  if (m_cm)
137  {
138  m_cm->setCategorize(c);
139  m_cm->setInterpolate(nullptr);
140  m_cm->setRecode(nullptr);
141  }
142 
143  return m_cm->clone();
144 }
145 
147 {
148  return m_ui.get();
149 }
150 
152 {
153  m_ui->m_tableWidget->setRowCount(0);
154 
155  if (!m_cm)
156  {
157  return;
158  }
159 
160  te::color::ColorBar* cb = nullptr;
161 
162  if (m_cm->getCategorize())
163  {
164  std::vector<te::se::ParameterValue*> t = m_cm->getCategorize()->getThresholds();
165  std::vector<te::se::ParameterValue*> tV = m_cm->getCategorize()->getThresholdValues();
166 
167  m_ui->m_slicesSpinBox->setValue(static_cast<int>(tV.size() - 2));
168 
169  m_ui->m_tableWidget->setRowCount(static_cast<int>(tV.size() - 2));
170 
171  te::color::RGBAColor initColor(te::se::GetString(tV[1]));
172  te::color::RGBAColor endColor(te::se::GetString(tV[tV.size() - 2]));
173 
174  if (loadColorBar)
175  cb = new te::color::ColorBar(initColor, endColor, 256);
176 
177  int count = 0;
178 
179  for (size_t i = 1; i < tV.size() - 1; ++i)
180  {
181  QColor color;
182  std::string lowerLimit;
183  std::string upperLimit;
184 
185  if (i == 0)
186  {
187  lowerLimit = "...";
188  upperLimit = te::se::GetString(t[i]);
189  color.setNamedColor(te::se::GetString(tV[i]).c_str());
190  }
191  else if (i == tV.size() - 1)
192  {
193  lowerLimit = te::se::GetString(t[i - 1]);
194  upperLimit = "...";
195  color.setNamedColor(te::se::GetString(tV[i]).c_str());
196  }
197  else
198  {
199  lowerLimit = te::se::GetString(t[i - 1]);
200  upperLimit = te::se::GetString(t[i]);
201  color.setNamedColor(te::se::GetString(tV[i]).c_str());
202  }
203 
204  if (loadColorBar)
205  {
206  if (count != 0 && count != static_cast<int>(tV.size()) - 2)
207  {
208  double pos = (1. / (tV.size() - 2)) * count;
209 
211 
212  cb->addColor(color, pos);
213  }
214  }
215 
216  ++count;
217 
218  //color
219  QPixmap pix(24, 24);
220  QPainter p(&pix);
221 
222  p.fillRect(0, 0, 24, 24, color);
223  p.setBrush(Qt::transparent);
224  p.setPen(Qt::black);
225  p.drawRect(0, 0, 23, 23);
226 
227  QIcon icon(pix);
228 
229  QTableWidgetItem* item = new QTableWidgetItem(pix, "");
230  item->setFlags(Qt::ItemIsEnabled);
231  m_ui->m_tableWidget->setItem(static_cast<int>(i - 1), 0, item);
232 
233  //value From
234  std::string rangeLower = lowerLimit;
235  QTableWidgetItem* itemRangeLower = new QTableWidgetItem();
236  itemRangeLower->setText(rangeLower.c_str());
237  itemRangeLower->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled | Qt::ItemIsEditable);
238  m_ui->m_tableWidget->setItem(static_cast<int>(i - 1), 1, itemRangeLower);
239 
240  //value To
241  std::string rangeUpper = upperLimit;
242  QTableWidgetItem* itemRangeUpper = new QTableWidgetItem();
243  itemRangeUpper->setText(rangeUpper.c_str());
244  itemRangeUpper->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled | Qt::ItemIsEditable);
245  m_ui->m_tableWidget->setItem(static_cast<int>(i - 1), 2, itemRangeUpper);
246  }
247  }
248 
249  if (cb)
250  {
251  disconnect(m_colorBar, SIGNAL(colorBarChanged()), this, SLOT(onApplyPushButtonClicked()));
252 
254  cbW->setColorBar(cb);
255 
256  connect(m_colorBar, SIGNAL(colorBarChanged()), this, SLOT(onApplyPushButtonClicked()));
257  }
258 
259  m_ui->m_tableWidget->resizeColumnsToContents();
260 #if (QT_VERSION >= 0x050000)
261  m_ui->m_tableWidget->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
262 #else
263  m_ui->m_tableWidget->horizontalHeader()->setResizeMode(QHeaderView::Stretch);
264 #endif
265 }
266 
268 {
271  const std::complex<double>* cmin = rsMin->at(band).m_minVal;
272  const std::complex<double>* cmax = rsMax->at(band).m_maxVal;
273  double min = cmin->real();
274  double max = cmax->real();
275 
276  QString strMin;
277  strMin.setNum(min);
278  m_ui->m_minValueLineEdit->setText(strMin);
279 
280  QString strMax;
281  strMax.setNum(max);
282  m_ui->m_maxValueLineEdit->setText(strMax);
283 
284  emit applyPushButtonClicked();
285 }
286 
288 {
289  delete m_cb;
290 
292 
293  int sliceValue = m_ui->m_slicesSpinBox->value();
294 
295  std::vector<te::color::RGBAColor> colorVec;
296 
297  colorVec = m_cb->getSlices(sliceValue);
298 
299  std::vector<te::se::Rule*> legVec;
300 
301  std::vector<double> vec;
302  vec.push_back(m_ui->m_minValueLineEdit->text().toDouble());
303  vec.push_back(m_ui->m_maxValueLineEdit->text().toDouble());
304 
305  te::map::GroupingByEqualSteps("raster", vec.begin(), vec.end(), sliceValue, legVec, m_ui->m_precisionSpinBox->value());
306 
307  m_ui->m_tableWidget->setRowCount(static_cast<int>(legVec.size()));
308 
309 
310  for (std::size_t t = 0; t < legVec.size(); ++t)
311  {
312  //color
313  QColor color(colorVec[t].getRed(), colorVec[t].getGreen(), colorVec[t].getBlue(), colorVec[t].getAlpha());
314 
315  QPixmap pix(24, 24);
316  QPainter p(&pix);
317 
318  p.fillRect(0, 0, 24, 24, color);
319  p.setBrush(Qt::transparent);
320  p.setPen(Qt::black);
321  p.drawRect(0, 0, 23, 23);
322 
323  QIcon icon(pix);
324 
325  QTableWidgetItem* item = new QTableWidgetItem(icon, "");
326  item->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled);
327  m_ui->m_tableWidget->setItem(static_cast<int>(t), 0, item);
328 
329  std::string rangeStrLower;
330  std::string rangeStrUpper;
331 
332  te::fe::GetFilterStepValues(legVec[t]->getFilter(), rangeStrLower, rangeStrUpper);
333 
334  //value lower
335  QTableWidgetItem* itemRangeLower = new QTableWidgetItem();
336  itemRangeLower->setText(rangeStrLower.c_str());
337  itemRangeLower->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled | Qt::ItemIsEditable);
338  m_ui->m_tableWidget->setItem(static_cast<int>(t), 1, itemRangeLower);
339 
340  //value upper
341  QTableWidgetItem* itemRangeUpper = new QTableWidgetItem();
342  itemRangeUpper->setText(rangeStrUpper.c_str());
343  itemRangeUpper->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled | Qt::ItemIsEditable);
344  m_ui->m_tableWidget->setItem(static_cast<int>(t), 2, itemRangeUpper);
345  }
346 
347  m_ui->m_tableWidget->resizeColumnsToContents();
348 
349 #if (QT_VERSION >= 0x050000)
350  m_ui->m_tableWidget->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
351 #else
352  m_ui->m_tableWidget->horizontalHeader()->setResizeMode(QHeaderView::Stretch);
353 #endif
354 
355  emit applyPushButtonClicked();
356 }
357 
359 {
360  int curCol = m_ui->m_tableWidget->currentColumn();
361 
362  if (curCol == 0)
363  {
364  QColor bgColor = QColor::fromRgb(item->icon().pixmap(24, 24).toImage().pixel(1, 1));
365 
366  QColor newBgColor = QColorDialog::getColor(bgColor, m_ui->m_tableWidget);
367 
368  if (newBgColor.isValid())
369  bgColor = newBgColor;
370 
371  QPixmap pix(24, 24);
372  pix.fill(bgColor);
373  QIcon icon(pix);
374 
375  item->setIcon(icon);
376  }
377 
378  emit applyPushButtonClicked();
379 }
void updateUi(bool loadColorBar=false)
Updates the widget form based on internal fill element.
void setScaleVisible(bool flag)
Sets scale of color bar visibility.
unsigned int band
void onTableWidgetItemDoubleClicked(QTableWidgetItem *item)
te::se::ColorMap * m_cm
SE Color Map element.
ColorMap * clone() const
Definition: ColorMap.cpp:100
The transformation of continuous values to distinct values (Categorize function). ...
Definition: Categorize.h:90
std::unique_ptr< Ui::SlicingColorMapForm > m_ui
Dialog form.
void setHeight(int value)
Sets the height of colobar.
void setLookupValue(ParameterValue *v)
Definition: Categorize.cpp:81
void setCategorize(Categorize *c)
Definition: ColorMap.cpp:67
void GroupingByEqualSteps(std::string attrName, iterator begin, iterator end, int nSteps, std::vector< te::se::Rule * > &rules, int precision=0)
It groups the values defined by a range of iterators using the equal steps algorithm.
The "ParameterValueType" uses WFS-Filter expressions to give values for SE graphic parameters...
te::qt::widgets::ColorCatalogWidget * m_colorBar
Widget used to pick a color.
const std::vector< ParameterValue * > & getThresholds() const
Definition: Categorize.cpp:109
TEFEEXPORT void GetFilterStepValues(const te::fe::Filter *filter, std::string &valueMin, std::string &valueMax)
static RasterSummaryManager & getInstance()
It returns a reference to the singleton instance.
An abstract class for raster data strucutures.
virtual std::size_t getNumberOfBands() const =0
Returns the number of bands (dimension of cells attribute values) in the raster.
boost::ptr_vector< BandSummary > RasterSummary
RasterSummary is just a typedef of a boost::ptr_vector.
Definition: RasterSummary.h:44
te::gm::Polygon * p
void setRecode(Recode *i)
Definition: ColorMap.cpp:89
const std::vector< te::color::RGBAColor > & getSlices(const int &n)
It generates color bar.
void addColor(const RGBAColor &color, const double &pos)
It adds a color in the color bar.
SlicingColorMapWidget(QWidget *parent=0, Qt::WindowFlags f=0)
Constructs a SlicingColorMapWidget which is a child of parent, with widget flags set to f...
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.
Categorize * getCategorize() const
Definition: ColorMap.cpp:73
It models the concept of color bar.
te::qt::widgets::colorbar::ColorBar * getColorBar()
A widget used to adjust the color map with the minimal required parameters.
void setColorBar(te::color::ColorBar *cb)
It sets the color bar.
Calculate the min value.
Calculate the max value.
te::rst::Raster * m_raster
TerraLib raster object.
void setInterpolate(Interpolate *i)
Definition: ColorMap.cpp:78
void addThreshold(ParameterValue *v)
Definition: Categorize.cpp:94
TESEEXPORT std::string GetString(const te::se::ParameterValue *param)
It gets the parameter value as a string.
A ColorMap defines either the colors of a pallette-type raster source or the mapping of numeric pixel...
Definition: ColorMap.h:61
void addValue(ParameterValue *v)
Definition: Categorize.cpp:99
te::color::ColorBar * m_cb
Terralib color bar object.
const std::vector< ParameterValue * > & getThresholdValues() const
Definition: Categorize.cpp:114
void setFallbackValue(const std::string &v)
Definition: se/Function.cpp:33