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/xydomain_p.h> |
31 | #include <private/qabstractaxis_p.h> |
32 | #include <QtCore/QtMath> |
33 | |
34 | QT_CHARTS_BEGIN_NAMESPACE |
35 | |
36 | XYDomain::XYDomain(QObject *parent) |
37 | : AbstractDomain(parent) |
38 | { |
39 | } |
40 | |
41 | XYDomain::~XYDomain() |
42 | { |
43 | } |
44 | |
45 | void XYDomain::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 | |
71 | void XYDomain::zoomIn(const QRectF &rect) |
72 | { |
73 | storeZoomReset(); |
74 | QRectF fixedRect = fixZoomRect(rect); |
75 | qreal dx = spanX() / m_size.width(); |
76 | qreal dy = spanY() / m_size.height(); |
77 | |
78 | qreal maxX = m_maxX; |
79 | qreal minX = m_minX; |
80 | qreal minY = m_minY; |
81 | qreal maxY = m_maxY; |
82 | |
83 | maxX = minX + dx * fixedRect.right(); |
84 | minX = minX + dx * fixedRect.left(); |
85 | minY = maxY - dy * fixedRect.bottom(); |
86 | maxY = maxY - dy * fixedRect.top(); |
87 | |
88 | if ((maxX - minX) == spanX()) { |
89 | minX = m_minX; |
90 | maxX = m_maxX; |
91 | } |
92 | if ((maxY - minY) == spanY()) { |
93 | minY = m_minY; |
94 | maxY = m_maxY; |
95 | } |
96 | |
97 | setRange(minX, maxX, minY, maxY); |
98 | } |
99 | |
100 | void XYDomain::zoomOut(const QRectF &rect) |
101 | { |
102 | storeZoomReset(); |
103 | QRectF fixedRect = fixZoomRect(rect); |
104 | qreal dx = spanX() / rect.width(); |
105 | qreal dy = spanY() / rect.height(); |
106 | |
107 | qreal maxX = m_maxX; |
108 | qreal minX = m_minX; |
109 | qreal minY = m_minY; |
110 | qreal maxY = m_maxY; |
111 | |
112 | minX = maxX - dx * fixedRect.right(); |
113 | maxX = minX + dx * m_size.width(); |
114 | maxY = minY + dy * fixedRect.bottom(); |
115 | minY = maxY - dy * m_size.height(); |
116 | |
117 | if ((maxX - minX) == spanX()) { |
118 | minX = m_minX; |
119 | maxX = m_maxX; |
120 | } |
121 | if ((maxY - minY) == spanY()) { |
122 | minY = m_minY; |
123 | maxY = m_maxY; |
124 | } |
125 | |
126 | setRange(minX, maxX, minY, maxY); |
127 | } |
128 | |
129 | void XYDomain::move(qreal dx, qreal dy) |
130 | { |
131 | if (m_reverseX) |
132 | dx = -dx; |
133 | if (m_reverseY) |
134 | dy = -dy; |
135 | |
136 | qreal x = spanX() / m_size.width(); |
137 | qreal y = spanY() / m_size.height(); |
138 | |
139 | qreal maxX = m_maxX; |
140 | qreal minX = m_minX; |
141 | qreal minY = m_minY; |
142 | qreal maxY = m_maxY; |
143 | |
144 | if (dx != 0) { |
145 | minX = minX + x * dx; |
146 | maxX = maxX + x * dx; |
147 | } |
148 | if (dy != 0) { |
149 | minY = minY + y * dy; |
150 | maxY = maxY + y * dy; |
151 | } |
152 | setRange(minX, maxX, minY, maxY); |
153 | } |
154 | |
155 | QPointF XYDomain::calculateGeometryPoint(const QPointF &point, bool &ok) const |
156 | { |
157 | const qreal xd = m_maxX - m_minX; |
158 | const qreal yd = m_maxY - m_minY; |
159 | if (qFuzzyIsNull(d: xd) || qFuzzyIsNull(d: yd)) |
160 | return QPointF(); |
161 | const qreal deltaX = m_size.width() / xd; |
162 | const qreal deltaY = m_size.height() / yd; |
163 | qreal x = (point.x() - m_minX) * deltaX; |
164 | if (m_reverseX) |
165 | x = m_size.width() - x; |
166 | qreal y = (point.y() - m_minY) * deltaY; |
167 | if (!m_reverseY) |
168 | y = m_size.height() - y; |
169 | ok = true; |
170 | return QPointF(x, y); |
171 | } |
172 | |
173 | QVector<QPointF> XYDomain::calculateGeometryPoints(const QVector<QPointF> &vector) const |
174 | { |
175 | const qreal xd = m_maxX - m_minX; |
176 | const qreal yd = m_maxY - m_minY; |
177 | if (qFuzzyIsNull(d: xd) || qFuzzyIsNull(d: yd)) |
178 | return QVector<QPointF>(); |
179 | const qreal deltaX = m_size.width() / xd; |
180 | const qreal deltaY = m_size.height() / yd; |
181 | |
182 | QVector<QPointF> result; |
183 | result.resize(size: vector.count()); |
184 | |
185 | for (int i = 0; i < vector.count(); ++i) { |
186 | qreal x = (vector[i].x() - m_minX) * deltaX; |
187 | if (m_reverseX) |
188 | x = m_size.width() - x; |
189 | qreal y = (vector[i].y() - m_minY) * deltaY; |
190 | if (!m_reverseY) |
191 | y = m_size.height() - y; |
192 | result[i].setX(x); |
193 | result[i].setY(y); |
194 | } |
195 | return result; |
196 | } |
197 | |
198 | QPointF XYDomain::calculateDomainPoint(const QPointF &point) const |
199 | { |
200 | const qreal xd = m_maxX - m_minX; |
201 | const qreal yd = m_maxY - m_minY; |
202 | if (qFuzzyIsNull(d: xd) || qFuzzyIsNull(d: yd)) |
203 | return QPointF(); |
204 | const qreal deltaX = m_size.width() / xd; |
205 | const qreal deltaY = m_size.height() / yd; |
206 | qreal x = m_reverseX ? (m_size.width() - point.x()) : point.x(); |
207 | x /= deltaX; |
208 | x += m_minX; |
209 | qreal y = m_reverseY ? point.y() : (m_size.height() - point.y()); |
210 | y /= deltaY; |
211 | y += m_minY; |
212 | return QPointF(x, y); |
213 | } |
214 | |
215 | // operators |
216 | |
217 | bool Q_AUTOTEST_EXPORT operator== (const XYDomain &domain1, const XYDomain &domain2) |
218 | { |
219 | return (qFuzzyCompare(p1: domain1.m_maxX, p2: domain2.m_maxX) |
220 | && qFuzzyCompare(p1: domain1.m_maxY, p2: domain2.m_maxY) |
221 | && qFuzzyCompare(p1: domain1.m_minX, p2: domain2.m_minX) |
222 | && qFuzzyCompare(p1: domain1.m_minY, p2: domain2.m_minY)); |
223 | } |
224 | |
225 | |
226 | bool Q_AUTOTEST_EXPORT operator!= (const XYDomain &domain1, const XYDomain &domain2) |
227 | { |
228 | return !(domain1 == domain2); |
229 | } |
230 | |
231 | |
232 | QDebug Q_AUTOTEST_EXPORT operator<<(QDebug dbg, const XYDomain &domain) |
233 | { |
234 | #ifdef QT_NO_TEXTSTREAM |
235 | Q_UNUSED(domain) |
236 | #else |
237 | dbg.nospace() << "AbstractDomain(" << domain.m_minX << ',' << domain.m_maxX << ',' << domain.m_minY << ',' << domain.m_maxY << ')' << domain.m_size; |
238 | #endif |
239 | return dbg.maybeSpace(); |
240 | } |
241 | |
242 | QT_CHARTS_END_NAMESPACE |
243 | |
244 | #include "moc_xydomain_p.cpp" |
245 | |