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

source code of qtcharts/src/charts/domain/logxlogypolardomain.cpp