Krita Source Code Documentation
Loading...
Searching...
No Matches
kis_global.h File Reference
#include <limits>
#include <KoConfig.h>
#include "kis_assert.h"
#include <QPoint>
#include <QPointF>
#include <QStringConverter>
#include <cmath>
#include <QLineF>
#include <QRect>
#include "kis_pointer_utils.h"
#include <type_traits>

Go to the source code of this file.

Macros

#define INTENT_ABSOLUTE_COLORIMETRIC   3
 
#define INTENT_PERCEPTUAL   0
 
#define INTENT_RELATIVE_COLORIMETRIC   1
 
#define INTENT_SATURATION   2
 
#define M_PI   3.14159265358979323846
 

Enumerations

enum  CursorStyle {
  CURSOR_STYLE_NO_CURSOR = 0 , CURSOR_STYLE_TOOLICON , CURSOR_STYLE_POINTER , CURSOR_STYLE_SMALL_ROUND ,
  CURSOR_STYLE_CROSSHAIR , CURSOR_STYLE_TRIANGLE_RIGHTHANDED , CURSOR_STYLE_TRIANGLE_LEFTHANDED , CURSOR_STYLE_BLACK_PIXEL ,
  CURSOR_STYLE_WHITE_PIXEL , CURSOR_STYLE_ERASER , N_CURSOR_STYLE_SIZE
}
 
enum  OldCursorStyle {
  OLD_CURSOR_STYLE_TOOLICON = 0 , OLD_CURSOR_STYLE_CROSSHAIR = 1 , OLD_CURSOR_STYLE_POINTER = 2 , OLD_CURSOR_STYLE_OUTLINE = 3 ,
  OLD_CURSOR_STYLE_NO_CURSOR = 4 , OLD_CURSOR_STYLE_SMALL_ROUND = 5 , OLD_CURSOR_STYLE_OUTLINE_CENTER_DOT = 6 , OLD_CURSOR_STYLE_OUTLINE_CENTER_CROSS = 7 ,
  OLD_CURSOR_STYLE_TRIANGLE_RIGHTHANDED = 8 , OLD_CURSOR_STYLE_TRIANGLE_LEFTHANDED = 9 , OLD_CURSOR_STYLE_OUTLINE_TRIANGLE_RIGHTHANDED = 10 , OLD_CURSOR_STYLE_OUTLINE_TRIANGLE_LEFTHANDED = 11
}
 
enum  OutlineStyle {
  OUTLINE_NONE = 0 , OUTLINE_CIRCLE , OUTLINE_FULL , OUTLINE_TILT ,
  N_OUTLINE_STYLE_SIZE
}
 

Functions

qreal bisectorAngle (qreal a, qreal b)
 
qreal incrementInDirection (qreal a, qreal inc, qreal direction)
 
template<typename T >
constexpr const T & kisBoundFast (const T &min, const T &val, const T &max)
 
template<typename T >
kisDegreesToRadians (T degrees)
 
qreal kisDistance (const QPointF &pt1, const QPointF &pt2)
 
qreal kisDistanceToLine (const QPointF &m, const QLineF &line)
 
QRect kisEnsureInRect (QRect rc, const QRect &bounds)
 
template<class T , typename U >
kisGrowRect (const T &rect, U offset)
 
QPointF kisProjectOnVector (const QPointF &base, const QPointF &v)
 
template<typename T >
kisRadiansToDegrees (T radians)
 
qreal kisSquareDistance (const QPointF &pt1, const QPointF &pt2)
 
QRect kisTrimLeft (int width, QRect &toTakeFrom)
 
QRectF kisTrimLeft (int width, QRectF &toTakeFrom)
 
QRect kisTrimTop (int height, QRect &toTakeFrom)
 
QRectF kisTrimTop (int height, QRectF &toTakeFrom)
 
template<typename T >
nextPowerOfTwo (T v)
 
template<typename T >
std::enable_if< std::is_floating_point< T >::value, T >::type normalizeAngle (T a)
 
template<typename T >
std::enable_if< std::is_floating_point< T >::value, T >::type normalizeAngleDegrees (T a)
 
template<typename T >
pow2 (const T &x)
 
template<typename T >
pow3 (const T &x)
 
qreal shortestAngularDistance (qreal a, qreal b)
 
template<typename PointType >
PointType snapToClosestAxis (PointType P)
 
template<typename PointType >
PointType snapToClosestNiceAngle (PointType point, PointType startPoint, qreal angle=(2 *M_PI)/24)
 

