xydomain.cpp
180 lines
| 4.9 KiB
| text/x-c
|
CppLexer
|
r794 | /**************************************************************************** | ||
** | ||||
|
r2432 | ** Copyright (C) 2013 Digia Plc | ||
|
r794 | ** All rights reserved. | ||
** For any questions to Digia, please use contact form at http://qt.digia.com | ||||
** | ||||
** This file is part of the Qt Commercial Charts Add-on. | ||||
** | ||||
** $QT_BEGIN_LICENSE$ | ||||
** Licensees holding valid Qt Commercial licenses may use this file in | ||||
** accordance with the Qt Commercial License Agreement provided with the | ||||
** Software or, alternatively, in accordance with the terms contained in | ||||
** a written agreement between you and Digia. | ||||
** | ||||
** If you have questions regarding the use of this file, please use | ||||
** contact form at http://qt.digia.com | ||||
** $QT_END_LICENSE$ | ||||
** | ||||
****************************************************************************/ | ||||
|
r2275 | #include "xydomain_p.h" | ||
|
r1698 | #include "qabstractaxis_p.h" | ||
|
r2273 | #include <qmath.h> | ||
|
r131 | |||
QTCOMMERCIALCHART_BEGIN_NAMESPACE | ||||
|
r2275 | XYDomain::XYDomain(QObject *parent) | ||
: AbstractDomain(parent) | ||||
|
r131 | { | ||
} | ||||
|
r2275 | XYDomain::~XYDomain() | ||
|
r131 | { | ||
} | ||||
|
r2275 | void XYDomain::setRange(qreal minX, qreal maxX, qreal minY, qreal maxY) | ||
|
r678 | { | ||
|
r1078 | bool axisXChanged = false; | ||
bool axisYChanged = false; | ||||
|
r439 | |||
|
r2242 | if (!qFuzzyCompare(m_minX, minX) || !qFuzzyCompare(m_maxX, maxX)) { | ||
|
r2097 | m_minX = minX; | ||
m_maxX = maxX; | ||||
axisXChanged = true; | ||||
|
r2289 | if(!m_signalsBlocked) | ||
emit rangeHorizontalChanged(m_minX, m_maxX); | ||||
|
r678 | } | ||
|
r2242 | if (!qFuzzyCompare(m_minY, minY) || !qFuzzyCompare(m_maxY, maxY)) { | ||
|
r2097 | m_minY = minY; | ||
m_maxY = maxY; | ||||
axisYChanged = true; | ||||
|
r2289 | if(!m_signalsBlocked) | ||
emit rangeVerticalChanged(m_minY, m_maxY); | ||||
|
r678 | } | ||
|
r2097 | if (axisXChanged || axisYChanged) | ||
|
r1698 | emit updated(); | ||
|
r439 | } | ||
|
r678 | |||
|
r131 | |||
|
r2275 | void XYDomain::zoomIn(const QRectF &rect) | ||
|
r131 | { | ||
|
r2273 | qreal dx = spanX() / m_size.width(); | ||
qreal dy = spanY() / m_size.height(); | ||||
|
r131 | |||
|
r1078 | qreal maxX = m_maxX; | ||
qreal minX = m_minX; | ||||
qreal minY = m_minY; | ||||
qreal maxY = m_maxY; | ||||
|
r439 | |||
|
r1078 | maxX = minX + dx * rect.right(); | ||
minX = minX + dx * rect.left(); | ||||
minY = maxY - dy * rect.bottom(); | ||||
maxY = maxY - dy * rect.top(); | ||||
|
r678 | |||
|
r2097 | setRange(minX, maxX, minY, maxY); | ||
|
r439 | } | ||
|
r2275 | void XYDomain::zoomOut(const QRectF &rect) | ||
|
r439 | { | ||
qreal dx = spanX() / rect.width(); | ||||
qreal dy = spanY() / rect.height(); | ||||
|
r131 | |||
|
r1078 | qreal maxX = m_maxX; | ||
qreal minX = m_minX; | ||||
qreal minY = m_minY; | ||||
qreal maxY = m_maxY; | ||||
|
r131 | |||
|
r1078 | minX = maxX - dx * rect.right(); | ||
|
r2273 | maxX = minX + dx * m_size.width(); | ||
|
r1078 | maxY = minY + dy * rect.bottom(); | ||
|
r2273 | minY = maxY - dy * m_size.height(); | ||
|
r1078 | |||
|
r2097 | setRange(minX, maxX, minY, maxY); | ||
|
r531 | } | ||
|
r2275 | void XYDomain::move(qreal dx, qreal dy) | ||
|
r531 | { | ||
|
r2273 | qreal x = spanX() / m_size.width(); | ||
qreal y = spanY() / m_size.height(); | ||||
|
r531 | |||
|
r1078 | qreal maxX = m_maxX; | ||
qreal minX = m_minX; | ||||
qreal minY = m_minY; | ||||
qreal maxY = m_maxY; | ||||
|
r2097 | if (dx != 0) { | ||
|
r1078 | minX = minX + x * dx; | ||
maxX = maxX + x * dx; | ||||
|
r531 | } | ||
|
r2097 | if (dy != 0) { | ||
|
r1078 | minY = minY + y * dy; | ||
maxY = maxY + y * dy; | ||||
|
r531 | } | ||
|
r2097 | setRange(minX, maxX, minY, maxY); | ||
|
r439 | } | ||
|
r2356 | QPointF XYDomain::calculateGeometryPoint(const QPointF &point, bool &ok) const | ||
|
r678 | { | ||
|
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() - m_minX) * deltaX; | ||||
qreal y = (point.y() - m_minY) * -deltaY + m_size.height(); | ||||
|
r2356 | ok = true; | ||
|
r2273 | return QPointF(x, y); | ||
|
r678 | } | ||
|
r2275 | QVector<QPointF> XYDomain::calculateGeometryPoints(const QList<QPointF>& vector) const | ||
|
r554 | { | ||
|
r2273 | const qreal deltaX = m_size.width() / (m_maxX - m_minX); | ||
const qreal deltaY = m_size.height() / (m_maxY - m_minY); | ||||
QVector<QPointF> result; | ||||
result.resize(vector.count()); | ||||
for (int i = 0; i < vector.count(); ++i) { | ||||
qreal x = (vector[i].x() - m_minX) * deltaX; | ||||
qreal y = (vector[i].y() - m_minY) * -deltaY + m_size.height(); | ||||
result[i].setX(x); | ||||
result[i].setY(y); | ||||
} | ||||
return result; | ||||
|
r554 | } | ||
|
r2275 | QPointF XYDomain::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); | ||||
} | ||||
// operators | ||||
|
r2275 | bool QTCOMMERCIALCHART_AUTOTEST_EXPORT operator== (const XYDomain &domain1, const XYDomain &domain2) | ||
|
r220 | { | ||
|
r2242 | return (qFuzzyCompare(domain1.m_maxX, domain2.m_maxX) && | ||
qFuzzyCompare(domain1.m_maxY, domain2.m_maxY) && | ||||
qFuzzyCompare(domain1.m_minX, domain2.m_minX) && | ||||
qFuzzyCompare(domain1.m_minY, domain2.m_minY)); | ||||
|
r220 | } | ||
|
r1066 | |||
|
r2275 | bool QTCOMMERCIALCHART_AUTOTEST_EXPORT operator!= (const XYDomain &domain1, const XYDomain &domain2) | ||
|
r220 | { | ||
return !(domain1 == domain2); | ||||
} | ||||
|
r1066 | |||
|
r2275 | QDebug QTCOMMERCIALCHART_AUTOTEST_EXPORT operator<<(QDebug dbg, const XYDomain &domain) | ||
|
r222 | { | ||
|
r2275 | dbg.nospace() << "AbstractDomain(" << domain.m_minX << ',' << domain.m_maxX << ',' << domain.m_minY << ',' << domain.m_maxY << ')' << domain.m_size; | ||
|
r222 | return dbg.maybeSpace(); | ||
} | ||||
|
r2275 | #include "moc_xydomain_p.cpp" | ||
|
r439 | |||
|
r131 | QTCOMMERCIALCHART_END_NAMESPACE | ||