| 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/logxlogypolardomain_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 | LogXLogYPolarDomain::LogXLogYPolarDomain(QObject *parent) | 
| 39 |     : PolarDomain(parent), | 
| 40 |       m_logLeftX(0), | 
| 41 |       m_logRightX(1), | 
| 42 |       m_logBaseX(10), | 
| 43 |       m_logInnerY(0), | 
| 44 |       m_logOuterY(1), | 
| 45 |       m_logBaseY(10) | 
| 46 | { | 
| 47 | } | 
| 48 |  | 
| 49 | LogXLogYPolarDomain::~LogXLogYPolarDomain() | 
| 50 | { | 
| 51 | } | 
| 52 |  | 
| 53 | void LogXLogYPolarDomain::setRange(qreal minX, qreal maxX, qreal minY, qreal maxY) | 
| 54 | { | 
| 55 |     bool axisXChanged = false; | 
| 56 |     bool axisYChanged = false; | 
| 57 |  | 
| 58 |     adjustLogDomainRanges(min&: minX, max&: maxX); | 
| 59 |     adjustLogDomainRanges(min&: minY, max&: maxY); | 
| 60 |  | 
| 61 |     if (!qFuzzyCompare(p1: m_minX, p2: minX) || !qFuzzyCompare(p1: m_maxX, p2: maxX)) { | 
| 62 |         m_minX = minX; | 
| 63 |         m_maxX = maxX; | 
| 64 |         axisXChanged = true; | 
| 65 |         qreal logMinX = std::log10(x: m_minX) / std::log10(x: m_logBaseX); | 
| 66 |         qreal logMaxX = std::log10(x: m_maxX) / std::log10(x: m_logBaseX); | 
| 67 |         m_logLeftX = logMinX < logMaxX ? logMinX : logMaxX; | 
| 68 |         m_logRightX = logMinX > logMaxX ? logMinX : logMaxX; | 
| 69 |         if (!m_signalsBlocked) | 
| 70 |             emit rangeHorizontalChanged(min: m_minX, max: m_maxX); | 
| 71 |     } | 
| 72 |  | 
| 73 |     if (!qFuzzyIsNull(d: m_minY - minY) || !qFuzzyIsNull(d: m_maxY - maxY)) { | 
| 74 |         m_minY = minY; | 
| 75 |         m_maxY = maxY; | 
| 76 |         axisYChanged = true; | 
| 77 |         qreal logMinY = std::log10(x: m_minY) / std::log10(x: m_logBaseY); | 
| 78 |         qreal logMaxY = std::log10(x: m_maxY) / std::log10(x: m_logBaseY); | 
| 79 |         m_logInnerY = logMinY < logMaxY ? logMinY : logMaxY; | 
| 80 |         m_logOuterY = logMinY > logMaxY ? logMinY : logMaxY; | 
| 81 |         if (!m_signalsBlocked) | 
| 82 |             emit rangeVerticalChanged(min: m_minY, max: m_maxY); | 
| 83 |     } | 
| 84 |  | 
| 85 |     if (axisXChanged || axisYChanged) | 
| 86 |         emit updated(); | 
| 87 | } | 
| 88 |  | 
| 89 | void LogXLogYPolarDomain::zoomIn(const QRectF &rect) | 
| 90 | { | 
| 91 |     storeZoomReset(); | 
| 92 |     qreal logLeftX = rect.left() * (m_logRightX - m_logLeftX) / m_size.width() + m_logLeftX; | 
| 93 |     qreal logRightX = rect.right() * (m_logRightX - m_logLeftX) / m_size.width() + m_logLeftX; | 
| 94 |     qreal leftX = qPow(x: m_logBaseX, y: logLeftX); | 
| 95 |     qreal rightX = qPow(x: m_logBaseX, y: logRightX); | 
| 96 |     qreal minX = leftX < rightX ? leftX : rightX; | 
| 97 |     qreal maxX = leftX > rightX ? leftX : rightX; | 
| 98 |  | 
| 99 |     qreal logLeftY = m_logOuterY - rect.bottom() * (m_logOuterY - m_logInnerY) / m_size.height(); | 
| 100 |     qreal logRightY = m_logOuterY - rect.top() * (m_logOuterY - m_logInnerY) / m_size.height(); | 
| 101 |     qreal leftY = qPow(x: m_logBaseY, y: logLeftY); | 
| 102 |     qreal rightY = qPow(x: m_logBaseY, y: logRightY); | 
| 103 |     qreal minY = leftY < rightY ? leftY : rightY; | 
| 104 |     qreal maxY = leftY > rightY ? leftY : rightY; | 
| 105 |  | 
| 106 |     setRange(minX, maxX, minY, maxY); | 
| 107 | } | 
| 108 |  | 
| 109 | void LogXLogYPolarDomain::zoomOut(const QRectF &rect) | 
| 110 | { | 
| 111 |     storeZoomReset(); | 
| 112 |     const qreal factorX = m_size.width() / rect.width(); | 
| 113 |  | 
| 114 |     qreal logLeftX = m_logLeftX + (m_logRightX - m_logLeftX) / 2.0 * (1.0 - factorX); | 
| 115 |     qreal logRIghtX = m_logLeftX + (m_logRightX - m_logLeftX) / 2.0 * (1.0 + factorX); | 
| 116 |     qreal leftX = qPow(x: m_logBaseX, y: logLeftX); | 
| 117 |     qreal rightX = qPow(x: m_logBaseX, y: logRIghtX); | 
| 118 |     qreal minX = leftX < rightX ? leftX : rightX; | 
| 119 |     qreal maxX = leftX > rightX ? leftX : rightX; | 
| 120 |  | 
| 121 |     const qreal factorY = m_size.height() / rect.height(); | 
| 122 |     qreal newLogMinY = m_logInnerY + (m_logOuterY - m_logInnerY) / 2.0 * (1.0 - factorY); | 
| 123 |     qreal newLogMaxY = m_logInnerY + (m_logOuterY - m_logInnerY) / 2.0 * (1.0 + factorY); | 
| 124 |     qreal leftY = qPow(x: m_logBaseY, y: newLogMinY); | 
| 125 |     qreal rightY = qPow(x: m_logBaseY, y: newLogMaxY); | 
| 126 |     qreal minY = leftY < rightY ? leftY : rightY; | 
| 127 |     qreal maxY = leftY > rightY ? leftY : rightY; | 
| 128 |  | 
| 129 |     setRange(minX, maxX, minY, maxY); | 
| 130 | } | 
| 131 |  | 
| 132 | void LogXLogYPolarDomain::move(qreal dx, qreal dy) | 
| 133 | { | 
| 134 |     qreal stepX = dx * (m_logRightX - m_logLeftX) / m_size.width(); | 
| 135 |     qreal leftX = qPow(x: m_logBaseX, y: m_logLeftX + stepX); | 
| 136 |     qreal rightX = qPow(x: m_logBaseX, y: m_logRightX + stepX); | 
| 137 |     qreal minX = leftX < rightX ? leftX : rightX; | 
| 138 |     qreal maxX = leftX > rightX ? leftX : rightX; | 
| 139 |  | 
| 140 |     qreal stepY = dy * (m_logOuterY - m_logInnerY) / m_radius; | 
| 141 |     qreal leftY = qPow(x: m_logBaseY, y: m_logInnerY + stepY); | 
| 142 |     qreal rightY = qPow(x: m_logBaseY, y: m_logOuterY + stepY); | 
| 143 |     qreal minY = leftY < rightY ? leftY : rightY; | 
| 144 |     qreal maxY = leftY > rightY ? leftY : rightY; | 
| 145 |  | 
| 146 |     setRange(minX, maxX, minY, maxY); | 
| 147 | } | 
| 148 |  | 
| 149 | qreal LogXLogYPolarDomain::toAngularCoordinate(qreal value, bool &ok) const | 
| 150 | { | 
| 151 |     qreal retVal; | 
| 152 |     if (value <= 0) { | 
| 153 |         ok = false; | 
| 154 |         retVal = 0.0; | 
| 155 |     } else { | 
| 156 |         ok =  true; | 
| 157 |         const qreal tickSpan = 360.0 / qAbs(t: m_logRightX - m_logLeftX); | 
| 158 |         const qreal logValue = std::log10(x: value) / std::log10(x: m_logBaseX); | 
| 159 |         const qreal valueDelta = logValue - m_logLeftX; | 
| 160 |  | 
| 161 |         retVal = valueDelta * tickSpan; | 
| 162 |     } | 
| 163 |     return retVal; | 
| 164 | } | 
| 165 |  | 
| 166 | qreal LogXLogYPolarDomain::toRadialCoordinate(qreal value, bool &ok) const | 
| 167 | { | 
| 168 |     qreal retVal; | 
| 169 |     if (value <= 0) { | 
| 170 |         ok = false; | 
| 171 |         retVal = 0.0; | 
| 172 |     } else { | 
| 173 |         ok =  true; | 
| 174 |         const qreal tickSpan = m_radius / qAbs(t: m_logOuterY - m_logInnerY); | 
| 175 |         const qreal logValue = std::log10(x: value) / std::log10(x: m_logBaseY); | 
| 176 |         const qreal valueDelta = logValue - m_logInnerY; | 
| 177 |  | 
| 178 |         retVal = valueDelta * tickSpan; | 
| 179 |  | 
| 180 |         if (retVal < 0.0) | 
| 181 |             retVal = 0.0; | 
| 182 |     } | 
| 183 |     return retVal; | 
| 184 | } | 
| 185 |  | 
| 186 | QPointF LogXLogYPolarDomain::calculateDomainPoint(const QPointF &point) const | 
| 187 | { | 
| 188 |     if (point == m_center) | 
| 189 |         return QPointF(0.0, m_minY); | 
| 190 |  | 
| 191 |     QLineF line(m_center, point); | 
| 192 |     qreal a = 90.0 - line.angle(); | 
| 193 |     if (a < 0.0) | 
| 194 |         a += 360.0; | 
| 195 |  | 
| 196 |     const qreal deltaX = 360.0 / qAbs(t: m_logRightX - m_logLeftX); | 
| 197 |     a = qPow(x: m_logBaseX, y: m_logLeftX + (a / deltaX)); | 
| 198 |  | 
| 199 |     const qreal deltaY = m_radius / qAbs(t: m_logOuterY - m_logInnerY); | 
| 200 |     qreal r = qPow(x: m_logBaseY, y: m_logInnerY + (line.length() / deltaY)); | 
| 201 |  | 
| 202 |     return QPointF(a, r); | 
| 203 | } | 
| 204 |  | 
| 205 | bool LogXLogYPolarDomain::attachAxis(QAbstractAxis *axis) | 
| 206 | { | 
| 207 |     AbstractDomain::attachAxis(axis); | 
| 208 |     QLogValueAxis *logAxis = qobject_cast<QLogValueAxis *>(object: axis); | 
| 209 |  | 
| 210 |     if (logAxis && logAxis->orientation() == Qt::Horizontal) { | 
| 211 |         QObject::connect(sender: logAxis, SIGNAL(baseChanged(qreal)), receiver: this, SLOT(handleHorizontalAxisBaseChanged(qreal))); | 
| 212 |         handleHorizontalAxisBaseChanged(baseX: logAxis->base()); | 
| 213 |     } else if (logAxis && logAxis->orientation() == Qt::Vertical){ | 
| 214 |         QObject::connect(sender: logAxis, SIGNAL(baseChanged(qreal)), receiver: this, SLOT(handleVerticalAxisBaseChanged(qreal))); | 
| 215 |         handleVerticalAxisBaseChanged(baseY: logAxis->base()); | 
| 216 |     } | 
| 217 |  | 
| 218 |     return true; | 
| 219 | } | 
| 220 |  | 
| 221 | bool LogXLogYPolarDomain::detachAxis(QAbstractAxis *axis) | 
| 222 | { | 
| 223 |     AbstractDomain::detachAxis(axis); | 
| 224 |     QLogValueAxis *logAxis = qobject_cast<QLogValueAxis *>(object: axis); | 
| 225 |  | 
| 226 |     if (logAxis && logAxis->orientation() == Qt::Horizontal) | 
| 227 |         QObject::disconnect(sender: logAxis, SIGNAL(baseChanged(qreal)), receiver: this, SLOT(handleHorizontalAxisBaseChanged(qreal))); | 
| 228 |     else if (logAxis && logAxis->orientation() == Qt::Vertical) | 
| 229 |         QObject::disconnect(sender: logAxis, SIGNAL(baseChanged(qreal)), receiver: this, SLOT(handleVerticalAxisBaseChanged(qreal))); | 
| 230 |  | 
| 231 |     return true; | 
| 232 | } | 
| 233 |  | 
| 234 | void LogXLogYPolarDomain::handleHorizontalAxisBaseChanged(qreal baseX) | 
| 235 | { | 
| 236 |     m_logBaseX = baseX; | 
| 237 |     qreal logMinX = std::log10(x: m_minX) / std::log10(x: m_logBaseX); | 
| 238 |     qreal logMaxX = std::log10(x: m_maxX) / std::log10(x: m_logBaseX); | 
| 239 |     m_logLeftX = logMinX < logMaxX ? logMinX : logMaxX; | 
| 240 |     m_logRightX = logMinX > logMaxX ? logMinX : logMaxX; | 
| 241 |     emit updated(); | 
| 242 | } | 
| 243 |  | 
| 244 | void LogXLogYPolarDomain::handleVerticalAxisBaseChanged(qreal baseY) | 
| 245 | { | 
| 246 |     m_logBaseY = baseY; | 
| 247 |     qreal logMinY = std::log10(x: m_minY) / std::log10(x: m_logBaseY); | 
| 248 |     qreal logMaxY = std::log10(x: m_maxY) / std::log10(x: m_logBaseY); | 
| 249 |     m_logInnerY = logMinY < logMaxY ? logMinY : logMaxY; | 
| 250 |     m_logOuterY = logMinY > logMaxY ? logMinY : logMaxY; | 
| 251 |     emit updated(); | 
| 252 | } | 
| 253 |  | 
| 254 | // operators | 
| 255 |  | 
| 256 | bool Q_AUTOTEST_EXPORT operator== (const LogXLogYPolarDomain &domain1, const LogXLogYPolarDomain &domain2) | 
| 257 | { | 
| 258 |     return (qFuzzyIsNull(d: domain1.m_maxX - domain2.m_maxX) | 
| 259 |             && qFuzzyIsNull(d: domain1.m_maxY - domain2.m_maxY) | 
| 260 |             && qFuzzyIsNull(d: domain1.m_minX - domain2.m_minX) | 
| 261 |             && qFuzzyIsNull(d: domain1.m_minY - domain2.m_minY)); | 
| 262 | } | 
| 263 |  | 
| 264 |  | 
| 265 | bool Q_AUTOTEST_EXPORT operator!= (const LogXLogYPolarDomain &domain1, const LogXLogYPolarDomain &domain2) | 
| 266 | { | 
| 267 |     return !(domain1 == domain2); | 
| 268 | } | 
| 269 |  | 
| 270 |  | 
| 271 | QDebug Q_AUTOTEST_EXPORT operator<<(QDebug dbg, const LogXLogYPolarDomain &domain) | 
| 272 | { | 
| 273 | #ifdef QT_NO_TEXTSTREAM | 
| 274 |     Q_UNUSED(domain) | 
| 275 | #else | 
| 276 |     dbg.nospace() << "AbstractDomain("  << domain.m_minX << ',' << domain.m_maxX << ',' << domain.m_minY << ',' << domain.m_maxY << ')' << domain.m_size; | 
| 277 | #endif | 
| 278 |     return dbg.maybeSpace(); | 
| 279 | } | 
| 280 |  | 
| 281 | QT_CHARTS_END_NAMESPACE | 
| 282 |  | 
| 283 | #include "moc_logxlogypolardomain_p.cpp" | 
| 284 |  |