chartlogvalueaxisx.cpp
147 lines
| 5.1 KiB
| text/x-c
|
CppLexer
|
r2274 | /**************************************************************************** | ||
** | ||||
|
r2776 | ** Copyright (C) 2015 The Qt Company Ltd | ||
|
r2274 | ** All rights reserved. | ||
|
r2776 | ** For any questions to The Qt Company, please use contact form at http://qt.io | ||
|
r2274 | ** | ||
|
r2740 | ** This file is part of the Qt Charts module. | ||
|
r2274 | ** | ||
|
r2740 | ** Licensees holding valid commercial license for Qt may use this file in | ||
** accordance with the Qt License Agreement provided with the Software | ||||
** or, alternatively, in accordance with the terms contained in a written | ||||
|
r2776 | ** agreement between you and The Qt Company. | ||
|
r2274 | ** | ||
** If you have questions regarding the use of this file, please use | ||||
|
r2740 | ** contact form at http://qt.io | ||
|
r2274 | ** | ||
****************************************************************************/ | ||||
|
r2714 | #include <private/chartlogvalueaxisx_p.h> | ||
#include <private/chartpresenter_p.h> | ||||
#include <QtCharts/QLogValueAxis> | ||||
#include <private/abstractchartlayout_p.h> | ||||
#include <QtWidgets/QGraphicsLayout> | ||||
#include <QtCore/QtMath> | ||||
#include <QtCore/QDebug> | ||||
|
r2775 | #include <cmath> | ||
|
r2274 | |||
|
r2712 | QT_CHARTS_BEGIN_NAMESPACE | ||
|
r2274 | |||
|
r2483 | ChartLogValueAxisX::ChartLogValueAxisX(QLogValueAxis *axis, QGraphicsItem *item) | ||
|
r2274 | : HorizontalAxis(axis, item), | ||
m_axis(axis) | ||||
{ | ||||
|
r2483 | QObject::connect(m_axis, SIGNAL(baseChanged(qreal)), this, SLOT(handleBaseChanged(qreal))); | ||
QObject::connect(m_axis, SIGNAL(labelFormatChanged(QString)), this, SLOT(handleLabelFormatChanged(QString))); | ||||
|
r2274 | } | ||
ChartLogValueAxisX::~ChartLogValueAxisX() | ||||
{ | ||||
} | ||||
QVector<qreal> ChartLogValueAxisX::calculateLayout() const | ||||
{ | ||||
QVector<qreal> points; | ||||
|
r2775 | qreal logMax = std::log10(m_axis->max()) / std::log10(m_axis->base()); | ||
qreal logMin = std::log10(m_axis->min()) / std::log10(m_axis->base()); | ||||
|
r2275 | qreal leftEdge = logMin < logMax ? logMin : logMax; | ||
|
r2775 | qreal ceilEdge = qCeil(leftEdge); | ||
int tickCount = qAbs(qCeil(logMax) - qCeil(logMin)); | ||||
|
r2274 | |||
|
r2837 | // If the high edge sits exactly on the tick value, add a tick | ||
qreal highValue = logMin < logMax ? logMax : logMin; | ||||
if (qFuzzyCompare(highValue, qreal(qCeil(highValue)))) | ||||
tickCount++; | ||||
|
r2759 | points.resize(tickCount); | ||
|
r2274 | const QRectF &gridRect = gridGeometry(); | ||
const qreal deltaX = gridRect.width() / qAbs(logMax - logMin); | ||||
|
r2759 | for (int i = 0; i < tickCount; ++i) | ||
|
r2483 | points[i] = (ceilEdge + qreal(i)) * deltaX - leftEdge * deltaX + gridRect.left(); | ||
|
r2274 | |||
return points; | ||||
} | ||||
void ChartLogValueAxisX::updateGeometry() | ||||
{ | ||||
|
r2483 | const QVector<qreal>& layout = ChartAxisElement::layout(); | ||
|
r2274 | setLabels(createLogValueLabels(m_axis->min(), m_axis->max(), m_axis->base(), layout.size(), m_axis->labelFormat())); | ||
HorizontalAxis::updateGeometry(); | ||||
} | ||||
|
r2322 | void ChartLogValueAxisX::handleBaseChanged(qreal base) | ||
{ | ||||
Q_UNUSED(base); | ||||
|
r2333 | QGraphicsLayoutItem::updateGeometry(); | ||
|
r2322 | if(presenter()) presenter()->layout()->invalidate(); | ||
} | ||||
|
r2274 | |||
|
r2323 | void ChartLogValueAxisX::handleLabelFormatChanged(const QString &format) | ||
{ | ||||
Q_UNUSED(format); | ||||
|
r2333 | QGraphicsLayoutItem::updateGeometry(); | ||
|
r2323 | if(presenter()) presenter()->layout()->invalidate(); | ||
} | ||||
|
r2274 | QSizeF ChartLogValueAxisX::sizeHint(Qt::SizeHint which, const QSizeF &constraint) const | ||
{ | ||||
Q_UNUSED(constraint) | ||||
QSizeF sh; | ||||
QSizeF base = HorizontalAxis::sizeHint(which, constraint); | ||||
QStringList ticksList; | ||||
|
r2775 | qreal logMax = std::log10(m_axis->max()) / std::log10(m_axis->base()); | ||
qreal logMin = std::log10(m_axis->min()) / std::log10(m_axis->base()); | ||||
int tickCount = qAbs(qCeil(logMax) - qCeil(logMin)); | ||||
|
r2837 | |||
// If the high edge sits exactly on the tick value, add a tick | ||||
qreal highValue = logMin < logMax ? logMax : logMin; | ||||
if (qFuzzyCompare(highValue, qreal(qCeil(highValue)))) | ||||
tickCount++; | ||||
|
r2460 | if (m_axis->max() > m_axis->min() && tickCount > 0) | ||
|
r2274 | ticksList = createLogValueLabels(m_axis->min(), m_axis->max(), m_axis->base(), tickCount, m_axis->labelFormat()); | ||
else | ||||
|
r2712 | ticksList.append(QStringLiteral(" ")); | ||
|
r2443 | // Width of horizontal axis sizeHint indicates the maximum distance labels can extend past | ||
// first and last ticks. Base width is irrelevant. | ||||
|
r2274 | qreal width = 0; | ||
qreal height = 0; | ||||
switch (which) { | ||||
case Qt::MinimumSize:{ | ||||
|
r2712 | QRectF boundingRect = ChartPresenter::textBoundingRect(axis()->labelsFont(), | ||
QStringLiteral("..."), | ||||
axis()->labelsAngle()); | ||||
|
r2443 | width = boundingRect.width() / 2.0; | ||
|
r2534 | height = boundingRect.height() + labelPadding() + base.height() + 1.0; | ||
|
r2412 | sh = QSizeF(width, height); | ||
|
r2274 | break; | ||
} | ||||
case Qt::PreferredSize: { | ||||
|
r2534 | qreal labelHeight = 0.0; | ||
qreal firstWidth = -1.0; | ||||
|
r2412 | foreach (const QString& s, ticksList) { | ||
|
r2539 | QRectF rect = ChartPresenter::textBoundingRect(axis()->labelsFont(), s, axis()->labelsAngle()); | ||
|
r2412 | labelHeight = qMax(rect.height(), labelHeight); | ||
|
r2443 | width = rect.width(); | ||
|
r2534 | if (firstWidth < 0.0) | ||
|
r2443 | firstWidth = width; | ||
|
r2412 | } | ||
|
r2534 | height = labelHeight + labelPadding() + base.height() + 1.0; | ||
width = qMax(width, firstWidth) / 2.0; | ||||
|
r2412 | sh = QSizeF(width, height); | ||
|
r2274 | break; | ||
} | ||||
default: | ||||
break; | ||||
} | ||||
return sh; | ||||
} | ||||
|
r2322 | #include "moc_chartlogvalueaxisx_p.cpp" | ||
|
r2712 | QT_CHARTS_END_NAMESPACE | ||