domain.cpp
302 lines
| 7.3 KiB
| text/x-c
|
CppLexer
/ src / domain.cpp
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$ | ||||
** | ||||
****************************************************************************/ | ||||
Michal Klocek
|
r131 | #include "domain_p.h" | ||
Michal Klocek
|
r678 | #include <cmath> | ||
Michal Klocek
|
r131 | |||
QTCOMMERCIALCHART_BEGIN_NAMESPACE | ||||
Tero Ahola
|
r1163 | Domain::Domain(QObject* parent) : QObject(parent), | ||
m_minX(0), | ||||
m_maxX(0), | ||||
m_minY(0), | ||||
m_maxY(0), | ||||
m_tickXCount(5), | ||||
m_tickYCount(5), | ||||
m_niceXNumbers(false), | ||||
m_niceYNumbers(false) | ||||
Michal Klocek
|
r131 | { | ||
} | ||||
Domain::~Domain() | ||||
{ | ||||
} | ||||
Michal Klocek
|
r439 | void Domain::setRange(qreal minX, qreal maxX, qreal minY, qreal maxY) | ||
{ | ||||
Michal Klocek
|
r678 | setRange(minX, maxX, minY, maxY,m_tickXCount,m_tickYCount); | ||
} | ||||
Michal Klocek
|
r439 | |||
Michal Klocek
|
r678 | void Domain::setRange(qreal minX, qreal maxX, qreal minY, qreal maxY,int tickXCount,int tickYCount) | ||
{ | ||||
Michal Klocek
|
r1078 | bool axisXChanged = false; | ||
bool axisYChanged = false; | ||||
Michal Klocek
|
r439 | |||
Michal Klocek
|
r678 | if(m_tickXCount!=tickXCount) { | ||
m_tickXCount=tickXCount; | ||||
Michal Klocek
|
r1078 | axisXChanged=true; | ||
Michal Klocek
|
r678 | } | ||
Michal Klocek
|
r439 | |||
Michal Klocek
|
r695 | if(m_tickYCount!=tickYCount) { | ||
Michal Klocek
|
r679 | m_tickYCount=tickYCount; | ||
Michal Klocek
|
r1078 | axisYChanged=true; | ||
Michal Klocek
|
r678 | } | ||
Jani Honkonen
|
r768 | if (!qFuzzyIsNull(m_minX - minX) || !qFuzzyIsNull(m_maxX - maxX)) { | ||
Michal Klocek
|
r1078 | if(m_niceXNumbers) looseNiceNumbers(minX, maxX, m_tickXCount); | ||
Michal Klocek
|
r678 | m_minX=minX; | ||
m_maxX=maxX; | ||||
Michal Klocek
|
r1078 | axisXChanged=true; | ||
Michal Klocek
|
r678 | } | ||
Jani Honkonen
|
r768 | if (!qFuzzyIsNull(m_minY - minY) || !qFuzzyIsNull(m_maxY - maxY)) { | ||
Michal Klocek
|
r1078 | if(m_niceYNumbers) looseNiceNumbers(minY, maxY, m_tickYCount); | ||
Michal Klocek
|
r678 | m_minY=minY; | ||
m_maxY=maxY; | ||||
Michal Klocek
|
r1078 | axisYChanged=true; | ||
Michal Klocek
|
r678 | } | ||
Michal Klocek
|
r1078 | if(axisXChanged || axisYChanged) { | ||
Michal Klocek
|
r678 | emit this->domainChanged(m_minX, m_maxX, m_minY, m_maxY); | ||
} | ||||
Michal Klocek
|
r1078 | if(axisXChanged) { | ||
Michal Klocek
|
r678 | emit rangeXChanged(minX,maxX, m_tickXCount); | ||
} | ||||
Michal Klocek
|
r1078 | if(axisYChanged) { | ||
Michal Klocek
|
r678 | emit rangeYChanged(minY,maxY, m_tickYCount); | ||
} | ||||
Michal Klocek
|
r439 | } | ||
Michal Klocek
|
r678 | |||
Michal Klocek
|
r439 | void Domain::setRangeX(qreal min, qreal max) | ||
{ | ||||
setRange(min,max,m_minY, m_maxY); | ||||
} | ||||
Michal Klocek
|
r679 | |||
void Domain::setRangeX(qreal min, qreal max, int tickCount) | ||||
{ | ||||
setRange(min,max,m_minY, m_maxY,tickCount,m_tickYCount); | ||||
} | ||||
Michal Klocek
|
r439 | void Domain::setRangeY(qreal min, qreal max) | ||
{ | ||||
Michal Klocek
|
r678 | setRange(m_minX, m_maxX, min, max); | ||
Michal Klocek
|
r439 | } | ||
Michal Klocek
|
r679 | void Domain::setRangeY(qreal min, qreal max,int tickCount) | ||
{ | ||||
setRange(m_minX, m_maxX, min, max,m_tickXCount,tickCount); | ||||
} | ||||
Michal Klocek
|
r439 | void Domain::setMinX(qreal min) | ||
{ | ||||
Michal Klocek
|
r678 | setRange(min, m_maxX, m_minY, m_maxY); | ||
Michal Klocek
|
r439 | } | ||
void Domain::setMaxX(qreal max) | ||||
{ | ||||
Michal Klocek
|
r678 | setRange(m_minX, max, m_minY, m_maxY); | ||
Michal Klocek
|
r439 | } | ||
void Domain::setMinY(qreal min) | ||||
{ | ||||
Michal Klocek
|
r678 | setRange(m_minX, m_maxX, min, m_maxY); | ||
Michal Klocek
|
r439 | } | ||
void Domain::setMaxY(qreal max) | ||||
{ | ||||
setRange(m_minX, m_maxX, m_minY, max); | ||||
} | ||||
Michal Klocek
|
r131 | qreal Domain::spanX() const | ||
{ | ||||
Michal Klocek
|
r439 | Q_ASSERT(m_maxX >= m_minX); | ||
return m_maxX - m_minX; | ||||
Michal Klocek
|
r131 | } | ||
qreal Domain::spanY() const | ||||
{ | ||||
Michal Klocek
|
r439 | Q_ASSERT(m_maxY >= m_minY); | ||
return m_maxY - m_minY; | ||||
Michal Klocek
|
r131 | } | ||
Michal Klocek
|
r388 | bool Domain::isEmpty() const | ||
{ | ||||
Jani Honkonen
|
r768 | return qFuzzyIsNull(spanX()) || qFuzzyIsNull(spanY()); | ||
Michal Klocek
|
r388 | } | ||
Michal Klocek
|
r439 | void Domain::zoomIn(const QRectF& rect, const QSizeF& size) | ||
Michal Klocek
|
r131 | { | ||
Michal Klocek
|
r439 | qreal dx = spanX() / size.width(); | ||
qreal dy = spanY() / size.height(); | ||||
Michal Klocek
|
r131 | |||
Michal Klocek
|
r1078 | qreal maxX = m_maxX; | ||
qreal minX = m_minX; | ||||
qreal minY = m_minY; | ||||
qreal maxY = m_maxY; | ||||
Michal Klocek
|
r439 | |||
Michal Klocek
|
r1078 | maxX = minX + dx * rect.right(); | ||
minX = minX + dx * rect.left(); | ||||
minY = maxY - dy * rect.bottom(); | ||||
maxY = maxY - dy * rect.top(); | ||||
Michal Klocek
|
r678 | |||
Michal Klocek
|
r1078 | int tickXCount = m_tickXCount; | ||
int tickYCount = m_tickYCount; | ||||
if(m_niceXNumbers) { | ||||
looseNiceNumbers(minX, maxX, tickXCount); | ||||
} | ||||
if(m_niceYNumbers) { | ||||
looseNiceNumbers(minY, maxY, tickYCount); | ||||
} | ||||
setRange(minX,maxX,minY,maxY,tickXCount,tickYCount); | ||||
Michal Klocek
|
r439 | } | ||
void Domain::zoomOut(const QRectF& rect, const QSizeF& size) | ||||
{ | ||||
qreal dx = spanX() / rect.width(); | ||||
qreal dy = spanY() / rect.height(); | ||||
Michal Klocek
|
r131 | |||
Michal Klocek
|
r1078 | qreal maxX = m_maxX; | ||
qreal minX = m_minX; | ||||
qreal minY = m_minY; | ||||
qreal maxY = m_maxY; | ||||
Michal Klocek
|
r131 | |||
Michal Klocek
|
r1078 | minX = maxX - dx * rect.right(); | ||
maxX = minX + dx * size.width(); | ||||
maxY = minY + dy * rect.bottom(); | ||||
minY = maxY - dy * size.height(); | ||||
int tickXCount = m_tickXCount; | ||||
int tickYCount = m_tickYCount; | ||||
Michal Klocek
|
r678 | |||
Michal Klocek
|
r1078 | if(m_niceXNumbers) { | ||
looseNiceNumbers(minX, maxX, tickXCount); | ||||
} | ||||
if(m_niceYNumbers) { | ||||
looseNiceNumbers(minY, maxY, tickYCount); | ||||
} | ||||
setRange(minX,maxX,minY,maxY,tickXCount,tickYCount); | ||||
Michal Klocek
|
r531 | } | ||
void Domain::move(int dx,int dy,const QSizeF& size) | ||||
{ | ||||
qreal x = spanX() / size.width(); | ||||
qreal y = spanY() / size.height(); | ||||
Michal Klocek
|
r1078 | qreal maxX = m_maxX; | ||
qreal minX = m_minX; | ||||
qreal minY = m_minY; | ||||
qreal maxY = m_maxY; | ||||
Michal Klocek
|
r678 | if(dx!=0) { | ||
Michal Klocek
|
r1078 | minX = minX + x * dx; | ||
maxX = maxX + x * dx; | ||||
Michal Klocek
|
r531 | } | ||
Michal Klocek
|
r678 | if(dy!=0) { | ||
Michal Klocek
|
r1078 | minY = minY + y * dy; | ||
maxY = maxY + y * dy; | ||||
Michal Klocek
|
r531 | } | ||
Michal Klocek
|
r1078 | setRange(minX,maxX,minY,maxY); | ||
Michal Klocek
|
r439 | } | ||
Michal Klocek
|
r687 | void Domain::handleAxisXChanged(qreal min,qreal max,int tickXCount,bool niceNumbers) | ||
Michal Klocek
|
r678 | { | ||
Michal Klocek
|
r1078 | if (m_niceXNumbers != niceNumbers) { | ||
m_niceXNumbers = niceNumbers; | ||||
Michal Klocek
|
r898 | //force recalculation | ||
m_minX = 0; | ||||
m_maxX = 0; | ||||
} | ||||
Michal Klocek
|
r678 | setRange(min,max,m_minY, m_maxY,tickXCount,m_tickYCount); | ||
} | ||||
Michal Klocek
|
r687 | void Domain::handleAxisYChanged(qreal min,qreal max,int tickYCount,bool niceNumbers) | ||
Michal Klocek
|
r554 | { | ||
Michal Klocek
|
r1078 | if (m_niceYNumbers != niceNumbers) { | ||
m_niceYNumbers = niceNumbers; | ||||
Michal Klocek
|
r898 | //force recalculation | ||
m_minY = 0; | ||||
m_maxY = 0; | ||||
} | ||||
Michal Klocek
|
r678 | setRange(m_minX, m_maxX, min, max,m_tickXCount,tickYCount); | ||
Michal Klocek
|
r554 | } | ||
Michal Klocek
|
r678 | //algorithm defined by Paul S.Heckbert GraphicalGems I | ||
Michal Klocek
|
r1078 | void Domain::looseNiceNumbers(qreal &min, qreal &max, int &ticksCount) const | ||
Michal Klocek
|
r439 | { | ||
Michal Klocek
|
r678 | qreal range = niceNumber(max-min,true); //range with ceiling | ||
qreal step = niceNumber(range/(ticksCount-1),false); | ||||
min = floor(min/step); | ||||
max = ceil(max/step); | ||||
ticksCount = int(max-min) +1; | ||||
min*=step; | ||||
max*=step; | ||||
Michal Klocek
|
r554 | } | ||
Michal Klocek
|
r678 | //nice numbers can be expressed as form of 1*10^n, 2* 10^n or 5*10^n | ||
Michal Klocek
|
r1078 | qreal Domain::niceNumber(qreal x,bool ceiling) const | ||
Michal Klocek
|
r554 | { | ||
Michal Klocek
|
r678 | qreal z = pow(10,floor(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; | ||||
Michal Klocek
|
r554 | } | ||
Michal Klocek
|
r678 | return q*z; | ||
Michal Klocek
|
r131 | } | ||
Michal Klocek
|
r220 | |||
Michal Klocek
|
r1066 | |||
bool QTCOMMERCIALCHART_AUTOTEST_EXPORT operator== (const Domain &domain1, const Domain &domain2) | ||||
Michal Klocek
|
r220 | { | ||
Jani Honkonen
|
r768 | 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 | |||
bool QTCOMMERCIALCHART_AUTOTEST_EXPORT operator!= (const Domain &domain1, const Domain &domain2) | ||||
Michal Klocek
|
r220 | { | ||
return !(domain1 == domain2); | ||||
} | ||||
Michal Klocek
|
r1066 | |||
QDebug QTCOMMERCIALCHART_AUTOTEST_EXPORT operator<<(QDebug dbg, const Domain &domain) | ||||
Michal Klocek
|
r222 | { | ||
Michal Klocek
|
r679 | dbg.nospace() << "Domain("<<domain.m_minX<<','<<domain.m_maxX<<','<<domain.m_minY<<','<<domain.m_maxY<<')' << domain.m_tickXCount << "," << domain.m_tickYCount ; | ||
Michal Klocek
|
r222 | return dbg.maybeSpace(); | ||
} | ||||
Michal Klocek
|
r439 | #include "moc_domain_p.cpp" | ||
Michal Klocek
|
r131 | QTCOMMERCIALCHART_END_NAMESPACE | ||