All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties 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 <QString>
38 #include <QColor>
39 #include <QMessageBox>
40 #include <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  chart->setAvoidConflicts(m_ui->m_avoidConflictsCheckBox->isChecked());
114 
115  // Is necessary compute the max value?
116  if(chart->getType() == te::map::Bar)
117  {
118  double maxValue = 0.0;
119  std::size_t precision = 5;
120 
121  // Gets the chart properties
122  const std::vector<std::string>& properties = chart->getProperties();
123 
124  // Gets the dataset
125  std::auto_ptr<te::da::DataSet> dataset(m_layer->getData());
126 
127  while(dataset->moveNext())
128  {
129  for(std::size_t i = 0; i < properties.size(); ++i)
130  {
131  QString qvalue(dataset->getAsString(properties[i], precision).c_str());
132 
133  double value = qvalue.toDouble();
134 
135  if(value > maxValue)
136  maxValue = value;
137  }
138  }
139 
140  chart->setMaxValue(maxValue);
141  }
142 
143  m_layer->setChart(chart);
144 
145  return true;
146 }
147 
149 {
150  m_chartMap.clear();
151 
152  int type = (int)chart->getType();
153 
154  for(int i = 0; i < m_ui->m_typeComboBox->count(); ++i)
155  {
156  int t = m_ui->m_typeComboBox->itemData(i).toInt();
157 
158  if(t == type)
159  {
160  m_ui->m_typeComboBox->setCurrentIndex(i);
161  break;
162  }
163  }
164 
165  m_ui->m_sizeSpinBox->setValue((int)chart->getHeight());
166  m_ui->m_contourWidthSpinBox->setValue((int)chart->getContourWidth());
167  m_colorPicker->setColor(te::qt::widgets::Convert2Qt(chart->getContourColor()));
168  m_ui->m_avoidConflictsCheckBox->setChecked(chart->getAvoidConflicts());
169 
170  for(std::size_t t = 0; t < chart->getProperties().size(); ++t)
171  {
172  std::string value = chart->getProperties()[t];
173  QColor c = te::qt::widgets::Convert2Qt(chart->getColor(t));
174  m_chartMap.insert(std::map<std::string, QColor>::value_type(value, c));
175  }
176 
177  updateUi();
178 }
179 
181 {
182  //add chart types
183  m_ui->m_typeComboBox->clear();
184  m_ui->m_typeComboBox->addItem(tr("Pie Chart"), QVariant(te::map::Pie));
185  m_ui->m_typeComboBox->addItem(tr("Bar Chart"), QVariant(te::map::Bar));
186 
187 
188  //set icons
189  m_ui->m_addToolButton->setIcon(QIcon::fromTheme("list-add"));
190  m_ui->m_removeToolButton->setIcon(QIcon::fromTheme("list-remove"));
191 }
192 
194 {
195  m_ui->m_tableWidget->setRowCount(0);
196 
197  std::map<std::string, QColor>::iterator it = m_chartMap.begin();
198 
199  while(it != m_chartMap.end())
200  {
201  int newrow = m_ui->m_tableWidget->rowCount();
202  m_ui->m_tableWidget->insertRow(newrow);
203 
204  QIcon icon(te::qt::widgets::CreatePixmapIcon(28, m_colorPicker->getColor(), it->second, m_ui->m_contourWidthSpinBox->value()));
205 
206  QTableWidgetItem* item = new QTableWidgetItem(icon, "");
207  item->setFlags(Qt::ItemIsEnabled);
208  m_ui->m_tableWidget->setItem(newrow, 0, item);
209 
210  //attr name
211  QTableWidgetItem* itemAttr = new QTableWidgetItem(it->first.c_str());
212  itemAttr->setFlags(Qt::ItemIsEnabled);
213  m_ui->m_tableWidget->setItem(newrow, 1, itemAttr);
214 
215  ++it;
216  }
217 }
218 
220 {
221  std::string value = m_ui->m_attrComboBox->currentText().toStdString();
222 
223  std::map<std::string, QColor>::iterator it = m_chartMap.find(value);
224 
225  if(it != m_chartMap.end())
226  {
227  QMessageBox::warning(this, tr("Warning"), tr("Attribute already selected."));
228  return;
229  }
230 
231  QColor c(rand() % 255,rand() % 255,rand() % 255);
232 
233  m_chartMap.insert(std::map<std::string, QColor>::value_type(value, c));
234 
235  updateUi();
236 }
237 
239 {
240  if(m_ui->m_tableWidget->currentRow() == -1)
241  return;
242 
243  std::string value = m_ui->m_tableWidget->item(m_ui->m_tableWidget->currentRow(), 1)->text().toStdString();
244 
245  std::map<std::string, QColor>::iterator it = m_chartMap.find(value);
246 
247  if(it != m_chartMap.end())
248  m_chartMap.erase(it);
249 
250  updateUi();
251 }
252 
254 {
255  if(column != 0)
256  return;
257 
258  std::string attr = m_ui->m_tableWidget->item(row, 1)->text().toStdString();
259 
260  std::map<std::string, QColor>::iterator it = m_chartMap.find(attr);
261 
262  if(it != m_chartMap.end())
263  {
264  QColor c = QColorDialog::getColor(it->second, this);
265 
266  it->second = c;
267 
268  updateUi();
269  }
270 }
271 
273 {
274  m_ui->m_attrComboBox->clear();
275 
276  std::auto_ptr<te::map::LayerSchema> dsType(m_layer->getSchema());
277 
278  for(size_t t = 0; t < dsType->getProperties().size(); ++t)
279  {
280  te::dt::Property* p = dsType->getProperty(t);
281 
282  int dataType = p->getType();
283 
284  switch(dataType)
285  {
286  case te::dt::INT16_TYPE:
287  case te::dt::INT32_TYPE:
288  case te::dt::INT64_TYPE:
289  case te::dt::FLOAT_TYPE:
290  case te::dt::DOUBLE_TYPE:
292  m_ui->m_attrComboBox->addItem(p->getName().c_str(), p->getType());
293 
294  default:
295  continue;
296  }
297  }
298 }
void setLayer(te::map::AbstractLayerPtr layer)
Set a layer.
void setChart(te::map::Chart *chart)
Update the interface with the chart properties.
ChartType
The chart types.
Definition: Enums.h:163
ChartType getType() const
Definition: Chart.cpp:76
TEQTWIDGETSEXPORT te::color::RGBAColor Convert2TerraLib(const QColor &color)
It converts a Qt Color to TerraLib Color.
Definition: Utils.cpp:211
const std::vector< std::string > & getProperties() const
Definition: Chart.cpp:81
void setContourColor(const te::color::RGBAColor &color)
Definition: Chart.cpp:105
void updateUi()
Updates the widget form based on internal fill element.
A widget used to build a chart.
void onAddToolButtonClicked()
Function used when the user clicked over the add tool button.
void setHeight(std::size_t height)
Definition: Chart.cpp:125
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...
It models a property definition.
Definition: Property.h:59
ColorPickerToolButton * m_colorPicker
The color picker used to customise the color of several chart parameters.
void onRemoveToolButtonClicked()
Function used when the user clicked over the remove tool button.
void initialize()
Internal method to initialize the widget (e.g.: color, combos, icons, etc.)
void setAvoidConflicts(bool on)
Definition: Chart.cpp:175
const te::color::RGBAColor & getColor(std::size_t i) const
Definition: Chart.cpp:86
Custom tool button used to pick a color.
void onItemClicked(int row, int column)
Function used when the user clicked over the table (color column).
This class represents the informations needed to build map charts.
Definition: Chart.h:51
void listAttributes()
List the layer attributes (from layer schema)
std::size_t getHeight() const
Definition: Chart.cpp:120
int getType() const
It returns the property data type.
Definition: Property.h:143
bool buildChart()
Creates the te::map::Chart object using the interface parameters.
std::size_t getContourWidth() const
Definition: Chart.cpp:110
const te::color::RGBAColor & getContourColor() const
Definition: Chart.cpp:100
std::auto_ptr< Ui::ChartLayerWidgetForm > m_ui
Widget form.
A helper class for 32-bit RGBA (Red-Green-Blue-Alpha channel) color.
Definition: RGBAColor.h:57
bool getAvoidConflicts() const
Definition: Chart.cpp:180
TEQTWIDGETSEXPORT QColor Convert2Qt(const te::color::RGBAColor &color)
It converts a TerraLib Color to Qt Color.
Definition: Utils.cpp:216
void setMaxValue(double value)
Definition: Chart.cpp:155
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
boost::intrusive_ptr< AbstractLayer > AbstractLayerPtr
void setContourWidth(std::size_t width)
Definition: Chart.cpp:115
const std::string & getName() const
It returns the property name.
Definition: Property.h:126