1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only
3
4#include <private/xydomain_p.h>
5#include <private/qabstractaxis_p.h>
6#include <QtCore/QtMath>
7
8QT_BEGIN_NAMESPACE
9
10XYDomain::XYDomain(QObject *parent)
11 : AbstractDomain(parent)
12{
13}
14
15XYDomain::~XYDomain()
16{
17}
18
19void XYDomain::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 XYDomain::zoomIn(const QRectF &rect)
46{
47 storeZoomReset();
48 QRectF fixedRect = fixZoomRect(rect);
49 qreal dx = spanX() / m_size.width();
50 qreal dy = spanY() / m_size.height();
51
52 qreal maxX = m_maxX;
53 qreal minX = m_minX;
54 qreal minY = m_minY;
55 qreal maxY = m_maxY;
56
57 maxX = minX + dx * fixedRect.right();
58 minX = minX + dx * fixedRect.left();
59 minY = maxY - dy * fixedRect.bottom();
60 maxY = maxY - dy * fixedRect.top();
61
62 if ((maxX - minX) == spanX()) {
63 minX = m_minX;
64 maxX = m_maxX;
65 }
66 if ((maxY - minY) == spanY()) {
67 minY = m_minY;
68 maxY = m_maxY;
69 }
70
71 setRange(minX, maxX, minY, maxY);
72}
73
74void XYDomain::zoomOut(const QRectF &rect)
75{
76 storeZoomReset();
77 QRectF fixedRect = fixZoomRect(rect);
78 qreal dx = spanX() / rect.width();
79 qreal dy = spanY() / rect.height();
80
81 qreal maxX = m_maxX;
82 qreal minX = m_minX;
83 qreal minY = m_minY;
84 qreal maxY = m_maxY;
85
86 minX = maxX - dx * fixedRect.right();
87 maxX = minX + dx * m_size.width();
88 maxY = minY + dy * fixedRect.bottom();
89 minY = maxY - dy * m_size.height();
90
91 if ((maxX - minX) == spanX()) {
92 minX = m_minX;
93 maxX = m_maxX;
94 }
95 if ((maxY - minY) == spanY()) {
96 minY = m_minY;
97 maxY = m_maxY;
98 }
99
100 setRange(minX, maxX, minY, maxY);
101}
102
103void XYDomain::move(qreal dx, qreal dy)
104{
105 if (m_reverseX)
106 dx = -dx;
107 if (m_reverseY)
108 dy = -dy;
109
110 qreal x = spanX() / m_size.width();
111 qreal y = spanY() / m_size.height();
112
113 qreal maxX = m_maxX;
114 qreal minX = m_minX;
115 qreal minY = m_minY;
116 qreal maxY = m_maxY;
117
118 if (dx != 0) {
119 minX = minX + x * dx;
120 maxX = maxX + x * dx;
121 }
122 if (dy != 0) {
123 minY = minY + y * dy;
124 maxY = maxY + y * dy;
125 }
126 setRange(minX, maxX, minY, maxY);
127}
128
129QPointF XYDomain::calculateGeometryPoint(const QPointF &point, bool &ok) const
130{
131 const qreal xd = m_maxX - m_minX;
132 const qreal yd = m_maxY - m_minY;
133 if (xd == 0.0 || yd == 0.0)
134 return QPointF();
135 const qreal deltaX = m_size.width() / xd;
136 const qreal deltaY = m_size.height() / yd;
137 qreal x = (point.x() - m_minX) * deltaX;
138 if (m_reverseX)
139 x = m_size.width() - x;
140 qreal y = (point.y() - m_minY) * deltaY;
141 if (!m_reverseY)
142 y = m_size.height() - y;
143 ok = true;
144 return QPointF(x, y);
145}
146
147QList<QPointF> XYDomain::calculateGeometryPoints(const QList<QPointF> &list) const
148{
149 const qreal xd = m_maxX - m_minX;
150 const qreal yd = m_maxY - m_minY;
151 if (xd == 0.0 || yd == 0.0)
152 return QList<QPointF>();
153 const qreal deltaX = m_size.width() / xd;
154 const qreal deltaY = m_size.height() / yd;
155
156 QList<QPointF> result;
157 result.resize(size: list.size());
158
159 for (int i = 0; i < list.size(); ++i) {
160 qreal x = (list[i].x() - m_minX) * deltaX;
161 if (m_reverseX)
162 x = m_size.width() - x;
163 qreal y = (list[i].y() - m_minY) * deltaY;
164 if (!m_reverseY)
165 y = m_size.height() - y;
166 result[i].setX(x);
167 result[i].setY(y);
168 }
169 return result;
170}
171
172QPointF XYDomain::calculateDomainPoint(const QPointF &point) const
173{
174 const qreal xd = m_maxX - m_minX;
175 const qreal yd = m_maxY - m_minY;
176 if (xd == 0.0 || yd == 0.0)
177 return QPointF();
178 const qreal deltaX = m_size.width() / xd;
179 const qreal deltaY = m_size.height() / yd;
180 qreal x = m_reverseX ? (m_size.width() - point.x()) : point.x();
181 x /= deltaX;
182 x += m_minX;
183 qreal y = m_reverseY ? point.y() : (m_size.height() - point.y());
184 y /= deltaY;
185 y += m_minY;
186 return QPointF(x, y);
187}
188
189// operators
190
191bool Q_AUTOTEST_EXPORT operator== (const XYDomain &domain1, const XYDomain &domain2)
192{
193 return (qFuzzyCompare(p1: domain1.m_maxX, p2: domain2.m_maxX)
194 && qFuzzyCompare(p1: domain1.m_maxY, p2: domain2.m_maxY)
195 && qFuzzyCompare(p1: domain1.m_minX, p2: domain2.m_minX)
196 && qFuzzyCompare(p1: domain1.m_minY, p2: domain2.m_minY));
197}
198
199
200bool Q_AUTOTEST_EXPORT operator!= (const XYDomain &domain1, const XYDomain &domain2)
201{
202 return !(domain1 == domain2);
203}
204
205
206QDebug Q_AUTOTEST_EXPORT operator<<(QDebug dbg, const XYDomain &domain)
207{
208#ifdef QT_NO_TEXTSTREAM
209 Q_UNUSED(domain);
210#else
211 dbg.nospace() << "AbstractDomain(" << domain.m_minX << ',' << domain.m_maxX << ',' << domain.m_minY << ',' << domain.m_maxY << ')' << domain.m_size;
212#endif
213 return dbg.maybeSpace();
214}
215
216QT_END_NAMESPACE
217
218#include "moc_xydomain_p.cpp"
219

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