1// Copyright (C) 2021 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only
3
4#include "chartcoloraxisx_p.h"
5#include <QtCharts/QColorAxis>
6#include <private/chartpresenter_p.h>
7#include <private/abstractchartlayout_p.h>
8#include <QtWidgets/QGraphicsLayout>
9
10QT_BEGIN_NAMESPACE
11
12ChartColorAxisX::ChartColorAxisX(QColorAxis *axis, QGraphicsItem *item)
13 : HorizontalAxis(axis, item, true)
14 , m_axis(axis)
15{
16
17}
18
19ChartColorAxisX::~ChartColorAxisX()
20{
21}
22
23QSizeF ChartColorAxisX::sizeHint(Qt::SizeHint which, const QSizeF &constraint) const
24{
25 Q_UNUSED(constraint);
26
27 QSizeF sh;
28
29 QSizeF base = HorizontalAxis::sizeHint(which, constraint);
30 const QStringList &ticksList = createColorLabels(min: min(), max: max(), ticks: m_axis->tickCount());
31 // Width of horizontal axis sizeHint indicates the maximum distance labels can extend past
32 // first and last ticks. Base width is irrelevant.
33 qreal width = 0;
34 qreal height = 0;
35
36 switch (which) {
37 case Qt::MinimumSize: {
38 if (labelsVisible()) {
39 QRectF boundingRect = ChartPresenter::textBoundingRect(
40 font: axis()->labelsFont(), QStringLiteral("..."), angle: axis()->labelsAngle());
41 width = boundingRect.width() / 2.0;
42 height = boundingRect.height() + labelPadding() + base.height() + m_axis->size()
43 + colorScalePadding() + 1.0;
44 } else {
45 width = 0;
46 height = base.height() + m_axis->size() + colorScalePadding() + 1.0;
47 }
48 sh = QSizeF(width, height);
49 break;
50 }
51 case Qt::PreferredSize: {
52 if (labelsVisible()) {
53 qreal labelHeight = 0.0;
54 qreal firstWidth = -1.0;
55 for (const QString &s : ticksList) {
56 QRectF rect = ChartPresenter::textBoundingRect(font: axis()->labelsFont(), text: s,
57 angle: axis()->labelsAngle());
58 labelHeight = qMax(a: rect.height(), b: labelHeight);
59 width = rect.width();
60 if (firstWidth < 0.0)
61 firstWidth = width;
62 }
63 height = labelHeight + labelPadding() + base.height() + m_axis->size() + colorScalePadding()
64 + 1.0;
65 width = qMax(a: width, b: firstWidth) / 2.0;
66 } else {
67 height = base.height() + m_axis->size() + colorScalePadding() + 1.0;
68 width = 0;
69 }
70 sh = QSizeF(width, height);
71 break;
72 }
73 default:
74 break;
75 }
76 return sh;
77}
78
79QList<qreal> ChartColorAxisX::calculateLayout() const
80{
81 int tickCount = m_axis->tickCount();
82
83 Q_ASSERT(tickCount >= 2);
84
85 QList<qreal> points;
86 points.resize(size: tickCount);
87
88 const QRectF &gridRect = gridGeometry();
89 const qreal deltaX = gridRect.width() / (qreal(tickCount) - 1.0);
90 for (int i = 0; i < tickCount; ++i)
91 points[i] = qreal(i) * deltaX + gridRect.left();
92 return points;
93}
94
95void ChartColorAxisX::updateGeometry()
96{
97 setLabels(createColorLabels(min: min(), max: max(), ticks: m_axis->tickCount()));
98 HorizontalAxis::updateGeometry();
99}
100
101
102QT_END_NAMESPACE
103
104#include "moc_chartcoloraxisx_p.cpp"
105

source code of qtcharts/src/charts/axis/coloraxis/chartcoloraxisx.cpp