Variables

constexpr const char * currentUnderlyingStyleNameProperty = "currentUnderlyingStyleName"
 
const quint8 MAX_SELECTED = std::numeric_limits<quint8>::max()
 
const quint8 MIN_SELECTED = std::numeric_limits<quint8>::min()
 
const double PRESSURE_DEFAULT = PRESSURE_MAX
 
const double PRESSURE_MAX = 1.0
 
const double PRESSURE_MIN = 0.0
 
const double PRESSURE_THRESHOLD = 5.0 / 255.0
 
const qint16 qint16_MAX = std::numeric_limits<qint16>::max()
 
const qint16 qint16_MIN = std::numeric_limits<qint16>::min()
 
const qint32 qint32_MAX = std::numeric_limits<qint32>::max()
 
const qint32 qint32_MIN = std::numeric_limits<qint32>::min()
 
const quint16 quint16_MAX = std::numeric_limits<quint16>::max()
 
const quint8 quint8_MAX = std::numeric_limits<quint8>::max()
 
const quint8 SELECTION_THRESHOLD = 1
 

Macro Definition Documentation

◆ INTENT_ABSOLUTE_COLORIMETRIC

#define INTENT_ABSOLUTE_COLORIMETRIC   3

Definition at line 106 of file kis_global.h.

◆ INTENT_PERCEPTUAL

#define INTENT_PERCEPTUAL   0

Definition at line 103 of file kis_global.h.

◆ INTENT_RELATIVE_COLORIMETRIC

#define INTENT_RELATIVE_COLORIMETRIC   1

Definition at line 104 of file kis_global.h.

◆ INTENT_SATURATION

#define INTENT_SATURATION   2

Definition at line 105 of file kis_global.h.

◆ M_PI

#define M_PI   3.14159265358979323846

Definition at line 111 of file kis_global.h.

Enumeration Type Documentation

◆ CursorStyle

Enumerator
CURSOR_STYLE_NO_CURSOR 
CURSOR_STYLE_TOOLICON 
CURSOR_STYLE_POINTER 
CURSOR_STYLE_SMALL_ROUND 
CURSOR_STYLE_CROSSHAIR 
CURSOR_STYLE_TRIANGLE_RIGHTHANDED 
CURSOR_STYLE_TRIANGLE_LEFTHANDED 
CURSOR_STYLE_BLACK_PIXEL 
CURSOR_STYLE_WHITE_PIXEL 
CURSOR_STYLE_ERASER 
N_CURSOR_STYLE_SIZE 

Definition at line 62 of file kis_global.h.

62 {
73
75};
@ 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

◆ OldCursorStyle

Enumerator
OLD_CURSOR_STYLE_TOOLICON 
OLD_CURSOR_STYLE_CROSSHAIR 
OLD_CURSOR_STYLE_POINTER 
OLD_CURSOR_STYLE_OUTLINE 
OLD_CURSOR_STYLE_NO_CURSOR 
OLD_CURSOR_STYLE_SMALL_ROUND 
OLD_CURSOR_STYLE_OUTLINE_CENTER_DOT 
OLD_CURSOR_STYLE_OUTLINE_CENTER_CROSS 
OLD_CURSOR_STYLE_TRIANGLE_RIGHTHANDED 
OLD_CURSOR_STYLE_TRIANGLE_LEFTHANDED 
OLD_CURSOR_STYLE_OUTLINE_TRIANGLE_RIGHTHANDED 
OLD_CURSOR_STYLE_OUTLINE_TRIANGLE_LEFTHANDED 

Definition at line 77 of file kis_global.h.

77 {
81
83
86
89
92
95};
@ 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

Enumerator
OUTLINE_NONE 
OUTLINE_CIRCLE 
OUTLINE_FULL 
OUTLINE_TILT 
N_OUTLINE_STYLE_SIZE 

Definition at line 53 of file kis_global.h.

53 {
54 OUTLINE_NONE = 0,
58
60};
@ 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

Function Documentation

◆ bisectorAngle()

qreal bisectorAngle ( qreal a,
qreal b )
inline

Definition at line 157 of file kis_global.h.

157 {
158 const qreal diff = shortestAngularDistance(a, b);
159 return incrementInDirection(a, 0.5 * diff, b);
160}
qreal incrementInDirection(qreal a, qreal inc, qreal direction)
Definition kis_global.h:147
qreal shortestAngularDistance(qreal a, qreal b)
Definition kis_global.h:140

