summaryrefslogtreecommitdiffstats
path: root/src/datavis3d/axis/qvalueaxis.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/datavis3d/axis/qvalueaxis.cpp')
-rw-r--r--src/datavis3d/axis/qvalueaxis.cpp87
1 files changed, 83 insertions, 4 deletions
diff --git a/src/datavis3d/axis/qvalueaxis.cpp b/src/datavis3d/axis/qvalueaxis.cpp
index f6ec52c4..54851965 100644
--- a/src/datavis3d/axis/qvalueaxis.cpp
+++ b/src/datavis3d/axis/qvalueaxis.cpp
@@ -85,6 +85,7 @@ void QValueAxis::setTickCount(int count)
{
if (dptr()->m_tickCount != count){
dptr()->m_tickCount = count;
+ dptr()->recreateLabels();
emit tickCountChanged(count);
}
}
@@ -94,6 +95,19 @@ int QValueAxis::tickCount() const
return dptrc()->m_tickCount;
}
+void QValueAxis::setSubTickCount(int count)
+{
+ if (dptr()->m_subTickCount != count) {
+ dptr()->m_subTickCount = count;
+ emit subTickCountChanged(count);
+ }
+}
+
+int QValueAxis::subTickCount() const
+{
+ return dptrc()->m_subTickCount;
+}
+
void QValueAxis::setAutoAdjustRange(bool autoAdjust)
{
if (dptr()->m_autoAdjust != autoAdjust) {
@@ -107,6 +121,20 @@ bool QValueAxis::isAutoAdjustRange() const
return dptrc()->m_autoAdjust;
}
+void QValueAxis::setLabelFormat(const QString &format)
+{
+ if (dptr()->m_labelFormat != format) {
+ dptr()->m_labelFormat = format;
+ dptr()->recreateLabels();
+ emit labelFormatChanged(format);
+ }
+}
+
+QString QValueAxis::labelFormat() const
+{
+ return dptrc()->m_labelFormat;
+}
+
QValueAxisPrivate *QValueAxis::dptr()
{
return static_cast<QValueAxisPrivate *>(d_ptr.data());
@@ -122,6 +150,7 @@ QValueAxisPrivate::QValueAxisPrivate(QValueAxis *q)
m_min(0.0),
m_max(10.0),
m_tickCount(10),
+ m_subTickCount(0),
m_autoAdjust(true)
{
}
@@ -132,23 +161,44 @@ QValueAxisPrivate::~QValueAxisPrivate()
void QValueAxisPrivate::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) {
- 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)
+ if (dirty) {
+ recreateLabels();
emit qptr()->rangeChanged(min, max);
+ }
}
void QValueAxisPrivate::setMin(qreal min)
{
if (m_min != min) {
- m_min = min;
+ if (min >= m_max) {
+ m_min = m_max - 1.0;
+ qWarning() << "Warning: Tried to set minimum to equal or larger than maximum for"
+ " value axis. Minimum automatically adjusted to a valid one:"
+ << min << "-->" << m_min;
+ } else {
+ m_min = min;
+ }
+ recreateLabels();
emit qptr()->rangeChanged(m_min, m_max);
}
}
@@ -156,11 +206,40 @@ void QValueAxisPrivate::setMin(qreal min)
void QValueAxisPrivate::setMax(qreal max)
{
if (m_max != max) {
- m_max = max;
+ if (max <= m_min) {
+ m_max = m_min + 1.0;
+ qWarning() << "Warning: Tried to set maximum to equal or smaller than minimum for"
+ " value axis. Maximum automatically adjusted to a valid one:"
+ << max << "-->" << m_max;
+ } else {
+ m_max = max;
+ }
+ recreateLabels();
emit qptr()->rangeChanged(m_min, m_max);
}
}
+void QValueAxisPrivate::recreateLabels()
+{
+ QStringList newLabels;
+ newLabels.reserve(m_tickCount + 1);
+
+ // First label is at axis min, which is an extra tick
+ qreal tickStep = (m_max - m_min) / m_tickCount;
+
+ for (int i = 0; i < m_tickCount; i++) {
+ // TODO Actually do proper formatting
+ newLabels.append(QString::number(m_min + (tickStep * i)));
+ }
+ // Ensure max label doesn't suffer from any rounding errors
+ newLabels.append(QString::number(m_max));
+
+ if (m_labels != newLabels) {
+ m_labels = newLabels;
+ emit q_ptr->labelsChanged();
+ }
+}
+
QValueAxis *QValueAxisPrivate::qptr()
{
return static_cast<QValueAxis *>(q_ptr);