All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
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 "ChartDisplay.h"
32 #include "ChartStyle.h"
33 #include "Enums.h"
34 #include "HistogramChart.h"
35 #include "ScatterChart.h"
36 #include "Utils.h"
37 
38 //Qwt
39 #include <qwt_plot_curve.h>
40 #include <qwt_plot_grid.h>
41 #include <qwt_plot_histogram.h>
42 #include <qwt_plot_magnifier.h>
43 #include <qwt_plot_panner.h>
44 #include <qwt_plot_picker.h>
45 #include <qwt_plot_zoomer.h>
46 #include <qwt_picker_machine.h>
47 #include <qwt_text.h>
48 #include <qwt_symbol.h>
49 
50 //Qt
51 #include <qapplication.h>
52 #include <QPen>
53 
54 //STL
55 #include <memory>
56 
57 te::qt::widgets::ChartDisplay::ChartDisplay(QWidget* parent, QString title, ChartStyle* style) :
58  QwtPlot(parent),
59  m_chartStyle(style)
60 {
61  if(!m_chartStyle)
63 
64  m_grid = new QwtPlotGrid;
65  m_grid->enableX(true);
66  m_grid->enableY(true);
67  m_grid->setMajorPen(QPen(Qt::black, 0, Qt::SolidLine));
68  m_grid->setMinorPen(QPen(Qt::gray, 0, Qt::DotLine));
69 
70  setTitle(title);
71  setAutoFillBackground( true );
72  setAutoReplot( true );
73 
74  // zoom in/out with the wheel
75  m_magnifier = new QwtPlotMagnifier(this->canvas());
76  m_magnifier->setMouseButton(Qt::MiddleButton);
77 
78  // Pan on the plotted chart
79  m_panner = new QwtPlotPanner(this->canvas());
80  m_panner->setMouseButton(Qt::RightButton);
81 
82  // Selection based on a rectangle - also works as a point based selection if the rectangle's width and height are both equal 0
83  m_leftPicker = new QwtPlotPicker(QwtPlot::xBottom, QwtPlot::yLeft, QwtPlotPicker::RectRubberBand, QwtPicker::AlwaysOff, this->canvas());
84  m_leftPicker->setStateMachine(new QwtPickerDragRectMachine );
85 
86  m_leftPointPicker = new QwtPlotPicker(QwtPlot::xBottom, QwtPlot::yLeft, QwtPlotPicker::CrossRubberBand, QwtPicker::AlwaysOff, this->canvas());
87  m_leftPointPicker->setStateMachine(new QwtPickerClickPointMachine);
88  m_leftPointPicker->setMousePattern(QwtEventPattern::MouseSelect1, Qt::LeftButton);
89 
90  m_rigthPointPicker = new QwtPlotPicker(QwtPlot::xBottom, QwtPlot::yLeft, QwtPlotPicker::CrossRubberBand, QwtPicker::AlwaysOff, this->canvas());
91  m_rigthPointPicker->setStateMachine(new QwtPickerClickPointMachine);
92  m_rigthPointPicker->setMousePattern(QwtEventPattern::MouseSelect1, Qt::RightButton);
93 
94  m_ctrlPicker = new QwtPlotPicker(QwtPlot::xBottom, QwtPlot::yLeft, QwtPlotPicker::RectRubberBand, QwtPicker::AlwaysOff, this->canvas());
95  m_ctrlPicker->setStateMachine(new QwtPickerDragRectMachine );
96  m_ctrlPicker->setMousePattern(QwtEventPattern::MouseSelect1, Qt::LeftButton, Qt::ControlModifier);
97 
98  m_shiftPicker = new QwtPlotPicker(QwtPlot::xBottom, QwtPlot::yLeft, QwtPlotPicker::RectRubberBand, QwtPicker::AlwaysOff, this->canvas());
99  m_shiftPicker->setStateMachine(new QwtPickerDragRectMachine );
100  m_shiftPicker->setMousePattern(QwtEventPattern::MouseSelect1, Qt::LeftButton, Qt::ShiftModifier);
101 
102  connect(m_leftPicker, SIGNAL(selected(const QRectF&)), SLOT(onRectPicked(const QRectF&)));
103  connect(m_ctrlPicker, SIGNAL(selected(const QRectF&)), SLOT(onRectPicked(const QRectF&)));
104  connect(m_shiftPicker, SIGNAL(selected(const QRectF&)), SLOT(onRectPicked(const QRectF&)));
105 
106  connect(m_leftPointPicker, SIGNAL(selected(const QPointF &)), SIGNAL(leftPointSelected(const QPointF &)));
107  connect(m_rigthPointPicker, SIGNAL(selected(const QPointF &)), SIGNAL(rigthPointSelected(const QPointF &)));
108 
109  canvas()->setCursor(Qt::CrossCursor);
110 }
111 
113 {
114  delete m_chartStyle;
115  delete m_ctrlPicker;
116  delete m_grid;
117  delete m_leftPicker;
118  delete m_magnifier;
119  delete m_panner;
120  delete m_shiftPicker;
121 }
122 
124 {
125  return m_chartStyle;
126 }
127 
129 {
130  m_chartStyle = newStyle;
131  adjustDisplay();
132 }
133 
135 {
136  if(oids)
137  {
138  QApplication::setOverrideCursor(Qt::WaitCursor);
139  const QwtPlotItemList& itmList = itemList();
140 
141  for ( QwtPlotItemIterator it = itmList.begin();
142  it != itmList.end(); ++it )
143  {
144  if ( ( *it )->rtti() == te::qt::widgets::SCATTER_CHART)
145  {
146  static_cast<te::qt::widgets::ScatterChart*>(*it)->highlight(oids);
147  break;
148  }
149  else if( ( *it )->rtti() == te::qt::widgets::HISTOGRAM_CHART )
150  {
151  static_cast<te::qt::widgets::HistogramChart*>(*it)->highlight(oids, dataType);
152  break;
153  }
154  }
155  QApplication::restoreOverrideCursor();
156  }
157 }
158 
160 {
161  const QwtPlotItemList& itmList = itemList();
162 
163  for ( QwtPlotItemIterator it = itmList.begin();
164  it != itmList.end(); ++it )
165  {
166  if ( ( *it )->rtti() == te::qt::widgets::SCATTER_CHART)
167  {
168  static_cast<te::qt::widgets::ScatterChart*>(*it)->setSelectionColor(selColor);
169  break;
170  }
171  else if( ( *it )->rtti() == te::qt::widgets::HISTOGRAM_CHART )
172  {
173  static_cast<te::qt::widgets::HistogramChart*>(*it)->setSelectionColor(selColor);
174  break;
175  }
176  }
177 }
178 
180 {
181  if(m_chartStyle)
182  {
183 
184  QwtText title( m_chartStyle->getTitle());
185  QwtText axisX(m_chartStyle->getAxisX());
186  QwtText axisY(m_chartStyle->getAxisY());
187 
188  title.setFont(m_chartStyle->getTitleFont());
189  axisX.setFont(m_chartStyle->getAxisFont());
190  axisY.setFont(m_chartStyle->getAxisFont());
191 
192  setTitle(title);
193  setAxisTitle( QwtPlot::yLeft, axisY);
194  setAxisTitle( QwtPlot::xBottom, axisX);
195 
196  if(m_chartStyle->getGridChecked())
197  m_grid->attach(this);
198  else
199  m_grid->detach();
200 
201  canvas()->setPalette(m_chartStyle->getColor());
202  }
203  updateLayout();
204 }
205 
207 {
208  QApplication::setOverrideCursor(Qt::WaitCursor);
209  const QwtPlotItemList& itmList = itemList();
210  for ( QwtPlotItemIterator it = itmList.begin();
211  it != itmList.end(); ++it )
212  {
213  if ( ( *it )->rtti() == te::qt::widgets::SCATTER_CHART)
214  {
215  if (QObject::sender() == m_ctrlPicker || QObject::sender() == m_shiftPicker)
216  emit selected(static_cast<te::qt::widgets::ScatterChart*>(*it)->highlight( rect), true);
217  else
218  emit selected(static_cast<te::qt::widgets::ScatterChart*>(*it)->highlight( rect), false);
219 
220  break;
221  }
222  else if( ( *it )->rtti() == te::qt::widgets::HISTOGRAM_CHART )
223  {
224  if (QObject::sender() == m_ctrlPicker || QObject::sender() == m_shiftPicker)
225  emit selected(static_cast<te::qt::widgets::HistogramChart*>(*it)->highlight( rect), true);
226  else
227  emit selected(static_cast<te::qt::widgets::HistogramChart*>(*it)->highlight( rect), false);
228 
229  break;
230  }
231  }
232  QApplication::restoreOverrideCursor();
233 }
void adjustDisplay()
Updates the general display settings according to the ChartStyle. The adjusted properties are: Title;...
QwtPlotGrid * m_grid
The display's grid.
Definition: ChartDisplay.h:158
QwtPlotPicker * m_leftPicker
The display's left button picker.
Definition: ChartDisplay.h:160
QwtPlotPanner * m_panner
The display's panner.
Definition: ChartDisplay.h:159
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's control button picker.
Definition: ChartDisplay.h:164
ChartStyle * m_chartStyle
The display's style.
Definition: ChartDisplay.h:157
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.
void setSelectionColor(QColor selColor)
color used to hgihlight selected objects on this display.
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.
A class to represent a scatter'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's shift button picker.
Definition: ChartDisplay.h:165
QwtPlotPicker * m_rigthPointPicker
The display's rigth button picker.
Definition: ChartDisplay.h:162
void selected(te::da::ObjectIdSet *, const bool &)
Emmit when objects were selected.
A class used to define a chartDisplay's style.
QwtPlotMagnifier * m_magnifier
The display's magnifinifier.
Definition: ChartDisplay.h:163
te::qt::widgets::ChartStyle * getStyle()
Returns a pointer to the display's style.
A class to represent a chart display.
void setStyle(te::qt::widgets::ChartStyle *newStyle)
It sets the display's style.
void leftPointSelected(const QPointF &pos)
Emmit when a clicked with left button was made.
QwtPlotPicker * m_leftPointPicker
The display's left button picker.
Definition: ChartDisplay.h:161