References incrementInDirection(), and shortestAngularDistance().

◆ incrementInDirection()

qreal incrementInDirection ( qreal a,
qreal inc,
qreal direction )
inline

Definition at line 147 of file kis_global.h.

147 {
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}

References shortestAngularDistance().

◆ kisBoundFast()

template<typename T >
constexpr const T & kisBoundFast ( const T & min,
const T & val,
const T & max )
inlineconstexpr

This is a fork of an old version of qBound. It has the following properties: 1) Does not have asserts (we cannot have them in blendmodes) 2) Does not have automatic type deduction. The user must explicitly type the common type in case of ambiguity.

Rules of thumb:

1) If you are writing time-critical code (e.g. blendmodes), use kisBoundFast() 2) Otherwise use qBound() or std::clamp (the latter may optionally have an assert as well)

Definition at line 37 of file kis_global.h.

38{
50 return qMax(min, qMin(max, val));
51}

◆ kisDegreesToRadians()

template<typename T >
T kisDegreesToRadians ( T degrees)
inline

Definition at line 176 of file kis_global.h.

176 {
177 return degrees * M_PI / 180.0;
178}
#define M_PI
Definition kis_global.h:111

References M_PI.

◆ kisDistance()

qreal kisDistance ( const QPointF & pt1,
const QPointF & pt2 )
inline

Definition at line 190 of file kis_global.h.

190 {
191 return std::sqrt(pow2(pt1.x() - pt2.x()) + pow2(pt1.y() - pt2.y()));
192}
T pow2(const T &x)
Definition kis_global.h:166

References pow2().

◆ kisDistanceToLine()

qreal kisDistanceToLine ( const QPointF & m,
const QLineF & line )
inline

Definition at line 234 of file kis_global.h.

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}
QPointF p2
QPointF p1
qreal distance(const QPointF &p1, const QPointF &p2)
#define C(i, j)
static bool qFuzzyCompare(half p1, half p2)

References A, B, C, distance(), p1, p2, pow2(), and qFuzzyCompare().

◆ kisEnsureInRect()

QRect kisEnsureInRect ( QRect rc,
const QRect & bounds )
inline

Definition at line 267 of file kis_global.h.

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}
#define bounds(x, a, b)

References bounds.

◆ kisGrowRect()

template<class T , typename U >
T kisGrowRect ( const T & rect,
U offset )
inline

Definition at line 186 of file kis_global.h.

186 {
187 return rect.adjusted(-offset, -offset, offset, offset);
188}

◆ kisProjectOnVector()

QPointF kisProjectOnVector ( const QPointF & base,
const QPointF & v )
inline

Definition at line 256 of file kis_global.h.

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}
qreal v

References pow2(), and v.

◆ kisRadiansToDegrees()

template<typename T >
T kisRadiansToDegrees ( T radians)
inline

Definition at line 181 of file kis_global.h.

181 {
182 return radians * 180.0 / M_PI;
183}

References M_PI.

◆ kisSquareDistance()

qreal kisSquareDistance ( const QPointF & pt1,
const QPointF & pt2 )
inline

Definition at line 194 of file kis_global.h.

194 {
195 return pow2(pt1.x() - pt2.x()) + pow2(pt1.y() - pt2.y());
196}

References pow2().

◆ kisTrimLeft() [1/2]

QRect kisTrimLeft ( int width,
QRect & toTakeFrom )
inline

Definition at line 297 of file kis_global.h.

298{
299 QRectF converted = QRectF(toTakeFrom);
300 QRectF toReturn = kisTrimLeft(width, converted);
301 toTakeFrom = converted.toAlignedRect();
302 return toReturn.toAlignedRect();
303}
QRectF kisTrimLeft(int width, QRectF &toTakeFrom)
Definition kis_global.h:288

References kisTrimLeft().

◆ kisTrimLeft() [2/2]

QRectF kisTrimLeft ( int width,
QRectF & toTakeFrom )
inline

Definition at line 288 of file kis_global.h.

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}

◆ kisTrimTop() [1/2]

QRect kisTrimTop ( int height,
QRect & toTakeFrom )
inline

Definition at line 314 of file kis_global.h.

315{
316 QRectF converted = QRectF(toTakeFrom);
317 QRectF toReturn = kisTrimTop(height, converted);
318 toTakeFrom = converted.toAlignedRect();
319 return toReturn.toAlignedRect();
320}
QRectF kisTrimTop(int height, QRectF &toTakeFrom)
Definition kis_global.h:305

