ChartDisplay.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 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/DataSetType.h"
29 #include "../../../dataaccess/dataset/ObjectIdSet.h"
30 #include "../../../se.h"
31 #include "../utils/ScopedCursor.h"
32 #include "ChartDisplay.h"
33 #include "ChartStyle.h"
34 #include "Enums.h"
35 #include "HistogramChart.h"
36 #include "ScatterChart.h"
37 #include "Utils.h"
38 
39 //Qwt
40 #include <qwt_plot_curve.h>
41 #include <qwt_plot_grid.h>
42 #include <qwt_plot_histogram.h>
43 #include <qwt_plot_magnifier.h>
44 #include <qwt_plot_panner.h>
45 #include <qwt_plot_picker.h>
46 #include <qwt_plot_zoomer.h>
47 #include <qwt_picker_machine.h>
48 #include <qwt_text.h>
49 #include <qwt_symbol.h>
50 
51 //Qt
52 #include <qapplication.h>
53 #include <QPen>
54 
55 //STL
56 #include <memory>
57 
59  QwtPlot(parent),
60  m_chartStyle(style)
61 {
62  if(!m_chartStyle)
64 
65  m_grid = new QwtPlotGrid;
66  m_grid->enableX(true);
67  m_grid->enableY(true);
68  m_grid->setMajorPen(QPen(Qt::black, 0, Qt::SolidLine));
69  m_grid->setMinorPen(QPen(Qt::gray, 0, Qt::DotLine));
70 
71  setTitle(title);
72  setAutoFillBackground( true );
73  setAutoReplot( true );
74 
75  // zoom in/out with the wheel
76  m_magnifier = new QwtPlotMagnifier(this->canvas());
77  m_magnifier->setMouseButton(Qt::MiddleButton);
78  m_magnifier->setWheelFactor(1.1);
79 
80  // Pan on the plotted chart
81  m_panner = new QwtPlotPanner(this->canvas());
82  m_panner->setMouseButton(Qt::MiddleButton, Qt::ControlModifier);
83 
84  // Selection based on a rectangle - also works as a point based selection if the rectangle's width and height are both equal 0
85  m_leftPicker = new QwtPlotPicker(QwtPlot::xBottom, QwtPlot::yLeft, QwtPlotPicker::RectRubberBand, QwtPicker::AlwaysOff, this->canvas());
86  m_leftPicker->setStateMachine(new QwtPickerDragRectMachine );
87 
88  m_leftPointPicker = new QwtPlotPicker(QwtPlot::xBottom, QwtPlot::yLeft, QwtPlotPicker::CrossRubberBand, QwtPicker::AlwaysOff, this->canvas());
89  m_leftPointPicker->setStateMachine(new QwtPickerClickPointMachine);
90  m_leftPointPicker->setMousePattern(QwtEventPattern::MouseSelect1, Qt::LeftButton);
91 
92  m_rigthPointPicker = new QwtPlotPicker(QwtPlot::xBottom, QwtPlot::yLeft, QwtPlotPicker::CrossRubberBand, QwtPicker::AlwaysOff, this->canvas());
93  m_rigthPointPicker->setStateMachine(new QwtPickerClickPointMachine);
94  m_rigthPointPicker->setMousePattern(QwtEventPattern::MouseSelect1, Qt::RightButton);
95 
96  m_ctrlPicker = new QwtPlotPicker(QwtPlot::xBottom, QwtPlot::yLeft, QwtPlotPicker::RectRubberBand, QwtPicker::AlwaysOff, this->canvas());
97  m_ctrlPicker->setStateMachine(new QwtPickerDragRectMachine );
98  m_ctrlPicker->setMousePattern(QwtEventPattern::MouseSelect1, Qt::LeftButton, Qt::ControlModifier);
99 
100  m_shiftPicker = new QwtPlotPicker(QwtPlot::xBottom, QwtPlot::yLeft, QwtPlotPicker::RectRubberBand, QwtPicker::AlwaysOff, this->canvas());
101  m_shiftPicker->setStateMachine(new QwtPickerDragRectMachine );
102  m_shiftPicker->setMousePattern(QwtEventPattern::MouseSelect1, Qt::LeftButton, Qt::ShiftModifier);
103 
104  connect(m_leftPicker, SIGNAL(selected(const QRectF&)), SLOT(onRectPicked(const QRectF&)));
105  connect(m_ctrlPicker, SIGNAL(selected(const QRectF&)), SLOT(onRectPicked(const QRectF&)));
106  connect(m_shiftPicker, SIGNAL(selected(const QRectF&)), SLOT(onRectPicked(const QRectF&)));
107 
108  connect(m_leftPointPicker, SIGNAL(selected(const QPointF &)), SIGNAL(leftPointSelected(const QPointF &)));
109  connect(m_rigthPointPicker, SIGNAL(selected(const QPointF &)), SIGNAL(rigthPointSelected(const QPointF &)));
110 
111  canvas()->setCursor(Qt::CrossCursor);
112 }
113 
115 {
116  delete m_chartStyle;
117  delete m_ctrlPicker;
118  delete m_grid;
119  delete m_leftPicker;
120  delete m_magnifier;
121  delete m_panner;
122  delete m_shiftPicker;
123 }
124 
126 {
127  return m_chartStyle;
128 }
129 
131 {
132  m_chartStyle = newStyle;
133  adjustDisplay();
134 }
135 
137 {
138  if(oids)
139  {
140  te::qt::widgets::ScopedCursor c(Qt::WaitCursor);
141  const QwtPlotItemList& itmList = itemList();
142 
143  for ( QwtPlotItemIterator it = itmList.begin();
144  it != itmList.end(); ++it )
145  {
146  if ( ( *it )->rtti() == te::qt::widgets::SCATTER_CHART)
147  {
148  static_cast<te::qt::widgets::ScatterChart*>(*it)->highlight(oids);
149  break;
150  }
151  else if( ( *it )->rtti() == te::qt::widgets::HISTOGRAM_CHART )
152  {
153  static_cast<te::qt::widgets::HistogramChart*>(*it)->highlight(oids, dataType);
154  break;
155  }
156  }
157  }
158 }
159 
161 {
162  const QwtPlotItemList& itmList = itemList();
163 
164  for ( QwtPlotItemIterator it = itmList.begin();
165  it != itmList.end(); ++it )
166  {
167  if ( ( *it )->rtti() == te::qt::widgets::SCATTER_CHART)
168  {
169  static_cast<te::qt::widgets::ScatterChart*>(*it)->setSelectionColor(selColor);
170  break;
171  }
172  else if( ( *it )->rtti() == te::qt::widgets::HISTOGRAM_CHART )
173  {
174  static_cast<te::qt::widgets::HistogramChart*>(*it)->setSelectionColor(selColor);
175  break;
176  }
177  }
178 }
179 
181 {
182  if(m_chartStyle)
183  {
184 
185  QwtText title( m_chartStyle->getTitle());
186  QwtText axisX(m_chartStyle->getAxisX());
187  QwtText axisY(m_chartStyle->getAxisY());
188 
189  title.setFont(m_chartStyle->getTitleFont());
190  axisX.setFont(m_chartStyle->getAxisFont());
191  axisY.setFont(m_chartStyle->getAxisFont());
192 
193  setTitle(title);
194  setAxisTitle( QwtPlot::yLeft, axisY);
195  setAxisTitle( QwtPlot::xBottom, axisX);
196 
198  m_grid->attach(this);
199  else
200  m_grid->detach();
201 
202  canvas()->setPalette(m_chartStyle->getColor());
203  }
204  updateLayout();
205 }
206 
208 {
209  enablePickers(enabled);
210  enableMagnifier(enabled);
211  enablePan(enabled);
212 }
213 
215 {
216  enableLeftPicker(enabled);
217  enableMultiSelection(enabled);
218 }
219 
221 {
222  m_leftPicker->setEnabled(enabled);
223 }
224 
226 {
227  m_ctrlPicker->setEnabled(enabled);
228  m_shiftPicker->setEnabled(enabled);
229 }
230 
232 {
233  m_magnifier->setEnabled(enabled);
234 }
235 
237 {
238  m_panner->setEnabled(enabled);
239 }
240 
242 {
243  m_leftPointPicker->setEnabled(enabled);
244  m_rigthPointPicker->setEnabled(enabled);
245 }
246 
248 {
249  te::qt::widgets::ScopedCursor c(Qt::WaitCursor);
250  const QwtPlotItemList& itmList = itemList();
251  for ( QwtPlotItemIterator it = itmList.begin();
252  it != itmList.end(); ++it )
253  {
254  if ( ( *it )->rtti() == te::qt::widgets::SCATTER_CHART)
255  {
256  if (QObject::sender() == m_ctrlPicker || QObject::sender() == m_shiftPicker)
257  emit selected(static_cast<te::qt::widgets::ScatterChart*>(*it)->highlight( rect), true);
258  else
259  emit selected(static_cast<te::qt::widgets::ScatterChart*>(*it)->highlight( rect), false);
260 
261  break;
262  }
263  else if( ( *it )->rtti() == te::qt::widgets::HISTOGRAM_CHART )
264  {
265  if (QObject::sender() == m_ctrlPicker || QObject::sender() == m_shiftPicker)
266  emit selected(static_cast<te::qt::widgets::HistogramChart*>(*it)->highlight( rect), true);
267  else
268  emit selected(static_cast<te::qt::widgets::HistogramChart*>(*it)->highlight( rect), false);
269 
270  break;
271  }
272  }
273 }
QString & getAxisX()
Returns a reference to the style&#39;s x axis label.
Definition: ChartStyle.cpp:86
QFont & getAxisFont()
Returns a reference to the axis&#39; font.
Definition: ChartStyle.cpp:106
void adjustDisplay()
Updates the general display settings according to the ChartStyle. The adjusted properties are: Title;...
QString & getAxisY()
Returns a reference to the style&#39;s y axis label.
Definition: ChartStyle.cpp:96
QwtPlotGrid * m_grid
The display&#39;s grid.
Definition: ChartDisplay.h:194
QwtPlotPicker * m_leftPicker
The display&#39;s left button picker.
Definition: ChartDisplay.h:196
QwtPlotPanner * m_panner
The display&#39;s panner.
Definition: ChartDisplay.h:195
A class that models the description of a dataset.
Definition: DataSetType.h:72
void highlight(const te::da::ObjectIdSet *oids, te::da::DataSetType *dataType)
Highlights the objects identified by oids.
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).
QwtPlotPicker * m_ctrlPicker
The display&#39;s control button picker.
Definition: ChartDisplay.h:200
void enablePan(bool enabled)
Enables or disables the tool used to execute pan functions.
void enableTools(bool enabled)
Enables or disables all the tools used to execute certain mouse actions. Such as selection, pan and zoomn;.
QString & getTitle()
Returns a reference to the style&#39;s Title.
Definition: ChartStyle.cpp:66
ChartStyle * m_chartStyle
The display&#39;s style.
Definition: ChartDisplay.h:193
void enablePointPickers(bool enabled)
Enables or disables the pickers used tbroadcast point selection signals.
This class represents a set of unique ids created in the same context. i.e. from the same data set...
Definition: ObjectIdSet.h:55
void setSelectionColor(QColor selColor)
color used to hgihlight selected objects on this chart.
void setSelectionColor(QColor selColor)
color used to hgihlight selected objects on this display.
QColor & getColor()
Returns a reference to the style&#39;s color.
Definition: ChartStyle.cpp:140
A class to represent a histogram chart.
A class to represent a scatter chart.
Definition: ScatterChart.h:55
void highlightOIds(const te::da::ObjectIdSet *oids, te::da::DataSetType *dataType)
Highlights the objects identified by oids.
QFont & getTitleFont()
Returns a reference to the title&#39;s font.
Definition: ChartStyle.cpp:76
A class to represent a scatter&#39;s chart.
void setSelectionColor(QColor selColor)
color used to hgihlight selected objects on this chart.
void rigthPointSelected(const QPointF &pos)
Emmit when a clicked with rigth button was made.
ChartDisplay(QWidget *parent=NULL, QString title="", ChartStyle *style=0)
Constructor.
void highlight(const te::da::ObjectIdSet *oids)
Highlights the objects identified by oids.
QwtPlotPicker * m_shiftPicker
The display&#39;s shift button picker.
Definition: ChartDisplay.h:201
QwtPlotPicker * m_rigthPointPicker
The display&#39;s rigth button picker.
Definition: ChartDisplay.h:198
void selected(te::da::ObjectIdSet *, const bool &)
Emmit when objects were selected.
bool getGridChecked()
Returns true if the grid is to be displayed, otherwise returns false.
Definition: ChartStyle.cpp:150
void enableLeftPicker(bool enabled)
Enables or disables the tool used to execute the selection functions.
A class used to define a chartDisplay&#39;s style.
QwtPlotMagnifier * m_magnifier
The display&#39;s magnifinifier.
Definition: ChartDisplay.h:199
te::qt::widgets::ChartStyle * getStyle()
Returns a pointer to the display&#39;s style.
A class to represent a chart display.
void enableMagnifier(bool enabled)
Enables or disables the tool used to execute zoom functions.
void setStyle(te::qt::widgets::ChartStyle *newStyle)
It sets the display&#39;s style.
void enablePickers(bool enabled)
Enables or disables the tools used to execute selection functions.
void leftPointSelected(const QPointF &pos)
Emmit when a clicked with left button was made.
An object that when created shows a cursor during its scope.
Definition: ScopedCursor.h:48
void enableMultiSelection(bool enabled)
Enables or disables the tool used to execute the multi selection functions.
This file contains a set of utility chart functions.
QwtPlotPicker * m_leftPointPicker
The display&#39;s left button picker.
Definition: ChartDisplay.h:197