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) 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 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(dataSize() > 500)
62  {
63  QwtSymbol* newSymbol = m_scatterStyle->getSymbol();
64  newSymbol->setSize(QSize(2 , 2));
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  if(this->boundingRect().contains(point.first, point.second))
174  highlightedPoints.push_back(QPointF(point.first, point.second));
175  }
176 
177  m_selection->setSamples(highlightedPoints);
178 
179  m_selection->attach(plot());
180  plot()->replot();
181 }
182 
184 {
185  const QwtScaleMap xMap = plot()->canvasMap( xAxis() );
186  const QwtScaleMap yMap = plot()->canvasMap( yAxis() );
187 
188  const double cx = xMap.transform( point.rx());
189  const double cy = yMap.transform( point.ry() );
190 
191  QPoint mappedPoint = QPoint(cx, cy);
192 
193  int index = closestPoint( mappedPoint);
194 
195  double x = sample(index).x();
196  double y = sample(index).y();
197  return m_scatter->find(x, y);
198 }
199 
201 {
202  QwtSeriesData<QPointF>* values = data();
203  std::vector<QPointF> selected;
204 
205  if(rect.width() == 0 && rect.height() == 0)
206  return highlight(QPoint(rect.x(), rect.y()));
207 
208  //Acquiring all the selected points:
209  for(size_t i = 0; i < values->size(); ++i)
210  {
211  if( rect.contains(values->sample(i)))
212  selected.push_back(values->sample(i));
213  }
214  return m_scatter->find(selected);
215 }
216 
218 {
219  m_selColor = selColor;
220 
221  //The highlighted color will always have an alpha aplied to it
222  m_selColor.setAlpha(100);
223 }
ScatterStyle * m_scatterStyle
The symbol that defines the look of a scatter's point.
Definition: ScatterChart.h:147
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:55
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:149
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:148
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.
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.