All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
ChartDisplay.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 ChartDisplay.cpp
22 
23  \brief A class to represent a chart display.
24 */
25 
26 // TerraLib
27 #include "../../../color/RGBAColor.h"
28 #include "../../../dataaccess/dataset/ObjectIdSet.h"
29 #include "../../../se.h"
30 #include "ChartDisplay.h"
31 #include "ChartStyle.h"
32 #include "Enums.h"
33 #include "HistogramChart.h"
34 #include "ScatterChart.h"
35 #include "Utils.h"
36 
37 //Qwt
38 #include <qwt_plot_curve.h>
39 #include <qwt_plot_grid.h>
40 #include <qwt_plot_histogram.h>
41 #include <qwt_plot_magnifier.h>
42 #include <qwt_plot_panner.h>
43 #include <qwt_plot_picker.h>
44 #include <qwt_plot_zoomer.h>
45 #include <qwt_picker_machine.h>
46 #include <qwt_text.h>
47 #include <qwt_symbol.h>
48 
49 //Qt
50 #include <qapplication.h>
51 #include <QtGui/QPen>
52 
53 //STL
54 #include <memory>
55 
56 te::qt::widgets::ChartDisplay::ChartDisplay(QWidget* parent, QString title, ChartStyle* style) :
57  QwtPlot(parent),
58  m_chartStyle(style)
59 {
60  if(!m_chartStyle)
62 
63  m_grid = new QwtPlotGrid;
64  m_grid->enableX(true);
65  m_grid->enableY(true);
66  m_grid->setMajorPen(QPen(Qt::black, 0, Qt::SolidLine));
67  m_grid->setMinorPen(QPen(Qt::gray, 0, Qt::DotLine));
68 
69  setTitle(title);
70  setAutoFillBackground( true );
71  setAutoReplot( true );
72 
73  // zoom in/out with the wheel
74  ( void ) new QwtPlotMagnifier( this->canvas() );
75 
76  // Pan on the plotted chart
77  m_panner = new QwtPlotPanner(this->canvas());
78  m_panner->setMouseButton(Qt::MidButton);
79 
80  // Selection based on a point
81  m_leftPicker = new QwtPlotPicker(this->canvas());
82  m_leftPicker->setStateMachine(new QwtPickerClickPointMachine );
83 
84  m_ctrlPicker = new QwtPlotPicker(this->canvas());
85  m_ctrlPicker->setStateMachine(new QwtPickerClickPointMachine );
86  m_ctrlPicker->setMousePattern(QwtEventPattern::MouseSelect1, Qt::LeftButton, Qt::ControlModifier);
87 
88  m_shiftPicker = new QwtPlotPicker(this->canvas());
89  m_shiftPicker->setStateMachine(new QwtPickerClickPointMachine );
90  m_shiftPicker->setMousePattern(QwtEventPattern::MouseSelect1, Qt::LeftButton, Qt::ShiftModifier);
91 
92  //The default shape of the cursor is a stander arrow, may vary depending on the type of chart being drawn.
93  //Can be updated using the set PickerStyle function.
94  canvas()->setCursor( Qt::ArrowCursor);
95 
96  connect(m_ctrlPicker, SIGNAL(selected(const QPointF&)), SLOT(onPointPicked(const QPointF&)));
97  connect(m_leftPicker, SIGNAL(selected(const QPointF&)), SLOT(onPointPicked(const QPointF&)));
98  connect(m_shiftPicker, SIGNAL(selected(const QPointF&)), SLOT(onPointPicked(const QPointF&)));
99 }
100 
102 {
103  delete m_chartStyle;
104  delete m_ctrlPicker;
105  delete m_grid;
106  delete m_leftPicker;
107  delete m_panner;
108  delete m_shiftPicker;
109 }
110 
112 {
113  switch(chartType)
114  {
116 
117  delete m_ctrlPicker;
118  delete m_leftPicker;
119  delete m_shiftPicker;
120 
121  m_leftPicker = new QwtPlotPicker(QwtPlot::xBottom, QwtPlot::yLeft, QwtPlotPicker::RectRubberBand, QwtPicker::AlwaysOff, this->canvas());
122  m_leftPicker->setStateMachine(new QwtPickerDragRectMachine );
123 
124  m_ctrlPicker = new QwtPlotPicker(QwtPlot::xBottom, QwtPlot::yLeft, QwtPlotPicker::RectRubberBand, QwtPicker::AlwaysOff, this->canvas());
125  m_ctrlPicker->setStateMachine(new QwtPickerDragRectMachine );
126  m_ctrlPicker->setMousePattern(QwtEventPattern::MouseSelect1, Qt::LeftButton, Qt::ControlModifier);
127 
128  m_shiftPicker = new QwtPlotPicker(QwtPlot::xBottom, QwtPlot::yLeft, QwtPlotPicker::RectRubberBand, QwtPicker::AlwaysOff, this->canvas());
129  m_shiftPicker->setStateMachine(new QwtPickerDragRectMachine );
130  m_shiftPicker->setMousePattern(QwtEventPattern::MouseSelect1, Qt::LeftButton, Qt::ShiftModifier);
131 
132  connect(m_leftPicker, SIGNAL(selected(const QRectF&)), SLOT(onRectPicked(const QRectF&)));
133  connect(m_ctrlPicker, SIGNAL(selected(const QRectF&)), SLOT(onRectPicked(const QRectF&)));
134  connect(m_shiftPicker, SIGNAL(selected(const QRectF&)), SLOT(onRectPicked(const QRectF&)));
135 
136  canvas()->setCursor(Qt::CrossCursor);
137  break;
138 
139  default:
140 
141  delete m_ctrlPicker;
142  delete m_leftPicker;
143  delete m_shiftPicker;
144 
145  m_leftPicker = new QwtPlotPicker(this->canvas());
146  m_leftPicker->setStateMachine(new QwtPickerClickPointMachine );
147 
148  m_ctrlPicker = new QwtPlotPicker(this->canvas());
149  m_ctrlPicker->setStateMachine(new QwtPickerClickPointMachine );
150  m_ctrlPicker->setMousePattern(QwtEventPattern::MouseSelect1, Qt::LeftButton, Qt::ControlModifier);
151 
152  m_shiftPicker = new QwtPlotPicker(this->canvas());
153  m_shiftPicker->setStateMachine(new QwtPickerClickPointMachine );
154  m_shiftPicker->setMousePattern(QwtEventPattern::MouseSelect1, Qt::LeftButton, Qt::ShiftModifier);
155 
156  connect(m_leftPicker, SIGNAL(selected(const QPointF&)), SLOT(onPointPicked(const QPointF&)));
157  connect(m_ctrlPicker, SIGNAL(selected(const QPointF&)), SLOT(onPointPicked(const QPointF&)));
158  connect(m_shiftPicker, SIGNAL(selected(const QPointF&)), SLOT(onPointPicked(const QPointF&)));
159  break;
160  }
161 }
162 
164 {
165  return m_chartStyle;
166 }
167 
169 {
170  m_chartStyle = newStyle;
171  adjustDisplay();
172 }
173 
175 {
176  if(oids)
177  {
178  QApplication::setOverrideCursor(Qt::WaitCursor);
179  const QwtPlotItemList& itmList = itemList();
180 
181  for ( QwtPlotItemIterator it = itmList.begin();
182  it != itmList.end(); ++it )
183  {
184  if ( ( *it )->rtti() == te::qt::widgets::SCATTER_CHART)
185  {
186  static_cast<te::qt::widgets::ScatterChart*>(*it)->highlight( oids);
187  break;
188  }
189  else if( ( *it )->rtti() == te::qt::widgets::HISTOGRAM_CHART )
190  {
191  static_cast<te::qt::widgets::HistogramChart*>(*it)->highlight( oids);
192  break;
193  }
194  }
195  QApplication::restoreOverrideCursor();
196  }
197 }
198 
200 {
201  const QwtPlotItemList& itmList = itemList();
202 
203  for ( QwtPlotItemIterator it = itmList.begin();
204  it != itmList.end(); ++it )
205  {
206  if ( ( *it )->rtti() == te::qt::widgets::SCATTER_CHART)
207  {
208  static_cast<te::qt::widgets::ScatterChart*>(*it)->setSelectionColor(selColor);
209  break;
210  }
211  else if( ( *it )->rtti() == te::qt::widgets::HISTOGRAM_CHART )
212  {
213  static_cast<te::qt::widgets::HistogramChart*>(*it)->setSelectionColor(selColor);
214  break;
215  }
216  }
217 }
218 
220 {
221  if(m_chartStyle)
222  {
223 
224  QwtText title( m_chartStyle->getTitle());
225  QwtText axisX(m_chartStyle->getAxisX());
226  QwtText axisY(m_chartStyle->getAxisY());
227 
228  title.setFont(m_chartStyle->getTitleFont());
229  axisX.setFont(m_chartStyle->getAxisFont());
230  axisY.setFont(m_chartStyle->getAxisFont());
231 
232  setTitle(title);
233  setAxisTitle( QwtPlot::yLeft, axisY);
234  setAxisTitle( QwtPlot::xBottom, axisX);
235 
236  if(m_chartStyle->getGridChecked())
237  m_grid->attach(this);
238  else
239  m_grid->detach();
240 
241  canvas()->setPalette(m_chartStyle->getColor());
242  }
243 }
244 
246 {
247  QApplication::setOverrideCursor(Qt::WaitCursor);
248  const QwtPlotItemList& itmList = itemList();
249  for ( QwtPlotItemIterator it = itmList.begin();
250  it != itmList.end(); ++it )
251  {
252  if ( ( *it )->rtti() == te::qt::widgets::SCATTER_CHART)
253  {
254  if (QObject::sender() == m_ctrlPicker)
255  emit selected(static_cast<te::qt::widgets::ScatterChart*>(*it)->highlight( pos), true);
256  else
257  emit selected(static_cast<te::qt::widgets::ScatterChart*>(*it)->highlight( pos), false);
258 
259  break;
260  }
261  else if( ( *it )->rtti() == te::qt::widgets::HISTOGRAM_CHART )
262  {
263  if (QObject::sender() == m_ctrlPicker || QObject::sender() == m_shiftPicker)
264  emit selected(static_cast<te::qt::widgets::HistogramChart*>(*it)->highlight( pos), true);
265  else
266  emit selected(static_cast<te::qt::widgets::HistogramChart*>(*it)->highlight( pos), false);
267 
268  break;
269  }
270  }
271  QApplication::restoreOverrideCursor();
272 }
273 
275 {
276  QApplication::setOverrideCursor(Qt::WaitCursor);
277  const QwtPlotItemList& itmList = itemList();
278  for ( QwtPlotItemIterator it = itmList.begin();
279  it != itmList.end(); ++it )
280  {
281  if ( ( *it )->rtti() == te::qt::widgets::SCATTER_CHART)
282  {
283  if (QObject::sender() == m_ctrlPicker || QObject::sender() == m_shiftPicker)
284  emit selected(static_cast<te::qt::widgets::ScatterChart*>(*it)->highlight( rect), true);
285  else
286  emit selected(static_cast<te::qt::widgets::ScatterChart*>(*it)->highlight( rect), false);
287 
288  break;
289  }
290  }
291  QApplication::restoreOverrideCursor();
292 }
QwtPlotPicker * m_ctrlPicker
The display&#39;s control button picker.
Definition: ChartDisplay.h:172
A class to represent a scatter&#39;s chart.
ChartStyle * m_chartStyle
The display&#39;s style.
Definition: ChartDisplay.h:168
void adjustDisplay()
Updates the general display settings according to the ChartStyle. The adjusted properties are: Title;...
void highlight(const te::da::ObjectIdSet *oids)
Highlights the objects identified by oids.
This class represents a set of unique ids created in the same context. i.e. from the same data set...
Definition: ObjectIdSet.h:53
ChartDisplay(QWidget *parent=NULL, QString title="", ChartStyle *style=0)
Constructor.
QwtPlotGrid * m_grid
The display&#39;s grid.
Definition: ChartDisplay.h:169
void onPointPicked(const QPointF &pos)
Called when the user clicks on the canvas area. Will highlight the data, if possible, depending on the type of chart being displayed (f.e. histogram, scatter, etc).
QwtPlotPicker * m_shiftPicker
The display&#39;s control button picker.
Definition: ChartDisplay.h:173
void onRectPicked(const QRectF &rect)
Called when the user selects an area of the canvas. Will highlight the data, if possible, depending on the type of chart being displayed (f.e. histogram, scatter, etc).
void setPickerStyle(int chartType)
It sets the QwtPlotPicker&#39;s state machine (selection mode)
QwtPlotPanner * m_panner
The display&#39;s panner.
Definition: ChartDisplay.h:170
QwtPlotPicker * m_leftPicker
The display&#39;s left button picker.
Definition: ChartDisplay.h:171
void highlightOIds(const te::da::ObjectIdSet *oids)
Highlights the objects identified by oids.
void setSelectionColor(QColor selColor)
color used to hgihlight selected objects on this display.
A class to represent a scatter chart.
Definition: ScatterChart.h:55
This file contains a set of utility chart functions.
void setStyle(te::qt::widgets::ChartStyle *newStyle)
It sets the display&#39;s style.
A class to represent a histogram chart.
void setSelectionColor(QColor selColor)
color used to hgihlight selected objects on this chart.
A class used to define a chartDisplay&#39;s style.
A class to represent a chart display.
void highlight(const te::da::ObjectIdSet *oids)
Highlights the objects identified by oids.
void setSelectionColor(QColor selColor)
color used to hgihlight selected objects on this chart.
void selected(te::da::ObjectIdSet *, const bool &)
Emmit when objects were selected.
te::qt::widgets::ChartStyle * getStyle()
Returns a pointer to the display&#39;s style.