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/Utils.h"
32 #include "../charts/ChartDisplay.h"
33 #include "../charts/ChartStyle.h"
34 #include "../charts/Histogram.h"
35 #include "../charts/HistogramChart.h"
36 #include "../charts/HistogramStyle.h"
37 #include "RasterHistogramWidget.h"
38 #include "ui_RasterHistogramWidgetForm.h"
39 
40 // Qt
41 #include <QGridLayout>
42 
43 //Qwt
44 #include <qwt_legend.h>
45 
46 //STL
47 #include <memory>
48 
50  : QWidget(parent, f),
51  m_ui(new Ui::RasterHistogramWidgetForm)
52 {
53  m_ui->setupUi(this);
54 
55  m_ui->m_applyToolButton->setIcon(QIcon::fromTheme("chart-bar"));
56 
57  m_minValueLine = 0;
58  m_maxValueLine = 0;
59 
60  //Creating and adjusting the chart Display's style.
62  m_chartStyle->setTitle(tr(""));
63  m_chartStyle->setAxisX(tr("Gray Level"));
64  m_chartStyle->setAxisY(tr("Frequency"));
66 
67  //build form
68  QGridLayout* layout = new QGridLayout(m_ui->m_widget);
69 
70  //Creating and adjusting the chart Display
71  m_chartDisplay = new te::qt::widgets::ChartDisplay(m_ui->m_widget, QString::fromStdString(""), m_chartStyle);
73  m_chartDisplay->show();
74  m_chartDisplay->replot();
75 
76  layout->addWidget(m_chartDisplay, 0, 0);
77  layout->setContentsMargins(0,0,0,0);
78 
81  m_histogramChartInput->setPen(Qt::black);
82  m_histogramChartInput->setBrush(QBrush(Qt::blue));
84  m_histogramChartInput->setTitle(tr("Input"));
85 
88  m_histogramChartOutput->setPen(Qt::black, 3.);
89  m_histogramChartOutput->setBrush(QBrush(QColor(255, 0, 0, 127)));
90  m_histogramChartOutput->setStyle(QwtPlotHistogram::Outline);
92  m_histogramChartOutput->setTitle(tr("Output"));
93 
94  m_chartDisplay->insertLegend(new QwtLegend(), QwtPlot::RightLegend);
95 
96  //connects
97  connect(m_ui->m_applyToolButton, SIGNAL(clicked()), this, SLOT(onApplyToolButtonClicked()));
98  connect(m_chartDisplay, SIGNAL(leftPointSelected(const QPointF &)), this, SLOT(onLeftPointSelected(const QPointF &)));
99  connect(m_chartDisplay, SIGNAL(rigthPointSelected(const QPointF &)), this, SLOT(onRigthPointSelected(const QPointF &)));
100 
101  //hide tool bar
102  m_ui->m_frame->setVisible(false);
103 }
104 
106 {
107 }
108 
109 Ui::RasterHistogramWidgetForm* te::qt::widgets::RasterHistogramWidget::getForm() const
110 {
111  return m_ui.get();
112 }
113 
115 {
116  m_inputRaster.reset(raster);
117 
118  //set bands from input raster
119  m_ui->m_bandComboBox->clear();
120 
121  for(unsigned int i = 0; i < m_inputRaster->getNumberOfBands(); ++i)
122  {
123  QString strBand;
124  strBand.setNum(i);
125 
126  m_ui->m_bandComboBox->addItem(strBand);
127  }
128 }
129 
131 {
132  m_outputRaster.reset(raster);
133 }
134 
136 {
137  QString toFind = QString::number(band);
138  int idx = m_ui->m_bandComboBox->findText(toFind);
139  m_ui->m_bandComboBox->setCurrentIndex(idx);
140 
141  if(m_inputRaster.get())
142  {
143  m_histogramInput->setValues(std::map<te::dt::AbstractData*, unsigned int>());
144 
145  std::map<double, unsigned int> values = m_inputRaster->getBand(band)->getHistogramR();
146 
147  for(std::map<double, unsigned int>::iterator it = values.begin(); it != values.end(); ++it)
148  {
149  m_histogramInput->insert(std::make_pair(new te::dt::Double(it->first), it->second));
150  }
151 
152  m_histogramInput->setMinValue(m_inputRaster->getBand(band)->getMinValue().real());
153 
154  m_histogramChartInput->setData();
155  }
156 
157  if(m_outputRaster.get())
158  {
159  m_histogramOutput->setValues(std::map<te::dt::AbstractData*, unsigned int>());
160 
161  double max = 0.;
162 
163  std::map<double, unsigned int> values = m_outputRaster->getBand(band)->getHistogramR();
164 
165  for(std::map<double, unsigned int>::iterator it = values.begin(); it != values.end(); ++it)
166  {
167  m_histogramOutput->insert(std::make_pair(new te::dt::Double(it->first), it->second));
168 
169  if(it->second > max)
170  max = it->second;
171  }
172 
173  m_histogramOutput->setMinValue(m_outputRaster->getBand(band)->getMinValue().real());
174 
175  m_histogramChartOutput->setData();
176  }
177 
178  m_chartDisplay->updateLayout();
179 
180  m_chartDisplay->replot();
181 }
182 
184 {
185  if(m_minValueLine)
186  {
187  if(enable)
188  m_minValueLine->attach(m_chartDisplay);
189  else
190  m_minValueLine->detach();
191  }
192 }
193 
195 {
196  if(!m_minValueLine)
197  {
198  m_minValueLine = new QwtPlotMarker();
199  m_minValueLine->setLabel(QString::fromLatin1("Minimum"));
200  m_minValueLine->setLabelAlignment(Qt::AlignLeft | Qt::AlignVCenter);
201  m_minValueLine->setLabelOrientation(Qt::Vertical);
202  m_minValueLine->setLineStyle(QwtPlotMarker::VLine);
203  m_minValueLine->setLinePen(Qt::darkRed, 2, Qt::DashDotLine);
204  m_minValueLine->attach(m_chartDisplay);
205  }
206 
207  m_minValueLine->setXValue(value);
208 
209  if(replot)
210  {
211  m_chartDisplay->updateLayout();
212 
213  m_chartDisplay->replot();
214  }
215 }
216 
218 {
219  if(m_minValueLine)
220  m_minValueLine->setLabel(value);
221 }
222 
224 {
225  if(m_maxValueLine)
226  {
227  if(enable)
228  m_maxValueLine->attach(m_chartDisplay);
229  else
230  m_maxValueLine->detach();
231  }
232 }
233 
235 {
236  if(!m_maxValueLine)
237  {
238  m_maxValueLine = new QwtPlotMarker();
239  m_maxValueLine->setLabel(QString::fromLatin1("Maximum"));
240  m_maxValueLine->setLabelAlignment(Qt::AlignLeft | Qt::AlignVCenter);
241  m_maxValueLine->setLabelOrientation(Qt::Vertical);
242  m_maxValueLine->setLineStyle(QwtPlotMarker::VLine);
243  m_maxValueLine->setLinePen(Qt::darkRed, 2, Qt::DashDotLine);
244  m_maxValueLine->attach(m_chartDisplay);
245  }
246 
247  m_maxValueLine->setXValue(value);
248 
249  if(replot)
250  {
251  m_chartDisplay->updateLayout();
252 
253  m_chartDisplay->replot();
254  }
255 }
256 
258 {
259  if(m_maxValueLine)
260  m_maxValueLine->setLabel(value);
261 }
262 
264 {
265  int index = m_ui->m_bandComboBox->currentIndex();
266 
267  drawHistogram(index);
268 }
269 
271 {
272  int xMin = (int)point.x();
273 
274  emit minValueSelected(xMin, m_ui->m_bandComboBox->currentIndex());
275 }
276 
278 {
279  int xMax = (int)point.x();
280 
281  emit maxValueSelected(xMax, m_ui->m_bandComboBox->currentIndex());
282 }
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
A class to represent a Histogram.
Definition: Histogram.h:56
void setTitle(QString newTitle)
It sets the style'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)
std::auto_ptr< Ui::RasterHistogramWidgetForm > m_ui
An abstract class for raster data strucutures.
Definition: Raster.h:71
void setAxisY(QString newAxisY)
It sets the style's y axis label.
Definition: ChartStyle.cpp:101
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's x axis label.
Definition: ChartStyle.cpp:91
te::qt::widgets::ChartStyle * m_chartStyle
void updateMinimumValueLine(int value, bool replot=false)
te::qt::widgets::HistogramChart * m_histogramChartInput
RasterHistogramWidget(QWidget *parent=0, Qt::WindowFlags f=0)
A template for atomic data types (integers, floats, strings and others).
Definition: SimpleData.h:59
void setInputRaster(te::rst::Raster *raster)