All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
ScatterChart.cpp
Go to the documentation of this file.
1 /* Copyright (C) 2010-2013 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 ScatterChart.cpp
22 
23  \brief A class to represent a scatter chart.
24 */
25 
26 // TerraLib
27 
28 
29 #include "../../../dataaccess/dataset/ObjectIdSet.h"
30 #include "Enums.h"
31 #include "ScatterChart.h"
32 #include "Scatter.h"
33 #include "ScatterStyle.h"
34 #include "Utils.h"
35 
36 //QWT
37 #include <qwt_symbol.h>
38 #include <qwt_plot.h>
39 
40 //STL
41 #include <limits>
42 
44  QwtPlotCurve(),
45  m_scatter(data)
46 {
47  //Set style
48  setStyle(QwtPlotCurve::NoCurve);
49  setPaintAttribute(QwtPlotCurve::FilterPoints );
50 
51  m_selection = new QwtPlotCurve();
52  m_selection->setStyle(QwtPlotCurve::NoCurve);
53  m_selection->setPaintAttribute(QwtPlotCurve::FilterPoints);
54  m_selection->attach(plot());
55 
56  //Set Values
57  setData();
58 
59  //Adjusting the symbol
61  if(m_scatter->sizeX() > 100 || m_scatter->sizeY() > 100)
62  {
63  QwtSymbol* newSymbol = m_scatterStyle->getSymbol();
64  newSymbol->setSize(QSize( 1, 1 ));
65  setSymbol(newSymbol);
66  }
67  else
68  {
69  setSymbol(new QwtSymbol( QwtSymbol::Ellipse, QBrush( Qt::gray ), QPen( Qt::black, 1 ), QSize( 8, 8 )));
70  }
71 
72  //The default selection color is green
73  m_selColor = ("#00FF00");
74  m_selection->setSymbol(new QwtSymbol( symbol()->style(), m_selColor, m_selColor, symbol()->size()));
75 }
76 
78  QwtPlotCurve(),
79  m_scatter(data),
80  m_scatterStyle(style)
81 {
82 
83  m_selection = new QwtPlotCurve();
84  m_selection->setStyle(QwtPlotCurve::NoCurve);
85  m_selection->setPaintAttribute(QwtPlotCurve::FilterPoints);
86  m_selection->attach(plot());
87 
88  //Set style
89  setStyle(QwtPlotCurve::NoCurve);
90  setPaintAttribute(QwtPlotCurve::FilterPoints);
91 
92  //Set Values
93  setData();
94 }
95 
97 {
98  QPolygonF samples;
99 
100  if(m_scatter->sizeX() < m_scatter->sizeY())
101  {
102  for (size_t i = 0; i < m_scatter->sizeX(); i++)
103  {
104  samples += QPointF( m_scatter->getX(i), m_scatter->getY(i));
105  }
106  }
107  else
108  {
109  for (size_t i = 0; i < m_scatter->sizeY(); i++)
110  {
111  samples += QPointF( m_scatter->getX(i), m_scatter->getY(i));
112  }
113  }
114 
115  setSamples( samples );
116 }
117 
119 {
120  delete m_scatter;
121  delete m_scatterStyle;
122  delete m_selection;
123 }
124 
126 {
128 }
129 
131 {
132  return m_scatter;
133 }
134 
136 {
137  delete m_scatter;
138  m_scatter = newScatter;
139 }
140 
142 {
143  return m_scatterStyle->clone();
144 }
145 
147 {
148  delete m_scatterStyle;
149  m_scatterStyle = newSymbol;
150 
151  //Updating the Chart's symbol based on the current style
152  setSymbol(m_scatterStyle->getSymbol());
153 
154  //Updating the selection symbol based on the current style;
155  QwtSymbol* selSymbol = new QwtSymbol( symbol()->style(), symbol()->brush().color().darker (180 ), symbol()->pen().color().darker( 180), symbol()->size());
156  QPixmap selPixmap = symbol()->pixmap();
157  selPixmap.fill(m_selColor);
158  selSymbol->setPixmap(selPixmap);
159  m_selection->setSymbol(selSymbol);
160 }
161 
163 {
164  //Removing the previous selection, if there was any.
165  m_selection->detach();
166 
167  std::set<te::da::ObjectId*, te::common::LessCmp<te::da::ObjectId*> >::const_iterator itObjSet;
168  QVector<QPointF> highlightedPoints;
169 
170  for(itObjSet = oids->begin(); itObjSet != oids->end(); ++itObjSet)
171  {
172  std::pair<double, double> point = m_scatter->find((*itObjSet));
173  highlightedPoints.push_back(QPointF(point.first, point.second));
174  }
175 
176  m_selection->setSamples(highlightedPoints);
177  m_selection->attach(plot());
178  plot()->replot();
179 }
180 
182 {
183  const QwtScaleMap xMap = plot()->canvasMap( xAxis() );
184  const QwtScaleMap yMap = plot()->canvasMap( yAxis() );
185 
186  const double cx = xMap.transform( point.rx());
187  const double cy = yMap.transform( point.ry() );
188 
189  QPoint mappedPoint = QPoint(cx, cy);
190 
191  int index = closestPoint( mappedPoint);
192 
193  double x = sample(index).x();
194  double y = sample(index).y();
195  return m_scatter->find(x, y);
196 }
197 
199 {
200  QwtSeriesData<QPointF>* values = data();
201  std::vector<QPointF> selected;
202 
203  if(rect.width() == 0 && rect.height() == 0)
204  return highlight(QPoint(rect.x(), rect.y()));
205 
206  //Acquiring all the selected points:
207  for(size_t i = 0; i < values->size(); ++i)
208  {
209  if( rect.contains(values->sample(i)))
210  selected.push_back(values->sample(i));
211  }
212  return m_scatter->find(selected);
213 }
214 
216 {
217  m_selColor = selColor;
218 
219  //The highlighted color will always have an alpha aplied to it
220  m_selColor.setAlpha(100);
221 }
ScatterStyle * m_scatterStyle
The symbol that defines the look of a scatter's point.
Definition: ScatterChart.h:149
A class used to define the style of a scatter's chart.
void setScatterStyle(ScatterStyle *newStyle)
It sets the chart's style.
QwtSymbol * getSymbol()
Returns a pointer to a QwtSymbol representing the current scatter's point style.
A class to represent a scatter.
Definition: Scatter.h:51
This class represents a set of unique ids created in the same context. i.e. from the same data set...
Definition: ObjectIdSet.h:53
This file contains a set of utility chart functions.
void setSelectionColor(QColor selColor)
color used to hgihlight selected objects on this chart.
virtual int rtti() const
Returns the chart's type.
QwtPlotCurve * m_selection
The PlotItems to be highlighted when a selection occurs;.
Definition: ScatterChart.h:151
std::size_t sizeX()
It returns the size of the scatter.
Definition: Scatter.cpp:76
ScatterStyle * getScatterStyle()
Returns a clone of the pointer to the scatter's style.
A class to represent a scatter's chart.
A class to represent a scatter.
ScatterStyle * clone()
Returns a pointer to a clone of this ScatterStyle.
void highlight(const te::da::ObjectIdSet *oids)
Highlights the objects identified by oids.
QColor m_selColor
THe color used to highlight selected obecjts.
Definition: ScatterChart.h:150
void setScatter(te::qt::widgets::Scatter *newScatter)
It sets the chart's scatter.
std::set< ObjectId *, te::common::LessCmp< ObjectId * > >::const_iterator end() const
Returns an iterator for the object ids in container.
ScatterChart(Scatter *data)
Constructor.
std::size_t sizeY()
It returns the size of the scatter.
Definition: Scatter.cpp:81
te::qt::widgets::Scatter * getScatter()
It returns the chart's scatter.
std::set< ObjectId *, te::common::LessCmp< ObjectId * > >::const_iterator begin() const
Returns an iterator for the object ids in container.
Scatter * m_scatter
The scatter that will be shown on this chart.
Definition: ScatterChart.h:148