qchart.cpp
185 lines
| 4.2 KiB
| text/x-c
|
CppLexer
/ src / qchart.cpp
|
r12 | #include "qchart.h" | ||
|
r85 | #include "qchartaxis.h" | ||
|
r131 | #include "chartpresenter_p.h" | ||
#include "chartdataset_p.h" | ||||
|
r42 | #include <QGraphicsScene> | ||
|
r115 | #include <QGraphicsSceneResizeEvent> | ||
|
r21 | #include <QDebug> | ||
|
r12 | |||
|
r264 | /*! | ||
\class QChart | ||||
\brief QtCommercial chart API. | ||||
QChart is a QGraphicsWidget that you can show in a QGraphicsScene. It manages the graphical | ||||
representation of different types of QChartSeries and other chart related objects like | ||||
QChartAxis and QChartLegend. If you simply want to show a chart in a layout, you can use the | ||||
convenience class QChartView instead of QChart. | ||||
*/ | ||||
|
r30 | QTCOMMERCIALCHART_BEGIN_NAMESPACE | ||
|
r12 | |||
|
r264 | /*! | ||
Constructs a chart object which is a child of parent. | ||||
*/ | ||||
|
r115 | QChart::QChart(QGraphicsItem *parent, Qt::WindowFlags wFlags) : QGraphicsWidget(parent,wFlags), | ||
|
r264 | m_backgroundItem(0), | ||
m_titleItem(0), | ||||
m_dataset(new ChartDataSet(this)), | ||||
m_presenter(new ChartPresenter(this,m_dataset)) | ||||
|
r12 | { | ||
} | ||||
|
r264 | /*! | ||
Destroys the object and it's children, like QChartSeries and QChartAxis object added to it. | ||||
*/ | ||||
QChart::~QChart() | ||||
{ | ||||
} | ||||
|
r12 | |||
|
r264 | /*! | ||
Adds the series and optional y axis onto the chart and takes the ownership of the objects. | ||||
If auto scaling is enabled, re-scales the axes the series is bound to (both the x axis and | ||||
the y axis). | ||||
*/ | ||||
void QChart::addSeries(QChartSeries* series, QChartAxis* axisY) | ||||
|
r223 | { | ||
|
r264 | m_dataset->addSeries(series, axisY); | ||
|
r223 | } | ||
|
r42 | |||
|
r223 | void QChart::removeSeries(QChartSeries* series) | ||
{ | ||||
m_dataset->removeSeries(series); | ||||
|
r42 | } | ||
|
r48 | |||
|
r258 | void QChart::removeAllSeries() | ||
{ | ||||
m_dataset->removeAllSeries(); | ||||
} | ||||
|
r122 | void QChart::setChartBackgroundBrush(const QBrush& brush) | ||
{ | ||||
|
r223 | createChartBackgroundItem(); | ||
|
r122 | m_backgroundItem->setBrush(brush); | ||
m_backgroundItem->update(); | ||||
} | ||||
void QChart::setChartBackgroundPen(const QPen& pen) | ||||
{ | ||||
|
r223 | createChartBackgroundItem(); | ||
|
r122 | m_backgroundItem->setPen(pen); | ||
m_backgroundItem->update(); | ||||
} | ||||
|
r192 | void QChart::setChartTitle(const QString& title) | ||
|
r69 | { | ||
|
r223 | createChartTitleItem(); | ||
|
r87 | m_titleItem->setPlainText(title); | ||
|
r192 | } | ||
void QChart::setChartTitleFont(const QFont& font) | ||||
{ | ||||
|
r223 | createChartTitleItem(); | ||
|
r87 | m_titleItem->setFont(font); | ||
|
r69 | } | ||
|
r223 | void QChart::createChartBackgroundItem() | ||
{ | ||||
if(!m_backgroundItem) { | ||||
m_backgroundItem = new QGraphicsRectItem(this); | ||||
|
r272 | m_backgroundItem->setPen(Qt::NoPen); | ||
|
r262 | m_backgroundItem->setZValue(ChartPresenter::BackgroundZValue); | ||
|
r223 | } | ||
} | ||||
void QChart::createChartTitleItem() | ||||
{ | ||||
|
r262 | if(!m_titleItem) { | ||
m_titleItem = new QGraphicsTextItem(this); | ||||
m_titleItem->setZValue(ChartPresenter::BackgroundZValue); | ||||
} | ||||
|
r223 | } | ||
|
r28 | int QChart::margin() const | ||
{ | ||||
|
r142 | return m_presenter->margin(); | ||
|
r28 | } | ||
|
r12 | void QChart::setMargin(int margin) | ||
{ | ||||
|
r143 | m_presenter->setMargin(margin); | ||
|
r12 | } | ||
|
r153 | void QChart::setChartTheme(QChart::ChartTheme theme) | ||
|
r64 | { | ||
|
r153 | m_presenter->setChartTheme(theme); | ||
|
r64 | } | ||
|
r153 | QChart::ChartTheme QChart::chartTheme() const | ||
|
r120 | { | ||
|
r153 | return m_presenter->chartTheme(); | ||
|
r120 | } | ||
|
r223 | void QChart::zoomIn() | ||
|
r67 | { | ||
|
r223 | if (!m_dataset->nextDomain()) { | ||
QRectF rect = m_presenter->geometry(); | ||||
rect.setWidth(rect.width()/2); | ||||
rect.setHeight(rect.height()/2); | ||||
rect.moveCenter(m_presenter->geometry().center()); | ||||
zoomIn(rect); | ||||
} | ||||
|
r67 | } | ||
|
r223 | void QChart::zoomIn(const QRectF& rect) | ||
|
r67 | { | ||
|
r223 | if(!rect.isValid()) return; | ||
QRectF r = rect.normalized(); | ||||
int margin = m_presenter->margin(); | ||||
r.translate(-margin, -margin); | ||||
m_dataset->addDomain(r,m_presenter->geometry()); | ||||
|
r67 | } | ||
void QChart::zoomOut() | ||||
{ | ||||
|
r223 | m_dataset->previousDomain(); | ||
|
r67 | } | ||
|
r93 | void QChart::zoomReset() | ||
{ | ||||
|
r223 | m_dataset->clearDomains(); | ||
|
r176 | } | ||
|
r223 | QChartAxis* QChart::axisX() const | ||
|
r176 | { | ||
|
r223 | return m_dataset->axisX(); | ||
|
r176 | } | ||
|
r223 | QChartAxis* QChart::axisY() const | ||
|
r176 | { | ||
|
r223 | return m_dataset->axisY(); | ||
|
r85 | } | ||
|
r115 | void QChart::resizeEvent(QGraphicsSceneResizeEvent *event) | ||
{ | ||||
m_rect = QRectF(QPoint(0,0),event->newSize()); | ||||
QRectF rect = m_rect.adjusted(margin(),margin(), -margin(), -margin()); | ||||
// recalculate title position | ||||
if (m_titleItem) { | ||||
QPointF center = m_rect.center() -m_titleItem->boundingRect().center(); | ||||
m_titleItem->setPos(center.x(),m_rect.top()/2 + margin()/2); | ||||
} | ||||
//recalculate background gradient | ||||
if (m_backgroundItem) { | ||||
m_backgroundItem->setRect(rect); | ||||
} | ||||
|
r117 | QGraphicsWidget::resizeEvent(event); | ||
|
r115 | update(); | ||
} | ||||
|
r64 | #include "moc_qchart.cpp" | ||
|
r48 | |||
|
r30 | QTCOMMERCIALCHART_END_NAMESPACE | ||