summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorLukas Kosinski <lukasz@scythe-studio.com>2021-05-24 22:50:53 +0200
committerLukas Kosinski <lukasz@scythe-studio.com>2021-06-02 13:09:06 +0200
commitad94166fefba448805d145d6c61a6b30209aa146 (patch)
tree0085338059ce59d27275d6804f66cee38df1f1f3 /src
parent92b136373dcca34ae2a32872885a84b0833d0143 (diff)
Add optional labels truncation to QAbstractAxis
This is a feature that allows users to switch off labels truncation that is by default performed if there is too little space for the label. By default truncateLabels is true, so it doesn't change previous behavior. If user doesn't want to have truncated labels, he can switch them off explicitly. There is also a labelsTruncated method added that returns true if there is at least one truncated label and false otherwise. The reason for this is to be able to programmatically check if labels are truncated. Task-number: QTBUG-89444 Change-Id: I8ab18a8d0335ddf31baf443a2046fee89ce58b38 Reviewed-by: Leena Miettinen <riitta-leena.miettinen@qt.io> Reviewed-by: Miikka Heikkinen <miikka.heikkinen@qt.io>
Diffstat (limited to 'src')
-rw-r--r--src/charts/axis/chartaxiselement.cpp8
-rw-r--r--src/charts/axis/chartaxiselement_p.h1
-rw-r--r--src/charts/axis/horizontalaxis.cpp31
-rw-r--r--src/charts/axis/qabstractaxis.cpp70
-rw-r--r--src/charts/axis/qabstractaxis.h11
-rw-r--r--src/charts/axis/qabstractaxis_p.h6
-rw-r--r--src/charts/axis/verticalaxis.cpp28
7 files changed, 140 insertions, 15 deletions
diff --git a/src/charts/axis/chartaxiselement.cpp b/src/charts/axis/chartaxiselement.cpp
index 06d919f4..93cbebda 100644
--- a/src/charts/axis/chartaxiselement.cpp
+++ b/src/charts/axis/chartaxiselement.cpp
@@ -122,6 +122,8 @@ void ChartAxisElement::connectSlots()
this, SLOT(handleGridLineColorChanged(const QColor&)));
QObject::connect(axis(), SIGNAL(minorGridLineColorChanged(const QColor&)),
this, SLOT(handleMinorGridLineColorChanged(const QColor&)));
+ QObject::connect(axis(), &QAbstractAxis::truncateLabelsChanged,
+ this, &ChartAxisElement::handleTruncateLabelsChanged);
if (axis()->type() == QAbstractAxis::AxisTypeCategory) {
QCategoryAxis *categoryAxis = static_cast<QCategoryAxis *>(axis());
@@ -157,6 +159,12 @@ void ChartAxisElement::handleLabelsPositionChanged()
presenter()->layout()->invalidate();
}
+void ChartAxisElement::handleTruncateLabelsChanged()
+{
+ QGraphicsLayoutItem::updateGeometry();
+ presenter()->layout()->invalidate();
+}
+
void ChartAxisElement::valueLabelEdited(qreal oldValue, qreal newValue)
{
qreal range = max() - min();
diff --git a/src/charts/axis/chartaxiselement_p.h b/src/charts/axis/chartaxiselement_p.h
index 712cfc51..ca19e6a5 100644
--- a/src/charts/axis/chartaxiselement_p.h
+++ b/src/charts/axis/chartaxiselement_p.h
@@ -167,6 +167,7 @@ public Q_SLOTS:
void handleMinorArrowVisibleChanged(bool visible);
void handleMinorGridVisibleChanged(bool visible);
void handleLabelsPositionChanged();
+ void handleTruncateLabelsChanged();
void valueLabelEdited(qreal oldValue, qreal newValue);
void dateTimeLabelEdited(const QDateTime &oldTime, const QDateTime &newTime);
diff --git a/src/charts/axis/horizontalaxis.cpp b/src/charts/axis/horizontalaxis.cpp
index bda222b5..8d7b0ec6 100644
--- a/src/charts/axis/horizontalaxis.cpp
+++ b/src/charts/axis/horizontalaxis.cpp
@@ -32,6 +32,7 @@
#include <QtCore/qmath.h>
#include <private/chartpresenter_p.h>
#include <private/horizontalaxis_p.h>
+#include <private/qabstractaxis_p.h>
QT_BEGIN_NAMESPACE
@@ -134,6 +135,8 @@ void HorizontalAxis::updateGeometry()
qreal last_label_max_x = 0;
+ bool labelsTruncated = false;
+
for (int i = 0; i < layout.size(); ++i) {
//items
QGraphicsLineItem *gridItem = static_cast<QGraphicsLineItem*>(lines.at(i));
@@ -160,15 +163,25 @@ void HorizontalAxis::updateGeometry()
if (text.isEmpty()) {
labelItem->setHtml(text);
} else {
- qreal labelWidth = axisRect.width() / layout.count() - (2 * labelPadding());
- // Replace digits with ellipsis "..." if number does not fit
- QString truncatedText = ChartPresenter::truncatedText(axis()->labelsFont(), text,
- axis()->labelsAngle(),
- labelWidth,
- availableSpace, boundingRect);
+ QString displayText = text;
+ if (axis()->truncateLabels()) {
+ qreal labelWidth = axisRect.width() / layout.count() - (2 * labelPadding());
+ // Replace digits with ellipsis "..." if number does not fit
+ displayText = ChartPresenter::truncatedText(axis()->labelsFont(), text,
+ axis()->labelsAngle(),
+ labelWidth,
+ availableSpace, boundingRect);
+ } else {
+ boundingRect = ChartPresenter::textBoundingRect(axis()->labelsFont(),
+ displayText, axis()->labelsAngle());
+ }
+
labelItem->setTextWidth(ChartPresenter::textBoundingRect(axis()->labelsFont(),
- truncatedText).width());
- labelItem->setHtml(truncatedText);
+ displayText).width());
+
+ labelItem->setHtml(displayText);
+
+ labelsTruncated |= displayText != text;
}
//label transformation origin point
@@ -320,6 +333,8 @@ void HorizontalAxis::updateGeometry()
tickItem->setVisible(gridLineVisible);
}
+ axis()->d_ptr->setLabelsTruncated(labelsTruncated);
+
updateMinorTickGeometry();
// begin/end grid line in case labels between
diff --git a/src/charts/axis/qabstractaxis.cpp b/src/charts/axis/qabstractaxis.cpp
index 98a60e2f..3fea583b 100644
--- a/src/charts/axis/qabstractaxis.cpp
+++ b/src/charts/axis/qabstractaxis.cpp
@@ -214,6 +214,35 @@ QT_BEGIN_NAMESPACE
*/
/*!
+ \property QAbstractAxis::labelsTruncated
+ \brief Returns \c true if at least one label on the axis is truncated.
+
+ Returned value will not be accurate before the axis is shown.
+*/
+/*!
+ \qmlproperty int AbstractAxis::labelsTruncated
+ Returns \c true if at least one label on the axis is truncated.
+
+ Returned value will not be accurate before the axis is shown.
+ \readonly
+*/
+
+/*!
+ \property QAbstractAxis::truncateLabels
+ \brief The truncation state of labels.
+
+ Indicates whether labels should be truncated if there is no enough space for full text.
+ It is equal to \c true by default.
+*/
+/*!
+ \qmlproperty int AbstractAxis::truncateLabels
+ The truncation state of labels.
+
+ Indicates whether labels should be truncated if there is no enough space for full text.
+ It is equal to \c true by default.
+*/
+
+/*!
\property QAbstractAxis::shadesVisible
\brief The visibility of the axis shades.
*/
@@ -374,6 +403,20 @@ QT_BEGIN_NAMESPACE
*/
/*!
+ \fn void QAbstractAxis::labelsTruncatedChanged(bool labelsTruncated)
+ \since 6.2
+ This signal is emitted in two cases; when the axis changes from having one or more truncated
+ labels to having no truncated labels, and when the axis changes from having no truncated
+ labels to having one or more truncated labels. Current state is identified by \a labelsTruncated.
+*/
+
+/*!
+ \fn void QAbstractAxis::truncateLabelsChanged(bool truncateLabels)
+ \since 6.2
+ This signal is emitted when the truncation of the labels changes to \a truncateLabels.
+*/
+
+/*!
\fn void QAbstractAxis::gridVisibleChanged(bool visible)
This signal is emitted when the visibility of the grid lines of the axis changes to \a visible.
*/
@@ -987,6 +1030,24 @@ bool QAbstractAxis::labelsEditable() const
return d_ptr->m_labelsEditable;
}
+bool QAbstractAxis::labelsTruncated() const
+{
+ return d_ptr->m_labelsTruncated;
+}
+
+void QAbstractAxis::setTruncateLabels(bool truncateLabels)
+{
+ if (d_ptr->m_truncateLabels != truncateLabels) {
+ d_ptr->m_truncateLabels = truncateLabels;
+ emit truncateLabelsChanged(d_ptr->m_truncateLabels);
+ }
+}
+
+bool QAbstractAxis::truncateLabels() const
+{
+ return d_ptr->m_truncateLabels;
+}
+
void QAbstractAxis::setReverse(bool reverse)
{
if (d_ptr->m_reverse != reverse && type() != QAbstractAxis::AxisTypeBarCategory) {
@@ -1034,6 +1095,15 @@ void QAbstractAxisPrivate::setAlignment( Qt::Alignment alignment)
m_alignment=alignment;
}
+void QAbstractAxisPrivate::setLabelsTruncated(bool labelsTruncated)
+{
+ Q_Q(QAbstractAxis);
+ if (m_labelsTruncated != labelsTruncated) {
+ m_labelsTruncated = labelsTruncated;
+ emit q->labelsTruncatedChanged(m_labelsTruncated);
+ }
+}
+
void QAbstractAxisPrivate::initializeTheme(ChartTheme* theme, bool forced)
{
if (forced || QChartPrivate::defaultPen() == m_axisPen)
diff --git a/src/charts/axis/qabstractaxis.h b/src/charts/axis/qabstractaxis.h
index 39a51865..3f265e03 100644
--- a/src/charts/axis/qabstractaxis.h
+++ b/src/charts/axis/qabstractaxis.h
@@ -54,6 +54,8 @@ class Q_CHARTS_EXPORT QAbstractAxis : public QObject
Q_PROPERTY(int labelsAngle READ labelsAngle WRITE setLabelsAngle NOTIFY labelsAngleChanged)
Q_PROPERTY(QFont labelsFont READ labelsFont WRITE setLabelsFont NOTIFY labelsFontChanged)
Q_PROPERTY(QColor labelsColor READ labelsColor WRITE setLabelsColor NOTIFY labelsColorChanged)
+ Q_PROPERTY(bool labelsTruncated READ labelsTruncated NOTIFY labelsTruncatedChanged REVISION(6, 2))
+ Q_PROPERTY(bool truncateLabels READ truncateLabels WRITE setTruncateLabels NOTIFY truncateLabelsChanged REVISION(6, 2))
//grid
Q_PROPERTY(bool gridVisible READ isGridLineVisible WRITE setGridLineVisible NOTIFY gridVisibleChanged)
Q_PROPERTY(QPen gridLinePen READ gridLinePen WRITE setGridLinePen NOTIFY gridLinePenChanged)
@@ -177,6 +179,11 @@ public:
void setLabelsEditable(bool editable = true);
bool labelsEditable() const;
+ bool labelsTruncated() const;
+
+ void setTruncateLabels(bool truncateLabels = true);
+ bool truncateLabels() const;
+
Q_SIGNALS:
void visibleChanged(bool visible);
void linePenChanged(const QPen &pen);
@@ -204,6 +211,8 @@ Q_SIGNALS:
void shadesBrushChanged(const QBrush &brush);
void reverseChanged(bool reverse);
void labelsEditableChanged(bool editable);
+ Q_REVISION(6, 2) void labelsTruncatedChanged(bool labelsTruncated);
+ Q_REVISION(6, 2) void truncateLabelsChanged(bool truncateLabels);
protected:
QScopedPointer<QAbstractAxisPrivate> d_ptr;
@@ -212,6 +221,8 @@ protected:
friend class ChartThemeManager;
friend class AbstractDomain;
friend class ChartAxisElement;
+ friend class HorizontalAxis;
+ friend class VerticalAxis;
friend class XYChart;
private:
diff --git a/src/charts/axis/qabstractaxis_p.h b/src/charts/axis/qabstractaxis_p.h
index 26814d0c..6b7da004 100644
--- a/src/charts/axis/qabstractaxis_p.h
+++ b/src/charts/axis/qabstractaxis_p.h
@@ -69,6 +69,7 @@ public:
Qt::Alignment alignment() const { return m_alignment; }
Qt::Orientation orientation() const { return m_orientation; }
void setAlignment( Qt::Alignment alignment);
+ void setLabelsTruncated(bool labelsTruncated);
virtual void initializeDomain(AbstractDomain *domain) = 0;
virtual void initializeGraphics(QGraphicsItem *parent) = 0;
@@ -122,6 +123,9 @@ private:
QFont m_labelsFont;
int m_labelsAngle = 0;
+ bool m_labelsTruncated = false;
+ bool m_truncateLabels = true;
+
bool m_titleVisible = true;
QBrush m_titleBrush;
QFont m_titleFont;
@@ -136,7 +140,7 @@ private:
bool m_reverse = false;
- friend class QAbstractAxis;
+ Q_DECLARE_PUBLIC(QAbstractAxis);
friend class ChartDataSet;
friend class ChartPresenter;
};
diff --git a/src/charts/axis/verticalaxis.cpp b/src/charts/axis/verticalaxis.cpp
index bc42d0ce..503213c8 100644
--- a/src/charts/axis/verticalaxis.cpp
+++ b/src/charts/axis/verticalaxis.cpp
@@ -32,6 +32,7 @@
#include <QtCore/qmath.h>
#include <private/chartpresenter_p.h>
#include <private/verticalaxis_p.h>
+#include <private/qabstractaxis_p.h>
QT_BEGIN_NAMESPACE
@@ -139,6 +140,8 @@ void VerticalAxis::updateGeometry()
QList<QGraphicsItem *> lines = gridItems();
QList<QGraphicsItem *> shades = shadeItems();
+ bool labelsTruncated = false;
+
for (int i = 0; i < layout.size(); ++i) {
//items
QGraphicsLineItem *gridItem = static_cast<QGraphicsLineItem *>(lines.at(i));
@@ -165,13 +168,24 @@ void VerticalAxis::updateGeometry()
if (text.isEmpty()) {
labelItem->setHtml(text);
} else {
- qreal labelHeight = (axisRect.height() / layout.count()) - (2 * labelPadding());
- QString truncatedText =
- ChartPresenter::truncatedText(axis()->labelsFont(), text, axis()->labelsAngle(),
- labelAvailableSpace, labelHeight, boundingRect);
+ QString displayText = text;
+ if (axis()->truncateLabels()) {
+ qreal labelHeight = (axisRect.height() / layout.count()) - (2 * labelPadding());
+ // Replace digits with ellipsis "..." if number does not fit
+ displayText =
+ ChartPresenter::truncatedText(axis()->labelsFont(), text, axis()->labelsAngle(),
+ labelAvailableSpace, labelHeight, boundingRect);
+ } else {
+ boundingRect = ChartPresenter::textBoundingRect(axis()->labelsFont(),
+ displayText, axis()->labelsAngle());
+ }
+
labelItem->setTextWidth(ChartPresenter::textBoundingRect(axis()->labelsFont(),
- truncatedText).width());
- labelItem->setHtml(truncatedText);
+ displayText).width());
+
+ labelItem->setHtml(displayText);
+
+ labelsTruncated |= displayText != text;
}
//label transformation origin point
@@ -335,6 +349,8 @@ void VerticalAxis::updateGeometry()
tickItem->setVisible(gridLineVisible);
}
+ axis()->d_ptr->setLabelsTruncated(labelsTruncated);
+
updateMinorTickGeometry();
// begin/end grid line in case labels between