RasterHistogramWidget.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/rp/RasterHistogramWidget.cpp
22 
23  \brief This file has the RasterHistogramWidget class.
24 */
25 
26 // TerraLib
27 #include "../../../dataaccess/dataset/DataSet.h"
28 #include "../../../dataaccess/utils/Utils.h"
29 #include "../../../raster/Band.h"
30 #include "../../../raster/Raster.h"
31 #include "../../../raster/RasterSummaryManager.h"
32 #include "../../../raster/Utils.h"
33 #include "../charts/ChartDisplay.h"
34 #include "../charts/ChartStyle.h"
35 #include "../charts/Histogram.h"
36 #include "../charts/HistogramChart.h"
37 #include "../charts/HistogramStyle.h"
38 #include "RasterHistogramWidget.h"
39 #include "ui_RasterHistogramWidgetForm.h"
40 
41 // Qt
42 #include <QGridLayout>
43 
44 //Qwt
45 #include <qwt_legend.h>
46 
47 //STL
48 #include <memory>
49 
51  : QWidget(parent, f),
52  m_ui(new Ui::RasterHistogramWidgetForm)
53 {
54  m_ui->setupUi(this);
55 
56  m_ui->m_applyToolButton->setIcon(QIcon::fromTheme("chart-bar"));
57 
58  m_inputRaster = nullptr;
59  m_minValueLine = nullptr;
60  m_maxValueLine = nullptr;
61 
62  //Creating and adjusting the chart Display's style.
64  m_chartStyle->setTitle(tr(""));
65  m_chartStyle->setAxisX(tr("Gray Level"));
66  m_chartStyle->setAxisY(tr("Frequency"));
68 
69  //build form
70  QGridLayout* layout = new QGridLayout(m_ui->m_widget);
71 
72  //Creating and adjusting the chart Display
73  m_chartDisplay = new te::qt::widgets::ChartDisplay(m_ui->m_widget, QString::fromUtf8(""), m_chartStyle);
75  m_chartDisplay->show();
76  m_chartDisplay->replot();
77 
78  layout->addWidget(m_chartDisplay, 0, 0);
79  layout->setContentsMargins(0,0,0,0);
80 
83  m_histogramChartInput->setPen(Qt::black);
84  m_histogramChartInput->setBrush(QBrush(Qt::blue));
86  m_histogramChartInput->setTitle(tr("Input"));
87 
90  m_histogramChartOutput->setPen(Qt::black, 3.);
91  m_histogramChartOutput->setBrush(QBrush(QColor(255, 0, 0, 127)));
92  m_histogramChartOutput->setStyle(QwtPlotHistogram::Outline);
94  m_histogramChartOutput->setTitle(tr("Output"));
95 
96  m_chartDisplay->insertLegend(new QwtLegend(), QwtPlot::RightLegend);
98 
99  //connects
100  connect(m_ui->m_applyToolButton, SIGNAL(clicked()), this, SLOT(onApplyToolButtonClicked()));
101  connect(m_chartDisplay, SIGNAL(leftPointSelected(const QPointF &)), this, SLOT(onLeftPointSelected(const QPointF &)));
102  connect(m_chartDisplay, SIGNAL(rigthPointSelected(const QPointF &)), this, SLOT(onRigthPointSelected(const QPointF &)));
103 
104  //hide tool bar
105  m_ui->m_frame->setVisible(false);
106 }
107 
109 
110 Ui::RasterHistogramWidgetForm* te::qt::widgets::RasterHistogramWidget::getForm() const
111 {
112  return m_ui.get();
113 }
114 
116 {
117  m_histogramChartInput->setBrush(QBrush(QColor(red, green, blue)));
118 }
119 
121 {
122  m_chartDisplay->enableTools(enable);
123 }
124 
126 {
129  m_chartDisplay->enablePan(enable);
130 }
131 
133 {
134  if(!raster)
135  return;
136 
137  m_inputRaster = raster;
138 
139  //set bands from input raster
140  m_ui->m_bandComboBox->clear();
141 
142  for(unsigned int i = 0; i < m_inputRaster->getNumberOfBands(); ++i)
143  {
144  QString strBand;
145  strBand.setNum(i);
146 
147  m_ui->m_bandComboBox->addItem(strBand);
148  }
149 }
150 
152 {
153  m_outputRaster.reset(raster);
154 }
155 
157 {
158  QString toFind = QString::number(band);
159  int idx = m_ui->m_bandComboBox->findText(toFind);
160  m_ui->m_bandComboBox->setCurrentIndex(idx);
161 
162  if(m_inputRaster)
163  {
164  m_histogramInput->setValues(std::map<te::dt::AbstractData*, unsigned int>());
165 
166  std::map<double, unsigned int>* values = te::rst::RasterSummaryManager::getInstance().get(m_inputRaster, te::rst::SUMMARY_R_HISTOGRAM)->at(band).m_histogramR;
167 
168  for(std::map<double, unsigned int>::iterator it = (*values).begin(); it != (*values).end(); ++it)
169  {
170  m_histogramInput->insert(std::make_pair(new te::dt::Double(it->first), it->second));
171  }
172 
175  }
176  else
177  m_histogramChartInput->detach();
178 
179  if(m_outputRaster.get())
180  {
181  m_histogramOutput->setValues(std::map<te::dt::AbstractData*, unsigned int>());
182 
183  double max = 0.;
184 
185  std::map<double, unsigned int> values = m_outputRaster->getBand(band)->getHistogramR();
186 
187  for(std::map<double, unsigned int>::iterator it = values.begin(); it != values.end(); ++it)
188  {
189  m_histogramOutput->insert(std::make_pair(new te::dt::Double(it->first), it->second));
190 
191  if(it->second > max)
192  max = it->second;
193  }
194 
197  }
198  else
199  m_histogramChartOutput->detach();
200 
201  m_chartDisplay->updateLayout();
202  m_chartDisplay->replot();
203 }
204 
206 {
207  if(m_minValueLine)
208  {
209  if(enable)
211  else
212  m_minValueLine->detach();
213  }
214 }
215 
217 {
218  if(!m_minValueLine)
219  {
220  m_minValueLine = new QwtPlotMarker();
221  m_minValueLine->setLabel(QString::fromUtf8("Minimum"));
222  m_minValueLine->setLabelAlignment(Qt::AlignLeft | Qt::AlignVCenter);
223  m_minValueLine->setLabelOrientation(Qt::Vertical);
224  m_minValueLine->setLineStyle(QwtPlotMarker::VLine);
225  m_minValueLine->setLinePen(Qt::darkRed, 2, Qt::DashDotLine);
227  }
228 
229  m_minValueLine->setXValue(value);
230 
231  if(replot)
232  {
233  m_chartDisplay->updateLayout();
234 
235  m_chartDisplay->replot();
236  }
237 }
238 
240 {
241  if(!m_minValueLine)
242  {
243  m_minValueLine = new QwtPlotMarker();
244  m_minValueLine->setLabel(QString::fromUtf8("Minimum"));
245  m_minValueLine->setLabelAlignment(Qt::AlignLeft | Qt::AlignVCenter);
246  m_minValueLine->setLabelOrientation(Qt::Vertical);
247  m_minValueLine->setLineStyle(QwtPlotMarker::VLine);
248  m_minValueLine->setLinePen(Qt::darkRed, 2, Qt::DashDotDotLine);
250  }
251 
252  m_minValueLine->setXValue(value);
253 
254  if(replot)
255  {
256  m_chartDisplay->updateLayout();
257 
258  m_chartDisplay->replot();
259  }
260 }
261 
263 {
264  if(m_minValueLine)
265  m_minValueLine->setLabel(value);
266 }
267 
269 {
270  if(m_maxValueLine)
271  {
272  if(enable)
274  else
275  m_maxValueLine->detach();
276  }
277 }
278 
280 {
281  if(!m_maxValueLine)
282  {
283  m_maxValueLine = new QwtPlotMarker();
284  m_maxValueLine->setLabel(QString::fromUtf8("Maximum"));
285  m_maxValueLine->setLabelAlignment(Qt::AlignLeft | Qt::AlignVCenter);
286  m_maxValueLine->setLabelOrientation(Qt::Vertical);
287  m_maxValueLine->setLineStyle(QwtPlotMarker::VLine);
288  m_maxValueLine->setLinePen(Qt::darkRed, 2, Qt::DashDotLine);
290  }
291 
292  m_maxValueLine->setXValue(value);
293 
294  if(replot)
295  {
296  m_chartDisplay->updateLayout();
297 
298  m_chartDisplay->replot();
299  }
300 }
301 
303 {
304  if(!m_maxValueLine)
305  {
306  m_maxValueLine = new QwtPlotMarker();
307  m_maxValueLine->setLabel(QString::fromUtf8("Maximum"));
308  m_maxValueLine->setLabelAlignment(Qt::AlignLeft | Qt::AlignVCenter);
309  m_maxValueLine->setLabelOrientation(Qt::Vertical);
310  m_maxValueLine->setLineStyle(QwtPlotMarker::VLine);
311  m_maxValueLine->setLinePen(Qt::darkRed, 2, Qt::DashDotDotLine);
313  }
314 
315  m_maxValueLine->setXValue(value);
316 
317  if(replot)
318  {
319  m_chartDisplay->updateLayout();
320 
321  m_chartDisplay->replot();
322  }
323 }
324 
326 {
327  if(m_maxValueLine)
328  m_maxValueLine->setLabel(value);
329 }
330 
332 {
333  delete m_minValueLine;
334  m_minValueLine = nullptr;
335 
336  delete m_maxValueLine;
337  m_maxValueLine = nullptr;
338 }
339 
341 {
342  m_histogramChartInput->detach();
343  m_histogramChartOutput->detach();
344  delete m_histogramInput;
345  delete m_histogramOutput;
346 
348 
349  m_inputRaster = nullptr;
350  m_outputRaster.reset(nullptr);
351 
354  m_histogramChartInput->setPen(Qt::black);
355  m_histogramChartInput->setBrush(QBrush(Qt::blue));
357  m_histogramChartInput->setTitle(tr("Input"));
358 
361  m_histogramChartOutput->setPen(Qt::black, 3.);
362  m_histogramChartOutput->setBrush(QBrush(QColor(255, 0, 0, 127)));
363  m_histogramChartOutput->setStyle(QwtPlotHistogram::Outline);
365  m_histogramChartOutput->setTitle(tr("Output"));
366 
367  m_chartDisplay->replot();
368 }
369 
371 {
372  int index = m_ui->m_bandComboBox->currentIndex();
373 
374  drawHistogram(index);
375 }
376 
378 {
379  emit minValueSelected((int)point.x(), m_ui->m_bandComboBox->currentIndex());
380 
381  emit minValueSelected((double)point.x(), m_ui->m_bandComboBox->currentIndex());
382 }
383 
385 {
386  emit maxValueSelected((int)point.x(), m_ui->m_bandComboBox->currentIndex());
387 
388  emit maxValueSelected((double)point.x(), m_ui->m_bandComboBox->currentIndex());
389 }
void setChartInputColor(int red, int green, int blue)
unsigned int band
te::qt::widgets::HistogramChart * m_histogramChartOutput
This file has the RasterHistogramWidget class.
void adjustDisplay()
Updates the general display settings according to the ChartStyle. The adjusted properties are: Title;...
te::qt::widgets::ChartDisplay * m_chartDisplay
void attach(QwtPlot *plot)
It atttaches a QwtPlot to this Cahrt.
void setGridChecked(bool newGridChecked)
It sets the boolean used to decided weather to display the grid or not.
Definition: ChartStyle.cpp:155
void enablePan(bool enabled)
Enables or disables the tool used to execute pan functions.
A class to represent a Histogram.
Definition: Histogram.h:56
void enableTools(bool enabled)
Enables or disables all the tools used to execute certain mouse actions. Such as selection, pan and zoomn;.
void setTitle(QString newTitle)
It sets the style&#39;s title.
Definition: ChartStyle.cpp:71
Ui::RasterHistogramWidgetForm * getForm() const
te::qt::widgets::Histogram * m_histogramInput
void updateMaximumValueLine(int value, bool replot=false)
void setOutputRaster(te::rst::Raster *raster)
static RasterSummaryManager & getInstance()
It returns a reference to the singleton instance.
An abstract class for raster data strucutures.
void setAxisY(QString newAxisY)
It sets the style&#39;s y axis label.
Definition: ChartStyle.cpp:101
virtual std::size_t getNumberOfBands() const =0
Returns the number of bands (dimension of cells attribute values) in the raster.
te::qt::widgets::Histogram * m_histogramOutput
A class to represent a chart display.
Definition: ChartDisplay.h:65
void setAxisX(QString newAxisX)
It sets the style&#39;s x axis label.
Definition: ChartStyle.cpp:91
te::qt::widgets::ChartStyle * m_chartStyle
void updateMinimumValueLine(int value, bool replot=false)
void setValues(std::map< te::dt::AbstractData *, unsigned int > values)
It sets the histogram&#39;s values.
Definition: Histogram.cpp:73
void maxValueSelected(int value, int band)
std::unique_ptr< Ui::RasterHistogramWidgetForm > m_ui
std::unique_ptr< te::rst::Raster > m_outputRaster
void minValueSelected(int value, int band)
te::qt::widgets::HistogramChart * m_histogramChartInput
RasterHistogramWidget(QWidget *parent=0, Qt::WindowFlags f=0)
void insert(std::pair< te::dt::AbstractData *, unsigned int > new_value, std::vector< te::da::ObjectId * > valuesOIds)
It adds a new value to the map containing the histogram values.
Definition: Histogram.cpp:125
A template for atomic data types (integers, floats, strings and others).
Definition: SimpleData.h:59
void enableMagnifier(bool enabled)
Enables or disables the tool used to execute zoom functions.
Calculate the histogram for the real part.
void enableMultiSelection(bool enabled)
Enables or disables the tool used to execute the multi selection functions.
void setInputRaster(te::rst::Raster *raster)