HistogramDataWidget.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/charts/HistogramDataWidget.cpp
22 
23  \brief A widget used to adjust a histogram's input data.
24 */
25 
26 //Terralib
27 #include "../../../dataaccess/dataset/DataSet.h"
28 #include "../../../dataaccess/dataset/DataSetType.h"
29 #include "../../../dataaccess/utils/Utils.h"
30 #include "../../../maptools/Utils.h"
31 #include "../../../qt/widgets/charts/Utils.h"
32 #include "../../../raster.h"
33 #include "../../../raster/RasterSummaryManager.h"
34 #include "../../../statistics/core/Enums.h"
35 #include "../../../statistics/core/Utils.h"
36 #include "../utils/ScopedCursor.h"
37 #include "Histogram.h"
38 #include "HistogramDataWidget.h"
39 #include "ui_HistogramDataWidgetForm.h"
40 
41 void updateSummary(te::da::DataSet* dataSet, Ui::HistogramDataWidgetForm* ui)
42 {
43  ui->m_summaryComboBox->clear();
44 
45  size_t selectedPropertyIdx = 0;
46  for (size_t i = 0; i < dataSet->getNumProperties(); i++)
47  {
48  if(ui->m_propertyComboBox->currentText().toUtf8().data() == dataSet->getPropertyName(i))
49  selectedPropertyIdx = i;
50  }
51 
52  int propType = dataSet->getPropertyDataType(selectedPropertyIdx);
53 
54  if(propType == te::dt::DATETIME_TYPE || propType == te::dt::STRING_TYPE)
55  {
56  ui->m_summaryComboBox->addItem(QString::fromUtf8("None"), QVariant(-1));
57  ui->m_summaryComboBox->addItem(QString(te::stat::GetStatSummaryFullName(te::stat::MIN_VALUE).c_str()), QVariant(te::stat::MIN_VALUE));
58  ui->m_summaryComboBox->addItem(QString(te::stat::GetStatSummaryFullName(te::stat::MAX_VALUE).c_str()), QVariant(te::stat::MAX_VALUE));
59  }
60  else
61  {
62  ui->m_summaryComboBox->addItem(QString::fromUtf8("None"), QVariant(-1));
63  ui->m_summaryComboBox->addItem(QString(te::stat::GetStatSummaryFullName(te::stat::MIN_VALUE).c_str()), QVariant(te::stat::MIN_VALUE));
64  ui->m_summaryComboBox->addItem(QString(te::stat::GetStatSummaryFullName(te::stat::MAX_VALUE).c_str()), QVariant(te::stat::MAX_VALUE));
65  ui->m_summaryComboBox->addItem(QString(te::stat::GetStatSummaryFullName(te::stat::MEAN).c_str()), QVariant(te::stat::MEAN));
66  ui->m_summaryComboBox->addItem(QString(te::stat::GetStatSummaryFullName(te::stat::SUM).c_str()), QVariant(te::stat::SUM));
67  ui->m_summaryComboBox->addItem(QString(te::stat::GetStatSummaryFullName(te::stat::STANDARD_DEVIATION).c_str()), QVariant(te::stat::STANDARD_DEVIATION));
68  ui->m_summaryComboBox->addItem(QString(te::stat::GetStatSummaryFullName(te::stat::VARIANCE).c_str()), QVariant(te::stat::VARIANCE));
69  ui->m_summaryComboBox->addItem(QString(te::stat::GetStatSummaryFullName(te::stat::SKEWNESS).c_str()), QVariant(te::stat::SKEWNESS));
70  ui->m_summaryComboBox->addItem(QString(te::stat::GetStatSummaryFullName(te::stat::KURTOSIS).c_str()), QVariant(te::stat::KURTOSIS));
71  ui->m_summaryComboBox->addItem(QString(te::stat::GetStatSummaryFullName(te::stat::AMPLITUDE).c_str()), QVariant(te::stat::AMPLITUDE));
72  ui->m_summaryComboBox->addItem(QString(te::stat::GetStatSummaryFullName(te::stat::MEDIAN).c_str()), QVariant(te::stat::MEDIAN));
73  ui->m_summaryComboBox->addItem(QString(te::stat::GetStatSummaryFullName(te::stat::VAR_COEFF).c_str()), QVariant(te::stat::VAR_COEFF));
74  }
75  ui->m_summaryComboBox->setCurrentIndex(0);
76 }
77 
79  : QWidget(parent, f),
80  m_ui(new Ui::HistogramDataWidgetForm),
81  m_layer(layer)
82 {
83  m_ui->setupUi(this);
84 
85  QString item;
86  std::unique_ptr<te::da::DataSet> dataset = m_layer->getData();
87  std::unique_ptr<te::da::DataSetType> dataType = m_layer->getSchema();
88 
89  if(dataType->hasRaster())
90  {
91  m_ui->m_readAllcheckBox->show();
92 
93  //Adjusting the widget to work with a raster file.
94  std::unique_ptr<te::rst::Raster> raster (te::map::GetRaster(m_layer.get()));
95 
98 
99  const std::complex<double>* cmin = rsMin->at(0).m_minVal;
100  const std::complex<double>* cmax = rsMax->at(0).m_maxVal;
101 
102  double min = cmin->real();
103  double max = cmax->real();
104 
105  size_t size = raster->getNumberOfBands();
106  m_ui->m_slicesSpinBox->setMinimum(0);
107 
108  int type = raster->getBandDataType(0);
109 
110  if((type >= te::dt::UCHAR_TYPE) && (type <= te::dt::UINT32_TYPE))
111  {
112  if (min >= 0 && max <= 255)
113  m_ui->m_slicesSpinBox->setMaximum(255);
114  else
115  m_ui->m_slicesSpinBox->setMaximum(max);
116  }
117 
118  for (size_t i = 0; i < size; i++)
119  {
120  item = QString::number(i);
121  m_ui->m_propertyComboBox->addItem((QString::fromUtf8("Band: ") + item), QVariant::fromValue(i));
122  }
123  }
124  else
125  {
126  m_ui->m_readAllcheckBox->hide();
127  for (std::size_t i = 0; i < dataset->getNumProperties(); i++)
128  {
129  if(dataset->getPropertyDataType(i) != te::dt::GEOMETRY_TYPE)
130  {
131  item = QString::fromUtf8(dataset->getPropertyName(i).c_str());
132  m_ui->m_propertyComboBox->addItem(item, QVariant::fromValue(i));
133  }
134  }
135  }
136 
137  updateSummary(dataset.get(), getForm());
138 
139  if(te::da::HasLinkedTable(dataType.get()))
140  {
141  m_ui->m_summaryComboBox->show();
142  m_ui->m_summaryLabel->show();
143  }
144  else
145  {
146  m_ui->m_summaryComboBox->hide();
147  m_ui->m_summaryLabel->hide();
148  }
149 
150 // connect signal and slots
151  connect(m_ui->m_propertyComboBox, SIGNAL(currentIndexChanged(QString)), this, SLOT(onPropertyComboBoxIndexChanged(QString)));
152  connect(m_ui->m_readAllcheckBox, SIGNAL(stateChanged(int)), this, SLOT(onReadAllCheckBoxStateChanged(int)));
153 }
154 
156 
157 Ui::HistogramDataWidgetForm* te::qt::widgets::HistogramDataWidget::getForm()
158 {
159  return m_ui.get();
160 }
161 
163 {
164  std::unique_ptr<te::da::DataSet> dataset = m_layer->getData();
165  std::unique_ptr<te::da::DataSetType> dataType = m_layer->getSchema();
166  te::qt::widgets::Histogram* histogram;
167 
168  if(dataType->hasRaster())
169  {
170  histogram = te::qt::widgets::createHistogram(m_layer, m_ui->m_propertyComboBox->itemData(m_ui->m_propertyComboBox->currentIndex()).toInt(),
171  m_ui->m_slicesSpinBox->value(), -1, m_ui->m_readAllcheckBox->isChecked());
172  }
173  else
174  {
175  //Getting the Columns that will be used to populate the chart
176 
177  size_t selectedPropertyIdx = 0;
178 
179  for (size_t i = 0; i < dataset->getNumProperties(); i++)
180  {
181  if(m_ui->m_propertyComboBox->currentText().toUtf8().data() == dataset->getPropertyName(i))
182  {
183  selectedPropertyIdx = i;
184  }
185  }
186 
187  int propType = dataset->getPropertyDataType(selectedPropertyIdx);
188  int stat = m_ui->m_summaryComboBox->itemData(m_ui->m_summaryComboBox->currentIndex()).toInt();
189 
190  if(propType == te::dt::DATETIME_TYPE || propType == te::dt::STRING_TYPE)
191  {
192  histogram = te::qt::widgets::createHistogram(m_layer, (int)selectedPropertyIdx, stat);
193  }
194  else
195  {
196  histogram = te::qt::widgets::createHistogram(m_layer, (int)selectedPropertyIdx, m_ui->m_slicesSpinBox->value(), stat);
197  }
198  }
199  return histogram;
200 }
201 
203 {
204  m_ui->m_propertyComboBox->setCurrentIndex(m_ui->m_propertyComboBox->findData(propId));
205  m_ui->m_propertyComboBox->setEnabled(false);
206 }
207 
209  QString /*text*/)
210 {
211  te::qt::widgets::ScopedCursor c(Qt::WaitCursor);
212  std::unique_ptr<te::da::DataSet> dataset = m_layer->getData();
213  std::unique_ptr<te::da::DataSetType> dataType = m_layer->getSchema();
214 
215  if(!dataType->hasRaster())
216  {
217  size_t selectedPropertyIdx= te::da::GetPropertyPos(dataset.get(), m_ui->m_propertyComboBox->currentText().toUtf8().data());
218  int propType = dataset->getPropertyDataType(selectedPropertyIdx);
219 
220  if(propType == te::dt::DATETIME_TYPE || propType == te::dt::STRING_TYPE)
221  {
222  m_ui->m_slicesSpinBox->setEnabled(false);
223  }
224  else
225  {
226  m_ui->m_slicesSpinBox->setEnabled(true);
227  }
228  }
229  else
230  {
231  std::unique_ptr<te::rst::Raster> raster (te::map::GetRaster(m_layer.get()));
232 
235 
236  const std::complex<double>* cmin = rsMin->at(m_ui->m_propertyComboBox->currentIndex()).m_minVal;
237  const std::complex<double>* cmax = rsMax->at(m_ui->m_propertyComboBox->currentIndex()).m_maxVal;
238 
239  double min = cmin->real();
240  double max = cmax->real();
241 
242  if (min >= 0 && max <= 255)
243  m_ui->m_slicesSpinBox->setMaximum(255);
244  else
245  m_ui->m_slicesSpinBox->setMaximum(max);
246 
247  m_ui->m_slicesSpinBox->setValue(255);
248  }
249 
250  updateSummary(dataset.get(), getForm());
251 
252  if(te::da::HasLinkedTable(dataType.get()))
253  {
254  m_ui->m_summaryComboBox->show();
255  m_ui->m_summaryLabel->show();
256  }
257  else
258  {
259  m_ui->m_summaryComboBox->hide();
260  m_ui->m_summaryLabel->hide();
261  }
262 }
263 
265 {
266  if (state == Qt::Checked)
267  m_ui->m_slicesSpinBox->setEnabled(true);
268  else if(state == Qt::CheckState::Unchecked)
269  m_ui->m_slicesSpinBox->setEnabled(false);
270 }
271 
273 {
274  return m_ui->m_summaryComboBox->currentText().toUtf8().data();
275 }
A widget used to adjust a histogram&#39;s input data.
te::map::AbstractLayerPtr m_layer
The layer that will be used to generate the histogram chart.
te::qt::widgets::Histogram * getHistogram()
Returns a pointer to the widget&#39;s form.
TEDATAACCESSEXPORT bool HasLinkedTable(te::da::DataSetType *type)
It checks if the datasettype has a linked table.
TEQTWIDGETSEXPORT Histogram * createHistogram(te::map::AbstractLayerPtr layer, int propId, int slices, int stat, bool readall=true)
Histogram Creator.
std::unique_ptr< Ui::HistogramDataWidgetForm > m_ui
The widget&#39;s form.
TEDATAACCESSEXPORT std::size_t GetPropertyPos(const DataSet *dataset, const std::string &name)
void setHistogramProperty(int propId)
Sets the property to be used in order to generate the histogram.
A class to represent a Histogram.
Definition: Histogram.h:56
A class to represent a histogram.
static RasterSummaryManager & getInstance()
It returns a reference to the singleton instance.
virtual int getPropertyDataType(std::size_t i) const =0
It returns the underlying data type of the property at position pos.
std::string getSummaryFunction()
Returns the name of the chosen summary function. Default is None.
boost::ptr_vector< BandSummary > RasterSummary
RasterSummary is just a typedef of a boost::ptr_vector.
Definition: RasterSummary.h:44
TESTATEXPORT std::string GetStatSummaryFullName(const int &e)
Get the statistical parameter full name ffrom its enumerator.
void updateSummary(te::da::DataSet *dataSet, Ui::HistogramDataWidgetForm *ui)
TEMAPEXPORT te::rst::Raster * GetRaster(AbstractLayer *layer)
It gets the raster referenced by the given data set layer.
A dataset is the unit of information manipulated by the data access module of TerraLib.
Ui::HistogramDataWidgetForm * getForm()
Returns a pointer to the widget&#39;s form.
virtual std::size_t getNumProperties() const =0
It returns the number of properties that composes an item of the dataset.
virtual std::string getPropertyName(std::size_t i) const =0
It returns the property name at position pos.
Calculate the min value.
Calculate the max value.
boost::intrusive_ptr< AbstractLayer > AbstractLayerPtr
An object that when created shows a cursor during its scope.
Definition: ScopedCursor.h:48
HistogramDataWidget(te::map::AbstractLayerPtr layer, QWidget *parent=0, Qt::WindowFlags f=0)
Constructor.