References kisTrimTop().

◆ kisTrimTop() [2/2]

QRectF kisTrimTop ( int height,
QRectF & toTakeFrom )
inline

Definition at line 305 of file kis_global.h.

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}

◆ nextPowerOfTwo()

template<typename T >
T nextPowerOfTwo ( T v)
inline

Definition at line 328 of file kis_global.h.

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}

References v.

◆ normalizeAngle()

template<typename T >
std::enable_if< std::is_floating_point< T >::value, T >::type normalizeAngle ( T a)

Definition at line 121 of file kis_global.h.

121 {
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}

References M_PI.

◆ normalizeAngleDegrees()

template<typename T >
std::enable_if< std::is_floating_point< T >::value, T >::type normalizeAngleDegrees ( T a)

Definition at line 132 of file kis_global.h.

132 {
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}

◆ pow2()

template<typename T >
T pow2 ( const T & x)
inline

Definition at line 166 of file kis_global.h.

166 {
167 return x * x;
168}

◆ pow3()

template<typename T >
T pow3 ( const T & x)
inline

Definition at line 171 of file kis_global.h.

171 {
172 return x * x * x;
173}

◆ shortestAngularDistance()

qreal shortestAngularDistance ( qreal a,
qreal b )
inline

Definition at line 140 of file kis_global.h.

140 {
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}

References M_PI.

◆ snapToClosestAxis()

template<typename PointType >
PointType snapToClosestAxis ( PointType P)
inline

Definition at line 199 of file kis_global.h.

199 {
200 if (qAbs(P.x()) < qAbs(P.y())) {
201 P.setX(0);
202 } else {
203 P.setY(0);
204 }
205 return P;
206}
#define P(i, j, k)

References P.

◆ snapToClosestNiceAngle()

template<typename PointType >
PointType snapToClosestNiceAngle ( PointType point,
PointType startPoint,
qreal angle = (2 * M_PI) / 24 )
inline

Definition at line 209 of file kis_global.h.

209 {
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}
qreal kisDistance(const QPointF &pt1, const QPointF &pt2)
Definition kis_global.h:190

References kisDistance(), and M_PI.

Variable Documentation

◆ currentUnderlyingStyleNameProperty

constexpr const char* currentUnderlyingStyleNameProperty = "currentUnderlyingStyleName"
constexpr

Definition at line 116 of file kis_global.h.

◆ MAX_SELECTED

const quint8 MAX_SELECTED = std::numeric_limits<quint8>::max()

Definition at line 32 of file kis_global.h.

◆ MIN_SELECTED

const quint8 MIN_SELECTED = std::numeric_limits<quint8>::min()

Definition at line 33 of file kis_global.h.

◆ PRESSURE_DEFAULT

const double PRESSURE_DEFAULT = PRESSURE_MAX

Definition at line 99 of file kis_global.h.

◆ PRESSURE_MAX

const double PRESSURE_MAX = 1.0

Definition at line 98 of file kis_global.h.

◆ PRESSURE_MIN

const double PRESSURE_MIN = 0.0

Definition at line 97 of file kis_global.h.

◆ PRESSURE_THRESHOLD

const double PRESSURE_THRESHOLD = 5.0 / 255.0

Definition at line 100 of file kis_global.h.

◆ qint16_MAX

const qint16 qint16_MAX = std::numeric_limits<qint16>::max()

Definition at line 28 of file kis_global.h.

◆ qint16_MIN

const qint16 qint16_MIN = std::numeric_limits<qint16>::min()

Definition at line 27 of file kis_global.h.

◆ qint32_MAX

const qint32 qint32_MAX = std::numeric_limits<qint32>::max()

Definition at line 29 of file kis_global.h.

◆ qint32_MIN

const qint32 qint32_MIN = std::numeric_limits<qint32>::min()

Definition at line 30 of file kis_global.h.

◆ quint16_MAX

const quint16 quint16_MAX = std::numeric_limits<quint16>::max()

Definition at line 25 of file kis_global.h.

◆ quint8_MAX

const quint8 quint8_MAX = std::numeric_limits<quint8>::max()

Definition at line 24 of file kis_global.h.

◆ SELECTION_THRESHOLD

const quint8 SELECTION_THRESHOLD = 1

Definition at line 34 of file kis_global.h.