1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only
3
4#include <private/xypolardomain_p.h>
5#include <private/qabstractaxis_p.h>
6#include <QtCore/QtMath>
7
8QT_BEGIN_NAMESPACE
9
10XYPolarDomain::XYPolarDomain(QObject *parent)
11 : PolarDomain(parent)
12{
13}
14
15XYPolarDomain::~XYPolarDomain()
16{
17}
18
19void XYPolarDomain::setRange(qreal minX, qreal maxX, qreal minY, qreal maxY)
20{
21 bool axisXChanged = false;
22 bool axisYChanged = false;
23
24 if (!qFuzzyCompare(p1: m_minX, p2: minX) || !qFuzzyCompare(p1: m_maxX, p2: maxX)) {
25 m_minX = minX;
26 m_maxX = maxX;
27 axisXChanged = true;
28 if (!m_signalsBlocked)
29 emit rangeHorizontalChanged(min: m_minX, max: m_maxX);
30 }
31
32 if (!qFuzzyCompare(p1: m_minY, p2: minY) || !qFuzzyCompare(p1: m_maxY, p2: maxY)) {
33 m_minY = minY;
34 m_maxY = maxY;
35 axisYChanged = true;
36 if (!m_signalsBlocked)
37 emit rangeVerticalChanged(min: m_minY, max: m_maxY);
38 }
39
40 if (axisXChanged || axisYChanged)
41 emit updated();
42}
43
44
45void XYPolarDomain::zoomIn(const QRectF &rect)
46{
47 storeZoomReset();
48 qreal dx = spanX() / m_size.width();
49 qreal dy = spanY() / m_size.height();
50
51 qreal maxX = m_maxX;
52 qreal minX = m_minX;
53 qreal minY = m_minY;
54 qreal maxY = m_maxY;
55
56 maxX = minX + dx * rect.right();
57 minX = minX + dx * rect.left();
58 minY = maxY - dy * rect.bottom();
59 maxY = maxY - dy * rect.top();
60
61 setRange(minX, maxX, minY, maxY);
62}
63
64void XYPolarDomain::zoomOut(const QRectF &rect)
65{
66 storeZoomReset();
67 qreal dx = spanX() / rect.width();
68 qreal dy = spanY() / rect.height();
69
70 qreal maxX = m_maxX;
71 qreal minX = m_minX;
72 qreal minY = m_minY;
73 qreal maxY = m_maxY;
74
75 minX = maxX - dx * rect.right();
76 maxX = minX + dx * m_size.width();
77 maxY = minY + dy * rect.bottom();
78 minY = maxY - dy * m_size.height();
79
80 setRange(minX, maxX, minY, maxY);
81}
82
83void XYPolarDomain::move(qreal dx, qreal dy)
84{
85 // One unit scrolls one degree angular and one pixel radial
86 qreal x = spanX() / 360.0;
87 qreal y = spanY() / m_radius;
88
89 qreal maxX = m_maxX;
90 qreal minX = m_minX;
91 qreal minY = m_minY;
92 qreal maxY = m_maxY;
93
94 if (dx != 0) {
95 minX = minX + x * dx;
96 maxX = maxX + x * dx;
97 }
98 if (dy != 0) {
99 minY = minY + y * dy;
100 maxY = maxY + y * dy;
101 }
102 setRange(minX, maxX, minY, maxY);
103}
104
105QPointF XYPolarDomain::calculateDomainPoint(const QPointF &point) const
106{
107 if (point == m_center)
108 return QPointF(0.0, m_minX);
109
110 QLineF line(m_center, point);
111 qreal a = 90.0 - line.angle();
112 if (a < 0.0)
113 a += 360.0;
114 a = ((a / 360.0) * (m_maxX - m_minX)) + m_minX;
115 qreal r = m_minY + ((m_maxY - m_minY) * (line.length() / m_radius));
116 return QPointF(a, r);
117}
118
119qreal XYPolarDomain::toAngularCoordinate(qreal value, bool &ok) const
120{
121 ok = true;
122 qreal f = (value - m_minX) / (m_maxX - m_minX);
123 return f * 360.0;
124}
125
126qreal XYPolarDomain::toRadialCoordinate(qreal value, bool &ok) const
127{
128 ok = true;
129 if (value < m_minY)
130 value = m_minY;
131
132 // Dont limit the max. The drawing should clip the stuff that goes out of the grid
133 qreal f = (value - m_minY) / (m_maxY - m_minY);
134
135 return f * m_radius;
136}
137
138// operators
139
140bool Q_AUTOTEST_EXPORT operator== (const XYPolarDomain &domain1, const XYPolarDomain &domain2)
141{
142 return (qFuzzyCompare(p1: domain1.m_maxX, p2: domain2.m_maxX)
143 && qFuzzyCompare(p1: domain1.m_maxY, p2: domain2.m_maxY)
144 && qFuzzyCompare(p1: domain1.m_minX, p2: domain2.m_minX)
145 && qFuzzyCompare(p1: domain1.m_minY, p2: domain2.m_minY));
146}
147
148
149bool Q_AUTOTEST_EXPORT operator!= (const XYPolarDomain &domain1, const XYPolarDomain &domain2)
150{
151 return !(domain1 == domain2);
152}
153
154
155QDebug Q_AUTOTEST_EXPORT operator<<(QDebug dbg, const XYPolarDomain &domain)
156{
157#ifdef QT_NO_TEXTSTREAM
158 Q_UNUSED(domain);
159#else
160 dbg.nospace() << "AbstractDomain(" << domain.m_minX << ',' << domain.m_maxX << ',' << domain.m_minY << ',' << domain.m_maxY << ')' << domain.m_size;
161#endif
162 return dbg.maybeSpace();
163}
164
165QT_END_NAMESPACE
166
167#include "moc_xypolardomain_p.cpp"
168

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