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) 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 <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  m_magnifier = new QwtPlotMagnifier(this->canvas());
75  m_magnifier->setMouseButton(Qt::MiddleButton);
76 
77  // Pan on the plotted chart
78  m_panner = new QwtPlotPanner(this->canvas());
79  m_panner->setMouseButton(Qt::RightButton);
80 
81  // Selection based on a rectangle - also works as a point based selection if the rectangle's width and height are both equal 0
82  m_leftPicker = new QwtPlotPicker(QwtPlot::xBottom, QwtPlot::yLeft, QwtPlotPicker::RectRubberBand, QwtPicker::AlwaysOff, this->canvas());
83  m_leftPicker->setStateMachine(new QwtPickerDragRectMachine );
84 
85  m_ctrlPicker = new QwtPlotPicker(QwtPlot::xBottom, QwtPlot::yLeft, QwtPlotPicker::RectRubberBand, QwtPicker::AlwaysOff, this->canvas());
86  m_ctrlPicker->setStateMachine(new QwtPickerDragRectMachine );
87  m_ctrlPicker->setMousePattern(QwtEventPattern::MouseSelect1, Qt::LeftButton, Qt::ControlModifier);
88 
89  m_shiftPicker = new QwtPlotPicker(QwtPlot::xBottom, QwtPlot::yLeft, QwtPlotPicker::RectRubberBand, QwtPicker::AlwaysOff, this->canvas());
90  m_shiftPicker->setStateMachine(new QwtPickerDragRectMachine );
91  m_shiftPicker->setMousePattern(QwtEventPattern::MouseSelect1, Qt::LeftButton, Qt::ShiftModifier);
92 
93  connect(m_leftPicker, SIGNAL(selected(const QRectF&)), SLOT(onRectPicked(const QRectF&)));
94  connect(m_ctrlPicker, SIGNAL(selected(const QRectF&)), SLOT(onRectPicked(const QRectF&)));
95  connect(m_shiftPicker, SIGNAL(selected(const QRectF&)), SLOT(onRectPicked(const QRectF&)));
96 
97  canvas()->setCursor(Qt::CrossCursor);
98 }
99 
101 {
102  delete m_chartStyle;
103  delete m_ctrlPicker;
104  delete m_grid;
105  delete m_leftPicker;
106  delete m_magnifier;
107  delete m_panner;
108  delete m_shiftPicker;
109 }
110 
112 {
113  return m_chartStyle;
114 }
115 
117 {
118  m_chartStyle = newStyle;
119  adjustDisplay();
120 }
121 
123 {
124  if(oids)
125  {
126  QApplication::setOverrideCursor(Qt::WaitCursor);
127  const QwtPlotItemList& itmList = itemList();
128 
129  for ( QwtPlotItemIterator it = itmList.begin();
130  it != itmList.end(); ++it )
131  {
132  if ( ( *it )->rtti() == te::qt::widgets::SCATTER_CHART)
133  {
134  static_cast<te::qt::widgets::ScatterChart*>(*it)->highlight( oids);
135  break;
136  }
137  else if( ( *it )->rtti() == te::qt::widgets::HISTOGRAM_CHART )
138  {
139  static_cast<te::qt::widgets::HistogramChart*>(*it)->highlight( oids);
140  break;
141  }
142  }
143  QApplication::restoreOverrideCursor();
144  }
145 }
146 
148 {
149  const QwtPlotItemList& itmList = itemList();
150 
151  for ( QwtPlotItemIterator it = itmList.begin();
152  it != itmList.end(); ++it )
153  {
154  if ( ( *it )->rtti() == te::qt::widgets::SCATTER_CHART)
155  {
156  static_cast<te::qt::widgets::ScatterChart*>(*it)->setSelectionColor(selColor);
157  break;
158  }
159  else if( ( *it )->rtti() == te::qt::widgets::HISTOGRAM_CHART )
160  {
161  static_cast<te::qt::widgets::HistogramChart*>(*it)->setSelectionColor(selColor);
162  break;
163  }
164  }
165 }
166 
168 {
169  if(m_chartStyle)
170  {
171 
172  QwtText title( m_chartStyle->getTitle());
173  QwtText axisX(m_chartStyle->getAxisX());
174  QwtText axisY(m_chartStyle->getAxisY());
175 
176  title.setFont(m_chartStyle->getTitleFont());
177  axisX.setFont(m_chartStyle->getAxisFont());
178  axisY.setFont(m_chartStyle->getAxisFont());
179 
180  setTitle(title);
181  setAxisTitle( QwtPlot::yLeft, axisY);
182  setAxisTitle( QwtPlot::xBottom, axisX);
183 
184  if(m_chartStyle->getGridChecked())
185  m_grid->attach(this);
186  else
187  m_grid->detach();
188 
189  canvas()->setPalette(m_chartStyle->getColor());
190  }
191 }
192 
194 {
195  QApplication::setOverrideCursor(Qt::WaitCursor);
196  const QwtPlotItemList& itmList = itemList();
197  for ( QwtPlotItemIterator it = itmList.begin();
198  it != itmList.end(); ++it )
199  {
200  if ( ( *it )->rtti() == te::qt::widgets::SCATTER_CHART)
201  {
202  if (QObject::sender() == m_ctrlPicker || QObject::sender() == m_shiftPicker)
203  emit selected(static_cast<te::qt::widgets::ScatterChart*>(*it)->highlight( rect), true);
204  else
205  emit selected(static_cast<te::qt::widgets::ScatterChart*>(*it)->highlight( rect), false);
206 
207  break;
208  }
209  else if( ( *it )->rtti() == te::qt::widgets::HISTOGRAM_CHART )
210  {
211  if (QObject::sender() == m_ctrlPicker || QObject::sender() == m_shiftPicker)
212  emit selected(static_cast<te::qt::widgets::HistogramChart*>(*it)->highlight( rect), true);
213  else
214  emit selected(static_cast<te::qt::widgets::HistogramChart*>(*it)->highlight( rect), false);
215 
216  break;
217  }
218  }
219  QApplication::restoreOverrideCursor();
220 }
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:148
QwtPlotPicker * m_leftPicker
The display's left button picker.
Definition: ChartDisplay.h:150
QwtPlotPanner * m_panner
The display's panner.
Definition: ChartDisplay.h:149
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:152
ChartStyle * m_chartStyle
The display's style.
Definition: ChartDisplay.h:147
void highlightOIds(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
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
A class to represent a scatter's chart.
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.
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:153
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:151
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.