All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
ChartLayerWidget.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/charts/ChartLayerWidget.cpp
22 
23  \brief A widget used to build a grouping.
24 */
25 
26 // TerraLib
27 #include "../../../color/RGBAColor.h"
28 #include "../../../common/STLUtils.h"
29 #include "../../../maptools/Chart.h"
30 #include "../../../maptools/Enums.h"
31 #include "../utils/ColorPickerToolButton.h"
32 #include "../Utils.h"
33 #include "ChartLayerWidget.h"
34 #include "ui_ChartLayerWidgetForm.h"
35 
36 // Qt
37 #include <QtCore/QString>
38 #include <QtGui/QColor>
39 #include <QtGui/QMessageBox>
40 #include <QtGui/QPainterPath>
41 
42 // STL
43 #include <memory>
44 
45 te::qt::widgets::ChartLayerWidget::ChartLayerWidget(QWidget* parent, Qt::WindowFlags f)
46  : QWidget(parent, f),
47  m_ui(new Ui::ChartLayerWidgetForm)
48 {
49  m_ui->setupUi(this);
50 
51 // Adjusting the color picker
52  QGridLayout* layout = new QGridLayout(m_ui->m_contourColorWidget);
53  layout->setContentsMargins(0, 0, 0, 0);
54 
55 //Color Picker
56  m_colorPicker = new te::qt::widgets::ColorPickerToolButton(m_ui->m_contourColorWidget);
57  m_colorPicker->setFixedSize(107, 24);
58  layout->addWidget(m_colorPicker);
59 
60  //connects
61  connect(m_ui->m_addToolButton, SIGNAL(clicked()), this, SLOT(onAddToolButtonClicked()));
62  connect(m_ui->m_removeToolButton, SIGNAL(clicked()), this, SLOT(onRemoveToolButtonClicked()));
63  connect(m_ui->m_contourWidthSpinBox, SIGNAL(valueChanged(int)), this, SLOT(updateUi()));
64  connect(m_ui->m_tableWidget, SIGNAL(cellClicked(int, int)), this, SLOT(onItemClicked(int, int)));
65  connect(m_colorPicker, SIGNAL(colorChanged(const QColor&)), this, SLOT(updateUi()));
66 
67  initialize();
68 }
69 
71 {
72  m_chartMap.clear();
73 }
74 
76 {
77  m_layer = layer;
78 
79  listAttributes();
80 }
81 
83 {
84  if(m_chartMap.empty())
85  {
86  QMessageBox::warning(this, tr("Warning"), tr("No attribute selected."));
87  return false;
88  }
89 
90  //chart type
91  int index = m_ui->m_typeComboBox->currentIndex();
92  int chartType = m_ui->m_typeComboBox->itemData(index).toInt();
93 
94  //get attributes
95  std::vector<std::string> attrs;
96  std::vector<te::color::RGBAColor> colors;
97 
98  std::map<std::string, QColor>::iterator it = m_chartMap.begin();
99 
100  while(it != m_chartMap.end())
101  {
102  attrs.push_back(it->first);
104  colors.push_back(c);
105 
106  ++it;
107  }
108 
109  te::map::Chart* chart = new te::map::Chart((te::map::ChartType)chartType, attrs, colors);
110  chart->setHeight((std::size_t)m_ui->m_sizeSpinBox->value());
111  chart->setContourWidth((std::size_t)m_ui->m_contourWidthSpinBox->value());
112  chart->setContourColor(te::qt::widgets::Convert2TerraLib(m_colorPicker->getColor()));
113 
114  // Is necessary compute the max value?
115  if(chart->getType() == te::map::Bar)
116  {
117  double maxValue = 0.0;
118  std::size_t precision = 5;
119 
120  // Gets the chart properties
121  const std::vector<std::string>& properties = chart->getProperties();
122 
123  // Gets the dataset
124  std::auto_ptr<te::da::DataSet> dataset(m_layer->getData());
125 
126  while(dataset->moveNext())
127  {
128  for(std::size_t i = 0; i < properties.size(); ++i)
129  {
130  QString qvalue(dataset->getAsString(properties[i], precision).c_str());
131 
132  double value = qvalue.toDouble();
133 
134  if(value > maxValue)
135  maxValue = value;
136  }
137  }
138 
139  chart->setMaxValue(maxValue);
140  }
141 
142  m_layer->setChart(chart);
143 
144  return true;
145 }
146 
148 {
149  m_chartMap.clear();
150 
151  int type = (int)chart->getType();
152 
153  for(int i = 0; i < m_ui->m_typeComboBox->count(); ++i)
154  {
155  int t = m_ui->m_typeComboBox->itemData(i).toInt();
156 
157  if(t == type)
158  {
159  m_ui->m_typeComboBox->setCurrentIndex(i);
160  break;
161  }
162  }
163 
164  m_ui->m_sizeSpinBox->setValue((int)chart->getHeight());
165  m_ui->m_contourWidthSpinBox->setValue((int)chart->getContourWidth());
166  m_colorPicker->setColor(te::qt::widgets::Convert2Qt(chart->getContourColor()));
167 
168  for(std::size_t t = 0; t < chart->getProperties().size(); ++t)
169  {
170  std::string value = chart->getProperties()[t];
171  QColor c = te::qt::widgets::Convert2Qt(chart->getColor(t));
172  m_chartMap.insert(std::map<std::string, QColor>::value_type(value, c));
173  }
174 
175  updateUi();
176 }
177 
179 {
180  //add chart types
181  m_ui->m_typeComboBox->clear();
182  m_ui->m_typeComboBox->addItem(tr("Pie Chart"), QVariant(te::map::Pie));
183  m_ui->m_typeComboBox->addItem(tr("Bar Chart"), QVariant(te::map::Bar));
184 
185 
186  //set icons
187  m_ui->m_addToolButton->setIcon(QIcon::fromTheme("list-add"));
188  m_ui->m_removeToolButton->setIcon(QIcon::fromTheme("list-remove"));
189 }
190 
192 {
193  m_ui->m_tableWidget->setRowCount(0);
194 
195  std::map<std::string, QColor>::iterator it = m_chartMap.begin();
196 
197  while(it != m_chartMap.end())
198  {
199  int newrow = m_ui->m_tableWidget->rowCount();
200  m_ui->m_tableWidget->insertRow(newrow);
201 
202  QIcon icon(te::qt::widgets::CreatePixmapIcon(28, m_colorPicker->getColor(), it->second, m_ui->m_contourWidthSpinBox->value()));
203 
204  QTableWidgetItem* item = new QTableWidgetItem(icon, "");
205  item->setFlags(Qt::ItemIsEnabled);
206  m_ui->m_tableWidget->setItem(newrow, 0, item);
207 
208  //attr name
209  QTableWidgetItem* itemAttr = new QTableWidgetItem(it->first.c_str());
210  itemAttr->setFlags(Qt::ItemIsEnabled);
211  m_ui->m_tableWidget->setItem(newrow, 1, itemAttr);
212 
213  ++it;
214  }
215 }
216 
218 {
219  std::string value = m_ui->m_attrComboBox->currentText().toStdString();
220 
221  std::map<std::string, QColor>::iterator it = m_chartMap.find(value);
222 
223  if(it != m_chartMap.end())
224  {
225  QMessageBox::warning(this, tr("Warning"), tr("Attribute already selected."));
226  return;
227  }
228 
229  QColor c(rand() % 255,rand() % 255,rand() % 255);
230 
231  m_chartMap.insert(std::map<std::string, QColor>::value_type(value, c));
232 
233  updateUi();
234 }
235 
237 {
238  if(m_ui->m_tableWidget->currentRow() == -1)
239  return;
240 
241  std::string value = m_ui->m_tableWidget->item(m_ui->m_tableWidget->currentRow(), 1)->text().toStdString();
242 
243  std::map<std::string, QColor>::iterator it = m_chartMap.find(value);
244 
245  if(it != m_chartMap.end())
246  m_chartMap.erase(it);
247 
248  updateUi();
249 }
250 
252 {
253  if(column != 0)
254  return;
255 
256  std::string attr = m_ui->m_tableWidget->item(row, 1)->text().toStdString();
257 
258  std::map<std::string, QColor>::iterator it = m_chartMap.find(attr);
259 
260  if(it != m_chartMap.end())
261  {
262  QColor c = QColorDialog::getColor(it->second, this);
263 
264  it->second = c;
265 
266  updateUi();
267  }
268 }
269 
271 {
272  m_ui->m_attrComboBox->clear();
273 
274  std::auto_ptr<te::map::LayerSchema> dsType(m_layer->getSchema());
275 
276  for(size_t t = 0; t < dsType->getProperties().size(); ++t)
277  {
278  te::dt::Property* p = dsType->getProperty(t);
279 
280  int dataType = p->getType();
281 
282  switch(dataType)
283  {
284  case te::dt::INT16_TYPE:
285  case te::dt::INT32_TYPE:
286  case te::dt::INT64_TYPE:
287  case te::dt::FLOAT_TYPE:
288  case te::dt::DOUBLE_TYPE:
290  m_ui->m_attrComboBox->addItem(p->getName().c_str(), p->getType());
291 
292  default:
293  continue;
294  }
295  }
296 }
TEQTWIDGETSEXPORT te::color::RGBAColor Convert2TerraLib(const QColor &color)
It converts a Qt Color to TerraLib Color.
Definition: Utils.cpp:211
void onAddToolButtonClicked()
Function used when the user clicked over the add tool button.
const te::color::RGBAColor & getContourColor() const
Definition: Chart.cpp:98
bool buildChart()
Creates the te::map::Chart object using the interface parameters.
std::size_t getContourWidth() const
Definition: Chart.cpp:108
ChartType getType() const
Definition: Chart.cpp:74
const std::string & getName() const
It returns the property name.
Definition: Property.h:126
void initialize()
Internal method to initialize the widget (e.g.: color, combos, icons, etc.)
void onItemClicked(int row, int column)
Function used when the user clicked over the table (color column).
TEQTWIDGETSEXPORT QPixmap CreatePixmapIcon(const int &size, const QColor &penColor, const QColor &brushColor, const int &contourSize)
It creates a pixmap to use as icon (to be used as legend icon).
Definition: Utils.cpp:324
void setMaxValue(double value)
Definition: Chart.cpp:153
const std::vector< std::string > & getProperties() const
Definition: Chart.cpp:79
void setHeight(std::size_t height)
Definition: Chart.cpp:123
void onRemoveToolButtonClicked()
Function used when the user clicked over the remove tool button.
ChartType
The chart types.
Definition: Enums.h:163
ColorPickerToolButton * m_colorPicker
The color picker used to customise the color of several chart parameters.
A helper class for 32-bit RGBA (Red-Green-Blue-Alpha channel) color.
Definition: RGBAColor.h:57
void updateUi()
Updates the widget form based on internal fill element.
ChartLayerWidget(QWidget *parent=0, Qt::WindowFlags f=0)
Constructs a basic fill widget which is a child of parent, with widget flags set to f...
A widget used to build a chart.
void setContourColor(const te::color::RGBAColor &color)
Definition: Chart.cpp:103
const te::color::RGBAColor & getColor(std::size_t i) const
Definition: Chart.cpp:84
TEQTWIDGETSEXPORT QColor Convert2Qt(const te::color::RGBAColor &color)
It converts a TerraLib Color to Qt Color.
Definition: Utils.cpp:216
It models a property definition.
Definition: Property.h:59
This class represents the informations needed to build map charts.
Definition: Chart.h:51
void setLayer(te::map::AbstractLayerPtr layer)
Set a layer.
Custom tool button used to pick a color.
void setChart(te::map::Chart *chart)
Update the interface with the chart properties.
int getType() const
It returns the property data type.
Definition: Property.h:143
boost::intrusive_ptr< AbstractLayer > AbstractLayerPtr
std::auto_ptr< Ui::ChartLayerWidgetForm > m_ui
Widget form.
std::size_t getHeight() const
Definition: Chart.cpp:118
void setContourWidth(std::size_t width)
Definition: Chart.cpp:113
void listAttributes()
List the layer attributes (from layer schema)