abstractdomain.cpp
223 lines
| 6.1 KiB
| text/x-c
|
CppLexer
Jani Honkonen
|
r794 | /**************************************************************************** | ||
** | ||||
** Copyright (C) 2012 Digia Plc | ||||
** 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$ | ||||
** | ||||
****************************************************************************/ | ||||
Marek Rosa
|
r2275 | #include "abstractdomain_p.h" | ||
Michal Klocek
|
r1698 | #include "qabstractaxis_p.h" | ||
Michal Klocek
|
r2273 | #include <qmath.h> | ||
Michal Klocek
|
r131 | |||
QTCOMMERCIALCHART_BEGIN_NAMESPACE | ||||
Marek Rosa
|
r2275 | AbstractDomain::AbstractDomain(QObject *parent) | ||
Jani Honkonen
|
r2097 | : QObject(parent), | ||
m_minX(0), | ||||
m_maxX(0), | ||||
m_minY(0), | ||||
Michal Klocek
|
r2273 | m_maxY(0), | ||
Michal Klocek
|
r2289 | m_signalsBlocked(false) | ||
Michal Klocek
|
r131 | { | ||
} | ||||
Marek Rosa
|
r2275 | AbstractDomain::~AbstractDomain() | ||
Michal Klocek
|
r131 | { | ||
} | ||||
Marek Rosa
|
r2275 | void AbstractDomain::setSize(const QSizeF& size) | ||
Michal Klocek
|
r2273 | { | ||
if(m_size!=size) | ||||
{ | ||||
m_size=size; | ||||
emit updated(); | ||||
} | ||||
} | ||||
Marek Rosa
|
r2275 | QSizeF AbstractDomain::size() const | ||
Michal Klocek
|
r2273 | { | ||
return m_size; | ||||
} | ||||
Marek Rosa
|
r2275 | void AbstractDomain::setRangeX(qreal min, qreal max) | ||
Michal Klocek
|
r439 | { | ||
Jani Honkonen
|
r2097 | setRange(min, max, m_minY, m_maxY); | ||
Michal Klocek
|
r439 | } | ||
Michal Klocek
|
r679 | |||
Marek Rosa
|
r2275 | void AbstractDomain::setRangeY(qreal min, qreal max) | ||
Michal Klocek
|
r439 | { | ||
Michal Klocek
|
r678 | setRange(m_minX, m_maxX, min, max); | ||
Michal Klocek
|
r439 | } | ||
Marek Rosa
|
r2275 | void AbstractDomain::setMinX(qreal min) | ||
Michal Klocek
|
r439 | { | ||
Michal Klocek
|
r678 | setRange(min, m_maxX, m_minY, m_maxY); | ||
Michal Klocek
|
r439 | } | ||
Marek Rosa
|
r2275 | void AbstractDomain::setMaxX(qreal max) | ||
Michal Klocek
|
r439 | { | ||
Michal Klocek
|
r678 | setRange(m_minX, max, m_minY, m_maxY); | ||
Michal Klocek
|
r439 | } | ||
Marek Rosa
|
r2275 | void AbstractDomain::setMinY(qreal min) | ||
Michal Klocek
|
r439 | { | ||
Michal Klocek
|
r678 | setRange(m_minX, m_maxX, min, m_maxY); | ||
Michal Klocek
|
r439 | } | ||
Marek Rosa
|
r2275 | void AbstractDomain::setMaxY(qreal max) | ||
Michal Klocek
|
r439 | { | ||
setRange(m_minX, m_maxX, m_minY, max); | ||||
} | ||||
Marek Rosa
|
r2275 | qreal AbstractDomain::spanX() const | ||
Michal Klocek
|
r131 | { | ||
Michal Klocek
|
r439 | Q_ASSERT(m_maxX >= m_minX); | ||
return m_maxX - m_minX; | ||||
Michal Klocek
|
r131 | } | ||
Marek Rosa
|
r2275 | qreal AbstractDomain::spanY() const | ||
Michal Klocek
|
r131 | { | ||
Michal Klocek
|
r439 | Q_ASSERT(m_maxY >= m_minY); | ||
return m_maxY - m_minY; | ||||
Michal Klocek
|
r131 | } | ||
Marek Rosa
|
r2275 | bool AbstractDomain::isEmpty() const | ||
Michal Klocek
|
r388 | { | ||
Marek Rosa
|
r2275 | return qFuzzyIsNull(spanX()) || qFuzzyIsNull(spanY()) || m_size.isEmpty() ; | ||
Michal Klocek
|
r388 | } | ||
Marek Rosa
|
r2275 | QPointF AbstractDomain::calculateDomainPoint(const QPointF &point) const | ||
Michal Klocek
|
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 | ||||
Marek Rosa
|
r2275 | void AbstractDomain::handleVerticalAxisRangeChanged(qreal min, qreal max) | ||
Michal Klocek
|
r2273 | { | ||
Michal Klocek
|
r2289 | setRangeY(min, max); | ||
Michal Klocek
|
r2273 | } | ||
Marek Rosa
|
r2275 | void AbstractDomain::handleHorizontalAxisRangeChanged(qreal min, qreal max) | ||
Michal Klocek
|
r2273 | { | ||
Michal Klocek
|
r2289 | setRangeX(min, max); | ||
Michal Klocek
|
r2273 | } | ||
Michal Klocek
|
r2289 | void AbstractDomain::blockRangeSignals(bool block) | ||
Michal Klocek
|
r2273 | { | ||
Michal Klocek
|
r2289 | if(m_signalsBlocked!=block){ | ||
m_signalsBlocked=block; | ||||
if(!block) { | ||||
emit rangeHorizontalChanged(m_minX,m_maxX); | ||||
emit rangeVerticalChanged(m_minY,m_maxY); | ||||
} | ||||
} | ||||
Michal Klocek
|
r2273 | } | ||
//algorithm defined by Paul S.Heckbert GraphicalGems I | ||||
Marek Rosa
|
r2275 | void AbstractDomain::looseNiceNumbers(qreal &min, qreal &max, int &ticksCount) | ||
Michal Klocek
|
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 | ||||
Marek Rosa
|
r2275 | qreal AbstractDomain::niceNumber(qreal x, bool ceiling) | ||
Michal Klocek
|
r2273 | { | ||
qreal z = qPow(10, qFloor(log10(x))); //find corresponding number of the form of 10^n than is smaller than x | ||||
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; | ||||
} | ||||
Michal Klocek
|
r2284 | bool AbstractDomain::attachAxis(QAbstractAxis* axis) | ||
{ | ||||
if(axis->orientation()==Qt::Vertical) { | ||||
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))); | ||||
} | ||||
if(axis->orientation()==Qt::Horizontal) { | ||||
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))); | ||||
} | ||||
return true; | ||||
} | ||||
bool AbstractDomain::detachAxis(QAbstractAxis* axis) | ||||
{ | ||||
if(axis->orientation()==Qt::Vertical) { | ||||
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))); | ||||
} | ||||
if(axis->orientation()==Qt::Horizontal) { | ||||
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; | ||||
} | ||||
Michal Klocek
|
r2273 | |||
// operators | ||||
Marek Rosa
|
r2275 | bool QTCOMMERCIALCHART_AUTOTEST_EXPORT operator== (const AbstractDomain &domain1, const AbstractDomain &domain2) | ||
Michal Klocek
|
r220 | { | ||
Marek Rosa
|
r2275 | 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)); | ||||
Michal Klocek
|
r220 | } | ||
Michal Klocek
|
r1066 | |||
Marek Rosa
|
r2275 | bool QTCOMMERCIALCHART_AUTOTEST_EXPORT operator!= (const AbstractDomain &domain1, const AbstractDomain &domain2) | ||
Michal Klocek
|
r220 | { | ||
return !(domain1 == domain2); | ||||
} | ||||
Michal Klocek
|
r1066 | |||
Marek Rosa
|
r2275 | QDebug QTCOMMERCIALCHART_AUTOTEST_EXPORT operator<<(QDebug dbg, const AbstractDomain &domain) | ||
Michal Klocek
|
r222 | { | ||
Marek Rosa
|
r2275 | dbg.nospace() << "AbstractDomain(" << domain.m_minX << ',' << domain.m_maxX << ',' << domain.m_minY << ',' << domain.m_maxY << ')' << domain.m_size; | ||
Michal Klocek
|
r222 | return dbg.maybeSpace(); | ||
} | ||||
Marek Rosa
|
r2275 | #include "moc_abstractdomain_p.cpp" | ||
Michal Klocek
|
r439 | |||
Michal Klocek
|
r131 | QTCOMMERCIALCHART_END_NAMESPACE | ||