qchartview.cpp
236 lines
| 5.8 KiB
| text/x-c
|
CppLexer
/ src / qchartview.cpp
Michal Klocek
|
r58 | #include "qchartview.h" | ||
#include "qchart.h" | ||||
Michal Klocek
|
r176 | #include "qchartaxis.h" | ||
Michal Klocek
|
r58 | #include <QGraphicsView> | ||
#include <QGraphicsScene> | ||||
#include <QRubberBand> | ||||
#include <QResizeEvent> | ||||
#include <QDebug> | ||||
QTCOMMERCIALCHART_BEGIN_NAMESPACE | ||||
QChartView::QChartView(QWidget *parent) : | ||||
Michal Klocek
|
r176 | QGraphicsView(parent), | ||
m_scene(new QGraphicsScene()), | ||||
m_chart(new QChart()), | ||||
m_rubberBand(0), | ||||
m_verticalRubberBand(false), | ||||
m_horizonalRubberBand(false) | ||||
{ | ||||
setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff); | ||||
Michal Klocek
|
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()); | ||||
Michal Klocek
|
r115 | m_chart->resize(size()); | ||
Michal Klocek
|
r58 | QWidget::resizeEvent(event); | ||
} | ||||
void QChartView::addSeries(QChartSeries* series) | ||||
{ | ||||
m_chart->addSeries(series); | ||||
} | ||||
Tero Ahola
|
r61 | QChartSeries* QChartView::createSeries(QChartSeries::QChartSeriesType type) | ||
Michal Klocek
|
r58 | { | ||
Tero Ahola
|
r61 | return m_chart->createSeries(type); | ||
Michal Klocek
|
r58 | } | ||
Michal Klocek
|
r67 | void QChartView::zoomInToRect(const QRect& rectangle) | ||
Michal Klocek
|
r58 | { | ||
Michal Klocek
|
r67 | m_chart->zoomInToRect(rectangle); | ||
Michal Klocek
|
r58 | } | ||
Michal Klocek
|
r67 | void QChartView::zoomIn() | ||
Michal Klocek
|
r58 | { | ||
Michal Klocek
|
r67 | m_chart->zoomIn(); | ||
Michal Klocek
|
r58 | } | ||
Michal Klocek
|
r67 | void QChartView::zoomOut() | ||
Michal Klocek
|
r58 | { | ||
Michal Klocek
|
r67 | m_chart->zoomOut(); | ||
Michal Klocek
|
r58 | } | ||
Michal Klocek
|
r67 | int QChartView::margin() const | ||
{ | ||||
return m_chart->margin(); | ||||
} | ||||
Michal Klocek
|
r69 | |||
void QChartView::setTitle(const QString& title) | ||||
{ | ||||
m_chart->setTitle(title); | ||||
} | ||||
Michal Klocek
|
r122 | void QChartView::setChartBackgroundBrush(const QBrush& brush) | ||
{ | ||||
Michal Klocek
|
r176 | m_chart->setChartBackgroundBrush(brush); | ||
Michal Klocek
|
r122 | } | ||
void QChartView::setChartBackgroundPen(const QPen& pen) | ||||
{ | ||||
Michal Klocek
|
r176 | m_chart->setChartBackgroundPen(pen); | ||
Michal Klocek
|
r122 | } | ||
Michal Klocek
|
r136 | |||
void QChartView::setRubberBandPolicy(const RubberBandPolicy policy) | ||||
{ | ||||
Michal Klocek
|
r176 | switch(policy) { | ||
Michal Klocek
|
r136 | case VerticalRubberBand: | ||
Michal Klocek
|
r176 | m_verticalRubberBand = true; | ||
m_horizonalRubberBand = false; | ||||
break; | ||||
Michal Klocek
|
r136 | case HorizonalRubberBand: | ||
Michal Klocek
|
r176 | m_verticalRubberBand = false; | ||
m_horizonalRubberBand = true; | ||||
break; | ||||
Michal Klocek
|
r136 | case RectangleRubberBand: | ||
Michal Klocek
|
r176 | m_verticalRubberBand = true; | ||
m_horizonalRubberBand = true; | ||||
break; | ||||
Michal Klocek
|
r136 | case NoRubberBand: | ||
default: | ||||
Michal Klocek
|
r176 | delete m_rubberBand; | ||
m_rubberBand=0; | ||||
m_horizonalRubberBand = false; | ||||
m_verticalRubberBand = false; | ||||
return; | ||||
Michal Klocek
|
r136 | } | ||
Michal Klocek
|
r176 | if(!m_rubberBand) { | ||
m_rubberBand = new QRubberBand(QRubberBand::Rectangle, this); | ||||
m_rubberBand->setEnabled(true); | ||||
Michal Klocek
|
r138 | } | ||
Michal Klocek
|
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) | ||||
{ | ||||
Michal Klocek
|
r176 | if(m_rubberBand && m_rubberBand->isVisible()) { | ||
Michal Klocek
|
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()); | ||||
Michal Klocek
|
r176 | } | ||
else { | ||||
Michal Klocek
|
r136 | QGraphicsView::mouseMoveEvent(event); | ||
} | ||||
} | ||||
void QChartView::mouseReleaseEvent(QMouseEvent *event) | ||||
{ | ||||
Michal Klocek
|
r176 | if(m_rubberBand) { | ||
if (event->button() == Qt::LeftButton && m_rubberBand->isVisible()) { | ||||
m_rubberBand->hide(); | ||||
QRect rect = m_rubberBand->geometry(); | ||||
m_chart->zoomInToRect(rect); | ||||
event->accept(); | ||||
} | ||||
Michal Klocek
|
r136 | |||
Michal Klocek
|
r176 | if(event->button()==Qt::RightButton) | ||
Michal Klocek
|
r136 | m_chart->zoomReset(); | ||
Michal Klocek
|
r176 | } | ||
else { | ||||
Michal Klocek
|
r136 | QGraphicsView::mouseReleaseEvent(event); | ||
} | ||||
} | ||||
void QChartView::keyPressEvent(QKeyEvent *event) | ||||
{ | ||||
switch (event->key()) { | ||||
Michal Klocek
|
r176 | case Qt::Key_Plus: | ||
Michal Klocek
|
r136 | zoomIn(); | ||
break; | ||||
Michal Klocek
|
r176 | case Qt::Key_Minus: | ||
Michal Klocek
|
r136 | zoomOut(); | ||
break; | ||||
Michal Klocek
|
r176 | default: | ||
Michal Klocek
|
r136 | QGraphicsView::keyPressEvent(event); | ||
break; | ||||
} | ||||
} | ||||
Michal Klocek
|
r153 | void QChartView::setChartTheme(QChart::ChartTheme theme) | ||
Michal Klocek
|
r136 | { | ||
Michal Klocek
|
r153 | m_chart->setChartTheme(theme); | ||
Michal Klocek
|
r136 | } | ||
Michal Klocek
|
r153 | QChart::ChartTheme QChartView::chartTheme() const | ||
{ | ||||
return m_chart->chartTheme(); | ||||
} | ||||
Michal Klocek
|
r136 | |||
Michal Klocek
|
r176 | void QChartView::setDefaultAxisX(const QChartAxis& axis) | ||
{ | ||||
m_chart->setDefaultAxisX(axis); | ||||
} | ||||
void QChartView::setDefaultAxisY(const QChartAxis& axis) | ||||
Michal Klocek
|
r155 | { | ||
Michal Klocek
|
r176 | m_chart->setDefaultAxisY(axis); | ||
Michal Klocek
|
r155 | } | ||
Michal Klocek
|
r176 | QChartAxis QChartView::defaultAxisX() const | ||
Michal Klocek
|
r155 | { | ||
Michal Klocek
|
r176 | return m_chart->defaultAxisX(); | ||
Michal Klocek
|
r155 | } | ||
Michal Klocek
|
r176 | QChartAxis QChartView::defaultAxisY() const | ||
Michal Klocek
|
r155 | { | ||
Michal Klocek
|
r176 | return m_chart->defaultAxisY(); | ||
Michal Klocek
|
r155 | } | ||
Michal Klocek
|
r176 | int QChartView::addAxisY(const QChartAxis& axis) | ||
Michal Klocek
|
r155 | { | ||
Michal Klocek
|
r176 | return m_chart->addAxisY(axis); | ||
Michal Klocek
|
r155 | } | ||
Michal Klocek
|
r176 | QChartAxis QChartView::axisY(int id) const | ||
Michal Klocek
|
r155 | { | ||
Michal Klocek
|
r176 | return m_chart->axisY(id); | ||
Michal Klocek
|
r155 | } | ||
Michal Klocek
|
r176 | void QChartView::removeAxisY(int id) | ||
{ | ||||
m_chart->removeAxisY(id); | ||||
} | ||||
Michal Klocek
|
r58 | QTCOMMERCIALCHART_END_NAMESPACE | ||