qchart.cpp
203 lines
| 4.8 KiB
| text/x-c
|
CppLexer
/ src / qchart.cpp
Michal Klocek
|
r12 | #include "qchart.h" | ||
Michal Klocek
|
r85 | #include "qchartaxis.h" | ||
Michal Klocek
|
r131 | #include "chartpresenter_p.h" | ||
#include "chartdataset_p.h" | ||||
Tero Ahola
|
r42 | #include <QGraphicsScene> | ||
Michal Klocek
|
r115 | #include <QGraphicsSceneResizeEvent> | ||
Michal Klocek
|
r21 | #include <QDebug> | ||
Michal Klocek
|
r12 | |||
Tero Ahola
|
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. | ||||
Marek Rosa
|
r274 | \sa QChartView | ||
Tero Ahola
|
r264 | */ | ||
Tero Ahola
|
r30 | QTCOMMERCIALCHART_BEGIN_NAMESPACE | ||
Michal Klocek
|
r12 | |||
Tero Ahola
|
r264 | /*! | ||
Constructs a chart object which is a child of parent. | ||||
*/ | ||||
Michal Klocek
|
r115 | QChart::QChart(QGraphicsItem *parent, Qt::WindowFlags wFlags) : QGraphicsWidget(parent,wFlags), | ||
Tero Ahola
|
r264 | m_backgroundItem(0), | ||
m_titleItem(0), | ||||
m_dataset(new ChartDataSet(this)), | ||||
m_presenter(new ChartPresenter(this,m_dataset)) | ||||
Michal Klocek
|
r12 | { | ||
} | ||||
Tero Ahola
|
r264 | /*! | ||
Destroys the object and it's children, like QChartSeries and QChartAxis object added to it. | ||||
*/ | ||||
QChart::~QChart() | ||||
{ | ||||
} | ||||
Michal Klocek
|
r12 | |||
Tero Ahola
|
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) | ||||
Michal Klocek
|
r223 | { | ||
Tero Ahola
|
r264 | m_dataset->addSeries(series, axisY); | ||
Michal Klocek
|
r223 | } | ||
Tero Ahola
|
r42 | |||
Marek Rosa
|
r274 | /*! | ||
Removes the QChartSeries specified in a perameter from the QChartView. | ||||
It releses its ownership of the specified QChartSeries object. | ||||
It does not delete the pointed QChartSeries data object | ||||
\sa removeSeries(), removeAllSeries() | ||||
*/ | ||||
Michal Klocek
|
r223 | void QChart::removeSeries(QChartSeries* series) | ||
{ | ||||
m_dataset->removeSeries(series); | ||||
Tero Ahola
|
r42 | } | ||
Tero Ahola
|
r48 | |||
Marek Rosa
|
r274 | /*! | ||
Removes all the QChartSeries that have been added to the QChartView | ||||
It also deletes the pointed QChartSeries data objects | ||||
\sa addSeries(), removeSeries() | ||||
*/ | ||||
Michal Klocek
|
r258 | void QChart::removeAllSeries() | ||
{ | ||||
m_dataset->removeAllSeries(); | ||||
} | ||||
Michal Klocek
|
r122 | void QChart::setChartBackgroundBrush(const QBrush& brush) | ||
{ | ||||
Michal Klocek
|
r223 | createChartBackgroundItem(); | ||
Michal Klocek
|
r122 | m_backgroundItem->setBrush(brush); | ||
m_backgroundItem->update(); | ||||
} | ||||
void QChart::setChartBackgroundPen(const QPen& pen) | ||||
{ | ||||
Michal Klocek
|
r223 | createChartBackgroundItem(); | ||
Michal Klocek
|
r122 | m_backgroundItem->setPen(pen); | ||
m_backgroundItem->update(); | ||||
} | ||||
Marek Rosa
|
r274 | /*! | ||
Sets the title description text that is rendered above the chart. | ||||
*/ | ||||
Michal Klocek
|
r192 | void QChart::setChartTitle(const QString& title) | ||
Michal Klocek
|
r69 | { | ||
Michal Klocek
|
r223 | createChartTitleItem(); | ||
Michal Klocek
|
r87 | m_titleItem->setPlainText(title); | ||
Michal Klocek
|
r192 | } | ||
Marek Rosa
|
r274 | /*! | ||
Sets the font that is used for rendering the description text that is rendered above the chart. | ||||
*/ | ||||
Michal Klocek
|
r192 | void QChart::setChartTitleFont(const QFont& font) | ||
{ | ||||
Michal Klocek
|
r223 | createChartTitleItem(); | ||
Michal Klocek
|
r87 | m_titleItem->setFont(font); | ||
Michal Klocek
|
r69 | } | ||
Michal Klocek
|
r223 | void QChart::createChartBackgroundItem() | ||
{ | ||||
if(!m_backgroundItem) { | ||||
m_backgroundItem = new QGraphicsRectItem(this); | ||||
Michal Klocek
|
r272 | m_backgroundItem->setPen(Qt::NoPen); | ||
Michal Klocek
|
r262 | m_backgroundItem->setZValue(ChartPresenter::BackgroundZValue); | ||
Michal Klocek
|
r223 | } | ||
} | ||||
void QChart::createChartTitleItem() | ||||
{ | ||||
Michal Klocek
|
r262 | if(!m_titleItem) { | ||
m_titleItem = new QGraphicsTextItem(this); | ||||
m_titleItem->setZValue(ChartPresenter::BackgroundZValue); | ||||
} | ||||
Michal Klocek
|
r223 | } | ||
Michal Klocek
|
r28 | int QChart::margin() const | ||
{ | ||||
Jani Honkonen
|
r142 | return m_presenter->margin(); | ||
Michal Klocek
|
r28 | } | ||
Michal Klocek
|
r12 | void QChart::setMargin(int margin) | ||
{ | ||||
Michal Klocek
|
r143 | m_presenter->setMargin(margin); | ||
Michal Klocek
|
r12 | } | ||
Michal Klocek
|
r153 | void QChart::setChartTheme(QChart::ChartTheme theme) | ||
Tero Ahola
|
r64 | { | ||
Michal Klocek
|
r153 | m_presenter->setChartTheme(theme); | ||
Tero Ahola
|
r64 | } | ||
Michal Klocek
|
r153 | QChart::ChartTheme QChart::chartTheme() const | ||
Tero Ahola
|
r120 | { | ||
Michal Klocek
|
r153 | return m_presenter->chartTheme(); | ||
Tero Ahola
|
r120 | } | ||
Michal Klocek
|
r223 | void QChart::zoomIn() | ||
Michal Klocek
|
r67 | { | ||
Michal Klocek
|
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); | ||||
} | ||||
Michal Klocek
|
r67 | } | ||
Michal Klocek
|
r223 | void QChart::zoomIn(const QRectF& rect) | ||
Michal Klocek
|
r67 | { | ||
Michal Klocek
|
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()); | ||||
Michal Klocek
|
r67 | } | ||
void QChart::zoomOut() | ||||
{ | ||||
Michal Klocek
|
r223 | m_dataset->previousDomain(); | ||
Michal Klocek
|
r67 | } | ||
Tero Ahola
|
r93 | void QChart::zoomReset() | ||
{ | ||||
Michal Klocek
|
r223 | m_dataset->clearDomains(); | ||
Michal Klocek
|
r176 | } | ||
Michal Klocek
|
r223 | QChartAxis* QChart::axisX() const | ||
Michal Klocek
|
r176 | { | ||
Michal Klocek
|
r223 | return m_dataset->axisX(); | ||
Michal Klocek
|
r176 | } | ||
Michal Klocek
|
r223 | QChartAxis* QChart::axisY() const | ||
Michal Klocek
|
r176 | { | ||
Michal Klocek
|
r223 | return m_dataset->axisY(); | ||
Michal Klocek
|
r85 | } | ||
Michal Klocek
|
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); | ||||
} | ||||
Michal Klocek
|
r117 | QGraphicsWidget::resizeEvent(event); | ||
Michal Klocek
|
r115 | update(); | ||
} | ||||
Tero Ahola
|
r64 | #include "moc_qchart.cpp" | ||
Tero Ahola
|
r48 | |||
Tero Ahola
|
r30 | QTCOMMERCIALCHART_END_NAMESPACE | ||