diff options
Diffstat (limited to 'src/datavisualization/axis')
-rw-r--r-- | src/datavisualization/axis/axis.pri | 12 | ||||
-rw-r--r-- | src/datavisualization/axis/q3dabstractaxis.cpp | 149 | ||||
-rw-r--r-- | src/datavisualization/axis/q3dabstractaxis.h | 88 | ||||
-rw-r--r-- | src/datavisualization/axis/q3dabstractaxis_p.h | 68 | ||||
-rw-r--r-- | src/datavisualization/axis/q3dcategoryaxis.cpp | 88 | ||||
-rw-r--r-- | src/datavisualization/axis/q3dcategoryaxis.h | 54 | ||||
-rw-r--r-- | src/datavisualization/axis/q3dcategoryaxis_p.h | 49 | ||||
-rw-r--r-- | src/datavisualization/axis/q3dvalueaxis.cpp | 332 | ||||
-rw-r--r-- | src/datavisualization/axis/q3dvalueaxis.h | 77 | ||||
-rw-r--r-- | src/datavisualization/axis/q3dvalueaxis_p.h | 69 |
10 files changed, 986 insertions, 0 deletions
diff --git a/src/datavisualization/axis/axis.pri b/src/datavisualization/axis/axis.pri new file mode 100644 index 00000000..4e96618b --- /dev/null +++ b/src/datavisualization/axis/axis.pri @@ -0,0 +1,12 @@ +HEADERS += \ + $$PWD/q3dabstractaxis.h \ + $$PWD/q3dabstractaxis_p.h \ + $$PWD/q3dvalueaxis.h \ + $$PWD/q3dvalueaxis_p.h \ + $$PWD/q3dcategoryaxis.h \ + $$PWD/q3dcategoryaxis_p.h + +SOURCES += \ + $$PWD/q3dabstractaxis.cpp \ + $$PWD/q3dvalueaxis.cpp \ + $$PWD/q3dcategoryaxis.cpp diff --git a/src/datavisualization/axis/q3dabstractaxis.cpp b/src/datavisualization/axis/q3dabstractaxis.cpp new file mode 100644 index 00000000..67f4f5fc --- /dev/null +++ b/src/datavisualization/axis/q3dabstractaxis.cpp @@ -0,0 +1,149 @@ +/**************************************************************************** +** +** Copyright (C) 2013 Digia Plc +** All rights reserved. +** For any questions to Digia, please use contact form at http://qt.digia.com +** +** This file is part of the QtDataVisualization module. +** +** Licensees holding valid Qt Enterprise licenses may use this file in +** accordance with the Qt Enterprise License Agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and Digia. +** +** If you have questions regarding the use of this file, please use +** contact form at http://qt.digia.com +** +****************************************************************************/ + +#include "q3dabstractaxis.h" +#include "q3dabstractaxis_p.h" + +QT_DATAVISUALIZATION_BEGIN_NAMESPACE + +/*! + * \class Q3DAbstractAxis + * \inmodule QtDataVisualization + * \brief Q3DAbstractAxis is base class for axes of a graph. + * \since 1.0.0 + * + * You should not need to use this class directly, but one of its subclasses instead. + * + * \sa Q3DCategoryAxis, Q3DValueAxis + */ + +/*! + * \enum Q3DAbstractAxis::AxisOrientation + * + * The orientation of the axis object. + * + * \value AxisOrientationNone + * \value AxisOrientationX + * \value AxisOrientationY + * \value AxisOrientationZ + */ + +/*! + * \enum Q3DAbstractAxis::AxisType + * + * The type of the axis object. + * + * \value AxisTypeNone + * \value AxisTypeCategory + * \value AxisTypeValue + */ + +/*! + * \internal + */ +Q3DAbstractAxis::Q3DAbstractAxis(Q3DAbstractAxisPrivate *d, QObject *parent) : + QObject(parent), + d_ptr(d) +{ +} + +/*! + * Destroys Q3DAbstractAxis. + */ +Q3DAbstractAxis::~Q3DAbstractAxis() +{ +} + +/*! + * \property Q3DAbstractAxis::title + * + * Defines the title for the axis. + */ +QString Q3DAbstractAxis::title() const +{ + return d_ptr->m_title; +} + +/*! + * \property Q3DAbstractAxis::labels + * + * Defines the labels for the axis. + */ +QStringList Q3DAbstractAxis::labels() const +{ + d_ptr->updateLabels(); + return d_ptr->m_labels; +} + +/*! + * \property Q3DAbstractAxis::orientation + * + * Defines the orientation of the axis, one of \c Q3DAbstractAxis::AxisOrientation. + */ +Q3DAbstractAxis::AxisOrientation Q3DAbstractAxis::orientation() const +{ + return d_ptr->m_orientation; +} + +/*! + * \property Q3DAbstractAxis::type + * + * Defines the type of the axis, one of \c Q3DAbstractAxis::AxisType. + */ +Q3DAbstractAxis::AxisType Q3DAbstractAxis::type() const +{ + return d_ptr->m_type; +} + +void Q3DAbstractAxis::setTitle(QString title) +{ + if (d_ptr->m_title != title) { + d_ptr->m_title = title; + emit titleChanged(title); + } +} + +// Q3DAbstractAxisPrivate + +Q3DAbstractAxisPrivate::Q3DAbstractAxisPrivate(Q3DAbstractAxis *q, Q3DAbstractAxis::AxisType type) + : QObject(0), + q_ptr(q), + m_orientation(Q3DAbstractAxis::AxisOrientationNone), + m_type(type), + m_isDefaultAxis(false) +{ +} + +Q3DAbstractAxisPrivate::~Q3DAbstractAxisPrivate() +{ +} + +void Q3DAbstractAxisPrivate::setOrientation(Q3DAbstractAxis::AxisOrientation orientation) +{ + if (m_orientation == Q3DAbstractAxis::AxisOrientationNone) + m_orientation = orientation; + else + Q_ASSERT("Attempted to reset axis orientation."); +} + +void Q3DAbstractAxisPrivate::updateLabels() +{ + // Default implementation does nothing +} + +QT_DATAVISUALIZATION_END_NAMESPACE diff --git a/src/datavisualization/axis/q3dabstractaxis.h b/src/datavisualization/axis/q3dabstractaxis.h new file mode 100644 index 00000000..6e89723a --- /dev/null +++ b/src/datavisualization/axis/q3dabstractaxis.h @@ -0,0 +1,88 @@ +/**************************************************************************** +** +** Copyright (C) 2013 Digia Plc +** All rights reserved. +** For any questions to Digia, please use contact form at http://qt.digia.com +** +** This file is part of the QtDataVisualization module. +** +** Licensees holding valid Qt Enterprise licenses may use this file in +** accordance with the Qt Enterprise License Agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and Digia. +** +** If you have questions regarding the use of this file, please use +** contact form at http://qt.digia.com +** +****************************************************************************/ + +#ifndef Q3DABSTRACTAXIS_H +#define Q3DABSTRACTAXIS_H + +#include <QtDataVisualization/qdatavisualizationenums.h> +#include <QObject> +#include <QScopedPointer> +#include <QVector> +#include <QStringList> + +QT_DATAVISUALIZATION_BEGIN_NAMESPACE + +class Q3DAbstractAxisPrivate; + +class QT_DATAVISUALIZATION_EXPORT Q3DAbstractAxis : public QObject +{ + Q_OBJECT + Q_ENUMS(AxisOrientation) + Q_ENUMS(AxisType) + Q_PROPERTY(QString title READ title WRITE setTitle NOTIFY titleChanged) + Q_PROPERTY(QStringList labels READ labels NOTIFY labelsChanged) + Q_PROPERTY(AxisOrientation orientation READ orientation) + Q_PROPERTY(AxisType type READ type) + +public: + enum AxisOrientation { + AxisOrientationNone = 0, + AxisOrientationX = 1, + AxisOrientationY = 2, + AxisOrientationZ = 4 + }; + + enum AxisType { + AxisTypeNone = 0, + AxisTypeCategory = 1, + AxisTypeValue = 2 + //AxisTypeLogValue = 6 // inherits valueaxis (4 + 2) // TODO + }; + +protected: + explicit Q3DAbstractAxis(Q3DAbstractAxisPrivate *d, QObject *parent = 0); + +public: + virtual ~Q3DAbstractAxis(); + + QString title() const; + QStringList labels() const; + + AxisOrientation orientation() const; + AxisType type() const; + +public slots: + void setTitle(QString title); + +signals: + void titleChanged(QString newTitle); + void labelsChanged(); + +protected: + QScopedPointer<Q3DAbstractAxisPrivate> d_ptr; + +private: + Q_DISABLE_COPY(Q3DAbstractAxis) + + friend class Abstract3DController; + friend class Bars3DController; +}; + +QT_DATAVISUALIZATION_END_NAMESPACE + +#endif // QABSTRACTAXIS_H diff --git a/src/datavisualization/axis/q3dabstractaxis_p.h b/src/datavisualization/axis/q3dabstractaxis_p.h new file mode 100644 index 00000000..f961e82e --- /dev/null +++ b/src/datavisualization/axis/q3dabstractaxis_p.h @@ -0,0 +1,68 @@ +/**************************************************************************** +** +** Copyright (C) 2013 Digia Plc +** All rights reserved. +** For any questions to Digia, please use contact form at http://qt.digia.com +** +** This file is part of the QtDataVisualization module. +** +** Licensees holding valid Qt Enterprise licenses may use this file in +** accordance with the Qt Enterprise License Agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and Digia. +** +** If you have questions regarding the use of this file, please use +** contact form at http://qt.digia.com +** +****************************************************************************/ + +// +// W A R N I N G +// ------------- +// +// This file is not part of the QtDataVisualization API. It exists purely as an +// implementation detail. This header file may change from version to +// version without notice, or even be removed. +// +// We mean it. + +#include "datavisualizationglobal_p.h" +#include "q3dabstractaxis.h" +#include "abstract3dcontroller_p.h" + +#ifndef Q3DABSTRACTAXIS_P_H +#define Q3DABSTRACTAXIS_P_H + +QT_DATAVISUALIZATION_BEGIN_NAMESPACE + +class Q3DAbstractAxisPrivate : public QObject +{ + Q_OBJECT +public: + Q3DAbstractAxisPrivate(Q3DAbstractAxis *q, Q3DAbstractAxis::AxisType type); + virtual ~Q3DAbstractAxisPrivate(); + + void setOrientation(Q3DAbstractAxis::AxisOrientation orientation); + + inline bool isDefaultAxis() { return m_isDefaultAxis; } + inline void setDefaultAxis(bool isDefault) { m_isDefaultAxis = isDefault; } + +protected: + virtual void updateLabels(); + + Q3DAbstractAxis *q_ptr; + + QString m_title; + QStringList m_labels; + Q3DAbstractAxis::AxisOrientation m_orientation; + Q3DAbstractAxis::AxisType m_type; + bool m_isDefaultAxis; + + friend class Q3DAbstractAxis; + friend class Q3DValueAxis; + friend class Q3DCategoryAxis; +}; + +QT_DATAVISUALIZATION_END_NAMESPACE + +#endif // QABSTRACTAXIS_P_H diff --git a/src/datavisualization/axis/q3dcategoryaxis.cpp b/src/datavisualization/axis/q3dcategoryaxis.cpp new file mode 100644 index 00000000..55a27f52 --- /dev/null +++ b/src/datavisualization/axis/q3dcategoryaxis.cpp @@ -0,0 +1,88 @@ +/**************************************************************************** +** +** Copyright (C) 2013 Digia Plc +** All rights reserved. +** For any questions to Digia, please use contact form at http://qt.digia.com +** +** This file is part of the QtDataVisualization module. +** +** Licensees holding valid Qt Enterprise licenses may use this file in +** accordance with the Qt Enterprise License Agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and Digia. +** +** If you have questions regarding the use of this file, please use +** contact form at http://qt.digia.com +** +****************************************************************************/ + +#include "q3dcategoryaxis.h" +#include "q3dcategoryaxis_p.h" + +QT_DATAVISUALIZATION_BEGIN_NAMESPACE + +/*! + * \class Q3DCategoryAxis + * \inmodule QtDataVisualization + * \brief The Q3DCategoryAxis class is used for manipulating an axis of a graph. + * \since 1.0.0 + * + * Q3DCategoryAxis provides an axis that can be given labels. The axis is divided into equal-sized + * categories based on \l {Q3DBars::setDataWindow()}{data window} size. + * + * Grid lines are drawn between categories, if visible. Labels are drawn to positions of categories + * if provided. + */ + +/*! + * Constructs Q3DCategoryAxis with \a parent. + */ +Q3DCategoryAxis::Q3DCategoryAxis(QObject *parent) : + Q3DAbstractAxis(new Q3DCategoryAxisPrivate(this), parent) +{ +} + +/*! + * Destroys Q3DCategoryAxis. + */ +Q3DCategoryAxis::~Q3DCategoryAxis() +{ +} + +/*! + * \property Q3DCategoryAxis::categoryLabels + * + * Defines labels for axis applied to categories. If there are fewer labels than categories, the + * remaining ones do not have a label. + */ +QStringList Q3DCategoryAxis::categoryLabels() const +{ + return labels(); +} + +void Q3DCategoryAxis::setCategoryLabels(const QStringList &labels) +{ + if (d_ptr->m_labels != labels) { + d_ptr->m_labels = labels; + emit labelsChanged(); + } +} + +/*! + * \internal + */ +Q3DCategoryAxisPrivate *Q3DCategoryAxis::dptr() +{ + return static_cast<Q3DCategoryAxisPrivate *>(d_ptr.data()); +} + +Q3DCategoryAxisPrivate::Q3DCategoryAxisPrivate(Q3DCategoryAxis *q) + : Q3DAbstractAxisPrivate(q, Q3DAbstractAxis::AxisTypeCategory) +{ +} + +Q3DCategoryAxisPrivate::~Q3DCategoryAxisPrivate() +{ +} + +QT_DATAVISUALIZATION_END_NAMESPACE diff --git a/src/datavisualization/axis/q3dcategoryaxis.h b/src/datavisualization/axis/q3dcategoryaxis.h new file mode 100644 index 00000000..8b7f966c --- /dev/null +++ b/src/datavisualization/axis/q3dcategoryaxis.h @@ -0,0 +1,54 @@ +/**************************************************************************** +** +** Copyright (C) 2013 Digia Plc +** All rights reserved. +** For any questions to Digia, please use contact form at http://qt.digia.com +** +** This file is part of the QtDataVisualization module. +** +** Licensees holding valid Qt Enterprise licenses may use this file in +** accordance with the Qt Enterprise License Agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and Digia. +** +** If you have questions regarding the use of this file, please use +** contact form at http://qt.digia.com +** +****************************************************************************/ + +#ifndef Q3DCATEGORYAXIS_H +#define Q3DCATEGORYAXIS_H + +#include <QtDataVisualization/q3dabstractaxis.h> + +QT_DATAVISUALIZATION_BEGIN_NAMESPACE + +class Q3DCategoryAxisPrivate; + +class QT_DATAVISUALIZATION_EXPORT Q3DCategoryAxis : public Q3DAbstractAxis +{ + Q_OBJECT + // Note: categoryLabels actually reads/writes the labels property in abstract axis, + // which is read only there. Since subclass cannot have property with same name, + // this partially duplicate property is necessary. + Q_PROPERTY(QStringList categoryLabels READ categoryLabels WRITE setCategoryLabels) + +public: + explicit Q3DCategoryAxis(QObject *parent = 0); + virtual ~Q3DCategoryAxis(); + + QStringList categoryLabels() const; + +public slots: + void setCategoryLabels(const QStringList &labels); + +protected: + Q3DCategoryAxisPrivate *dptr(); + +private: + Q_DISABLE_COPY(Q3DCategoryAxis) +}; + +QT_DATAVISUALIZATION_END_NAMESPACE + +#endif // QCATEGORYAXIS_H diff --git a/src/datavisualization/axis/q3dcategoryaxis_p.h b/src/datavisualization/axis/q3dcategoryaxis_p.h new file mode 100644 index 00000000..f7fa7b35 --- /dev/null +++ b/src/datavisualization/axis/q3dcategoryaxis_p.h @@ -0,0 +1,49 @@ +/**************************************************************************** +** +** Copyright (C) 2013 Digia Plc +** All rights reserved. +** For any questions to Digia, please use contact form at http://qt.digia.com +** +** This file is part of the QtDataVisualization module. +** +** Licensees holding valid Qt Enterprise licenses may use this file in +** accordance with the Qt Enterprise License Agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and Digia. +** +** If you have questions regarding the use of this file, please use +** contact form at http://qt.digia.com +** +****************************************************************************/ + +// +// W A R N I N G +// ------------- +// +// This file is not part of the QtDataVisualization API. It exists purely as an +// implementation detail. This header file may change from version to +// version without notice, or even be removed. +// +// We mean it. + +#include "q3dcategoryaxis.h" +#include "q3dabstractaxis_p.h" +#include "qbardataitem.h" + +#ifndef QCATEGORYAXIS_P_H +#define QCATEGORYAXIS_P_H + +QT_DATAVISUALIZATION_BEGIN_NAMESPACE + +class Q3DCategoryAxisPrivate : public Q3DAbstractAxisPrivate +{ + Q_OBJECT + +public: + Q3DCategoryAxisPrivate(Q3DCategoryAxis *q); + virtual ~Q3DCategoryAxisPrivate(); +}; + +QT_DATAVISUALIZATION_END_NAMESPACE + +#endif // QCATEGORYAXIS_P_H diff --git a/src/datavisualization/axis/q3dvalueaxis.cpp b/src/datavisualization/axis/q3dvalueaxis.cpp new file mode 100644 index 00000000..a17fef69 --- /dev/null +++ b/src/datavisualization/axis/q3dvalueaxis.cpp @@ -0,0 +1,332 @@ +/**************************************************************************** +** +** Copyright (C) 2013 Digia Plc +** All rights reserved. +** For any questions to Digia, please use contact form at http://qt.digia.com +** +** This file is part of the QtDataVisualization module. +** +** Licensees holding valid Qt Enterprise licenses may use this file in +** accordance with the Qt Enterprise License Agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and Digia. +** +** If you have questions regarding the use of this file, please use +** contact form at http://qt.digia.com +** +****************************************************************************/ + +#include "q3dvalueaxis.h" +#include "q3dvalueaxis_p.h" +#include "utils_p.h" + +QT_DATAVISUALIZATION_BEGIN_NAMESPACE + +/*! + * \class Q3DValueAxis + * \inmodule QtDataVisualization + * \brief The Q3DValueAxis class is used for manipulating an axis of a graph. + * \since 1.0.0 + * + * Q3DValueAxis provides an axis that can be given a range of values and segment and subsegment + * counts to divide the range into. + * + * Labels are drawn between each segment. Grid lines are drawn between each segment and each + * subsegment. \note If visible, there will always be at least two grid lines and labels indicating + * the minimum and the maximum values of the range, as there is always at least one segment. + */ + +/*! + * Constructs Q3DValueAxis with the given \a parent. + */ +Q3DValueAxis::Q3DValueAxis(QObject *parent) : + Q3DAbstractAxis(new Q3DValueAxisPrivate(this), parent) +{ +} + +/*! + * Destroys Q3DValueAxis. + */ +Q3DValueAxis::~Q3DValueAxis() +{ +} + +/*! + * Sets value range of the axis from \a min to \a max. + * When setting the range, the max is adjusted if necessary, to ensure that the range remains valid. + */ +void Q3DValueAxis::setRange(qreal min, qreal max) +{ + dptr()->setRange(min, max); + setAutoAdjustRange(false); +} + +/*! + * \property Q3DValueAxis::min + * + * Defines the minimum value on the axis. + * When setting this property the max is adjusted if necessary, to ensure that the range remains + * valid. + */ +void Q3DValueAxis::setMin(qreal min) +{ + dptr()->setMin(min); + setAutoAdjustRange(false); +} + +/*! + * \property Q3DValueAxis::max + * + * Defines the maximum value on the axis. + * When setting this property the min is adjusted if necessary, to ensure that the range remains + * valid. + */ +void Q3DValueAxis::setMax(qreal max) +{ + dptr()->setMax(max); + setAutoAdjustRange(false); +} + +qreal Q3DValueAxis::min() const +{ + return dptrc()->m_min; +} + +qreal Q3DValueAxis::max() const +{ + return dptrc()->m_max; +} + +/*! + * \property Q3DValueAxis::segmentCount + * + * Defines the number of segments on the axis. This indicates how many labels are drawn. The number + * of grid lines to be drawn is calculated with formula: \c {segments * subsegments + 1}. + * The preset default is \c 5, and it can not be below \c 1. + * + * \sa setSubSegmentCount() + */ +void Q3DValueAxis::setSegmentCount(int count) +{ + if (count <= 0) { + qWarning() << "Warning: Illegal segment count automatically adjusted to a legal one:" + << count << "-> 1"; + count = 1; + } + if (dptr()->m_segmentCount != count){ + dptr()->m_segmentCount = count; + dptr()->emitLabelsChanged(); + emit segmentCountChanged(count); + } +} + +int Q3DValueAxis::segmentCount() const +{ + return dptrc()->m_segmentCount; +} + +/*! + * \property Q3DValueAxis::subSegmentCount + * + * Defines the number of subsegments inside each segment on the axis. Grid lines are drawn between + * each subsegment, in addition to each segment. + * The preset default is \c 1, and it can not be below \c 1. + * + * \sa setSegmentCount() + **/ +void Q3DValueAxis::setSubSegmentCount(int count) +{ + if (count <= 0) { + qWarning() << "Warning: Illegal subsegment count automatically adjusted to a legal one:" + << count << "-> 1"; + count = 1; + } + if (dptr()->m_subSegmentCount != count) { + dptr()->m_subSegmentCount = count; + emit subSegmentCountChanged(count); + } +} + +int Q3DValueAxis::subSegmentCount() const +{ + return dptrc()->m_subSegmentCount; +} + +/*! + * \property Q3DValueAxis::autoAdjustRange + * + * Tells the axis to automatically calculate the range instead of setting range or adjusting min or + * max property. + * + * \sa setRange(), setMin(), setMax() + */ +void Q3DValueAxis::setAutoAdjustRange(bool autoAdjust) +{ + if (dptr()->m_autoAdjust != autoAdjust) { + dptr()->m_autoAdjust = autoAdjust; + emit autoAdjustRangeChanged(autoAdjust); + } +} + +bool Q3DValueAxis::isAutoAdjustRange() const +{ + return dptrc()->m_autoAdjust; +} + +/*! + * \property Q3DValueAxis::labelFormat + * + * Defines the label format to be used for the labels on this axis. Supported specifiers are: + * \c {d, i, o, x, X, f, F, e, E, g, G, c}. See QString::sprintf() for additional details. + * + * Usage example: + * + * \c {axis->setLabelFormat("%.2f mm");} + */ +void Q3DValueAxis::setLabelFormat(const QString &format) +{ + if (dptr()->m_labelFormat != format) { + dptr()->m_labelFormat = format; + dptr()->emitLabelsChanged(); + emit labelFormatChanged(format); + } +} + +QString Q3DValueAxis::labelFormat() const +{ + return dptrc()->m_labelFormat; +} + +/*! + * \internal + */ +Q3DValueAxisPrivate *Q3DValueAxis::dptr() +{ + return static_cast<Q3DValueAxisPrivate *>(d_ptr.data()); +} + +/*! + * \internal + */ +const Q3DValueAxisPrivate *Q3DValueAxis::dptrc() const +{ + return static_cast<const Q3DValueAxisPrivate *>(d_ptr.data()); +} + +Q3DValueAxisPrivate::Q3DValueAxisPrivate(Q3DValueAxis *q) + : Q3DAbstractAxisPrivate(q, Q3DAbstractAxis::AxisTypeValue), + m_min(0.0), + m_max(10.0), + m_segmentCount(5), + m_subSegmentCount(1), + m_autoAdjust(true), + m_labelFormat(Utils::defaultLabelFormat()), + m_labelsDirty(true) +{ +} + +Q3DValueAxisPrivate::~Q3DValueAxisPrivate() +{ +} + +void Q3DValueAxisPrivate::setRange(qreal min, qreal max) +{ + // If min >= max, we adjust ranges so that + // m_max becomes (min + 1.0) + // as axes need some kind of valid range. + // TODO: Make "reverse" axes work (i.e. min > max) + bool dirty = false; + if (m_min != min) { + m_min = min; + dirty = true; + } + if (m_max != max) { + if (min >= max) { + m_max = min + 1.0; + qWarning() << "Warning: Tried to set invalid range for value axis." + " Range automatically adjusted to a valid one:" + << min << "-" << max << "-->" << m_min << "-" << m_max; + } else { + m_max = max; + } + dirty = true; + } + if (dirty) { + emitLabelsChanged(); + emit qptr()->rangeChanged(min, max); + } +} + +void Q3DValueAxisPrivate::setMin(qreal min) +{ + if (m_min != min) { + if (min >= m_max) { + qreal oldMax = m_max; + m_max = min + 1.0; + qWarning() << "Warning: Tried to set minimum to equal or larger than maximum for" + " value axis. Maximum automatically adjusted to a valid one:" + << oldMax << "-->" << m_max; + } + m_min = min; + emitLabelsChanged(); + emit qptr()->rangeChanged(m_min, m_max); + } +} + +void Q3DValueAxisPrivate::setMax(qreal max) +{ + if (m_max != max) { + if (max <= m_min) { + qreal oldMin = m_min; + m_min = max - 1.0; + qWarning() << "Warning: Tried to set maximum to equal or smaller than minimum for" + " value axis. Minimum automatically adjusted to a valid one:" + << oldMin << "-->" << m_min; + } + m_max = max; + emitLabelsChanged(); + emit qptr()->rangeChanged(m_min, m_max); + } +} + +void Q3DValueAxisPrivate::emitLabelsChanged() +{ + m_labelsDirty = true; + emit q_ptr->labelsChanged(); +} + +void Q3DValueAxisPrivate::updateLabels() +{ + if (!m_labelsDirty) + return; + + m_labelsDirty = false; + + QStringList newLabels; + newLabels.reserve(m_segmentCount + 1); + + // First label is at axis min, which is an extra segment + qreal segmentStep = (m_max - m_min) / m_segmentCount; + + QString formatString(m_labelFormat); + Utils::ParamType paramType = Utils::findFormatParamType(formatString); + QByteArray formatArray = formatString.toUtf8(); + + for (int i = 0; i < m_segmentCount; i++) { + qreal value = m_min + (segmentStep * i); + newLabels.append(Utils::formatLabel(formatArray, paramType, value)); + } + + // Ensure max label doesn't suffer from any rounding errors + newLabels.append(Utils::formatLabel(formatArray, paramType, m_max)); + + if (m_labels != newLabels) + m_labels = newLabels; +} + +Q3DValueAxis *Q3DValueAxisPrivate::qptr() +{ + return static_cast<Q3DValueAxis *>(q_ptr); +} + +QT_DATAVISUALIZATION_END_NAMESPACE diff --git a/src/datavisualization/axis/q3dvalueaxis.h b/src/datavisualization/axis/q3dvalueaxis.h new file mode 100644 index 00000000..eb00f27f --- /dev/null +++ b/src/datavisualization/axis/q3dvalueaxis.h @@ -0,0 +1,77 @@ +/**************************************************************************** +** +** Copyright (C) 2013 Digia Plc +** All rights reserved. +** For any questions to Digia, please use contact form at http://qt.digia.com +** +** This file is part of the QtDataVisualization module. +** +** Licensees holding valid Qt Enterprise licenses may use this file in +** accordance with the Qt Enterprise License Agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and Digia. +** +** If you have questions regarding the use of this file, please use +** contact form at http://qt.digia.com +** +****************************************************************************/ + +#ifndef QVALUEAXIS_H +#define QVALUEAXIS_H + +#include <QtDataVisualization/q3dabstractaxis.h> + +QT_DATAVISUALIZATION_BEGIN_NAMESPACE + +class Q3DValueAxisPrivate; + +class QT_DATAVISUALIZATION_EXPORT Q3DValueAxis : public Q3DAbstractAxis +{ + Q_OBJECT + Q_PROPERTY(qreal min READ min WRITE setMin NOTIFY rangeChanged) + Q_PROPERTY(qreal max READ max WRITE setMax NOTIFY rangeChanged) + Q_PROPERTY(int segmentCount READ segmentCount WRITE setSegmentCount NOTIFY segmentCountChanged) + Q_PROPERTY(int subSegmentCount READ subSegmentCount WRITE setSubSegmentCount NOTIFY subSegmentCountChanged) + Q_PROPERTY(bool autoAdjustRange READ isAutoAdjustRange WRITE setAutoAdjustRange NOTIFY autoAdjustRangeChanged) + Q_PROPERTY(QString labelFormat READ labelFormat WRITE setLabelFormat NOTIFY labelFormatChanged) + +public: + explicit Q3DValueAxis(QObject *parent = 0); + virtual ~Q3DValueAxis(); + + qreal min() const; + qreal max() const; + int segmentCount() const; + int subSegmentCount() const; + bool isAutoAdjustRange() const; + QString labelFormat() const; + +public slots: + void setRange(qreal min, qreal max); + void setMin(qreal min); + void setMax(qreal max); + void setSegmentCount(int count); + void setSubSegmentCount(int count); + void setAutoAdjustRange(bool autoAdjust); + void setLabelFormat(const QString &format); + +signals: + void rangeChanged(qreal min, qreal max); + void segmentCountChanged(int count); + void subSegmentCountChanged(int count); + void autoAdjustRangeChanged(bool autoAdjust); + void labelFormatChanged(QString format); + +protected: + Q3DValueAxisPrivate *dptr(); + const Q3DValueAxisPrivate *dptrc() const; + +private: + Q_DISABLE_COPY(Q3DValueAxis) + friend class Bars3DController; + friend class Scatter3DController; +}; + +QT_DATAVISUALIZATION_END_NAMESPACE + +#endif // QVALUEAXIS_H diff --git a/src/datavisualization/axis/q3dvalueaxis_p.h b/src/datavisualization/axis/q3dvalueaxis_p.h new file mode 100644 index 00000000..8fcf50da --- /dev/null +++ b/src/datavisualization/axis/q3dvalueaxis_p.h @@ -0,0 +1,69 @@ +/**************************************************************************** +** +** Copyright (C) 2013 Digia Plc +** All rights reserved. +** For any questions to Digia, please use contact form at http://qt.digia.com +** +** This file is part of the QtDataVisualization module. +** +** Licensees holding valid Qt Enterprise licenses may use this file in +** accordance with the Qt Enterprise License Agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and Digia. +** +** If you have questions regarding the use of this file, please use +** contact form at http://qt.digia.com +** +****************************************************************************/ + +// +// W A R N I N G +// ------------- +// +// This file is not part of the QtDataVisualization API. It exists purely as an +// implementation detail. This header file may change from version to +// version without notice, or even be removed. +// +// We mean it. + +#include "q3dvalueaxis.h" +#include "q3dabstractaxis_p.h" + +#ifndef QVALUEAXIS_P_H +#define QVALUEAXIS_P_H + +QT_DATAVISUALIZATION_BEGIN_NAMESPACE + +class Q3DValueAxisPrivate : public Q3DAbstractAxisPrivate +{ + Q_OBJECT + +public: + Q3DValueAxisPrivate(Q3DValueAxis *q); + virtual ~Q3DValueAxisPrivate(); + + void setRange(qreal min, qreal max); + void setMin(qreal min); + void setMax (qreal max); + +protected: + void emitLabelsChanged(); + virtual void updateLabels(); + + qreal m_min; + qreal m_max; + int m_segmentCount; + int m_subSegmentCount; + bool m_autoAdjust; + QString m_labelFormat; + bool m_labelsDirty; + +private: + Q3DValueAxis *qptr(); + + friend class Q3DValueAxis; +}; + +QT_DATAVISUALIZATION_END_NAMESPACE + +#endif // QVALUEAXIS_P_H |