diff options
author | Miikka Heikkinen <miikka.heikkinen@digia.com> | 2014-03-18 15:33:00 +0200 |
---|---|---|
committer | Miikka Heikkinen <miikka.heikkinen@digia.com> | 2014-03-25 08:17:53 +0200 |
commit | 724bcb35136ed1af699fe8631b9297deb07571ad (patch) | |
tree | 165ea553da056e0620a8c09ed94874c2b1e9e05a /src/datavisualization/axis | |
parent | ba812351a1577163a1c9794b667f2b4e3acb9373 (diff) |
Actually use axis formatter in renderer.
Task-number: QTRD-2787
Change-Id: I0ced8e506928df5fba2e8df94258b53457f4412e
Reviewed-by: Mika Salmela <mika.salmela@digia.com>
Diffstat (limited to 'src/datavisualization/axis')
-rw-r--r-- | src/datavisualization/axis/qabstract3daxis.cpp | 6 | ||||
-rw-r--r-- | src/datavisualization/axis/qvalue3daxis.cpp | 3 | ||||
-rw-r--r-- | src/datavisualization/axis/qvalue3daxis.h | 3 | ||||
-rw-r--r-- | src/datavisualization/axis/qvalue3daxis_p.h | 1 | ||||
-rw-r--r-- | src/datavisualization/axis/qvalue3daxisformatter.cpp | 243 | ||||
-rw-r--r-- | src/datavisualization/axis/qvalue3daxisformatter.h | 38 | ||||
-rw-r--r-- | src/datavisualization/axis/qvalue3daxisformatter_p.h | 4 |
7 files changed, 107 insertions, 191 deletions
diff --git a/src/datavisualization/axis/qabstract3daxis.cpp b/src/datavisualization/axis/qabstract3daxis.cpp index e01f980c..3b60ff5d 100644 --- a/src/datavisualization/axis/qabstract3daxis.cpp +++ b/src/datavisualization/axis/qabstract3daxis.cpp @@ -407,11 +407,11 @@ void QAbstract3DAxisPrivate::setMax(float max) float oldMin = m_min; m_min = max - 1.0f; if (!allowNegatives() && m_min < 0.0f) { - if (allowZero()) { + if (allowZero()) m_min = 0.0f; - } else { + else m_min = max / 2.0f; // Need some positive value smaller than max - } + if (!allowMinMaxSame() && max == 0.0f) { m_min = oldMin; qWarning() << "Unable to set maximum value to zero."; diff --git a/src/datavisualization/axis/qvalue3daxis.cpp b/src/datavisualization/axis/qvalue3daxis.cpp index e93f440f..841349dd 100644 --- a/src/datavisualization/axis/qvalue3daxis.cpp +++ b/src/datavisualization/axis/qvalue3daxis.cpp @@ -269,8 +269,9 @@ void QValue3DAxisPrivate::updateLabels() QStringList newLabels; newLabels.reserve(m_segmentCount + 1); + m_formatter->d_ptr->recalculate(); for (int i = 0; i <= m_segmentCount; i++) { - float value = m_formatter->valueAt(m_formatter->gridPosition(i)); + float value = m_formatter->valueAt(m_formatter->gridPositions().at(i)); newLabels.append(m_formatter->stringForValue(value, m_labelFormat)); } diff --git a/src/datavisualization/axis/qvalue3daxis.h b/src/datavisualization/axis/qvalue3daxis.h index c5982f33..bb90f6d3 100644 --- a/src/datavisualization/axis/qvalue3daxis.h +++ b/src/datavisualization/axis/qvalue3daxis.h @@ -62,10 +62,11 @@ protected: private: Q_DISABLE_COPY(QValue3DAxis) + friend class Abstract3DController; friend class Bars3DController; friend class Scatter3DController; friend class Surface3DController; - friend class QValue3DAxisFormatter; + friend class QValue3DAxisFormatterPrivate; }; QT_END_NAMESPACE_DATAVISUALIZATION diff --git a/src/datavisualization/axis/qvalue3daxis_p.h b/src/datavisualization/axis/qvalue3daxis_p.h index c198db75..ff281f0c 100644 --- a/src/datavisualization/axis/qvalue3daxis_p.h +++ b/src/datavisualization/axis/qvalue3daxis_p.h @@ -67,6 +67,7 @@ private: QValue3DAxis *qptr(); friend class QValue3DAxis; + friend class Abstract3DController; }; QT_END_NAMESPACE_DATAVISUALIZATION diff --git a/src/datavisualization/axis/qvalue3daxisformatter.cpp b/src/datavisualization/axis/qvalue3daxisformatter.cpp index 5f943bd3..c8e0a662 100644 --- a/src/datavisualization/axis/qvalue3daxisformatter.cpp +++ b/src/datavisualization/axis/qvalue3daxisformatter.cpp @@ -30,19 +30,23 @@ QT_BEGIN_NAMESPACE_DATAVISUALIZATION * This class provides formatting rules for a linear QValue3DAxis. Subclass it if you * want to implement custom value axes. * + * The base class has no public API beyond constructors and destructors. It is meant to be only + * used internally. However, subclasses may implement public properties as needed. + * * \sa QLog3DAxisFormatter */ /*! * \qmltype ValueAxis3DFormatter * \inqmlmodule QtDataVisualization - * \since QtDataVisualization 1.0 + * \since QtDataVisualization 1.1 * \ingroup datavisualization_qml * \instantiates QValue3DAxisFormatter * \brief ValueAxis3DFormatter is base type for value axis formatters. * * This type provides formatting rules for a linear ValueAxis3D. * This type is the default type for ValueAxis3D and thus never needs to be explicitly created. + * This type has not public functionality. */ /*! @@ -89,157 +93,29 @@ bool QValue3DAxisFormatter::allowZero() const } /*! - * \return the normalized position along the axis for the grid line indicated by the \a index. - * The total amount of grid lines is equal to segment count plus one. The returned value should be - * between -1.0 (for minimum value) and 1.0 (for maximum value), inclusive. - * The grid line at the index zero corresponds to the minimum value of the axis. - * This value is used to position the segment grid lines when the graph is rendered. - * - * \sa doRecalculate(), gridPositions() - */ -float QValue3DAxisFormatter::gridPosition(int index) -{ - d_ptr->recalculate(); - - Q_ASSERT(d_ptr->m_gridPositions.size() > index); - - return d_ptr->m_gridPositions.at(index); -} - -/*! - * \return the normalized position along the axis for a subsegment grid line of a segment. - * The \a subGridIndex and \a segmentIndex specify the desired subgrid line. - * The returned value should be between -1.0 (for minimum value) and 1.0 (for maximum value), - * inclusive. - * The amount of subgrid lines per segment is equal to subsegment count minus one. - * This value is used to position the subsegment grid lines when the graph is rendered. - * - * \sa doRecalculate(), subGridPositions() - */ -float QValue3DAxisFormatter::subGridPosition(int subGridIndex, int segmentIndex) -{ - d_ptr->recalculate(); - - Q_ASSERT(d_ptr->m_subGridPositions.size() > subGridIndex); - Q_ASSERT(d_ptr->m_subGridPositions.at(0).size() > segmentIndex); - - return d_ptr->m_subGridPositions.at(segmentIndex).at(subGridIndex); -} - -/*! - * \return the normalized position along the axis for given label \a index. - * The total amount of labels is equal to segment count plus one. The label \a index zero - * corresponds to the minimum value of the axis. - * The returned value should be between -1.0 (for minimum value) and 1.0 (for maximum value), - * inclusive. This value is used to position the axis labels when the graph is rendered. - * - * \sa doRecalculate(), labelPositions() - */ -float QValue3DAxisFormatter::labelPosition(int index) -{ - d_ptr->recalculate(); - - Q_ASSERT(d_ptr->m_labelPositions.size() > index); - - return d_ptr->m_labelPositions.at(index); -} - -/*! - * This method can be used to convert a value to a string using formatter specific formatting - * rules supported by the labelFormat property of the parent axis. - * - * \return the formatted label string using \a value and \a format. - * - * \sa doRecalculate(), doStringForValue(), QValue3DAxis::labelFormat - */ -QString QValue3DAxisFormatter::stringForValue(float value, const QString &format) -{ - d_ptr->recalculate(); - return doStringForValue(value, format); -} - -/*! - * \return the normalized position along the axis for the given \a value. - * The returned value should be between -1.0 (for minimum value) and 1.0 (for maximum value), - * inclusive. This value is used to position the items when the graph is rendered. - * - * \sa doRecalculate(), doPositionAt() - */ -float QValue3DAxisFormatter::positionAt(float value) -{ - d_ptr->recalculate(); - return doPositionAt(value); -} - -/*! - * \return the value at the normalized \a position along the axis. - * The \a position value should be between -1.0 (for minimum value) and 1.0 (for maximum value), - * inclusive. - * - * \sa doRecalculate(), doValueAt() - */ -float QValue3DAxisFormatter::valueAt(float position) -{ - d_ptr->recalculate(); - return doValueAt(position); -} - -/*! * Creates a new empty instance of this formatter. Must be reimplemented in a subclass. * * \return the new instance. The renderer uses this method to cache a copy of the * the formatter. The ownership of the new copy transfers to the caller. */ -QValue3DAxisFormatter *QValue3DAxisFormatter::createNewInstance() +QValue3DAxisFormatter *QValue3DAxisFormatter::createNewInstance() const { return new QValue3DAxisFormatter(); } /*! - * Populates the \a copy with the values of this formatter. - * - * \sa doPopulateCopy() - */ -void QValue3DAxisFormatter::populateCopy(QValue3DAxisFormatter ©) -{ - d_ptr->recalculate(); - doPopulateCopy(copy); -} - -/*! - * Marks this formatter dirty, prompting the renderer to make a new copy of its cache on the next - * renderer synchronization. Recalculation is also triggered on next access to the values. - * This method should be called by a subclass whenever the formatter - * is changed in a way that affects the resolved values. - */ -void QValue3DAxisFormatter::markDirty() -{ - d_ptr->m_needsRecalculate = true; - if (d_ptr->m_axis) - emit d_ptr->m_axis->dptr()->formatterDirty(); -} - -/*! - * \return the parent axis. The parent axis must only be accessed in doRecalculate() - * method to maintain thread safety in environments using a threaded renderer. - * - * \sa doRecalculate() - */ -QValue3DAxis *QValue3DAxisFormatter::axis() const -{ - return d_ptr->m_axis; -} - -/*! * This method populates the label and grid line position arrays, as well as calculates * any values needed for mapping between value and position. It is allowed to access * the parent axis from inside this function. * * This method must be reimplemented in a subclass. * + * See gridPositions(), subGridPositions(), and labelPositions() documentation about the arrays + * that need to be populated. + * * \sa gridPositions(), subGridPositions(), labelPositions(), axis() */ -void QValue3DAxisFormatter::doRecalculate() +void QValue3DAxisFormatter::recalculate() { d_ptr->doRecalculate(); } @@ -250,9 +126,9 @@ void QValue3DAxisFormatter::doRecalculate() * * \return the formatted label string using \a value and \a format. * - * \sa doRecalculate(), stringForValue(), QValue3DAxis::labelFormat + * \sa recalculate(), stringForValue(), QValue3DAxis::labelFormat */ -QString QValue3DAxisFormatter::doStringForValue(float value, const QString &format) +QString QValue3DAxisFormatter::stringForValue(float value, const QString &format) const { return d_ptr->stringForValue(value, format); } @@ -262,12 +138,12 @@ QString QValue3DAxisFormatter::doStringForValue(float value, const QString &form * between the parent axis minimum and maximum values. * * \return the normalized position along the axis for the given \a value. - * The returned value should be between -1.0 (for minimum value) and 1.0 (for maximum value), + * The returned value should be between 0.0 (for minimum value) and 1.0 (for maximum value), * inclusive, if the value is within the parent axis range. * * \sa doRecalculate(), positionAt() */ -float QValue3DAxisFormatter::doPositionAt(float value) +float QValue3DAxisFormatter::positionAt(float value) const { return d_ptr->positionAt(value); } @@ -277,12 +153,12 @@ float QValue3DAxisFormatter::doPositionAt(float value) * between the parent axis minimum and maximum values. * * \return the value at the normalized \a position along the axis. - * The \a position value should be between -1.0 (for minimum value) and 1.0 (for maximum value), + * The \a position value should be between 0.0 (for minimum value) and 1.0 (for maximum value), * inclusive to obtain values within the parent axis range. * * \sa doRecalculate(), positionAt() */ -float QValue3DAxisFormatter::doValueAt(float position) +float QValue3DAxisFormatter::valueAt(float position) const { return d_ptr->valueAt(position); } @@ -294,31 +170,50 @@ float QValue3DAxisFormatter::doValueAt(float position) * * \return the new copy. The ownership of the new copy transfers to the caller. */ -void QValue3DAxisFormatter::doPopulateCopy(QValue3DAxisFormatter ©) +void QValue3DAxisFormatter::populateCopy(QValue3DAxisFormatter ©) const +{ + d_ptr->doPopulateCopy(*(copy.d_ptr.data())); +} + +/*! + * Marks this formatter dirty, prompting the renderer to make a new copy of its cache on the next + * renderer synchronization. This method should be called by a subclass whenever the formatter + * is changed in a way that affects the resolved values. + */ +void QValue3DAxisFormatter::markDirty() { - copy.d_ptr->m_min = d_ptr->m_min; - copy.d_ptr->m_max = d_ptr->m_max; + d_ptr->markDirty(); +} + +/*! + * \return the parent axis. The parent axis must only be accessed in recalculate() + * method to maintain thread safety in environments using a threaded renderer. + * + * \sa recalculate() + */ +QValue3DAxis *QValue3DAxisFormatter::axis() const +{ + return d_ptr->m_axis; } /*! * \return Returns a reference to the array of normalized grid positions. * The array size is equal to the segment count of the parent axis plus one. + * The values should be between 0.0 (for minimum value) and 1.0 (for maximum value), inclusive. * The grid line at the index zero corresponds to the minimum value of the axis. - * - * \sa gridPosition() */ -QVector<float> &QValue3DAxisFormatter::gridPositions() +QVector<float> &QValue3DAxisFormatter::gridPositions() const { return d_ptr->m_gridPositions; } /*! * \return Returns a reference to the array of normalized subgrid positions. - * The array size is equal to subsegment count of the parent axis minus one. - * - * \sa subGridPosition() + * The array size is equal to segment count of tha parent axis times subsegment count of the parent + * axis minus one. + * The values should be between 0.0 (for minimum value) and 1.0 (for maximum value), inclusive. */ -QVector<QVector<float> > &QValue3DAxisFormatter::subGridPositions() +QVector<QVector<float> > &QValue3DAxisFormatter::subGridPositions() const { return d_ptr->m_subGridPositions; } @@ -326,10 +221,10 @@ QVector<QVector<float> > &QValue3DAxisFormatter::subGridPositions() /*! * \return Returns a reference to the array of normalized label positions. * The array size is equal to the segment count of the parent axis plus one. - * - * \sa labelPosition() + * The values should be between 0.0 (for minimum value) and 1.0 (for maximum value), inclusive. + * The label at the index zero corresponds to the minimum value of the axis. */ -QVector<float> &QValue3DAxisFormatter::labelPositions() +QVector<float> &QValue3DAxisFormatter::labelPositions() const { return d_ptr->m_labelPositions; } @@ -356,7 +251,7 @@ void QValue3DAxisFormatterPrivate::recalculate() // Only recalculate if we need to and have m_axis pointer. If we do not have // m_axis, either we are not attached to an axis or this is a renderer cache. if (m_axis && m_needsRecalculate) - q_ptr->doRecalculate(); + q_ptr->recalculate(); } void QValue3DAxisFormatterPrivate::doRecalculate() @@ -371,12 +266,13 @@ void QValue3DAxisFormatterPrivate::doRecalculate() int subGridCount = m_axis->subSegmentCount() - 1; m_min = m_axis->min(); m_max = m_axis->max(); - m_rangeNormalizer = (m_max - m_min) / 2.0f; + m_rangeNormalizer = (m_max - m_min); m_gridPositions.resize(segmentCount + 1); - float segmentStep = 2.0f / float(segmentCount); + float segmentStep = 1.0f / float(segmentCount); float subSegmentStep = 0; + if (subGridCount > 0) { subSegmentStep = segmentStep / (subGridCount + 1); m_subGridPositions.resize(segmentCount); @@ -389,12 +285,12 @@ void QValue3DAxisFormatterPrivate::doRecalculate() // Calculate positions for (int i = 0; i < segmentCount; i++) { - float gridValue = -1.0f + segmentStep * i; + float gridValue = segmentStep * i; m_gridPositions[i] = gridValue; m_labelPositions[i] = gridValue; if (m_subGridPositions.size()) { for (int j = 0; j < subGridCount; j++) - m_subGridPositions[i][j] = gridValue + subSegmentStep * i; + m_subGridPositions[i][j] = gridValue + subSegmentStep * (j + 1); } } @@ -403,6 +299,23 @@ void QValue3DAxisFormatterPrivate::doRecalculate() m_labelPositions[segmentCount] = 1.0f; } +void QValue3DAxisFormatterPrivate::populateCopy(QValue3DAxisFormatter ©) +{ + recalculate(); + q_ptr->populateCopy(copy); +} + +void QValue3DAxisFormatterPrivate::doPopulateCopy(QValue3DAxisFormatterPrivate ©) +{ + copy.m_min = m_min; + copy.m_max = m_max; + copy.m_rangeNormalizer = m_rangeNormalizer; + + copy.m_gridPositions = m_gridPositions; + copy.m_labelPositions = m_labelPositions; + copy.m_subGridPositions = m_subGridPositions; +} + QString QValue3DAxisFormatterPrivate::stringForValue(float value, const QString &format) { if (m_previousLabelFormat.compare(format)) { @@ -417,12 +330,12 @@ QString QValue3DAxisFormatterPrivate::stringForValue(float value, const QString float QValue3DAxisFormatterPrivate::positionAt(float value) const { - return (((value - m_min) / m_rangeNormalizer) - 1.0f); + return ((value - m_min) / m_rangeNormalizer); } float QValue3DAxisFormatterPrivate::valueAt(float position) const { - return (((position + 1.0f) * m_rangeNormalizer) + m_min); + return ((position * m_rangeNormalizer) + m_min); } void QValue3DAxisFormatterPrivate::setAxis(QValue3DAxis *axis) @@ -430,18 +343,20 @@ void QValue3DAxisFormatterPrivate::setAxis(QValue3DAxis *axis) Q_ASSERT(axis); connect(axis, &QValue3DAxis::segmentCountChanged, - this, &QValue3DAxisFormatterPrivate::setNeedsRecalculate); + this, &QValue3DAxisFormatterPrivate::markDirty); connect(axis, &QValue3DAxis::subSegmentCountChanged, - this, &QValue3DAxisFormatterPrivate::setNeedsRecalculate); + this, &QValue3DAxisFormatterPrivate::markDirty); connect(axis, &QAbstract3DAxis::rangeChanged, - this, &QValue3DAxisFormatterPrivate::setNeedsRecalculate); + this, &QValue3DAxisFormatterPrivate::markDirty); m_axis = axis; } -void QValue3DAxisFormatterPrivate::setNeedsRecalculate() +void QValue3DAxisFormatterPrivate::markDirty() { m_needsRecalculate = true; + if (m_axis && m_axis->orientation() != QAbstract3DAxis::AxisOrientationNone) + emit m_axis->dptr()->formatterDirty(); } QT_END_NAMESPACE_DATAVISUALIZATION diff --git a/src/datavisualization/axis/qvalue3daxisformatter.h b/src/datavisualization/axis/qvalue3daxisformatter.h index a67b6094..14ecdc15 100644 --- a/src/datavisualization/axis/qvalue3daxisformatter.h +++ b/src/datavisualization/axis/qvalue3daxisformatter.h @@ -38,33 +38,22 @@ public: explicit QValue3DAxisFormatter(QObject *parent = 0); virtual ~QValue3DAxisFormatter(); +protected: virtual bool allowNegatives() const; virtual bool allowZero() const; + virtual QValue3DAxisFormatter *createNewInstance() const; + virtual void recalculate(); + virtual QString stringForValue(float value, const QString &format) const; + virtual float positionAt(float value) const; + virtual float valueAt(float position) const; + virtual void populateCopy(QValue3DAxisFormatter ©) const; - // Getters not const as they can trigger recalculate - float gridPosition(int index); - float subGridPosition(int subGridIndex, int segmentIndex); - float labelPosition(int index); - QString stringForValue(float value, const QString &format); - float positionAt(float value); - float valueAt(float position); - - virtual QValue3DAxisFormatter *createNewInstance(); - void populateCopy(QValue3DAxisFormatter ©); - -protected: void markDirty(); QValue3DAxis *axis() const; - virtual void doRecalculate(); - virtual QString doStringForValue(float value, const QString &format); - virtual float doPositionAt(float value); - virtual float doValueAt(float position); - virtual void doPopulateCopy(QValue3DAxisFormatter ©); - - QVector<float> &gridPositions(); - QVector<QVector<float> > &subGridPositions(); - QVector<float> &labelPositions(); + QVector<float> &gridPositions() const; + QVector<QVector<float> > &subGridPositions() const; + QVector<float> &labelPositions() const; QScopedPointer<QValue3DAxisFormatterPrivate> d_ptr; @@ -72,8 +61,15 @@ private: Q_DISABLE_COPY(QValue3DAxisFormatter) friend class Abstract3DController; + friend class Abstract3DRenderer; + friend class Bars3DRenderer; + friend class Scatter3DRenderer; + friend class Surface3DRenderer; + friend class SurfaceObject; friend class QValue3DAxisFormatterPrivate; friend class QValue3DAxis; + friend class QValue3DAxisPrivate; + friend class AxisRenderCache; }; QT_END_NAMESPACE_DATAVISUALIZATION diff --git a/src/datavisualization/axis/qvalue3daxisformatter_p.h b/src/datavisualization/axis/qvalue3daxisformatter_p.h index 90b0f49e..d9f90934 100644 --- a/src/datavisualization/axis/qvalue3daxisformatter_p.h +++ b/src/datavisualization/axis/qvalue3daxisformatter_p.h @@ -48,6 +48,8 @@ public: void recalculate(); void doRecalculate(); + void populateCopy(QValue3DAxisFormatter ©); + void doPopulateCopy(QValue3DAxisFormatterPrivate ©); QString stringForValue(float value, const QString &format); float positionAt(float value) const; @@ -56,7 +58,7 @@ public: void setAxis(QValue3DAxis *axis); public slots: - void setNeedsRecalculate(); + void markDirty(); protected: QValue3DAxisFormatter *q_ptr; |