Krita Source Code Documentation
Loading...
Searching...
No Matches
kis_global.h
Go to the documentation of this file.
1/*
2 * SPDX-FileCopyrightText: 2000 Matthias Elter <elter@kde.org>
3 * SPDX-FileCopyrightText: 2002 Patrick Julien <freak@codepimps.org>
4 * SPDX-FileCopyrightText: 2021 L. E. Segovia <amy@amyspark.me>
5 *
6 * SPDX-License-Identifier: GPL-2.0-or-later
7 */
8#ifndef KISGLOBAL_H_
9#define KISGLOBAL_H_
10
11#include <limits>
12
13#include <KoConfig.h>
14#include "kis_assert.h"
15
16#include <QPoint>
17#include <QPointF>
18
19#if (QT_VERSION >= QT_VERSION_CHECK(6, 0, 0))
20#include <QStringConverter>
21#endif
22
23
24const quint8 quint8_MAX = std::numeric_limits<quint8>::max();
25const quint16 quint16_MAX = std::numeric_limits<quint16>::max();
26
27const qint16 qint16_MIN = std::numeric_limits<qint16>::min();
28const qint16 qint16_MAX = std::numeric_limits<qint16>::max();
29const qint32 qint32_MAX = std::numeric_limits<qint32>::max();
30const qint32 qint32_MIN = std::numeric_limits<qint32>::min();
31
32const quint8 MAX_SELECTED = std::numeric_limits<quint8>::max();
33const quint8 MIN_SELECTED = std::numeric_limits<quint8>::min();
34const quint8 SELECTION_THRESHOLD = 1;
35
36template <typename T>
37constexpr inline const T &kisBoundFast(const T &min, const T &val, const T &max)
38{
50 return qMax(min, qMin(max, val));
51}
52
61
76
96
97const double PRESSURE_MIN = 0.0;
98const double PRESSURE_MAX = 1.0;
100const double PRESSURE_THRESHOLD = 5.0 / 255.0;
101
102// copy of lcms.h
103#define INTENT_PERCEPTUAL 0
104#define INTENT_RELATIVE_COLORIMETRIC 1
105#define INTENT_SATURATION 2
106#define INTENT_ABSOLUTE_COLORIMETRIC 3
107
108#include <cmath>
109
110#ifndef M_PI
111#define M_PI 3.14159265358979323846
112#endif
113
114// Name of the property in the KisApplication that contains the name
115// of the current style, even if there is a stylesheet applied
116constexpr const char *currentUnderlyingStyleNameProperty = "currentUnderlyingStyleName";
117
118// converts \p a to [0, 2 * M_PI) range
119template<typename T>
120typename std::enable_if<std::is_floating_point<T>::value, T>::type
122 if (a < T(0.0)) {
123 a = T(2 * M_PI) + std::fmod(a, T(2 * M_PI));
124 }
125
126 return a >= T(2 * M_PI) ? std::fmod(a, T(2 * M_PI)) : a;
127}
128
129// converts \p a to [0, 360.0) range
130template<typename T>
131typename std::enable_if<std::is_floating_point<T>::value, T>::type
133 if (a < T(0.0)) {
134 a = T(360.0) + std::fmod(a, T(360.0));
135 }
136
137 return a >= T(360.0) ? std::fmod(a, T(360.0)) : a;
138}
139
140inline qreal shortestAngularDistance(qreal a, qreal b) {
141 qreal dist = fmod(qAbs(a - b), 2 * M_PI);
142 if (dist > M_PI) dist = 2 * M_PI - dist;
143
144 return dist;
145}
146
147inline qreal incrementInDirection(qreal a, qreal inc, qreal direction) {
148 qreal b1 = a + inc;
149 qreal b2 = a - inc;
150
151 qreal d1 = shortestAngularDistance(b1, direction);
152 qreal d2 = shortestAngularDistance(b2, direction);
153
154 return d1 < d2 ? b1 : b2;
155}
156
157inline qreal bisectorAngle(qreal a, qreal b) {
158 const qreal diff = shortestAngularDistance(a, b);
159 return incrementInDirection(a, 0.5 * diff, b);
160}
161
162
163
164
165template<typename T>
166inline T pow2(const T& x) {
167 return x * x;
168}
169
170template<typename T>
171inline T pow3(const T& x) {
172 return x * x * x;
173}
174
175template<typename T>
176inline T kisDegreesToRadians(T degrees) {
177 return degrees * M_PI / 180.0;
178}
179
180template<typename T>
181inline T kisRadiansToDegrees(T radians) {
182 return radians * 180.0 / M_PI;
183}
184
185template<class T, typename U>
186inline T kisGrowRect(const T &rect, U offset) {
187 return rect.adjusted(-offset, -offset, offset, offset);
188}
189
190inline qreal kisDistance(const QPointF &pt1, const QPointF &pt2) {
191 return std::sqrt(pow2(pt1.x() - pt2.x()) + pow2(pt1.y() - pt2.y()));
192}
193
194inline qreal kisSquareDistance(const QPointF &pt1, const QPointF &pt2) {
195 return pow2(pt1.x() - pt2.x()) + pow2(pt1.y() - pt2.y());
196}
197
198template<typename PointType>
199inline PointType snapToClosestAxis(PointType P) {
200 if (qAbs(P.x()) < qAbs(P.y())) {
201 P.setX(0);
202 } else {
203 P.setY(0);
204 }
205 return P;
206}
207
208template<typename PointType>
209inline PointType snapToClosestNiceAngle(PointType point, PointType startPoint, qreal angle = (2 * M_PI) / 24) {
210 // default angle = 15 degrees
211
212 const QPointF lineVector = point - startPoint;
213 qreal lineAngle = std::atan2(lineVector.y(), lineVector.x());
214
215 if (lineAngle < 0) {
216 lineAngle += 2 * M_PI;
217 }
218
219 const quint32 constrainedLineIndex = static_cast<quint32>((lineAngle / angle) + 0.5);
220 const qreal constrainedLineAngle = constrainedLineIndex * angle;
221
222 const qreal lineLength = kisDistance(lineVector, QPointF());
223
224 const QPointF constrainedLineVector(lineLength * std::cos(constrainedLineAngle), lineLength * std::sin(constrainedLineAngle));
225
226 const QPointF result = startPoint + constrainedLineVector;
227
228 return result;
229}
230
231
232#include <QLineF>
233
234inline qreal kisDistanceToLine(const QPointF &m, const QLineF &line)
235{
236 const QPointF &p1 = line.p1();
237 const QPointF &p2 = line.p2();
238
239 qreal distance = 0;
240
241 if (qFuzzyCompare(p1.x(), p2.x())) {
242 distance = qAbs(m.x() - p2.x());
243 } else if (qFuzzyCompare(p1.y(), p2.y())) {
244 distance = qAbs(m.y() - p2.y());
245 } else {
246 qreal A = 1;
247 qreal B = - (p1.x() - p2.x()) / (p1.y() - p2.y());
248 qreal C = - p1.x() - B * p1.y();
249
250 distance = qAbs(A * m.x() + B * m.y() + C) / std::sqrt(pow2(A) + pow2(B));
251 }
252
253 return distance;
254}
255
256inline QPointF kisProjectOnVector(const QPointF &base, const QPointF &v)
257{
258 const qreal prod = base.x() * v.x() + base.y() * v.y();
259 const qreal lengthSq = pow2(base.x()) + pow2(base.y());
260 qreal coeff = prod / lengthSq;
261
262 return coeff * base;
263}
264
265#include <QRect>
266
267inline QRect kisEnsureInRect(QRect rc, const QRect &bounds)
268{
269 if(rc.right() > bounds.right()) {
270 rc.translate(bounds.right() - rc.right(), 0);
271 }
272
273 if(rc.left() < bounds.left()) {
274 rc.translate(bounds.left() - rc.left(), 0);
275 }
276
277 if(rc.bottom() > bounds.bottom()) {
278 rc.translate(0, bounds.bottom() - rc.bottom());
279 }
280
281 if(rc.top() < bounds.top()) {
282 rc.translate(0, bounds.top() - rc.top());
283 }
284
285 return rc;
286}
287
288inline QRectF kisTrimLeft( int width, QRectF &toTakeFrom)
289{
290 QPointF trimmedOrigin = toTakeFrom.topLeft();
291 QSize trimmedSize = QSize(width, toTakeFrom.height());
292 toTakeFrom.setWidth(toTakeFrom.width() - width);
293 toTakeFrom.translate(width, 0);
294 return QRectF(trimmedOrigin, trimmedSize);
295}
296
297inline QRect kisTrimLeft( int width, QRect &toTakeFrom)
298{
299 QRectF converted = QRectF(toTakeFrom);
300 QRectF toReturn = kisTrimLeft(width, converted);
301 toTakeFrom = converted.toAlignedRect();
302 return toReturn.toAlignedRect();
303}
304
305inline QRectF kisTrimTop( int height, QRectF& toTakeFrom)
306{
307 QPointF trimmedOrigin = toTakeFrom.topLeft();
308 QSize trimmedSize = QSize(toTakeFrom.width(), height);
309 toTakeFrom.setHeight(toTakeFrom.height() - height);
310 toTakeFrom.translate(0, height);
311 return QRectF(trimmedOrigin, trimmedSize);
312}
313
314inline QRect kisTrimTop( int height, QRect& toTakeFrom)
315{
316 QRectF converted = QRectF(toTakeFrom);
317 QRectF toReturn = kisTrimTop(height, converted);
318 toTakeFrom = converted.toAlignedRect();
319 return toReturn.toAlignedRect();
320}
321
322#include "kis_pointer_utils.h"
323#include <type_traits>
324
325// Makes compilers happy because Linux and macOS differ on how they define
326// quint64 (unsigned long long) vs. size_t (unsigned long (int)).
327template <typename T>
328inline T nextPowerOfTwo(T v)
329{
330 static_assert(std::is_integral<T>::value, "Value has to be an integral number");
331 using base_type = typename std::conditional<sizeof(T) == sizeof(quint64), quint64, quint32>::type;
332 using common_type = typename std::conditional<std::is_signed<T>::value, typename std::make_signed<base_type>::type, typename std::make_unsigned<base_type>::type>::type;
333 return static_cast<T>(qNextPowerOfTwo(static_cast<common_type>(v)));
334}
335
336
337
338#endif // KISGLOBAL_H_
339
qreal v
QPointF p2
QPointF p1
qreal distance(const QPointF &p1, const QPointF &p2)
#define C(i, j)
#define P(i, j, k)
static bool qFuzzyCompare(half p1, half p2)
#define bounds(x, a, b)
const double PRESSURE_DEFAULT
Definition kis_global.h:99
qreal incrementInDirection(qreal a, qreal inc, qreal direction)
Definition kis_global.h:147
const quint8 SELECTION_THRESHOLD
Definition kis_global.h:34
qreal kisDistance(const QPointF &pt1, const QPointF &pt2)
Definition kis_global.h:190
const double PRESSURE_THRESHOLD
Definition kis_global.h:100
OldCursorStyle
Definition kis_global.h:77
@ OLD_CURSOR_STYLE_OUTLINE_TRIANGLE_RIGHTHANDED
Definition kis_global.h:93
@ OLD_CURSOR_STYLE_OUTLINE
Definition kis_global.h:82
@ OLD_CURSOR_STYLE_OUTLINE_CENTER_CROSS
Definition kis_global.h:88
@ OLD_CURSOR_STYLE_CROSSHAIR
Definition kis_global.h:79
@ OLD_CURSOR_STYLE_NO_CURSOR
Definition kis_global.h:84
@ OLD_CURSOR_STYLE_TOOLICON
Definition kis_global.h:78
@ OLD_CURSOR_STYLE_TRIANGLE_LEFTHANDED
Definition kis_global.h:91
@ OLD_CURSOR_STYLE_POINTER
Definition kis_global.h:80
@ OLD_CURSOR_STYLE_OUTLINE_CENTER_DOT
Definition kis_global.h:87
@ OLD_CURSOR_STYLE_OUTLINE_TRIANGLE_LEFTHANDED
Definition kis_global.h:94
@ OLD_CURSOR_STYLE_TRIANGLE_RIGHTHANDED
Definition kis_global.h:90
@ OLD_CURSOR_STYLE_SMALL_ROUND
Definition kis_global.h:85
OutlineStyle
Definition kis_global.h:53
@ OUTLINE_CIRCLE
Definition kis_global.h:55
@ N_OUTLINE_STYLE_SIZE
Definition kis_global.h:59
@ OUTLINE_FULL
Definition kis_global.h:56
@ OUTLINE_TILT
Definition kis_global.h:57
@ OUTLINE_NONE
Definition kis_global.h:54
T pow3(const T &x)
Definition kis_global.h:171
const qint16 qint16_MIN
Definition kis_global.h:27
T kisGrowRect(const T &rect, U offset)
Definition kis_global.h:186
T kisRadiansToDegrees(T radians)
Definition kis_global.h:181
constexpr const T & kisBoundFast(const T &min, const T &val, const T &max)
Definition kis_global.h:37
T pow2(const T &x)
Definition kis_global.h:166
PointType snapToClosestNiceAngle(PointType point, PointType startPoint, qreal angle=(2 *M_PI)/24)
Definition kis_global.h:209
const quint8 MAX_SELECTED
Definition kis_global.h:32
const qint16 qint16_MAX
Definition kis_global.h:28
const qint32 qint32_MIN
Definition kis_global.h:30
std::enable_if< std::is_floating_point< T >::value, T >::type normalizeAngleDegrees(T a)
Definition kis_global.h:132
const quint16 quint16_MAX
Definition kis_global.h:25
qreal shortestAngularDistance(qreal a, qreal b)
Definition kis_global.h:140
T kisDegreesToRadians(T degrees)
Definition kis_global.h:176
constexpr const char * currentUnderlyingStyleNameProperty
Definition kis_global.h:116
qreal kisDistanceToLine(const QPointF &m, const QLineF &line)
Definition kis_global.h:234
T nextPowerOfTwo(T v)
Definition kis_global.h:328
qreal kisSquareDistance(const QPointF &pt1, const QPointF &pt2)
Definition kis_global.h:194
const double PRESSURE_MAX
Definition kis_global.h:98
std::enable_if< std::is_floating_point< T >::value, T >::type normalizeAngle(T a)
Definition kis_global.h:121
qreal bisectorAngle(qreal a, qreal b)
Definition kis_global.h:157
const quint8 quint8_MAX
Definition kis_global.h:24
const quint8 MIN_SELECTED
Definition kis_global.h:33
QPointF kisProjectOnVector(const QPointF &base, const QPointF &v)
Definition kis_global.h:256
QRectF kisTrimTop(int height, QRectF &toTakeFrom)
Definition kis_global.h:305
const qint32 qint32_MAX
Definition kis_global.h:29
QRect kisEnsureInRect(QRect rc, const QRect &bounds)
Definition kis_global.h:267
QRectF kisTrimLeft(int width, QRectF &toTakeFrom)
Definition kis_global.h:288
PointType snapToClosestAxis(PointType P)
Definition kis_global.h:199
const double PRESSURE_MIN
Definition kis_global.h:97
CursorStyle
Definition kis_global.h:62
@ CURSOR_STYLE_POINTER
Definition kis_global.h:65
@ CURSOR_STYLE_SMALL_ROUND
Definition kis_global.h:66
@ CURSOR_STYLE_CROSSHAIR
Definition kis_global.h:67
@ CURSOR_STYLE_TOOLICON
Definition kis_global.h:64
@ CURSOR_STYLE_TRIANGLE_RIGHTHANDED
Definition kis_global.h:68
@ CURSOR_STYLE_WHITE_PIXEL
Definition kis_global.h:71
@ CURSOR_STYLE_BLACK_PIXEL
Definition kis_global.h:70
@ N_CURSOR_STYLE_SIZE
Definition kis_global.h:74
@ CURSOR_STYLE_TRIANGLE_LEFTHANDED
Definition kis_global.h:69
@ CURSOR_STYLE_NO_CURSOR
Definition kis_global.h:63
@ CURSOR_STYLE_ERASER
Definition kis_global.h:72
#define M_PI
Definition kis_global.h:111