| 1 | /**************************************************************************** |
| 2 | ** |
| 3 | ** Copyright (C) 2016 The Qt Company Ltd. |
| 4 | ** Contact: https://www.qt.io/licensing/ |
| 5 | ** |
| 6 | ** This file is part of the Qt Charts module of the Qt Toolkit. |
| 7 | ** |
| 8 | ** $QT_BEGIN_LICENSE:GPL$ |
| 9 | ** Commercial License Usage |
| 10 | ** Licensees holding valid commercial Qt licenses may use this file in |
| 11 | ** accordance with the commercial license agreement provided with the |
| 12 | ** Software or, alternatively, in accordance with the terms contained in |
| 13 | ** a written agreement between you and The Qt Company. For licensing terms |
| 14 | ** and conditions see https://www.qt.io/terms-conditions. For further |
| 15 | ** information use the contact form at https://www.qt.io/contact-us. |
| 16 | ** |
| 17 | ** GNU General Public License Usage |
| 18 | ** Alternatively, this file may be used under the terms of the GNU |
| 19 | ** General Public License version 3 or (at your option) any later version |
| 20 | ** approved by the KDE Free Qt Foundation. The licenses are as published by |
| 21 | ** the Free Software Foundation and appearing in the file LICENSE.GPL3 |
| 22 | ** included in the packaging of this file. Please review the following |
| 23 | ** information to ensure the GNU General Public License requirements will |
| 24 | ** be met: https://www.gnu.org/licenses/gpl-3.0.html. |
| 25 | ** |
| 26 | ** $QT_END_LICENSE$ |
| 27 | ** |
| 28 | ****************************************************************************/ |
| 29 | |
| 30 | #include <private/logxydomain_p.h> |
| 31 | #include <private/qabstractaxis_p.h> |
| 32 | #include <QtCharts/QLogValueAxis> |
| 33 | #include <QtCore/QtMath> |
| 34 | #include <cmath> |
| 35 | |
| 36 | QT_CHARTS_BEGIN_NAMESPACE |
| 37 | |
| 38 | LogXYDomain::LogXYDomain(QObject *parent) |
| 39 | : AbstractDomain(parent), |
| 40 | m_logLeftX(0), |
| 41 | m_logRightX(1), |
| 42 | m_logBaseX(10) |
| 43 | { |
| 44 | } |
| 45 | |
| 46 | LogXYDomain::~LogXYDomain() |
| 47 | { |
| 48 | } |
| 49 | |
| 50 | void LogXYDomain::setRange(qreal minX, qreal maxX, qreal minY, qreal maxY) |
| 51 | { |
| 52 | bool axisXChanged = false; |
| 53 | bool axisYChanged = false; |
| 54 | |
| 55 | adjustLogDomainRanges(min&: minX, max&: maxX); |
| 56 | |
| 57 | if (!qFuzzyCompare(p1: m_minX, p2: minX) || !qFuzzyCompare(p1: m_maxX, p2: maxX)) { |
| 58 | m_minX = minX; |
| 59 | m_maxX = maxX; |
| 60 | axisXChanged = true; |
| 61 | qreal logMinX = std::log10(x: m_minX) / std::log10(x: m_logBaseX); |
| 62 | qreal logMaxX = std::log10(x: m_maxX) / std::log10(x: m_logBaseX); |
| 63 | m_logLeftX = logMinX < logMaxX ? logMinX : logMaxX; |
| 64 | m_logRightX = logMinX > logMaxX ? logMinX : logMaxX; |
| 65 | if(!m_signalsBlocked) |
| 66 | emit rangeHorizontalChanged(min: m_minX, max: m_maxX); |
| 67 | } |
| 68 | |
| 69 | if (!qFuzzyIsNull(d: m_minY - minY) || !qFuzzyIsNull(d: m_maxY - maxY)) { |
| 70 | m_minY = minY; |
| 71 | m_maxY = maxY; |
| 72 | axisYChanged = true; |
| 73 | if (!m_signalsBlocked) |
| 74 | emit rangeVerticalChanged(min: m_minY, max: m_maxY); |
| 75 | } |
| 76 | |
| 77 | if (axisXChanged || axisYChanged) |
| 78 | emit updated(); |
| 79 | } |
| 80 | |
| 81 | void LogXYDomain::zoomIn(const QRectF &rect) |
| 82 | { |
| 83 | storeZoomReset(); |
| 84 | QRectF fixedRect = fixZoomRect(rect); |
| 85 | qreal logLeftX = fixedRect.left() * (m_logRightX - m_logLeftX) / m_size.width() + m_logLeftX; |
| 86 | qreal logRightX = fixedRect.right() * (m_logRightX - m_logLeftX) / m_size.width() + m_logLeftX; |
| 87 | qreal leftX = qPow(x: m_logBaseX, y: logLeftX); |
| 88 | qreal rightX = qPow(x: m_logBaseX, y: logRightX); |
| 89 | qreal minX = leftX < rightX ? leftX : rightX; |
| 90 | qreal maxX = leftX > rightX ? leftX : rightX; |
| 91 | |
| 92 | qreal dy = spanY() / m_size.height(); |
| 93 | qreal minY = m_minY; |
| 94 | qreal maxY = m_maxY; |
| 95 | |
| 96 | minY = maxY - dy * fixedRect.bottom(); |
| 97 | maxY = maxY - dy * fixedRect.top(); |
| 98 | |
| 99 | setRange(minX, maxX, minY, maxY); |
| 100 | } |
| 101 | |
| 102 | void LogXYDomain::zoomOut(const QRectF &rect) |
| 103 | { |
| 104 | storeZoomReset(); |
| 105 | QRectF fixedRect = fixZoomRect(rect); |
| 106 | const qreal factorX = m_size.width() / fixedRect.width(); |
| 107 | |
| 108 | qreal logLeftX = m_logLeftX + (m_logRightX - m_logLeftX) / 2 * (1 - factorX); |
| 109 | qreal logRightX = m_logLeftX + (m_logRightX - m_logLeftX) / 2 * (1 + factorX); |
| 110 | qreal leftX = qPow(x: m_logBaseX, y: logLeftX); |
| 111 | qreal rightX = qPow(x: m_logBaseX, y: logRightX); |
| 112 | qreal minX = leftX < rightX ? leftX : rightX; |
| 113 | qreal maxX = leftX > rightX ? leftX : rightX; |
| 114 | |
| 115 | qreal dy = spanY() / fixedRect.height(); |
| 116 | qreal minY = m_minY; |
| 117 | qreal maxY = m_maxY; |
| 118 | |
| 119 | maxY = minY + dy * fixedRect.bottom(); |
| 120 | minY = maxY - dy * m_size.height(); |
| 121 | |
| 122 | if (logRightX > m_size.width()) |
| 123 | return; |
| 124 | |
| 125 | if (qIsInf(d: maxX)) |
| 126 | return; |
| 127 | |
| 128 | setRange(minX, maxX, minY, maxY); |
| 129 | } |
| 130 | |
| 131 | void LogXYDomain::move(qreal dx, qreal dy) |
| 132 | { |
| 133 | if (m_reverseX) |
| 134 | dx = -dx; |
| 135 | if (m_reverseY) |
| 136 | dy = -dy; |
| 137 | |
| 138 | qreal stepX = dx * (m_logRightX - m_logLeftX) / m_size.width(); |
| 139 | qreal leftX = qPow(x: m_logBaseX, y: m_logLeftX + stepX); |
| 140 | qreal rightX = qPow(x: m_logBaseX, y: m_logRightX + stepX); |
| 141 | qreal minX = leftX < rightX ? leftX : rightX; |
| 142 | qreal maxX = leftX > rightX ? leftX : rightX; |
| 143 | |
| 144 | qreal y = spanY() / m_size.height(); |
| 145 | qreal minY = m_minY; |
| 146 | qreal maxY = m_maxY; |
| 147 | |
| 148 | if (dy != 0) { |
| 149 | minY = minY + y * dy; |
| 150 | maxY = maxY + y * dy; |
| 151 | } |
| 152 | setRange(minX, maxX, minY, maxY); |
| 153 | } |
| 154 | |
| 155 | QPointF LogXYDomain::calculateGeometryPoint(const QPointF &point, bool &ok) const |
| 156 | { |
| 157 | const qreal deltaX = m_size.width() / (m_logRightX - m_logLeftX); |
| 158 | const qreal deltaY = m_size.height() / (m_maxY - m_minY); |
| 159 | |
| 160 | qreal x(0); |
| 161 | qreal y = (point.y() - m_minY) * deltaY; |
| 162 | if (!m_reverseY) |
| 163 | y = m_size.height() - y; |
| 164 | if (point.x() > 0) { |
| 165 | x = ((std::log10(x: point.x()) / std::log10(x: m_logBaseX)) - m_logLeftX) * deltaX; |
| 166 | if (m_reverseX) |
| 167 | x = m_size.width() - x; |
| 168 | ok = true; |
| 169 | } else { |
| 170 | x = 0; |
| 171 | qWarning() << "Logarithms of zero and negative values are undefined." ; |
| 172 | ok = false; |
| 173 | } |
| 174 | return QPointF(x, y); |
| 175 | } |
| 176 | |
| 177 | QVector<QPointF> LogXYDomain::calculateGeometryPoints(const QVector<QPointF> &vector) const |
| 178 | { |
| 179 | const qreal deltaX = m_size.width() / (m_logRightX - m_logLeftX); |
| 180 | const qreal deltaY = m_size.height() / (m_maxY - m_minY); |
| 181 | |
| 182 | QVector<QPointF> result; |
| 183 | result.resize(size: vector.count()); |
| 184 | |
| 185 | for (int i = 0; i < vector.count(); ++i) { |
| 186 | if (vector[i].x() > 0) { |
| 187 | qreal x = ((std::log10(x: vector[i].x()) / std::log10(x: m_logBaseX)) - m_logLeftX) * deltaX; |
| 188 | if (m_reverseX) |
| 189 | x = m_size.width() - x; |
| 190 | qreal y = (vector[i].y() - m_minY) * deltaY; |
| 191 | if (!m_reverseY) |
| 192 | y = m_size.height() - y; |
| 193 | result[i].setX(x); |
| 194 | result[i].setY(y); |
| 195 | } else { |
| 196 | qWarning() << "Logarithms of zero and negative values are undefined." ; |
| 197 | return QVector<QPointF>(); |
| 198 | } |
| 199 | |
| 200 | } |
| 201 | return result; |
| 202 | } |
| 203 | |
| 204 | QPointF LogXYDomain::calculateDomainPoint(const QPointF &point) const |
| 205 | { |
| 206 | const qreal deltaX = m_size.width() / (m_logRightX - m_logLeftX); |
| 207 | const qreal deltaY = m_size.height() / (m_maxY - m_minY); |
| 208 | qreal x = m_reverseX ? (m_size.width() - point.x()) : point.x(); |
| 209 | x = qPow(x: m_logBaseX, y: m_logLeftX + x / deltaX); |
| 210 | qreal y = m_reverseY ? point.y() : (m_size.height() - point.y()); |
| 211 | y /= deltaY; |
| 212 | y += m_minY; |
| 213 | return QPointF(x, y); |
| 214 | } |
| 215 | |
| 216 | bool LogXYDomain::attachAxis(QAbstractAxis *axis) |
| 217 | { |
| 218 | AbstractDomain::attachAxis(axis); |
| 219 | QLogValueAxis *logAxis = qobject_cast<QLogValueAxis *>(object: axis); |
| 220 | |
| 221 | if (logAxis && logAxis->orientation() == Qt::Horizontal) { |
| 222 | QObject::connect(sender: logAxis, SIGNAL(baseChanged(qreal)), receiver: this, SLOT(handleHorizontalAxisBaseChanged(qreal))); |
| 223 | handleHorizontalAxisBaseChanged(baseX: logAxis->base()); |
| 224 | } |
| 225 | |
| 226 | return true; |
| 227 | } |
| 228 | |
| 229 | bool LogXYDomain::detachAxis(QAbstractAxis *axis) |
| 230 | { |
| 231 | AbstractDomain::detachAxis(axis); |
| 232 | QLogValueAxis *logAxis = qobject_cast<QLogValueAxis *>(object: axis); |
| 233 | |
| 234 | if (logAxis && logAxis->orientation() == Qt::Horizontal) |
| 235 | QObject::disconnect(sender: logAxis, SIGNAL(baseChanged(qreal)), receiver: this, SLOT(handleHorizontalAxisBaseChanged(qreal))); |
| 236 | |
| 237 | return true; |
| 238 | } |
| 239 | |
| 240 | void LogXYDomain::handleHorizontalAxisBaseChanged(qreal baseX) |
| 241 | { |
| 242 | m_logBaseX = baseX; |
| 243 | qreal logMinX = std::log10(x: m_minX) / std::log10(x: m_logBaseX); |
| 244 | qreal logMaxX = std::log10(x: m_maxX) / std::log10(x: m_logBaseX); |
| 245 | m_logLeftX = logMinX < logMaxX ? logMinX : logMaxX; |
| 246 | m_logRightX = logMinX > logMaxX ? logMinX : logMaxX; |
| 247 | emit updated(); |
| 248 | } |
| 249 | |
| 250 | // operators |
| 251 | |
| 252 | bool Q_AUTOTEST_EXPORT operator== (const LogXYDomain &domain1, const LogXYDomain &domain2) |
| 253 | { |
| 254 | return (qFuzzyIsNull(d: domain1.m_maxX - domain2.m_maxX) |
| 255 | && qFuzzyIsNull(d: domain1.m_maxY - domain2.m_maxY) |
| 256 | && qFuzzyIsNull(d: domain1.m_minX - domain2.m_minX) |
| 257 | && qFuzzyIsNull(d: domain1.m_minY - domain2.m_minY)); |
| 258 | } |
| 259 | |
| 260 | |
| 261 | bool Q_AUTOTEST_EXPORT operator!= (const LogXYDomain &domain1, const LogXYDomain &domain2) |
| 262 | { |
| 263 | return !(domain1 == domain2); |
| 264 | } |
| 265 | |
| 266 | |
| 267 | QDebug Q_AUTOTEST_EXPORT operator<<(QDebug dbg, const LogXYDomain &domain) |
| 268 | { |
| 269 | #ifdef QT_NO_TEXTSTREAM |
| 270 | Q_UNUSED(domain) |
| 271 | #else |
| 272 | dbg.nospace() << "AbstractDomain(" << domain.m_minX << ',' << domain.m_maxX << ',' << domain.m_minY << ',' << domain.m_maxY << ')' << domain.m_size; |
| 273 | #endif |
| 274 | return dbg.maybeSpace(); |
| 275 | } |
| 276 | |
| 277 | QT_CHARTS_END_NAMESPACE |
| 278 | |
| 279 | #include "moc_logxydomain_p.cpp" |
| 280 | |