abstractdomain.cpp
264 lines
| 7.0 KiB
| text/x-c
|
CppLexer
|
r794 | /**************************************************************************** | ||
** | ||||
|
r2776 | ** Copyright (C) 2015 The Qt Company Ltd | ||
|
r794 | ** All rights reserved. | ||
|
r2776 | ** For any questions to The Qt Company, please use contact form at http://qt.io | ||
|
r794 | ** | ||
|
r2740 | ** This file is part of the Qt Charts module. | ||
|
r794 | ** | ||
|
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. | ||
|
r794 | ** | ||
** If you have questions regarding the use of this file, please use | ||||
|
r2740 | ** contact form at http://qt.io | ||
|
r794 | ** | ||
****************************************************************************/ | ||||
|
r2714 | #include <private/abstractdomain_p.h> | ||
#include <private/qabstractaxis_p.h> | ||||
#include <QtCore/QtMath> | ||||
|
r2775 | #include <cmath> | ||
|
r131 | |||
|
r2712 | QT_CHARTS_BEGIN_NAMESPACE | ||
|
r131 | |||
|
r2275 | AbstractDomain::AbstractDomain(QObject *parent) | ||
|
r2097 | : QObject(parent), | ||
m_minX(0), | ||||
m_maxX(0), | ||||
m_minY(0), | ||||
|
r2273 | m_maxY(0), | ||
|
r2545 | m_signalsBlocked(false), | ||
m_zoomed(false), | ||||
m_zoomResetMinX(0), | ||||
m_zoomResetMaxX(0), | ||||
m_zoomResetMinY(0), | ||||
m_zoomResetMaxY(0) | ||||
|
r131 | { | ||
} | ||||
|
r2275 | AbstractDomain::~AbstractDomain() | ||
|
r131 | { | ||
} | ||||
|
r2483 | void AbstractDomain::setSize(const QSizeF &size) | ||
|
r2273 | { | ||
|
r2712 | if (m_size != size) { | ||
m_size=size; | ||||
emit updated(); | ||||
} | ||||
|
r2273 | } | ||
|
r2275 | QSizeF AbstractDomain::size() const | ||
|
r2273 | { | ||
|
r2712 | return m_size; | ||
|
r2273 | } | ||
|
r2275 | void AbstractDomain::setRangeX(qreal min, qreal max) | ||
|
r439 | { | ||
|
r2097 | setRange(min, max, m_minY, m_maxY); | ||
|
r439 | } | ||
|
r679 | |||
|
r2275 | void AbstractDomain::setRangeY(qreal min, qreal max) | ||
|
r439 | { | ||
|
r678 | setRange(m_minX, m_maxX, min, max); | ||
|
r439 | } | ||
|
r2275 | void AbstractDomain::setMinX(qreal min) | ||
|
r439 | { | ||
|
r678 | setRange(min, m_maxX, m_minY, m_maxY); | ||
|
r439 | } | ||
|
r2275 | void AbstractDomain::setMaxX(qreal max) | ||
|
r439 | { | ||
|
r678 | setRange(m_minX, max, m_minY, m_maxY); | ||
|
r439 | } | ||
|
r2275 | void AbstractDomain::setMinY(qreal min) | ||
|
r439 | { | ||
|
r678 | setRange(m_minX, m_maxX, min, m_maxY); | ||
|
r439 | } | ||
|
r2275 | void AbstractDomain::setMaxY(qreal max) | ||
|
r439 | { | ||
setRange(m_minX, m_maxX, m_minY, max); | ||||
} | ||||
|
r2275 | qreal AbstractDomain::spanX() const | ||
|
r131 | { | ||
|
r439 | Q_ASSERT(m_maxX >= m_minX); | ||
return m_maxX - m_minX; | ||||
|
r131 | } | ||
|
r2275 | qreal AbstractDomain::spanY() const | ||
|
r131 | { | ||
|
r439 | Q_ASSERT(m_maxY >= m_minY); | ||
return m_maxY - m_minY; | ||||
|
r131 | } | ||
|
r2275 | bool AbstractDomain::isEmpty() const | ||
|
r388 | { | ||
|
r2711 | return qFuzzyCompare(spanX(), 0) || qFuzzyCompare(spanY(), 0) || m_size.isEmpty(); | ||
|
r388 | } | ||
|
r2275 | QPointF AbstractDomain::calculateDomainPoint(const QPointF &point) const | ||
|
r2273 | { | ||
const qreal deltaX = m_size.width() / (m_maxX - m_minX); | ||||
const qreal deltaY = m_size.height() / (m_maxY - m_minY); | ||||
qreal x = point.x() / deltaX + m_minX; | ||||
qreal y = (point.y() - m_size.height()) / (-deltaY) + m_minY; | ||||
return QPointF(x, y); | ||||
} | ||||
// handlers | ||||
|
r2275 | void AbstractDomain::handleVerticalAxisRangeChanged(qreal min, qreal max) | ||
|
r2273 | { | ||
|
r2289 | setRangeY(min, max); | ||
|
r2273 | } | ||
|
r2275 | void AbstractDomain::handleHorizontalAxisRangeChanged(qreal min, qreal max) | ||
|
r2273 | { | ||
|
r2289 | setRangeX(min, max); | ||
|
r2273 | } | ||
|
r2289 | void AbstractDomain::blockRangeSignals(bool block) | ||
|
r2273 | { | ||
|
r2483 | if (m_signalsBlocked!=block) { | ||
|
r2289 | m_signalsBlocked=block; | ||
|
r2483 | if (!block) { | ||
|
r2289 | emit rangeHorizontalChanged(m_minX,m_maxX); | ||
emit rangeVerticalChanged(m_minY,m_maxY); | ||||
} | ||||
} | ||||
|
r2273 | } | ||
|
r2545 | void AbstractDomain::zoomReset() | ||
{ | ||||
if (m_zoomed) { | ||||
setRange(m_zoomResetMinX, | ||||
m_zoomResetMaxX, | ||||
m_zoomResetMinY, | ||||
m_zoomResetMaxY); | ||||
m_zoomed = false; | ||||
} | ||||
} | ||||
void AbstractDomain::storeZoomReset() | ||||
{ | ||||
if (!m_zoomed) { | ||||
m_zoomed = true; | ||||
m_zoomResetMinX = m_minX; | ||||
m_zoomResetMaxX = m_maxX; | ||||
m_zoomResetMinY = m_minY; | ||||
m_zoomResetMaxY = m_maxY; | ||||
} | ||||
} | ||||
|
r2273 | //algorithm defined by Paul S.Heckbert GraphicalGems I | ||
|
r2275 | void AbstractDomain::looseNiceNumbers(qreal &min, qreal &max, int &ticksCount) | ||
|
r2273 | { | ||
qreal range = niceNumber(max - min, true); //range with ceiling | ||||
qreal step = niceNumber(range / (ticksCount - 1), false); | ||||
min = qFloor(min / step); | ||||
max = qCeil(max / step); | ||||
ticksCount = int(max - min) + 1; | ||||
min *= step; | ||||
max *= step; | ||||
} | ||||
//nice numbers can be expressed as form of 1*10^n, 2* 10^n or 5*10^n | ||||
|
r2275 | qreal AbstractDomain::niceNumber(qreal x, bool ceiling) | ||
|
r2273 | { | ||
|
r2775 | qreal z = qPow(10, qFloor(std::log10(x))); //find corresponding number of the form of 10^n than is smaller than x | ||
|
r2273 | qreal q = x / z; //q<10 && q>=1; | ||
if (ceiling) { | ||||
if (q <= 1.0) q = 1; | ||||
else if (q <= 2.0) q = 2; | ||||
else if (q <= 5.0) q = 5; | ||||
else q = 10; | ||||
} else { | ||||
if (q < 1.5) q = 1; | ||||
else if (q < 3.0) q = 2; | ||||
else if (q < 7.0) q = 5; | ||||
else q = 10; | ||||
} | ||||
return q * z; | ||||
} | ||||
|
r2483 | bool AbstractDomain::attachAxis(QAbstractAxis *axis) | ||
|
r2284 | { | ||
|
r2483 | if (axis->orientation() == Qt::Vertical) { | ||
|
r2712 | QObject::connect(axis->d_ptr.data(), SIGNAL(rangeChanged(qreal,qreal)), this, SLOT(handleVerticalAxisRangeChanged(qreal,qreal))); | ||
QObject::connect(this, SIGNAL(rangeVerticalChanged(qreal,qreal)), axis->d_ptr.data(), SLOT(handleRangeChanged(qreal,qreal))); | ||||
} | ||||
|
r2284 | |||
|
r2483 | if (axis->orientation() == Qt::Horizontal) { | ||
|
r2712 | QObject::connect(axis->d_ptr.data(), SIGNAL(rangeChanged(qreal,qreal)), this, SLOT(handleHorizontalAxisRangeChanged(qreal,qreal))); | ||
QObject::connect(this, SIGNAL(rangeHorizontalChanged(qreal,qreal)), axis->d_ptr.data(), SLOT(handleRangeChanged(qreal,qreal))); | ||||
} | ||||
|
r2284 | |||
|
r2712 | return true; | ||
|
r2284 | } | ||
|
r2483 | bool AbstractDomain::detachAxis(QAbstractAxis *axis) | ||
|
r2284 | { | ||
|
r2483 | if (axis->orientation() == Qt::Vertical) { | ||
|
r2284 | QObject::disconnect(axis->d_ptr.data(), SIGNAL(rangeChanged(qreal,qreal)), this, SLOT(handleVerticalAxisRangeChanged(qreal,qreal))); | ||
QObject::disconnect(this, SIGNAL(rangeVerticalChanged(qreal,qreal)), axis->d_ptr.data(), SLOT(handleRangeChanged(qreal,qreal))); | ||||
} | ||||
|
r2483 | if (axis->orientation() == Qt::Horizontal) { | ||
|
r2284 | QObject::disconnect(axis->d_ptr.data(), SIGNAL(rangeChanged(qreal,qreal)), this, SLOT(handleHorizontalAxisRangeChanged(qreal,qreal))); | ||
QObject::disconnect(this, SIGNAL(rangeHorizontalChanged(qreal,qreal)), axis->d_ptr.data(), SLOT(handleRangeChanged(qreal,qreal))); | ||||
} | ||||
return true; | ||||
} | ||||
|
r2273 | |||
// operators | ||||
|
r2712 | bool QT_CHARTS_AUTOTEST_EXPORT operator== (const AbstractDomain &domain1, const AbstractDomain &domain2) | ||
|
r220 | { | ||
|
r2483 | return (qFuzzyIsNull(domain1.m_maxX - domain2.m_maxX) | ||
&& qFuzzyIsNull(domain1.m_maxY - domain2.m_maxY) | ||||
&& qFuzzyIsNull(domain1.m_minX - domain2.m_minX) | ||||
&& qFuzzyIsNull(domain1.m_minY - domain2.m_minY)); | ||||
|
r220 | } | ||
|
r1066 | |||
|
r2712 | bool QT_CHARTS_AUTOTEST_EXPORT operator!= (const AbstractDomain &domain1, const AbstractDomain &domain2) | ||
|
r220 | { | ||
return !(domain1 == domain2); | ||||
} | ||||
|
r1066 | |||
|
r2712 | QDebug QT_CHARTS_AUTOTEST_EXPORT operator<<(QDebug dbg, const AbstractDomain &domain) | ||
|
r222 | { | ||
|
r2620 | #ifdef QT_NO_TEXTSTREAM | ||
Q_UNUSED(domain) | ||||
#else | ||||
|
r2275 | dbg.nospace() << "AbstractDomain(" << domain.m_minX << ',' << domain.m_maxX << ',' << domain.m_minY << ',' << domain.m_maxY << ')' << domain.m_size; | ||
|
r2620 | #endif | ||
|
r222 | return dbg.maybeSpace(); | ||
} | ||||
|
r2483 | // This function adjusts min/max ranges to failsafe values if negative/zero values are attempted. | ||
void AbstractDomain::adjustLogDomainRanges(qreal &min, qreal &max) | ||||
{ | ||||
if (min <= 0) { | ||||
min = 1.0; | ||||
if (max <= min) | ||||
max = min + 1.0; | ||||
} | ||||
} | ||||
|
r2275 | #include "moc_abstractdomain_p.cpp" | ||
|
r439 | |||
|
r2712 | QT_CHARTS_END_NAMESPACE | ||