| 1 | // Copyright (C) 2021 The Qt Company Ltd. |
| 2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only |
| 3 | |
| 4 | #undef QT_NO_FOREACH // this file contains unported legacy Q_FOREACH uses |
| 5 | |
| 6 | #include <QtCharts/qlogvalueaxis.h> |
| 7 | #include <QtCore/qmath.h> |
| 8 | #include <QtWidgets/qgraphicslayout.h> |
| 9 | #include <private/abstractchartlayout_p.h> |
| 10 | #include <private/chartlogvalueaxisx_p.h> |
| 11 | #include <private/chartpresenter_p.h> |
| 12 | |
| 13 | QT_BEGIN_NAMESPACE |
| 14 | |
| 15 | ChartLogValueAxisX::ChartLogValueAxisX(QLogValueAxis *axis, QGraphicsItem *item) |
| 16 | : HorizontalAxis(axis, item), |
| 17 | m_axis(axis) |
| 18 | { |
| 19 | QObject::connect(sender: m_axis, SIGNAL(baseChanged(qreal)), receiver: this, SLOT(handleBaseChanged(qreal))); |
| 20 | QObject::connect(sender: m_axis, SIGNAL(labelFormatChanged(QString)), receiver: this, SLOT(handleLabelFormatChanged(QString))); |
| 21 | } |
| 22 | |
| 23 | ChartLogValueAxisX::~ChartLogValueAxisX() |
| 24 | { |
| 25 | } |
| 26 | |
| 27 | QList<qreal> ChartLogValueAxisX::calculateLayout() const |
| 28 | { |
| 29 | QList<qreal> points; |
| 30 | points.resize(size: m_axis->tickCount()); |
| 31 | |
| 32 | const qreal logMax = qLn(v: m_axis->max()) / qLn(v: m_axis->base()); |
| 33 | const qreal logMin = qLn(v: m_axis->min()) / qLn(v: m_axis->base()); |
| 34 | const qreal leftEdge = qMin(a: logMin, b: logMax); |
| 35 | const qreal ceilEdge = std::ceil(x: leftEdge); |
| 36 | |
| 37 | const QRectF &gridRect = gridGeometry(); |
| 38 | const qreal deltaX = gridRect.width() / qAbs(t: logMax - logMin); |
| 39 | for (int i = 0; i < m_axis->tickCount(); ++i) |
| 40 | points[i] = (ceilEdge + qreal(i)) * deltaX - leftEdge * deltaX + gridRect.left(); |
| 41 | |
| 42 | return points; |
| 43 | } |
| 44 | |
| 45 | void ChartLogValueAxisX::updateGeometry() |
| 46 | { |
| 47 | const QList<qreal> &layout = ChartAxisElement::layout(); |
| 48 | setLabels(createLogValueLabels(min: m_axis->min(), max: m_axis->max(), base: m_axis->base(), ticks: layout.size(), format: m_axis->labelFormat())); |
| 49 | HorizontalAxis::updateGeometry(); |
| 50 | } |
| 51 | |
| 52 | void ChartLogValueAxisX::handleBaseChanged(qreal base) |
| 53 | { |
| 54 | Q_UNUSED(base); |
| 55 | QGraphicsLayoutItem::updateGeometry(); |
| 56 | if(presenter()) presenter()->layout()->invalidate(); |
| 57 | } |
| 58 | |
| 59 | void ChartLogValueAxisX::handleLabelFormatChanged(const QString &format) |
| 60 | { |
| 61 | Q_UNUSED(format); |
| 62 | QGraphicsLayoutItem::updateGeometry(); |
| 63 | if(presenter()) presenter()->layout()->invalidate(); |
| 64 | } |
| 65 | |
| 66 | QSizeF ChartLogValueAxisX::sizeHint(Qt::SizeHint which, const QSizeF &constraint) const |
| 67 | { |
| 68 | Q_UNUSED(constraint); |
| 69 | |
| 70 | const QSizeF base = HorizontalAxis::sizeHint(which, constraint); |
| 71 | const int tickCount = m_axis->tickCount(); |
| 72 | QStringList ticksList; |
| 73 | QSizeF sh; |
| 74 | |
| 75 | if (m_axis->max() > m_axis->min() && tickCount > 0) |
| 76 | ticksList = createLogValueLabels(min: m_axis->min(), max: m_axis->max(), base: m_axis->base(), ticks: tickCount, format: m_axis->labelFormat()); |
| 77 | else |
| 78 | ticksList.append(QStringLiteral(" " )); |
| 79 | // Width of horizontal axis sizeHint indicates the maximum distance labels can extend past |
| 80 | // first and last ticks. Base width is irrelevant. |
| 81 | qreal width = 0; |
| 82 | qreal height = 0; |
| 83 | |
| 84 | switch (which) { |
| 85 | case Qt::MinimumSize:{ |
| 86 | if (labelsVisible()) { |
| 87 | QRectF boundingRect = ChartPresenter::textBoundingRect(font: axis()->labelsFont(), |
| 88 | QStringLiteral("..." ), |
| 89 | angle: axis()->labelsAngle()); |
| 90 | width = boundingRect.width() / 2.0; |
| 91 | height = boundingRect.height() + labelPadding() + base.height() + 1.0; |
| 92 | } else { |
| 93 | width = 0; |
| 94 | height = base.height() + 1.0; |
| 95 | } |
| 96 | sh = QSizeF(width, height); |
| 97 | break; |
| 98 | } |
| 99 | case Qt::PreferredSize: { |
| 100 | if (labelsVisible()) { |
| 101 | qreal labelHeight = 0.0; |
| 102 | qreal firstWidth = -1.0; |
| 103 | foreach (const QString& s, ticksList) { |
| 104 | QRectF rect = ChartPresenter::textBoundingRect(font: axis()->labelsFont(), text: s, angle: axis()->labelsAngle()); |
| 105 | labelHeight = qMax(a: rect.height(), b: labelHeight); |
| 106 | width = rect.width(); |
| 107 | if (firstWidth < 0.0) |
| 108 | firstWidth = width; |
| 109 | } |
| 110 | height = labelHeight + labelPadding() + base.height() + 1.0; |
| 111 | width = qMax(a: width, b: firstWidth) / 2.0; |
| 112 | } else { |
| 113 | height = base.height() + 1.0; |
| 114 | width = 0; |
| 115 | } |
| 116 | sh = QSizeF(width, height); |
| 117 | break; |
| 118 | } |
| 119 | default: |
| 120 | break; |
| 121 | } |
| 122 | |
| 123 | return sh; |
| 124 | } |
| 125 | |
| 126 | QT_END_NAMESPACE |
| 127 | |
| 128 | #include "moc_chartlogvalueaxisx_p.cpp" |
| 129 | |