qchartwidget.cpp
100 lines
| 2.6 KiB
| text/x-c
|
CppLexer
/ src / qchartwidget.cpp
Tero Ahola
|
r19 | #include "qchartwidget.h" | ||
Michal Klocek
|
r21 | #include "qchartseries.h" | ||
Tero Ahola
|
r19 | #include <QGraphicsView> | ||
#include <QGraphicsScene> | ||||
Michal Klocek
|
r21 | #include <QResizeEvent> | ||
Tero Ahola
|
r19 | |||
Tero Ahola
|
r30 | QTCOMMERCIALCHART_BEGIN_NAMESPACE | ||
Tero Ahola
|
r19 | |||
QChartWidget::QChartWidget(QWidget *parent) : | ||||
Tero Ahola
|
r77 | QGraphicsView(parent), | ||
Tero Ahola
|
r93 | m_rubberBand(QRubberBand::Rectangle, this), | ||
m_originX(0) | ||||
Tero Ahola
|
r19 | { | ||
Tero Ahola
|
r29 | setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding); | ||
Michal Klocek
|
r58 | m_scene = new QGraphicsScene(); | ||
setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff); | ||||
setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff); | ||||
setScene(m_scene); | ||||
Tero Ahola
|
r61 | |||
Michal Klocek
|
r53 | m_chart = new QChart(); | ||
m_scene->addItem(m_chart); | ||||
Tero Ahola
|
r77 | m_rubberBand.setEnabled(true); // TODO: should zoom be enabled by default? | ||
Tero Ahola
|
r55 | show(); | ||
Tero Ahola
|
r19 | } | ||
QChartWidget::~QChartWidget() | ||||
{ | ||||
} | ||||
Michal Klocek
|
r21 | void QChartWidget::resizeEvent(QResizeEvent *event) | ||
Tero Ahola
|
r19 | { | ||
Michal Klocek
|
r53 | m_scene->setSceneRect(0,0,size().width(),size().height()); | ||
m_chart->setSize(size()); | ||||
Michal Klocek
|
r21 | QWidget::resizeEvent(event); | ||
} | ||||
Tero Ahola
|
r19 | |||
Tero Ahola
|
r29 | QSize QChartWidget::sizeHint() const | ||
{ | ||||
// TODO: calculate size hint based on contents? | ||||
return QSize(100, 100); | ||||
} | ||||
Michal Klocek
|
r21 | |||
Tero Ahola
|
r77 | void QChartWidget::mousePressEvent(QMouseEvent *event) | ||
{ | ||||
if(m_rubberBand.isEnabled() && event->button() == Qt::LeftButton) { | ||||
int margin = m_chart->margin(); | ||||
QRect rect(margin, margin, width() - 2 * margin, height() - 2 * margin); | ||||
Tero Ahola
|
r93 | if (rect.contains(event->pos())) { | ||
m_originX = event->pos().x(); | ||||
m_rubberBand.setGeometry(QRect(m_originX, 0, 0, height())); | ||||
Tero Ahola
|
r77 | m_rubberBand.show(); | ||
event->accept(); | ||||
} | ||||
} | ||||
} | ||||
void QChartWidget::mouseMoveEvent(QMouseEvent *event) | ||||
{ | ||||
if(m_rubberBand.isVisible()) | ||||
Tero Ahola
|
r93 | m_rubberBand.setGeometry(QRect(m_originX, 0, | ||
event->pos().x() - m_originX, height()).normalized()); | ||||
Tero Ahola
|
r77 | } | ||
void QChartWidget::mouseReleaseEvent(QMouseEvent *event) | ||||
{ | ||||
if (event->button() == Qt::LeftButton && m_rubberBand.isVisible()) { | ||||
m_rubberBand.hide(); | ||||
QRect rect = m_rubberBand.geometry(); | ||||
m_chart->zoomInToRect(rect); | ||||
event->accept(); | ||||
} | ||||
Tero Ahola
|
r93 | if(event->button()==Qt::RightButton) | ||
m_chart->zoomReset(); | ||||
Tero Ahola
|
r77 | } | ||
Michal Klocek
|
r21 | void QChartWidget::addSeries(QChartSeries* series) | ||
{ | ||||
Michal Klocek
|
r53 | m_chart->addSeries(series); | ||
Tero Ahola
|
r19 | } | ||
Tero Ahola
|
r61 | QChartSeries* QChartWidget::createSeries(QChartSeries::QChartSeriesType type) | ||
Tero Ahola
|
r42 | { | ||
Tero Ahola
|
r61 | return m_chart->createSeries(type); | ||
Tero Ahola
|
r42 | } | ||
Tero Ahola
|
r64 | |||
Tero Ahola
|
r75 | void QChartWidget::setTheme(QChart::ChartThemeId theme) | ||
Tero Ahola
|
r64 | { | ||
m_chart->setTheme(theme); | ||||
} | ||||
Tero Ahola
|
r77 | void QChartWidget::setZoomEnabled(bool enabled) | ||
{ | ||||
m_rubberBand.setEnabled(enabled); | ||||
} | ||||
Tero Ahola
|
r19 | #include "moc_qchartwidget.cpp" | ||
Tero Ahola
|
r30 | QTCOMMERCIALCHART_END_NAMESPACE | ||