From 853a3ddfabfc46ae75b50460d5347c99771f8e79 2012-06-05 09:20:02 From: Tero Ahola Date: 2012-06-05 09:20:02 Subject: [PATCH] Use qobject_cast instead of reinterpret_cast in xy series casts --- diff --git a/plugins/declarative/declarativelineseries.cpp b/plugins/declarative/declarativelineseries.cpp index de9b0ad..8ee95d4 100644 --- a/plugins/declarative/declarativelineseries.cpp +++ b/plugins/declarative/declarativelineseries.cpp @@ -30,6 +30,11 @@ DeclarativeLineSeries::DeclarativeLineSeries(QObject *parent) : { } +QXYSeries *DeclarativeLineSeries::xySeries() +{ + return this; +} + QDeclarativeListProperty DeclarativeLineSeries::declarativeChildren() { return QDeclarativeListProperty(this, 0, &appendDeclarativeChildren); diff --git a/plugins/declarative/declarativelineseries.h b/plugins/declarative/declarativelineseries.h index 6219680..846767b 100644 --- a/plugins/declarative/declarativelineseries.h +++ b/plugins/declarative/declarativelineseries.h @@ -40,6 +40,7 @@ class DeclarativeLineSeries : public QLineSeries, public DeclarativeXySeries, pu public: explicit DeclarativeLineSeries(QObject *parent = 0); + QXYSeries *xySeries(); QDeclarativeListProperty declarativeChildren(); public: // from QDeclarativeParserStatus diff --git a/plugins/declarative/declarativescatterseries.cpp b/plugins/declarative/declarativescatterseries.cpp index 15a7c3f..2560bb8 100644 --- a/plugins/declarative/declarativescatterseries.cpp +++ b/plugins/declarative/declarativescatterseries.cpp @@ -30,6 +30,11 @@ DeclarativeScatterSeries::DeclarativeScatterSeries(QObject *parent) : { } +QXYSeries *DeclarativeScatterSeries::xySeries() +{ + return this; +} + QDeclarativeListProperty DeclarativeScatterSeries::declarativeChildren() { return QDeclarativeListProperty(this, 0, &appendDeclarativeChildren); diff --git a/plugins/declarative/declarativescatterseries.h b/plugins/declarative/declarativescatterseries.h index 428c4a7..ff7b1d4 100644 --- a/plugins/declarative/declarativescatterseries.h +++ b/plugins/declarative/declarativescatterseries.h @@ -40,6 +40,7 @@ class DeclarativeScatterSeries : public QScatterSeries, public DeclarativeXySeri public: explicit DeclarativeScatterSeries(QObject *parent = 0); + QXYSeries *xySeries(); QDeclarativeListProperty declarativeChildren(); QColor brushColor(); void setBrushColor(QColor color); diff --git a/plugins/declarative/declarativesplineseries.cpp b/plugins/declarative/declarativesplineseries.cpp index 4dcdff5..1c9e4e7 100644 --- a/plugins/declarative/declarativesplineseries.cpp +++ b/plugins/declarative/declarativesplineseries.cpp @@ -30,6 +30,11 @@ DeclarativeSplineSeries::DeclarativeSplineSeries(QObject *parent) : { } +QXYSeries *DeclarativeSplineSeries::xySeries() +{ + return this; +} + QDeclarativeListProperty DeclarativeSplineSeries::declarativeChildren() { return QDeclarativeListProperty(this, 0, &appendDeclarativeChildren); diff --git a/plugins/declarative/declarativesplineseries.h b/plugins/declarative/declarativesplineseries.h index 880e9c6..21fa276 100644 --- a/plugins/declarative/declarativesplineseries.h +++ b/plugins/declarative/declarativesplineseries.h @@ -40,6 +40,7 @@ class DeclarativeSplineSeries : public QSplineSeries, public DeclarativeXySeries public: explicit DeclarativeSplineSeries(QObject *parent = 0); + QXYSeries *xySeries(); QDeclarativeListProperty declarativeChildren(); public: // from QDeclarativeParserStatus diff --git a/plugins/declarative/declarativexyseries.cpp b/plugins/declarative/declarativexyseries.cpp index 07bf338..4a7ea1b 100644 --- a/plugins/declarative/declarativexyseries.cpp +++ b/plugins/declarative/declarativexyseries.cpp @@ -42,8 +42,9 @@ void DeclarativeXySeries::classBegin() void DeclarativeXySeries::componentComplete() { - // All the inherited objects must be of type QXYSeries, so it is safe to cast - QXYSeries *series = reinterpret_cast(this); + QXYSeries *series = qobject_cast(xySeries()); + Q_ASSERT(series); + foreach(QObject *child, series->children()) { if (qobject_cast(child)) { // TODO: @@ -61,13 +62,15 @@ void DeclarativeXySeries::componentComplete() QColor DeclarativeXySeries::penColor() { // All the inherited objects must be of type QXYSeries, so it is safe to cast - QXYSeries *series = reinterpret_cast(this); + QXYSeries *series = qobject_cast(xySeries()); + Q_ASSERT(series); return series->pen().color(); } void DeclarativeXySeries::setPenColor(QColor color) { - QXYSeries *series = reinterpret_cast(this); + QXYSeries *series = qobject_cast(xySeries()); + Q_ASSERT(series); QPen pen = series->pen(); pen.setColor(color); series->setPen(pen); @@ -75,7 +78,8 @@ void DeclarativeXySeries::setPenColor(QColor color) DeclarativeXyPoint *DeclarativeXySeries::at(int index) { - QXYSeries *series = reinterpret_cast(this); + QXYSeries *series = qobject_cast(xySeries()); + Q_ASSERT(series); if (index < series->count()) { QPointF point = series->points().at(index); DeclarativeXyPoint *xyPoint = new DeclarativeXyPoint(series); diff --git a/plugins/declarative/declarativexyseries.h b/plugins/declarative/declarativexyseries.h index e699a1f..e00ef16 100644 --- a/plugins/declarative/declarativexyseries.h +++ b/plugins/declarative/declarativexyseries.h @@ -24,6 +24,7 @@ #include "qchartglobal.h" #include "declarativexypoint.h" #include +#include QTCOMMERCIALCHART_BEGIN_NAMESPACE @@ -39,6 +40,7 @@ public: public: void classBegin(); void componentComplete(); + virtual QXYSeries *xySeries() = 0; QColor penColor(); void setPenColor(QColor color); DeclarativeXyPoint *at(int index);