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