qchartview.cpp
226 lines
| 5.5 KiB
| text/x-c
|
CppLexer
/ src / qchartview.cpp
|
r58 | #include "qchartview.h" | ||
#include "qchart.h" | ||||
|
r176 | #include "qchartaxis.h" | ||
|
r58 | #include <QGraphicsView> | ||
#include <QGraphicsScene> | ||||
#include <QRubberBand> | ||||
#include <QResizeEvent> | ||||
#include <QDebug> | ||||
|
r233 | /*! | ||
\class QChartView | ||||
\brief Chart widget. | ||||
*/ | ||||
|
r58 | QTCOMMERCIALCHART_BEGIN_NAMESPACE | ||
QChartView::QChartView(QWidget *parent) : | ||||
|
r176 | QGraphicsView(parent), | ||
|
r204 | m_scene(new QGraphicsScene(this)), | ||
|
r176 | m_chart(new QChart()), | ||
m_rubberBand(0), | ||||
m_verticalRubberBand(false), | ||||
m_horizonalRubberBand(false) | ||||
{ | ||||
setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff); | ||||
|
r58 | setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff); | ||
setScene(m_scene); | ||||
m_chart->setMargin(50); | ||||
m_scene->addItem(m_chart); | ||||
setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding); | ||||
} | ||||
QChartView::~QChartView() | ||||
{ | ||||
} | ||||
void QChartView::resizeEvent(QResizeEvent *event) | ||||
{ | ||||
m_scene->setSceneRect(0,0,size().width(),size().height()); | ||||
|
r115 | m_chart->resize(size()); | ||
|
r58 | QWidget::resizeEvent(event); | ||
} | ||||
|
r223 | void QChartView::addSeries(QChartSeries* series,QChartAxis *axisY) | ||
|
r58 | { | ||
|
r223 | m_chart->addSeries(series,axisY); | ||
|
r58 | } | ||
|
r245 | void QChartView::removeSeries(QChartSeries* series) | ||
{ | ||||
m_chart->removeSeries(series); | ||||
} | ||||
|
r258 | void QChartView::removeAllSeries() | ||
{ | ||||
m_chart->removeAllSeries(); | ||||
} | ||||
|
r223 | void QChartView::zoomIn() | ||
|
r58 | { | ||
|
r223 | m_chart->zoomIn(); | ||
|
r58 | } | ||
|
r223 | void QChartView::zoomIn(const QRect& rect) | ||
|
r58 | { | ||
|
r223 | m_chart->zoomIn(rect); | ||
|
r58 | } | ||
|
r67 | void QChartView::zoomOut() | ||
|
r58 | { | ||
|
r67 | m_chart->zoomOut(); | ||
|
r58 | } | ||
|
r67 | int QChartView::margin() const | ||
{ | ||||
return m_chart->margin(); | ||||
} | ||||
|
r69 | |||
|
r192 | void QChartView::setChartTitle(const QString& title) | ||
|
r69 | { | ||
|
r192 | m_chart->setChartTitle(title); | ||
} | ||||
void QChartView::setChartTitleFont(const QFont& font) | ||||
{ | ||||
m_chart->setChartTitleFont(font); | ||||
|
r69 | } | ||
|
r122 | void QChartView::setChartBackgroundBrush(const QBrush& brush) | ||
{ | ||||
|
r176 | m_chart->setChartBackgroundBrush(brush); | ||
|
r122 | } | ||
void QChartView::setChartBackgroundPen(const QPen& pen) | ||||
{ | ||||
|
r176 | m_chart->setChartBackgroundPen(pen); | ||
|
r122 | } | ||
|
r136 | |||
void QChartView::setRubberBandPolicy(const RubberBandPolicy policy) | ||||
{ | ||||
|
r176 | switch(policy) { | ||
|
r136 | case VerticalRubberBand: | ||
|
r176 | m_verticalRubberBand = true; | ||
m_horizonalRubberBand = false; | ||||
break; | ||||
|
r136 | case HorizonalRubberBand: | ||
|
r176 | m_verticalRubberBand = false; | ||
m_horizonalRubberBand = true; | ||||
break; | ||||
|
r136 | case RectangleRubberBand: | ||
|
r176 | m_verticalRubberBand = true; | ||
m_horizonalRubberBand = true; | ||||
break; | ||||
|
r136 | case NoRubberBand: | ||
default: | ||||
|
r176 | delete m_rubberBand; | ||
m_rubberBand=0; | ||||
m_horizonalRubberBand = false; | ||||
m_verticalRubberBand = false; | ||||
return; | ||||
|
r136 | } | ||
|
r176 | if(!m_rubberBand) { | ||
m_rubberBand = new QRubberBand(QRubberBand::Rectangle, this); | ||||
m_rubberBand->setEnabled(true); | ||||
|
r138 | } | ||
|
r136 | } | ||
QChartView::RubberBandPolicy QChartView::rubberBandPolicy() const | ||||
{ | ||||
if(m_horizonalRubberBand && m_verticalRubberBand) return RectangleRubberBand; | ||||
if(m_horizonalRubberBand) return HorizonalRubberBand; | ||||
if(m_verticalRubberBand) return VerticalRubberBand; | ||||
return NoRubberBand; | ||||
} | ||||
void QChartView::mousePressEvent(QMouseEvent *event) | ||||
{ | ||||
if(m_rubberBand && m_rubberBand->isEnabled() && event->button() == Qt::LeftButton) { | ||||
int margin = m_chart->margin(); | ||||
QRect rect(margin, margin, width() - 2 * margin, height() - 2 * margin); | ||||
if (rect.contains(event->pos())) { | ||||
m_rubberBandOrigin = event->pos(); | ||||
m_rubberBand->setGeometry(QRect(m_rubberBandOrigin, QSize())); | ||||
m_rubberBand->show(); | ||||
event->accept(); | ||||
} | ||||
} | ||||
} | ||||
void QChartView::mouseMoveEvent(QMouseEvent *event) | ||||
{ | ||||
|
r176 | if(m_rubberBand && m_rubberBand->isVisible()) { | ||
|
r136 | int margin = m_chart->margin(); | ||
QRect rect(margin, margin, width() - 2 * margin, height() - 2 * margin); | ||||
int width = event->pos().x() - m_rubberBandOrigin.x(); | ||||
int height = event->pos().y() - m_rubberBandOrigin.y(); | ||||
if(!m_verticalRubberBand) { | ||||
m_rubberBandOrigin.setY(rect.top()); | ||||
height = rect.height(); | ||||
} | ||||
if(!m_horizonalRubberBand) { | ||||
m_rubberBandOrigin.setX(rect.left()); | ||||
width= rect.width(); | ||||
} | ||||
m_rubberBand->setGeometry(QRect(m_rubberBandOrigin.x(),m_rubberBandOrigin.y(), width,height).normalized()); | ||||
|
r176 | } | ||
else { | ||||
|
r136 | QGraphicsView::mouseMoveEvent(event); | ||
} | ||||
} | ||||
void QChartView::mouseReleaseEvent(QMouseEvent *event) | ||||
{ | ||||
|
r176 | if(m_rubberBand) { | ||
if (event->button() == Qt::LeftButton && m_rubberBand->isVisible()) { | ||||
m_rubberBand->hide(); | ||||
QRect rect = m_rubberBand->geometry(); | ||||
|
r223 | m_chart->zoomIn(rect); | ||
|
r176 | event->accept(); | ||
} | ||||
|
r136 | |||
|
r176 | if(event->button()==Qt::RightButton) | ||
|
r136 | m_chart->zoomReset(); | ||
|
r176 | } | ||
else { | ||||
|
r136 | QGraphicsView::mouseReleaseEvent(event); | ||
} | ||||
} | ||||
void QChartView::keyPressEvent(QKeyEvent *event) | ||||
{ | ||||
switch (event->key()) { | ||||
|
r176 | case Qt::Key_Plus: | ||
|
r136 | zoomIn(); | ||
break; | ||||
|
r176 | case Qt::Key_Minus: | ||
|
r136 | zoomOut(); | ||
break; | ||||
|
r176 | default: | ||
|
r136 | QGraphicsView::keyPressEvent(event); | ||
break; | ||||
} | ||||
} | ||||
|
r153 | void QChartView::setChartTheme(QChart::ChartTheme theme) | ||
|
r136 | { | ||
|
r153 | m_chart->setChartTheme(theme); | ||
|
r136 | } | ||
|
r153 | QChart::ChartTheme QChartView::chartTheme() const | ||
{ | ||||
return m_chart->chartTheme(); | ||||
} | ||||
|
r136 | |||
|
r223 | QChartAxis* QChartView::axisX() const | ||
|
r155 | { | ||
|
r223 | return m_chart->axisX(); | ||
|
r155 | } | ||
|
r223 | QChartAxis* QChartView::axisY() const | ||
|
r155 | { | ||
|
r223 | return m_chart->axisY(); | ||
|
r155 | } | ||
|
r58 | QTCOMMERCIALCHART_END_NAMESPACE | ||