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/xypolardomain_p.h>
31#include <private/qabstractaxis_p.h>
32#include <QtCore/QtMath>
33
34QT_CHARTS_BEGIN_NAMESPACE
35
36XYPolarDomain::XYPolarDomain(QObject *parent)
37 : PolarDomain(parent)
38{
39}
40
41XYPolarDomain::~XYPolarDomain()
42{
43}
44
45void XYPolarDomain::setRange(qreal minX, qreal maxX, qreal minY, qreal maxY)
46{
47 bool axisXChanged = false;
48 bool axisYChanged = false;
49
50 if (!qFuzzyCompare(p1: m_minX, p2: minX) || !qFuzzyCompare(p1: m_maxX, p2: maxX)) {
51 m_minX = minX;
52 m_maxX = maxX;
53 axisXChanged = true;
54 if (!m_signalsBlocked)
55 emit rangeHorizontalChanged(min: m_minX, max: m_maxX);
56 }
57
58 if (!qFuzzyCompare(p1: m_minY, p2: minY) || !qFuzzyCompare(p1: m_maxY, p2: maxY)) {
59 m_minY = minY;
60 m_maxY = maxY;
61 axisYChanged = true;
62 if (!m_signalsBlocked)
63 emit rangeVerticalChanged(min: m_minY, max: m_maxY);
64 }
65
66 if (axisXChanged || axisYChanged)
67 emit updated();
68}
69
70
71void XYPolarDomain::zoomIn(const QRectF &rect)
72{
73 storeZoomReset();
74 qreal dx = spanX() / m_size.width();
75 qreal dy = spanY() / m_size.height();
76
77 qreal maxX = m_maxX;
78 qreal minX = m_minX;
79 qreal minY = m_minY;
80 qreal maxY = m_maxY;
81
82 maxX = minX + dx * rect.right();
83 minX = minX + dx * rect.left();
84 minY = maxY - dy * rect.bottom();
85 maxY = maxY - dy * rect.top();
86
87 setRange(minX, maxX, minY, maxY);
88}
89
90void XYPolarDomain::zoomOut(const QRectF &rect)
91{
92 storeZoomReset();
93 qreal dx = spanX() / rect.width();
94 qreal dy = spanY() / rect.height();
95
96 qreal maxX = m_maxX;
97 qreal minX = m_minX;
98 qreal minY = m_minY;
99 qreal maxY = m_maxY;
100
101 minX = maxX - dx * rect.right();
102 maxX = minX + dx * m_size.width();
103 maxY = minY + dy * rect.bottom();
104 minY = maxY - dy * m_size.height();
105
106 setRange(minX, maxX, minY, maxY);
107}
108
109void XYPolarDomain::move(qreal dx, qreal dy)
110{
111 // One unit scrolls one degree angular and one pixel radial
112 qreal x = spanX() / 360.0;
113 qreal y = spanY() / m_radius;
114
115 qreal maxX = m_maxX;
116 qreal minX = m_minX;
117 qreal minY = m_minY;
118 qreal maxY = m_maxY;
119
120 if (dx != 0) {
121 minX = minX + x * dx;
122 maxX = maxX + x * dx;
123 }
124 if (dy != 0) {
125 minY = minY + y * dy;
126 maxY = maxY + y * dy;
127 }
128 setRange(minX, maxX, minY, maxY);
129}
130
131QPointF XYPolarDomain::calculateDomainPoint(const QPointF &point) const
132{
133 if (point == m_center)
134 return QPointF(0.0, m_minX);
135
136 QLineF line(m_center, point);
137 qreal a = 90.0 - line.angle();
138 if (a < 0.0)
139 a += 360.0;
140 a = ((a / 360.0) * (m_maxX - m_minX)) + m_minX;
141 qreal r = m_minY + ((m_maxY - m_minY) * (line.length() / m_radius));
142 return QPointF(a, r);
143}
144
145qreal XYPolarDomain::toAngularCoordinate(qreal value, bool &ok) const
146{
147 ok = true;
148 qreal f = (value - m_minX) / (m_maxX - m_minX);
149 return f * 360.0;
150}
151
152qreal XYPolarDomain::toRadialCoordinate(qreal value, bool &ok) const
153{
154 ok = true;
155 if (value < m_minY)
156 value = m_minY;
157
158 // Dont limit the max. The drawing should clip the stuff that goes out of the grid
159 qreal f = (value - m_minY) / (m_maxY - m_minY);
160
161 return f * m_radius;
162}
163
164// operators
165
166bool Q_AUTOTEST_EXPORT operator== (const XYPolarDomain &domain1, const XYPolarDomain &domain2)
167{
168 return (qFuzzyCompare(p1: domain1.m_maxX, p2: domain2.m_maxX)
169 && qFuzzyCompare(p1: domain1.m_maxY, p2: domain2.m_maxY)
170 && qFuzzyCompare(p1: domain1.m_minX, p2: domain2.m_minX)
171 && qFuzzyCompare(p1: domain1.m_minY, p2: domain2.m_minY));
172}
173
174
175bool Q_AUTOTEST_EXPORT operator!= (const XYPolarDomain &domain1, const XYPolarDomain &domain2)
176{
177 return !(domain1 == domain2);
178}
179
180
181QDebug Q_AUTOTEST_EXPORT operator<<(QDebug dbg, const XYPolarDomain &domain)
182{
183#ifdef QT_NO_TEXTSTREAM
184 Q_UNUSED(domain)
185#else
186 dbg.nospace() << "AbstractDomain(" << domain.m_minX << ',' << domain.m_maxX << ',' << domain.m_minY << ',' << domain.m_maxY << ')' << domain.m_size;
187#endif
188 return dbg.maybeSpace();
189}
190
191QT_CHARTS_END_NAMESPACE
192
193#include "moc_xypolardomain_p.cpp"
194

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